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 <atalk/errchk.h>
56 #define min(a,b) ((a) < (b) ? (a) : (b))
58 #ifndef DSI_TCPMAXPEND
59 #define DSI_TCPMAXPEND 20 /* max # of pending connections */
60 #endif /* DSI_TCPMAXPEND */
62 #ifndef DSI_TCPTIMEOUT
63 #define DSI_TCPTIMEOUT 120 /* timeout in seconds for connections */
64 #endif /* ! DSI_TCPTIMEOUT */
67 /* FIXME/SOCKLEN_T: socklen_t is a unix98 feature. */
69 #define SOCKLEN_T unsigned int
70 #endif /* ! SOCKLEN_T */
72 static void dsi_tcp_close(DSI *dsi)
74 if (dsi->socket == -1)
81 /* alarm handler for tcp_open */
82 static void timeout_handler(int sig _U_)
84 LOG(log_error, logtype_dsi, "dsi_tcp_open: connection timed out");
88 static struct itimerval itimer;
89 /* accept the socket and do a little sanity checking */
90 static int dsi_tcp_open(DSI *dsi)
95 len = sizeof(dsi->client);
96 dsi->socket = accept(dsi->serversock, (struct sockaddr *) &dsi->client, &len);
100 struct request_info req;
101 request_init(&req, RQ_DAEMON, "afpd", RQ_FILE, dsi->socket, NULL);
103 if (!hosts_access(&req)) {
104 LOG(deny_severity, logtype_dsi, "refused connect from %s", eval_client(&req));
106 errno = ECONNREFUSED;
115 getitimer(ITIMER_PROF, &itimer);
116 if (0 == (pid = fork()) ) { /* child */
117 static struct itimerval timer = {{0, 0}, {DSI_TCPTIMEOUT, 0}};
118 struct sigaction newact, oldact;
119 uint8_t block[DSI_BLOCKSIZ];
123 server_reset_signal();
126 /* install an alarm to deal with non-responsive connections */
127 newact.sa_handler = timeout_handler;
128 sigemptyset(&newact.sa_mask);
130 sigemptyset(&oldact.sa_mask);
132 setitimer(ITIMER_PROF, &itimer, NULL);
134 if ((sigaction(SIGALRM, &newact, &oldact) < 0) ||
135 (setitimer(ITIMER_REAL, &timer, NULL) < 0)) {
136 LOG(log_error, logtype_dsi, "dsi_tcp_open: %s", strerror(errno));
141 /* read in commands. this is similar to dsi_receive except
142 * for the fact that we do some sanity checking to prevent
143 * delinquent connections from causing mischief. */
145 /* read in the first two bytes */
146 len = dsi_stream_read(dsi, block, 2);
148 /* connection already closed, don't log it (normal OSX 10.3 behaviour) */
151 if (len < 2 || (block[0] > DSIFL_MAX) || (block[1] > DSIFUNC_MAX)) {
152 LOG(log_error, logtype_dsi, "dsi_tcp_open: invalid header");
156 /* read in the rest of the header */
158 while (stored < DSI_BLOCKSIZ) {
159 len = dsi_stream_read(dsi, block + stored, sizeof(block) - stored);
163 LOG(log_error, logtype_dsi, "dsi_tcp_open: stream_read: %s", strerror(errno));
168 dsi->header.dsi_flags = block[0];
169 dsi->header.dsi_command = block[1];
170 memcpy(&dsi->header.dsi_requestID, block + 2,
171 sizeof(dsi->header.dsi_requestID));
172 memcpy(&dsi->header.dsi_code, block + 4, sizeof(dsi->header.dsi_code));
173 memcpy(&dsi->header.dsi_len, block + 8, sizeof(dsi->header.dsi_len));
174 memcpy(&dsi->header.dsi_reserved, block + 12,
175 sizeof(dsi->header.dsi_reserved));
176 dsi->clientID = ntohs(dsi->header.dsi_requestID);
178 /* make sure we don't over-write our buffers. */
179 dsi->cmdlen = min(ntohl(dsi->header.dsi_len), DSI_CMDSIZ);
182 while (stored < dsi->cmdlen) {
183 len = dsi_stream_read(dsi, dsi->commands + stored, dsi->cmdlen - stored);
187 LOG(log_error, logtype_dsi, "dsi_tcp_open: stream_read: %s", strerror(errno));
192 /* stop timer and restore signal handler */
194 memset(&timer, 0, sizeof(timer));
195 setitimer(ITIMER_REAL, &timer, NULL);
196 sigaction(SIGALRM, &oldact, NULL);
199 LOG(log_info, logtype_dsi, "AFP/TCP session from %s:%u",
200 getip_string((struct sockaddr *)&dsi->client),
201 getip_port((struct sockaddr *)&dsi->client));
204 /* send back our pid */
208 /* get it from the interface list */
213 static void guess_interface(DSI *dsi, const char *hostname, const char *port)
216 char **start, **list;
218 struct sockaddr_in *sa = (struct sockaddr_in *)&dsi->server;
220 start = list = getifacelist();
224 fd = socket(PF_INET, SOCK_STREAM, 0);
226 while (list && *list) {
227 strlcpy(ifr.ifr_name, *list, sizeof(ifr.ifr_name));
231 if (ioctl(dsi->serversock, SIOCGIFFLAGS, &ifr) < 0)
234 if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_SLAVE))
237 if (!(ifr.ifr_flags & (IFF_UP | IFF_RUNNING)) )
240 if (ioctl(fd, SIOCGIFADDR, &ifr) < 0)
243 memset(&dsi->server, 0, sizeof(struct sockaddr_storage));
244 sa->sin_family = AF_INET;
245 sa->sin_port = htons(atoi(port));
246 sa->sin_addr = ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr;
248 LOG(log_info, logtype_dsi, "dsi_tcp: '%s:%s' on interface '%s' will be used instead.",
249 getip_string((struct sockaddr *)&dsi->server), port, ifr.ifr_name);
252 LOG(log_info, logtype_dsi, "dsi_tcp (Chooser will not select afp/tcp) "
253 "Check to make sure %s is in /etc/hosts and the correct domain is in "
254 "/etc/resolv.conf: %s", hostname, strerror(errno));
258 freeifacelist(start);
262 #ifndef AI_NUMERICSERV
263 #define AI_NUMERICSERV 0
266 /* this needs to accept passed in addresses */
267 int dsi_tcp_init(DSI *dsi, const char *hostname, const char *inaddress, const char *inport)
274 struct addrinfo hints, *servinfo, *p;
276 /* Check whether address is of the from IP:PORT and split */
279 if (address && strchr(address, ':')) {
280 EC_NULL_LOG( address = a = strdup(address) );
286 /* Prepare hint for getaddrinfo */
287 memset(&hints, 0, sizeof hints);
288 #if !defined(FREEBSD)
289 hints.ai_family = AF_UNSPEC;
291 hints.ai_socktype = SOCK_STREAM;
292 hints.ai_flags = AI_NUMERICSERV;
295 hints.ai_flags |= AI_PASSIVE;
297 hints.ai_family = AF_INET6;
300 hints.ai_flags |= AI_NUMERICHOST;
302 hints.ai_family = AF_UNSPEC;
305 if ((ret = getaddrinfo(address ? address : NULL, port, &hints, &servinfo)) != 0) {
306 LOG(log_error, logtype_dsi, "dsi_tcp_init: getaddrinfo: %s\n", gai_strerror(ret));
310 /* loop through all the results and bind to the first we can */
311 for (p = servinfo; p != NULL; p = p->ai_next) {
312 if ((dsi->serversock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
313 LOG(log_info, logtype_dsi, "dsi_tcp_init: socket: %s", strerror(errno));
318 * Set some socket options:
319 * SO_REUSEADDR deals w/ quick close/opens
320 * TCP_NODELAY diables Nagle
324 setsockopt(dsi->serversock, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
326 #if defined(FREEBSD) && defined(IPV6_BINDV6ONLY)
328 setsockopt(dsi->serversock, IPPROTO_IPV6, IPV6_BINDV6ONLY, (char *)&on, sizeof (on));
331 #ifdef USE_TCP_NODELAY
333 #define SOL_TCP IPPROTO_TCP
336 setsockopt(dsi->serversock, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag));
337 #endif /* USE_TCP_NODELAY */
339 if (bind(dsi->serversock, p->ai_addr, p->ai_addrlen) == -1) {
340 close(dsi->serversock);
341 LOG(log_info, logtype_dsi, "dsi_tcp_init: bind: %s\n", strerror(errno));
345 if (listen(dsi->serversock, DSI_TCPMAXPEND) < 0) {
346 close(dsi->serversock);
347 LOG(log_info, logtype_dsi, "dsi_tcp_init: listen: %s\n", strerror(errno));
355 LOG(log_error, logtype_dsi, "dsi_tcp_init: no suitable network config for TCP socket");
356 freeaddrinfo(servinfo);
360 /* Copy struct sockaddr to struct sockaddr_storage */
361 memcpy(&dsi->server, p->ai_addr, p->ai_addrlen);
362 freeaddrinfo(servinfo);
364 /* Point protocol specific functions to tcp versions */
365 dsi->proto_open = dsi_tcp_open;
366 dsi->proto_close = dsi_tcp_close;
368 /* get real address for GetStatus. */
371 /* address is a parameter, use it 'as is' */
375 /* Prepare hint for getaddrinfo */
376 memset(&hints, 0, sizeof hints);
377 hints.ai_family = AF_UNSPEC;
378 hints.ai_socktype = SOCK_STREAM;
380 if ((ret = getaddrinfo(hostname, port, &hints, &servinfo)) != 0) {
381 LOG(log_info, logtype_dsi, "dsi_tcp_init: getaddrinfo '%s': %s\n", hostname, gai_strerror(ret));
385 for (p = servinfo; p != NULL; p = p->ai_next) {
386 if (p->ai_family == AF_INET) { // IPv4
387 struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
388 if ( (ipv4->sin_addr.s_addr & htonl(0x7f000000)) != htonl(0x7f000000) )
391 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
392 unsigned char ipv6loopb[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
393 if ((memcmp(ipv6->sin6_addr.s6_addr, ipv6loopb, 16)) != 0)
399 /* Store found address in dsi->server */
400 memcpy(&dsi->server, p->ai_addr, p->ai_addrlen);
401 freeaddrinfo(servinfo);
404 LOG(log_info, logtype_dsi, "dsi_tcp: hostname '%s' resolves to loopback address", hostname);
405 freeaddrinfo(servinfo);
408 guess_interface(dsi, hostname, port ? port : "548");