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 */
20 #endif /* HAVE_NETDB_H */
21 #include <sys/types.h>
23 #include <sys/socket.h>
26 #include <sys/ioctl.h>
29 #include <net/route.h>
32 #include <netinet/tcp.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
37 #include <atalk/logger.h>
40 #include <sys/sockio.h>
45 int allow_severity = log_info;
46 int deny_severity = log_warning;
49 #include <atalk/dsi.h>
50 #include <atalk/compat.h>
51 #include <atalk/util.h>
52 #include <atalk/errchk.h>
54 #define min(a,b) ((a) < (b) ? (a) : (b))
56 #ifndef DSI_TCPMAXPEND
57 #define DSI_TCPMAXPEND 20 /* max # of pending connections */
58 #endif /* DSI_TCPMAXPEND */
60 #ifndef DSI_TCPTIMEOUT
61 #define DSI_TCPTIMEOUT 120 /* timeout in seconds for connections */
62 #endif /* ! DSI_TCPTIMEOUT */
65 /* FIXME/SOCKLEN_T: socklen_t is a unix98 feature. */
67 #define SOCKLEN_T unsigned int
68 #endif /* ! SOCKLEN_T */
70 static void dsi_tcp_close(DSI *dsi)
72 if (dsi->socket == -1)
79 /* alarm handler for tcp_open */
80 static void timeout_handler(int sig _U_)
82 LOG(log_error, logtype_dsi, "dsi_tcp_open: connection timed out");
86 static struct itimerval itimer;
87 /* accept the socket and do a little sanity checking */
88 static int dsi_tcp_open(DSI *dsi)
93 len = sizeof(dsi->client);
94 dsi->socket = accept(dsi->serversock, (struct sockaddr *) &dsi->client, &len);
98 struct request_info req;
99 request_init(&req, RQ_DAEMON, "afpd", RQ_FILE, dsi->socket, NULL);
101 if (!hosts_access(&req)) {
102 LOG(deny_severity, logtype_dsi, "refused connect from %s", eval_client(&req));
104 errno = ECONNREFUSED;
113 getitimer(ITIMER_PROF, &itimer);
114 if (0 == (pid = fork()) ) { /* child */
115 static struct itimerval timer = {{0, 0}, {DSI_TCPTIMEOUT, 0}};
116 struct sigaction newact, oldact;
117 uint8_t block[DSI_BLOCKSIZ];
121 server_reset_signal();
124 /* install an alarm to deal with non-responsive connections */
125 newact.sa_handler = timeout_handler;
126 sigemptyset(&newact.sa_mask);
128 sigemptyset(&oldact.sa_mask);
130 setitimer(ITIMER_PROF, &itimer, NULL);
132 if ((sigaction(SIGALRM, &newact, &oldact) < 0) ||
133 (setitimer(ITIMER_REAL, &timer, NULL) < 0)) {
134 LOG(log_error, logtype_dsi, "dsi_tcp_open: %s", strerror(errno));
139 /* read in commands. this is similar to dsi_receive except
140 * for the fact that we do some sanity checking to prevent
141 * delinquent connections from causing mischief. */
143 /* read in the first two bytes */
144 len = dsi_stream_read(dsi, block, 2);
146 /* connection already closed, don't log it (normal OSX 10.3 behaviour) */
149 if (len < 2 || (block[0] > DSIFL_MAX) || (block[1] > DSIFUNC_MAX)) {
150 LOG(log_error, logtype_dsi, "dsi_tcp_open: invalid header");
154 /* read in the rest of the header */
156 while (stored < DSI_BLOCKSIZ) {
157 len = dsi_stream_read(dsi, block + stored, sizeof(block) - stored);
161 LOG(log_error, logtype_dsi, "dsi_tcp_open: stream_read: %s", strerror(errno));
166 dsi->header.dsi_flags = block[0];
167 dsi->header.dsi_command = block[1];
168 memcpy(&dsi->header.dsi_requestID, block + 2,
169 sizeof(dsi->header.dsi_requestID));
170 memcpy(&dsi->header.dsi_code, block + 4, sizeof(dsi->header.dsi_code));
171 memcpy(&dsi->header.dsi_len, block + 8, sizeof(dsi->header.dsi_len));
172 memcpy(&dsi->header.dsi_reserved, block + 12,
173 sizeof(dsi->header.dsi_reserved));
174 dsi->clientID = ntohs(dsi->header.dsi_requestID);
176 /* make sure we don't over-write our buffers. */
177 dsi->cmdlen = min(ntohl(dsi->header.dsi_len), DSI_CMDSIZ);
180 while (stored < dsi->cmdlen) {
181 len = dsi_stream_read(dsi, dsi->commands + stored, dsi->cmdlen - stored);
185 LOG(log_error, logtype_dsi, "dsi_tcp_open: stream_read: %s", strerror(errno));
190 /* stop timer and restore signal handler */
192 memset(&timer, 0, sizeof(timer));
193 setitimer(ITIMER_REAL, &timer, NULL);
194 sigaction(SIGALRM, &oldact, NULL);
197 LOG(log_info, logtype_dsi, "AFP/TCP session from %s:%u",
198 getip_string((struct sockaddr *)&dsi->client),
199 getip_port((struct sockaddr *)&dsi->client));
202 /* send back our pid */
206 /* get it from the interface list */
211 static void guess_interface(DSI *dsi, const char *hostname, const char *port)
214 char **start, **list;
216 struct sockaddr_in *sa = (struct sockaddr_in *)&dsi->server;
218 start = list = getifacelist();
222 fd = socket(PF_INET, SOCK_STREAM, 0);
224 while (list && *list) {
225 strlcpy(ifr.ifr_name, *list, sizeof(ifr.ifr_name));
229 if (ioctl(dsi->serversock, SIOCGIFFLAGS, &ifr) < 0)
232 if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_SLAVE))
235 if (!(ifr.ifr_flags & (IFF_UP | IFF_RUNNING)) )
238 if (ioctl(fd, SIOCGIFADDR, &ifr) < 0)
241 memset(&dsi->server, 0, sizeof(struct sockaddr_storage));
242 sa->sin_family = AF_INET;
243 sa->sin_port = htons(atoi(port));
244 sa->sin_addr = ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr;
246 LOG(log_info, logtype_dsi, "dsi_tcp: '%s:%s' on interface '%s' will be used instead.",
247 getip_string((struct sockaddr *)&dsi->server), port, ifr.ifr_name);
251 LOG(log_note, logtype_dsi, "dsi_tcp: couldn't find network interface with IP address to advertice, "
252 "check to make sure \"%s\" is in /etc/hosts or can be resolved with DNS, or "
253 "add a netinterface that is not a loopback or point-2-point type", hostname);
257 freeifacelist(start);
261 #ifndef AI_NUMERICSERV
262 #define AI_NUMERICSERV 0
265 /* this needs to accept passed in addresses */
266 int dsi_tcp_init(DSI *dsi, const char *hostname, const char *inaddress, const char *inport)
273 struct addrinfo hints, *servinfo, *p;
275 /* Check whether address is of the from IP:PORT and split */
278 if (address && strchr(address, ':')) {
279 EC_NULL_LOG( address = a = strdup(address) );
285 /* Prepare hint for getaddrinfo */
286 memset(&hints, 0, sizeof hints);
287 #if !defined(FREEBSD)
288 hints.ai_family = AF_UNSPEC;
290 hints.ai_socktype = SOCK_STREAM;
291 hints.ai_flags = AI_NUMERICSERV;
294 hints.ai_flags |= AI_PASSIVE;
296 hints.ai_family = AF_INET6;
299 hints.ai_flags |= AI_NUMERICHOST;
301 hints.ai_family = AF_UNSPEC;
304 if ((ret = getaddrinfo(address ? address : NULL, port, &hints, &servinfo)) != 0) {
305 LOG(log_error, logtype_dsi, "dsi_tcp_init: getaddrinfo: %s\n", gai_strerror(ret));
309 /* loop through all the results and bind to the first we can */
310 for (p = servinfo; p != NULL; p = p->ai_next) {
311 if ((dsi->serversock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
312 LOG(log_info, logtype_dsi, "dsi_tcp_init: socket: %s", strerror(errno));
317 * Set some socket options:
318 * SO_REUSEADDR deals w/ quick close/opens
319 * TCP_NODELAY diables Nagle
323 setsockopt(dsi->serversock, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
325 #if defined(FREEBSD) && defined(IPV6_BINDV6ONLY)
327 setsockopt(dsi->serversock, IPPROTO_IPV6, IPV6_BINDV6ONLY, (char *)&on, sizeof (on));
331 #define SOL_TCP IPPROTO_TCP
334 setsockopt(dsi->serversock, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag));
336 if (bind(dsi->serversock, p->ai_addr, p->ai_addrlen) == -1) {
337 close(dsi->serversock);
338 LOG(log_info, logtype_dsi, "dsi_tcp_init: bind: %s\n", strerror(errno));
342 if (listen(dsi->serversock, DSI_TCPMAXPEND) < 0) {
343 close(dsi->serversock);
344 LOG(log_info, logtype_dsi, "dsi_tcp_init: listen: %s\n", strerror(errno));
352 LOG(log_error, logtype_dsi, "dsi_tcp_init: no suitable network config for TCP socket");
353 freeaddrinfo(servinfo);
357 /* Copy struct sockaddr to struct sockaddr_storage */
358 memcpy(&dsi->server, p->ai_addr, p->ai_addrlen);
359 freeaddrinfo(servinfo);
361 /* Point protocol specific functions to tcp versions */
362 dsi->proto_open = dsi_tcp_open;
363 dsi->proto_close = dsi_tcp_close;
365 /* get real address for GetStatus. */
368 /* address is a parameter, use it 'as is' */
372 /* Prepare hint for getaddrinfo */
373 memset(&hints, 0, sizeof hints);
374 hints.ai_family = AF_UNSPEC;
375 hints.ai_socktype = SOCK_STREAM;
377 if ((err = getaddrinfo(hostname, port, &hints, &servinfo)) != 0) {
378 LOG(log_info, logtype_dsi, "dsi_tcp_init: getaddrinfo '%s': %s\n", hostname, gai_strerror(err));
382 for (p = servinfo; p != NULL; p = p->ai_next) {
383 if (p->ai_family == AF_INET) { // IPv4
384 struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
385 if ( (ipv4->sin_addr.s_addr & htonl(0x7f000000)) != htonl(0x7f000000) )
388 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
389 unsigned char ipv6loopb[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
390 if ((memcmp(ipv6->sin6_addr.s6_addr, ipv6loopb, 16)) != 0)
396 /* Store found address in dsi->server */
397 memcpy(&dsi->server, p->ai_addr, p->ai_addrlen);
398 freeaddrinfo(servinfo);
401 LOG(log_info, logtype_dsi, "dsi_tcp: hostname '%s' resolves to loopback address", hostname);
402 freeaddrinfo(servinfo);
405 guess_interface(dsi, hostname, port ? port : "548");