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];
131 server_reset_signal();
134 /* install an alarm to deal with non-responsive connections */
135 newact.sa_handler = timeout_handler;
136 sigemptyset(&newact.sa_mask);
138 sigemptyset(&oldact.sa_mask);
140 setitimer(ITIMER_PROF, &itimer, NULL);
142 if ((sigaction(SIGALRM, &newact, &oldact) < 0) ||
143 (setitimer(ITIMER_REAL, &timer, NULL) < 0)) {
144 LOG(log_error, logtype_dsi, "dsi_tcp_open: %s", strerror(errno));
149 /* read in commands. this is similar to dsi_receive except
150 * for the fact that we do some sanity checking to prevent
151 * delinquent connections from causing mischief. */
153 /* read in the first two bytes */
154 len = dsi_stream_read(dsi, block, 2);
156 /* connection already closed, don't log it (normal OSX 10.3 behaviour) */
159 if (len < 2 || (block[0] > DSIFL_MAX) || (block[1] > DSIFUNC_MAX)) {
160 LOG(log_error, logtype_dsi, "dsi_tcp_open: invalid header");
164 /* read in the rest of the header */
166 while (stored < DSI_BLOCKSIZ) {
167 len = dsi_stream_read(dsi, block + stored, sizeof(block) - stored);
171 LOG(log_error, logtype_dsi, "dsi_tcp_open: stream_read: %s", strerror(errno));
176 dsi->header.dsi_flags = block[0];
177 dsi->header.dsi_command = block[1];
178 memcpy(&dsi->header.dsi_requestID, block + 2,
179 sizeof(dsi->header.dsi_requestID));
180 memcpy(&dsi->header.dsi_code, block + 4, sizeof(dsi->header.dsi_code));
181 memcpy(&dsi->header.dsi_len, block + 8, sizeof(dsi->header.dsi_len));
182 memcpy(&dsi->header.dsi_reserved, block + 12,
183 sizeof(dsi->header.dsi_reserved));
184 dsi->clientID = ntohs(dsi->header.dsi_requestID);
186 /* make sure we don't over-write our buffers. */
187 dsi->cmdlen = min(ntohl(dsi->header.dsi_len), DSI_CMDSIZ);
190 while (stored < dsi->cmdlen) {
191 len = dsi_stream_read(dsi, dsi->commands + stored, dsi->cmdlen - stored);
195 LOG(log_error, logtype_dsi, "dsi_tcp_open: stream_read: %s", strerror(errno));
200 /* stop timer and restore signal handler */
202 memset(&timer, 0, sizeof(timer));
203 setitimer(ITIMER_REAL, &timer, NULL);
204 sigaction(SIGALRM, &oldact, NULL);
207 LOG(log_info, logtype_dsi, "AFP/TCP session from %s:%u",
208 getip_string((struct sockaddr *)&dsi->client),
209 getip_port((struct sockaddr *)&dsi->client));
212 /* send back our pid */
216 /* get it from the interface list */
221 static void guess_interface(DSI *dsi, const char *hostname, const char *port)
224 char **start, **list;
226 struct sockaddr_in *sa = (struct sockaddr_in *)&dsi->server;
228 start = list = getifacelist();
232 fd = socket(PF_INET, SOCK_STREAM, 0);
234 while (list && *list) {
235 strlcpy(ifr.ifr_name, *list, sizeof(ifr.ifr_name));
239 if (ioctl(dsi->serversock, SIOCGIFFLAGS, &ifr) < 0)
242 if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_SLAVE))
245 if (!(ifr.ifr_flags & (IFF_UP | IFF_RUNNING)) )
248 if (ioctl(fd, SIOCGIFADDR, &ifr) < 0)
251 memset(&dsi->server, 0, sizeof(struct sockaddr_storage));
252 sa->sin_family = AF_INET;
253 sa->sin_port = htons(atoi(port));
254 sa->sin_addr = ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr;
256 LOG(log_info, logtype_dsi, "dsi_tcp: '%s:%s' on interface '%s' will be used instead.",
257 getip_string((struct sockaddr *)&dsi->server), port, ifr.ifr_name);
260 LOG(log_info, logtype_dsi, "dsi_tcp (Chooser will not select afp/tcp) "
261 "Check to make sure %s is in /etc/hosts and the correct domain is in "
262 "/etc/resolv.conf: %s", hostname, strerror(errno));
266 freeifacelist(start);
270 #ifndef AI_NUMERICSERV
271 #define AI_NUMERICSERV 0
274 /* this needs to accept passed in addresses */
275 int dsi_tcp_init(DSI *dsi, const char *hostname, const char *address,
276 const char *port, const int proxy)
280 struct addrinfo hints, *servinfo, *p;
282 dsi->protocol = DSI_TCPIP;
284 /* Prepare hint for getaddrinfo */
285 memset(&hints, 0, sizeof hints);
286 #if !defined(FREEBSD)
287 hints.ai_family = AF_UNSPEC;
289 hints.ai_socktype = SOCK_STREAM;
290 hints.ai_flags = AI_NUMERICSERV;
293 hints.ai_flags |= AI_PASSIVE;
295 hints.ai_family = AF_INET6;
298 hints.ai_flags |= AI_NUMERICHOST;
300 hints.ai_family = AF_UNSPEC;
303 if ((ret = getaddrinfo(address ? address : NULL, port ? port : "548", &hints, &servinfo)) != 0) {
304 LOG(log_error, logtype_dsi, "dsi_tcp_init: getaddrinfo: %s\n", gai_strerror(ret));
308 /* create a socket */
310 dsi->serversock = -1;
312 /* loop through all the results and bind to the first we can */
313 for (p = servinfo; p != NULL; p = p->ai_next) {
314 if ((dsi->serversock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
315 LOG(log_info, logtype_dsi, "dsi_tcp_init: socket: %s", strerror(errno));
320 * Set some socket options:
321 * SO_REUSEADDR deals w/ quick close/opens
322 * TCP_NODELAY diables Nagle
326 setsockopt(dsi->serversock, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
328 #if defined(FREEBSD) && defined(IPV6_BINDV6ONLY)
330 setsockopt(dsi->serversock, IPPROTO_IPV6, IPV6_BINDV6ONLY, (char *)&on, sizeof (on));
333 #ifdef USE_TCP_NODELAY
335 #define SOL_TCP IPPROTO_TCP
338 setsockopt(dsi->serversock, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag));
339 #endif /* USE_TCP_NODELAY */
341 if (bind(dsi->serversock, p->ai_addr, p->ai_addrlen) == -1) {
342 close(dsi->serversock);
343 LOG(log_info, logtype_dsi, "dsi_tcp_init: bind: %s\n", strerror(errno));
347 if (listen(dsi->serversock, DSI_TCPMAXPEND) < 0) {
348 close(dsi->serversock);
349 LOG(log_info, logtype_dsi, "dsi_tcp_init: listen: %s\n", strerror(errno));
357 LOG(log_error, logtype_dsi, "dsi_tcp_init: no suitable network config for TCP socket");
358 freeaddrinfo(servinfo);
362 /* Copy struct sockaddr to struct sockaddr_storage */
363 memcpy(&dsi->server, p->ai_addr, p->ai_addrlen);
364 freeaddrinfo(servinfo);
367 /* Point protocol specific functions to tcp versions */
368 dsi->proto_open = dsi_tcp_open;
369 dsi->proto_close = dsi_tcp_close;
371 /* get real address for GetStatus. */
374 /* address is a parameter, use it 'as is' */
378 /* Prepare hint for getaddrinfo */
379 memset(&hints, 0, sizeof hints);
380 hints.ai_family = AF_UNSPEC;
381 hints.ai_socktype = SOCK_STREAM;
383 if ((ret = getaddrinfo(hostname, port ? port : "548", &hints, &servinfo)) != 0) {
384 LOG(log_info, logtype_dsi, "dsi_tcp_init: getaddrinfo '%s': %s\n", hostname, gai_strerror(ret));
388 for (p = servinfo; p != NULL; p = p->ai_next) {
389 if (p->ai_family == AF_INET) { // IPv4
390 struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
391 if ( (ipv4->sin_addr.s_addr & htonl(0x7f000000)) != htonl(0x7f000000) )
394 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
395 unsigned char ipv6loopb[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
396 if ((memcmp(ipv6->sin6_addr.s6_addr, ipv6loopb, 16)) != 0)
402 /* Store found address in dsi->server */
403 memcpy(&dsi->server, p->ai_addr, p->ai_addrlen);
404 freeaddrinfo(servinfo);
407 LOG(log_info, logtype_dsi, "dsi_tcp: hostname '%s' resolves to loopback address", hostname);
408 freeaddrinfo(servinfo);
411 guess_interface(dsi, hostname, port ? port : "548");