2 * $Id: dsi_tcp.c,v 1.21 2009-11-06 14:37:26 franklahm 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 static void dsi_tcp_timeout(DSI *dsi)
92 /* 2 seconds delay, most of the time it translates to 4 seconds:
93 * send/write returns first with whatever it has written and the
94 * second time it returns EAGAIN
99 /* Note: write isn't a restartable syscall if there's a timeout on the socket
100 * we have to test for EINTR
102 if (setsockopt(dsi->socket, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0) {
103 LOG(log_error, logtype_default, "dsi_tcp_open: unable to set timeout %s", strerror(errno));
108 /* alarm handler for tcp_open */
109 static void timeout_handler(int sig _U_)
111 LOG(log_error, logtype_default, "dsi_tcp_open: connection timed out");
115 static struct itimerval itimer;
116 /* accept the socket and do a little sanity checking */
117 static int dsi_tcp_open(DSI *dsi)
122 len = sizeof(dsi->client);
123 dsi->socket = accept(dsi->serversock, (struct sockaddr *) &dsi->client, &len);
127 struct request_info req;
128 request_init(&req, RQ_DAEMON, dsi->program, RQ_FILE, dsi->socket, NULL);
130 if (!hosts_access(&req)) {
131 LOG(deny_severity, logtype_default, "refused connect from %s", eval_client(&req));
133 errno = ECONNREFUSED;
142 getitimer(ITIMER_PROF, &itimer);
143 if (0 == (pid = fork()) ) { /* child */
144 static struct itimerval timer = {{0, 0}, {DSI_TCPTIMEOUT, 0}};
145 struct sigaction newact, oldact;
146 u_int8_t block[DSI_BLOCKSIZ];
150 server_reset_signal();
153 /* install an alarm to deal with non-responsive connections */
154 newact.sa_handler = timeout_handler;
155 sigemptyset(&newact.sa_mask);
157 sigemptyset(&oldact.sa_mask);
159 setitimer(ITIMER_PROF, &itimer, NULL);
161 if ((sigaction(SIGALRM, &newact, &oldact) < 0) ||
162 (setitimer(ITIMER_REAL, &timer, NULL) < 0)) {
163 LOG(log_error, logtype_default, "dsi_tcp_open: %s", strerror(errno));
168 /* read in commands. this is similar to dsi_receive except
169 * for the fact that we do some sanity checking to prevent
170 * delinquent connections from causing mischief. */
172 /* read in the first two bytes */
173 len = dsi_stream_read(dsi, block, 2);
175 /* connection already closed, don't log it (normal OSX 10.3 behaviour) */
178 if (len < 2 || (block[0] > DSIFL_MAX) || (block[1] > DSIFUNC_MAX)) {
179 LOG(log_error, logtype_default, "dsi_tcp_open: invalid header");
183 /* read in the rest of the header */
185 while (stored < DSI_BLOCKSIZ) {
186 len = dsi_stream_read(dsi, block + stored, sizeof(block) - stored);
190 LOG(log_error, logtype_default, "dsi_tcp_open: stream_read: %s", strerror(errno));
195 dsi->header.dsi_flags = block[0];
196 dsi->header.dsi_command = block[1];
197 memcpy(&dsi->header.dsi_requestID, block + 2,
198 sizeof(dsi->header.dsi_requestID));
199 memcpy(&dsi->header.dsi_code, block + 4, sizeof(dsi->header.dsi_code));
200 memcpy(&dsi->header.dsi_len, block + 8, sizeof(dsi->header.dsi_len));
201 memcpy(&dsi->header.dsi_reserved, block + 12,
202 sizeof(dsi->header.dsi_reserved));
203 dsi->clientID = ntohs(dsi->header.dsi_requestID);
205 /* make sure we don't over-write our buffers. */
206 dsi->cmdlen = min(ntohl(dsi->header.dsi_len), DSI_CMDSIZ);
209 while (stored < dsi->cmdlen) {
210 len = dsi_stream_read(dsi, dsi->commands + stored, dsi->cmdlen - stored);
214 LOG(log_error, logtype_default, "dsi_tcp_open: stream_read: %s", strerror(errno));
219 /* stop timer and restore signal handler */
221 memset(&timer, 0, sizeof(timer));
222 setitimer(ITIMER_REAL, &timer, NULL);
223 sigaction(SIGALRM, &oldact, NULL);
226 dsi_tcp_timeout(dsi);
228 LOG(log_info, logtype_default, "AFP/TCP session from %s:%u",
229 getip_string((struct sockaddr *)&dsi->client),
230 getip_port((struct sockaddr *)&dsi->client));
233 /* send back our pid */
237 /* this needs to accept passed in addresses */
238 int dsi_tcp_init(DSI *dsi, const char *hostname, const char *address,
239 const char *port, const int proxy)
243 struct addrinfo hints, *servinfo, *p;
245 dsi->protocol = DSI_TCPIP;
247 /* Prepare hint for getaddrinfo */
248 memset(&hints, 0, sizeof hints);
249 hints.ai_family = AF_UNSPEC;
250 hints.ai_socktype = SOCK_STREAM;
251 hints.ai_flags = AI_NUMERICSERV;
253 hints.ai_flags |= AI_PASSIVE;
255 hints.ai_flags |= AI_NUMERICHOST;
257 if ((ret = getaddrinfo(address ? address : NULL, port ? port : "548", &hints, &servinfo)) != 0) {
258 LOG(log_error, logtype_default, "dsi_tcp_init: getaddrinfo: %s\n", gai_strerror(ret));
262 /* create a socket */
264 dsi->serversock = -1;
266 /* loop through all the results and bind to the first we can */
267 for (p = servinfo; p != NULL; p = p->ai_next) {
268 if ((dsi->serversock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
269 LOG(log_info, logtype_default, "dsi_tcp_init: socket: %s", strerror(errno));
274 * Set some socket options:
275 * SO_REUSEADDR deals w/ quick close/opens
276 * TCP_NODELAY diables Nagle
280 setsockopt(dsi->serversock, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
283 #ifdef USE_TCP_NODELAY
285 #define SOL_TCP IPPROTO_TCP
288 setsockopt(dsi->serversock, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag));
289 #endif /* USE_TCP_NODELAY */
291 if (bind(dsi->serversock, p->ai_addr, p->ai_addrlen) == -1) {
292 close(dsi->serversock);
293 LOG(log_info, logtype_default, "dsi_tcp_init: bind: %s\n", strerror(errno));
297 if (listen(dsi->serversock, DSI_TCPMAXPEND) < 0) {
298 close(dsi->serversock);
299 LOG(log_info, logtype_default, "dsi_tcp_init: listen: %s\n", strerror(errno));
307 LOG(log_error, logtype_default, "dsi_tcp_init: no suitable network config for TCP socket");
308 freeaddrinfo(servinfo);
312 /* Copy struct sockaddr to struct sockaddr_storage */
313 memcpy(&dsi->server, p->ai_addr, p->ai_addrlen);
314 freeaddrinfo(servinfo);
317 /* Point protocol specific functions to tcp versions */
318 dsi->proto_open = dsi_tcp_open;
319 dsi->proto_close = dsi_tcp_close;
321 /* get real address for GetStatus. */
324 /* address is a parameter, use it 'as is' */
328 /* Prepare hint for getaddrinfo */
329 memset(&hints, 0, sizeof hints);
330 hints.ai_family = AF_UNSPEC;
331 hints.ai_socktype = SOCK_STREAM;
333 if ((ret = getaddrinfo(hostname, port ? port : "548", &hints, &servinfo)) != 0) {
334 LOG(log_debug, logtype_default, "dsi_tcp_init: getaddrinfo '%s': %s\n", hostname, gai_strerror(ret));
338 for (p = servinfo; p != NULL; p = p->ai_next) {
339 if (p->ai_family == AF_INET) { // IPv4
340 struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
341 if ( (ipv4->sin_addr.s_addr & htonl(0x7f000000)) != htonl(0x7f000000) )
343 LOG(log_info, logtype_default, "dsi_tcp: hostname '%s' resolves to loopback address", hostname);
345 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
346 unsigned char ipv6loopb[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
347 if ((memcmp(ipv6->sin6_addr.s6_addr, ipv6loopb, 16)) != 0)
349 LOG(log_info, logtype_default, "dsi_tcp: hostname '%s' resolves to loopback address", hostname);
354 /* Store found address in dsi->server */
355 memcpy(&dsi->server, p->ai_addr, p->ai_addrlen);
356 freeaddrinfo(servinfo);
359 freeaddrinfo(servinfo);
362 LOG(log_warning, logtype_default, "dsi_tcp: cannot resolve hostname '%s'", hostname);
363 /* get it from the interface list */
365 char **start, **list;
368 start = list = getifacelist();
369 fd = socket(PF_INET, SOCK_STREAM, 0);
371 while (list && *list) {
372 strlcpy(ifr.ifr_name, *list, sizeof(ifr.ifr_name));
379 if (ioctl(dsi->serversock, SIOCGIFFLAGS, &ifr) < 0)
382 if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_SLAVE))
385 if (!(ifr.ifr_flags & (IFF_UP | IFF_RUNNING)) )
388 if (ioctl(fd, SIOCGIFADDR, &ifr) < 0)
391 memcpy(&dsi->server, &ifr.ifr_addr, sizeof(struct sockaddr_storage));
392 LOG(log_info, logtype_default, "dsi_tcp: '%s' on interface '%s' will be used instead.",
393 getip_string((struct sockaddr *)&dsi->server), ifr.ifr_name);
396 LOG(log_info, logtype_default, "dsi_tcp (Chooser will not select afp/tcp) "
397 "Check to make sure %s is in /etc/hosts and the correct domain is in "
398 "/etc/resolv.conf: %s", hostname, strerror(errno));
403 freeifacelist(start);