2 * $Id: dsi_tcp.c,v 1.25 2009-12-08 22:34:37 didg Exp $
4 * Copyright (c) 1997, 1998 Adrian Sun (asun@zoology.washington.edu)
5 * All rights reserved. See COPYRIGHT.
7 * this provides both proto_open() and proto_close() to account for
8 * protocol specific initialization and shutdown procedures. all the
9 * read/write stuff is done in dsi_stream.c. */
13 #endif /* HAVE_CONFIG_H */
15 #define USE_TCP_NODELAY
22 #endif /* HAVE_UNISTD_H */
26 #endif /* HAVE_NETDB_H */
27 #include <sys/types.h>
29 #include <sys/socket.h>
33 #endif /* HAVE_STDINT_H */
35 #include <sys/ioctl.h>
38 #include <net/route.h>
41 #include <netinet/tcp.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
46 #include <atalk/logger.h>
49 #include <sys/sockio.h>
54 int allow_severity = log_info;
55 int deny_severity = log_warning;
58 #include <atalk/dsi.h>
59 #include <atalk/compat.h>
60 #include <atalk/util.h>
61 #include <netatalk/endian.h>
62 #include "dsi_private.h"
64 #define min(a,b) ((a) < (b) ? (a) : (b))
66 #ifndef DSI_TCPMAXPEND
67 #define DSI_TCPMAXPEND 20 /* max # of pending connections */
68 #endif /* DSI_TCPMAXPEND */
70 #ifndef DSI_TCPTIMEOUT
71 #define DSI_TCPTIMEOUT 120 /* timeout in seconds for connections */
72 #endif /* ! DSI_TCPTIMEOUT */
75 /* FIXME/SOCKLEN_T: socklen_t is a unix98 feature. */
77 #define SOCKLEN_T unsigned int
78 #endif /* ! SOCKLEN_T */
80 static void dsi_tcp_close(DSI *dsi)
82 if (dsi->socket == -1)
89 /* alarm handler for tcp_open */
90 static void timeout_handler(int sig _U_)
92 LOG(log_error, logtype_dsi, "dsi_tcp_open: connection timed out");
96 static struct itimerval itimer;
97 /* accept the socket and do a little sanity checking */
98 static int dsi_tcp_open(DSI *dsi)
103 len = sizeof(dsi->client);
104 dsi->socket = accept(dsi->serversock, (struct sockaddr *) &dsi->client, &len);
108 struct request_info req;
109 request_init(&req, RQ_DAEMON, dsi->program, RQ_FILE, dsi->socket, NULL);
111 if (!hosts_access(&req)) {
112 LOG(deny_severity, logtype_dsi, "refused connect from %s", eval_client(&req));
114 errno = ECONNREFUSED;
123 getitimer(ITIMER_PROF, &itimer);
124 if (0 == (pid = fork()) ) { /* child */
125 static struct itimerval timer = {{0, 0}, {DSI_TCPTIMEOUT, 0}};
126 struct sigaction newact, oldact;
127 u_int8_t block[DSI_BLOCKSIZ];
130 /* Immediateyl mark globally that we're a child now */
134 server_reset_signal();
137 /* install an alarm to deal with non-responsive connections */
138 newact.sa_handler = timeout_handler;
139 sigemptyset(&newact.sa_mask);
141 sigemptyset(&oldact.sa_mask);
143 setitimer(ITIMER_PROF, &itimer, NULL);
145 if ((sigaction(SIGALRM, &newact, &oldact) < 0) ||
146 (setitimer(ITIMER_REAL, &timer, NULL) < 0)) {
147 LOG(log_error, logtype_dsi, "dsi_tcp_open: %s", strerror(errno));
152 /* read in commands. this is similar to dsi_receive except
153 * for the fact that we do some sanity checking to prevent
154 * delinquent connections from causing mischief. */
156 /* read in the first two bytes */
157 len = dsi_stream_read(dsi, block, 2);
159 /* connection already closed, don't log it (normal OSX 10.3 behaviour) */
162 if (len < 2 || (block[0] > DSIFL_MAX) || (block[1] > DSIFUNC_MAX)) {
163 LOG(log_error, logtype_dsi, "dsi_tcp_open: invalid header");
167 /* read in the rest of the header */
169 while (stored < DSI_BLOCKSIZ) {
170 len = dsi_stream_read(dsi, block + stored, sizeof(block) - stored);
174 LOG(log_error, logtype_dsi, "dsi_tcp_open: stream_read: %s", strerror(errno));
179 dsi->header.dsi_flags = block[0];
180 dsi->header.dsi_command = block[1];
181 memcpy(&dsi->header.dsi_requestID, block + 2,
182 sizeof(dsi->header.dsi_requestID));
183 memcpy(&dsi->header.dsi_code, block + 4, sizeof(dsi->header.dsi_code));
184 memcpy(&dsi->header.dsi_len, block + 8, sizeof(dsi->header.dsi_len));
185 memcpy(&dsi->header.dsi_reserved, block + 12,
186 sizeof(dsi->header.dsi_reserved));
187 dsi->clientID = ntohs(dsi->header.dsi_requestID);
189 /* make sure we don't over-write our buffers. */
190 dsi->cmdlen = min(ntohl(dsi->header.dsi_len), DSI_CMDSIZ);
193 while (stored < dsi->cmdlen) {
194 len = dsi_stream_read(dsi, dsi->commands + stored, dsi->cmdlen - stored);
198 LOG(log_error, logtype_dsi, "dsi_tcp_open: stream_read: %s", strerror(errno));
203 /* stop timer and restore signal handler */
205 memset(&timer, 0, sizeof(timer));
206 setitimer(ITIMER_REAL, &timer, NULL);
207 sigaction(SIGALRM, &oldact, NULL);
210 LOG(log_info, logtype_dsi, "AFP/TCP session from %s:%u",
211 getip_string((struct sockaddr *)&dsi->client),
212 getip_port((struct sockaddr *)&dsi->client));
215 /* send back our pid */
219 /* get it from the interface list */
224 static void guess_interface(DSI *dsi, const char *hostname, const char *port)
227 char **start, **list;
229 struct sockaddr_in *sa = (struct sockaddr_in *)&dsi->server;
231 start = list = getifacelist();
235 fd = socket(PF_INET, SOCK_STREAM, 0);
237 while (list && *list) {
238 strlcpy(ifr.ifr_name, *list, sizeof(ifr.ifr_name));
242 if (ioctl(dsi->serversock, SIOCGIFFLAGS, &ifr) < 0)
245 if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_SLAVE))
248 if (!(ifr.ifr_flags & (IFF_UP | IFF_RUNNING)) )
251 if (ioctl(fd, SIOCGIFADDR, &ifr) < 0)
254 memset(&dsi->server, 0, sizeof(struct sockaddr_storage));
255 sa->sin_family = AF_INET;
256 sa->sin_port = htons(atoi(port));
257 sa->sin_addr = ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr;
259 LOG(log_info, logtype_dsi, "dsi_tcp: '%s:%s' on interface '%s' will be used instead.",
260 getip_string((struct sockaddr *)&dsi->server), port, ifr.ifr_name);
263 LOG(log_info, logtype_dsi, "dsi_tcp (Chooser will not select afp/tcp) "
264 "Check to make sure %s is in /etc/hosts and the correct domain is in "
265 "/etc/resolv.conf: %s", hostname, strerror(errno));
269 freeifacelist(start);
273 #ifndef AI_NUMERICSERV
274 #define AI_NUMERICSERV 0
277 /* this needs to accept passed in addresses */
278 int dsi_tcp_init(DSI *dsi, const char *hostname, const char *address,
279 const char *port, const int proxy)
283 struct addrinfo hints, *servinfo, *p;
285 dsi->protocol = DSI_TCPIP;
287 /* Prepare hint for getaddrinfo */
288 memset(&hints, 0, sizeof hints);
289 #if !defined(FREEBSD)
290 hints.ai_family = AF_UNSPEC;
292 hints.ai_socktype = SOCK_STREAM;
293 hints.ai_flags = AI_NUMERICSERV;
296 hints.ai_flags |= AI_PASSIVE;
298 hints.ai_family = AF_INET6;
301 hints.ai_flags |= AI_NUMERICHOST;
303 hints.ai_family = AF_UNSPEC;
306 if ((ret = getaddrinfo(address ? address : NULL, port ? port : "548", &hints, &servinfo)) != 0) {
307 LOG(log_error, logtype_dsi, "dsi_tcp_init: getaddrinfo: %s\n", gai_strerror(ret));
311 /* create a socket */
313 dsi->serversock = -1;
315 /* loop through all the results and bind to the first we can */
316 for (p = servinfo; p != NULL; p = p->ai_next) {
317 if ((dsi->serversock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
318 LOG(log_info, logtype_dsi, "dsi_tcp_init: socket: %s", strerror(errno));
323 * Set some socket options:
324 * SO_REUSEADDR deals w/ quick close/opens
325 * TCP_NODELAY diables Nagle
329 setsockopt(dsi->serversock, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
331 #if defined(FREEBSD) && defined(IPV6_BINDV6ONLY)
333 setsockopt(dsi->serversock, IPPROTO_IPV6, IPV6_BINDV6ONLY, (char *)&on, sizeof (on));
336 #ifdef USE_TCP_NODELAY
338 #define SOL_TCP IPPROTO_TCP
341 setsockopt(dsi->serversock, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag));
342 #endif /* USE_TCP_NODELAY */
344 if (bind(dsi->serversock, p->ai_addr, p->ai_addrlen) == -1) {
345 close(dsi->serversock);
346 LOG(log_info, logtype_dsi, "dsi_tcp_init: bind: %s\n", strerror(errno));
350 if (listen(dsi->serversock, DSI_TCPMAXPEND) < 0) {
351 close(dsi->serversock);
352 LOG(log_info, logtype_dsi, "dsi_tcp_init: listen: %s\n", strerror(errno));
360 LOG(log_error, logtype_dsi, "dsi_tcp_init: no suitable network config for TCP socket");
361 freeaddrinfo(servinfo);
365 /* Copy struct sockaddr to struct sockaddr_storage */
366 memcpy(&dsi->server, p->ai_addr, p->ai_addrlen);
367 freeaddrinfo(servinfo);
370 /* Point protocol specific functions to tcp versions */
371 dsi->proto_open = dsi_tcp_open;
372 dsi->proto_close = dsi_tcp_close;
374 /* get real address for GetStatus. */
377 /* address is a parameter, use it 'as is' */
381 /* Prepare hint for getaddrinfo */
382 memset(&hints, 0, sizeof hints);
383 hints.ai_family = AF_UNSPEC;
384 hints.ai_socktype = SOCK_STREAM;
386 if ((ret = getaddrinfo(hostname, port ? port : "548", &hints, &servinfo)) != 0) {
387 LOG(log_info, logtype_dsi, "dsi_tcp_init: getaddrinfo '%s': %s\n", hostname, gai_strerror(ret));
391 for (p = servinfo; p != NULL; p = p->ai_next) {
392 if (p->ai_family == AF_INET) { // IPv4
393 struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
394 if ( (ipv4->sin_addr.s_addr & htonl(0x7f000000)) != htonl(0x7f000000) )
397 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
398 unsigned char ipv6loopb[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
399 if ((memcmp(ipv6->sin6_addr.s6_addr, ipv6loopb, 16)) != 0)
405 /* Store found address in dsi->server */
406 memcpy(&dsi->server, p->ai_addr, p->ai_addrlen);
407 freeaddrinfo(servinfo);
410 LOG(log_info, logtype_dsi, "dsi_tcp: hostname '%s' resolves to loopback address", hostname);
411 freeaddrinfo(servinfo);
414 guess_interface(dsi, hostname, port ? port : "548");