2 * Copyright (c) 1997, 1998 Adrian Sun (asun@zoology.washington.edu)
3 * All rights reserved. See COPYRIGHT.
5 * this provides both proto_open() and proto_close() to account for
6 * protocol specific initialization and shutdown procedures. all the
7 * read/write stuff is done in dsi_stream.c. */
11 #endif /* HAVE_CONFIG_H */
13 #define USE_TCP_NODELAY
22 #endif /* HAVE_NETDB_H */
23 #include <sys/types.h>
25 #include <sys/socket.h>
28 #include <sys/ioctl.h>
31 #include <net/route.h>
34 #include <netinet/tcp.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
39 #include <atalk/logger.h>
42 #include <sys/sockio.h>
47 int allow_severity = log_info;
48 int deny_severity = log_warning;
51 #include <atalk/dsi.h>
52 #include <atalk/compat.h>
53 #include <atalk/util.h>
54 #include <netatalk/endian.h>
55 #include "dsi_private.h"
57 #define min(a,b) ((a) < (b) ? (a) : (b))
59 #ifndef DSI_TCPMAXPEND
60 #define DSI_TCPMAXPEND 20 /* max # of pending connections */
61 #endif /* DSI_TCPMAXPEND */
63 #ifndef DSI_TCPTIMEOUT
64 #define DSI_TCPTIMEOUT 120 /* timeout in seconds for connections */
65 #endif /* ! DSI_TCPTIMEOUT */
68 /* FIXME/SOCKLEN_T: socklen_t is a unix98 feature. */
70 #define SOCKLEN_T unsigned int
71 #endif /* ! SOCKLEN_T */
73 static void dsi_tcp_close(DSI *dsi)
75 if (dsi->socket == -1)
82 /* alarm handler for tcp_open */
83 static void timeout_handler(int sig _U_)
85 LOG(log_error, logtype_dsi, "dsi_tcp_open: connection timed out");
89 static struct itimerval itimer;
90 /* accept the socket and do a little sanity checking */
91 static int dsi_tcp_open(DSI *dsi)
96 len = sizeof(dsi->client);
97 dsi->socket = accept(dsi->serversock, (struct sockaddr *) &dsi->client, &len);
101 struct request_info req;
102 request_init(&req, RQ_DAEMON, dsi->program, RQ_FILE, dsi->socket, NULL);
104 if (!hosts_access(&req)) {
105 LOG(deny_severity, logtype_dsi, "refused connect from %s", eval_client(&req));
107 errno = ECONNREFUSED;
116 getitimer(ITIMER_PROF, &itimer);
117 if (0 == (pid = fork()) ) { /* child */
118 static struct itimerval timer = {{0, 0}, {DSI_TCPTIMEOUT, 0}};
119 struct sigaction newact, oldact;
120 u_int8_t block[DSI_BLOCKSIZ];
124 server_reset_signal();
127 /* install an alarm to deal with non-responsive connections */
128 newact.sa_handler = timeout_handler;
129 sigemptyset(&newact.sa_mask);
131 sigemptyset(&oldact.sa_mask);
133 setitimer(ITIMER_PROF, &itimer, NULL);
135 if ((sigaction(SIGALRM, &newact, &oldact) < 0) ||
136 (setitimer(ITIMER_REAL, &timer, NULL) < 0)) {
137 LOG(log_error, logtype_dsi, "dsi_tcp_open: %s", strerror(errno));
142 /* read in commands. this is similar to dsi_receive except
143 * for the fact that we do some sanity checking to prevent
144 * delinquent connections from causing mischief. */
146 /* read in the first two bytes */
147 len = dsi_stream_read(dsi, block, 2);
149 /* connection already closed, don't log it (normal OSX 10.3 behaviour) */
152 if (len < 2 || (block[0] > DSIFL_MAX) || (block[1] > DSIFUNC_MAX)) {
153 LOG(log_error, logtype_dsi, "dsi_tcp_open: invalid header");
157 /* read in the rest of the header */
159 while (stored < DSI_BLOCKSIZ) {
160 len = dsi_stream_read(dsi, block + stored, sizeof(block) - stored);
164 LOG(log_error, logtype_dsi, "dsi_tcp_open: stream_read: %s", strerror(errno));
169 dsi->header.dsi_flags = block[0];
170 dsi->header.dsi_command = block[1];
171 memcpy(&dsi->header.dsi_requestID, block + 2,
172 sizeof(dsi->header.dsi_requestID));
173 memcpy(&dsi->header.dsi_code, block + 4, sizeof(dsi->header.dsi_code));
174 memcpy(&dsi->header.dsi_len, block + 8, sizeof(dsi->header.dsi_len));
175 memcpy(&dsi->header.dsi_reserved, block + 12,
176 sizeof(dsi->header.dsi_reserved));
177 dsi->clientID = ntohs(dsi->header.dsi_requestID);
179 /* make sure we don't over-write our buffers. */
180 dsi->cmdlen = min(ntohl(dsi->header.dsi_len), DSI_CMDSIZ);
183 while (stored < dsi->cmdlen) {
184 len = dsi_stream_read(dsi, dsi->commands + stored, dsi->cmdlen - stored);
188 LOG(log_error, logtype_dsi, "dsi_tcp_open: stream_read: %s", strerror(errno));
193 /* stop timer and restore signal handler */
195 memset(&timer, 0, sizeof(timer));
196 setitimer(ITIMER_REAL, &timer, NULL);
197 sigaction(SIGALRM, &oldact, NULL);
200 LOG(log_info, logtype_dsi, "AFP/TCP session from %s:%u",
201 getip_string((struct sockaddr *)&dsi->client),
202 getip_port((struct sockaddr *)&dsi->client));
205 /* send back our pid */
209 /* get it from the interface list */
214 static void guess_interface(DSI *dsi, const char *hostname, const char *port)
217 char **start, **list;
219 struct sockaddr_in *sa = (struct sockaddr_in *)&dsi->server;
221 start = list = getifacelist();
225 fd = socket(PF_INET, SOCK_STREAM, 0);
227 while (list && *list) {
228 strlcpy(ifr.ifr_name, *list, sizeof(ifr.ifr_name));
232 if (ioctl(dsi->serversock, SIOCGIFFLAGS, &ifr) < 0)
235 if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_SLAVE))
238 if (!(ifr.ifr_flags & (IFF_UP | IFF_RUNNING)) )
241 if (ioctl(fd, SIOCGIFADDR, &ifr) < 0)
244 memset(&dsi->server, 0, sizeof(struct sockaddr_storage));
245 sa->sin_family = AF_INET;
246 sa->sin_port = htons(atoi(port));
247 sa->sin_addr = ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr;
249 LOG(log_info, logtype_dsi, "dsi_tcp: '%s:%s' on interface '%s' will be used instead.",
250 getip_string((struct sockaddr *)&dsi->server), port, ifr.ifr_name);
253 LOG(log_info, logtype_dsi, "dsi_tcp (Chooser will not select afp/tcp) "
254 "Check to make sure %s is in /etc/hosts and the correct domain is in "
255 "/etc/resolv.conf: %s", hostname, strerror(errno));
259 freeifacelist(start);
263 #ifndef AI_NUMERICSERV
264 #define AI_NUMERICSERV 0
267 /* this needs to accept passed in addresses */
268 int dsi_tcp_init(DSI *dsi, const char *hostname, const char *address,
269 const char *port, const int proxy)
273 struct addrinfo hints, *servinfo, *p;
275 dsi->protocol = DSI_TCPIP;
277 /* Prepare hint for getaddrinfo */
278 memset(&hints, 0, sizeof hints);
279 #if !defined(FREEBSD)
280 hints.ai_family = AF_UNSPEC;
282 hints.ai_socktype = SOCK_STREAM;
283 hints.ai_flags = AI_NUMERICSERV;
286 hints.ai_flags |= AI_PASSIVE;
288 hints.ai_family = AF_INET6;
291 hints.ai_flags |= AI_NUMERICHOST;
293 hints.ai_family = AF_UNSPEC;
296 if ((ret = getaddrinfo(address ? address : NULL, port ? port : "548", &hints, &servinfo)) != 0) {
297 LOG(log_error, logtype_dsi, "dsi_tcp_init: getaddrinfo: %s\n", gai_strerror(ret));
301 /* create a socket */
303 dsi->serversock = -1;
305 /* loop through all the results and bind to the first we can */
306 for (p = servinfo; p != NULL; p = p->ai_next) {
307 if ((dsi->serversock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
308 LOG(log_info, logtype_dsi, "dsi_tcp_init: socket: %s", strerror(errno));
313 * Set some socket options:
314 * SO_REUSEADDR deals w/ quick close/opens
315 * TCP_NODELAY diables Nagle
319 setsockopt(dsi->serversock, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
321 #if defined(FREEBSD) && defined(IPV6_BINDV6ONLY)
323 setsockopt(dsi->serversock, IPPROTO_IPV6, IPV6_BINDV6ONLY, (char *)&on, sizeof (on));
326 #ifdef USE_TCP_NODELAY
328 #define SOL_TCP IPPROTO_TCP
331 setsockopt(dsi->serversock, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag));
332 #endif /* USE_TCP_NODELAY */
334 if (bind(dsi->serversock, p->ai_addr, p->ai_addrlen) == -1) {
335 close(dsi->serversock);
336 LOG(log_info, logtype_dsi, "dsi_tcp_init: bind: %s\n", strerror(errno));
340 if (listen(dsi->serversock, DSI_TCPMAXPEND) < 0) {
341 close(dsi->serversock);
342 LOG(log_info, logtype_dsi, "dsi_tcp_init: listen: %s\n", strerror(errno));
350 LOG(log_error, logtype_dsi, "dsi_tcp_init: no suitable network config for TCP socket");
351 freeaddrinfo(servinfo);
355 /* Copy struct sockaddr to struct sockaddr_storage */
356 memcpy(&dsi->server, p->ai_addr, p->ai_addrlen);
357 freeaddrinfo(servinfo);
360 /* Point protocol specific functions to tcp versions */
361 dsi->proto_open = dsi_tcp_open;
362 dsi->proto_close = dsi_tcp_close;
364 /* get real address for GetStatus. */
367 /* address is a parameter, use it 'as is' */
371 /* Prepare hint for getaddrinfo */
372 memset(&hints, 0, sizeof hints);
373 hints.ai_family = AF_UNSPEC;
374 hints.ai_socktype = SOCK_STREAM;
376 if ((ret = getaddrinfo(hostname, port ? port : "548", &hints, &servinfo)) != 0) {
377 LOG(log_info, logtype_dsi, "dsi_tcp_init: getaddrinfo '%s': %s\n", hostname, gai_strerror(ret));
381 for (p = servinfo; p != NULL; p = p->ai_next) {
382 if (p->ai_family == AF_INET) { // IPv4
383 struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
384 if ( (ipv4->sin_addr.s_addr & htonl(0x7f000000)) != htonl(0x7f000000) )
387 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
388 unsigned char ipv6loopb[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
389 if ((memcmp(ipv6->sin6_addr.s6_addr, ipv6loopb, 16)) != 0)
395 /* Store found address in dsi->server */
396 memcpy(&dsi->server, p->ai_addr, p->ai_addrlen);
397 freeaddrinfo(servinfo);
400 LOG(log_info, logtype_dsi, "dsi_tcp: hostname '%s' resolves to loopback address", hostname);
401 freeaddrinfo(servinfo);
404 guess_interface(dsi, hostname, port ? port : "548");