2 * $Id: dsi_tcp.c,v 1.16 2009-10-25 06:13:11 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 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,
128 struct request_info req;
129 request_init(&req, RQ_DAEMON, dsi->program, RQ_FILE, dsi->socket, NULL);
131 if (!hosts_access(&req)) {
132 LOG(deny_severity, logtype_default, "refused connect from %s", eval_client(&req));
134 errno = ECONNREFUSED;
143 getitimer(ITIMER_PROF, &itimer);
144 if (0 == (pid = fork()) ) { /* child */
145 static struct itimerval timer = {{0, 0}, {DSI_TCPTIMEOUT, 0}};
146 struct sigaction newact, oldact;
147 u_int8_t block[DSI_BLOCKSIZ];
151 server_reset_signal();
154 /* install an alarm to deal with non-responsive connections */
155 newact.sa_handler = timeout_handler;
156 sigemptyset(&newact.sa_mask);
158 sigemptyset(&oldact.sa_mask);
160 setitimer(ITIMER_PROF, &itimer, NULL);
162 if ((sigaction(SIGALRM, &newact, &oldact) < 0) ||
163 (setitimer(ITIMER_REAL, &timer, NULL) < 0)) {
164 LOG(log_error, logtype_default, "dsi_tcp_open: %s", strerror(errno));
169 /* read in commands. this is similar to dsi_receive except
170 * for the fact that we do some sanity checking to prevent
171 * delinquent connections from causing mischief. */
173 /* read in the first two bytes */
174 len = dsi_stream_read(dsi, block, 2);
176 /* connection already closed, don't log it (normal OSX 10.3 behaviour) */
179 if (len < 2 || (block[0] > DSIFL_MAX) || (block[1] > DSIFUNC_MAX)) {
180 LOG(log_error, logtype_default, "dsi_tcp_open: invalid header");
184 /* read in the rest of the header */
186 while (stored < DSI_BLOCKSIZ) {
187 len = dsi_stream_read(dsi, block + stored, sizeof(block) - stored);
191 LOG(log_error, logtype_default, "dsi_tcp_open: stream_read: %s", strerror(errno));
196 dsi->header.dsi_flags = block[0];
197 dsi->header.dsi_command = block[1];
198 memcpy(&dsi->header.dsi_requestID, block + 2,
199 sizeof(dsi->header.dsi_requestID));
200 memcpy(&dsi->header.dsi_code, block + 4, sizeof(dsi->header.dsi_code));
201 memcpy(&dsi->header.dsi_len, block + 8, sizeof(dsi->header.dsi_len));
202 memcpy(&dsi->header.dsi_reserved, block + 12,
203 sizeof(dsi->header.dsi_reserved));
204 dsi->clientID = ntohs(dsi->header.dsi_requestID);
206 /* make sure we don't over-write our buffers. */
207 dsi->cmdlen = min(ntohl(dsi->header.dsi_len), DSI_CMDSIZ);
210 while (stored < dsi->cmdlen) {
211 len = dsi_stream_read(dsi, dsi->commands + stored, dsi->cmdlen - stored);
215 LOG(log_error, logtype_default, "dsi_tcp_open: stream_read: %s", strerror(errno));
220 /* stop timer and restore signal handler */
222 memset(&timer, 0, sizeof(timer));
223 setitimer(ITIMER_REAL, &timer, NULL);
224 sigaction(SIGALRM, &oldact, NULL);
227 dsi_tcp_timeout(dsi);
229 LOG(log_info, logtype_default,"ASIP session:%u(%d) from %s:%u(%d)",
230 ntohs(dsi->server.sin_port), dsi->serversock,
231 inet_ntoa(dsi->client.sin_addr), ntohs(dsi->client.sin_port),
235 /* send back our pid */
239 /* this needs to accept passed in addresses */
240 int dsi_tcp_init(DSI *dsi, const char *hostname, const char *address,
241 const u_int16_t ipport, const int proxy)
243 struct servent *service;
244 struct hostent *host;
247 dsi->protocol = DSI_TCPIP;
249 /* create a socket */
251 dsi->serversock = -1;
252 else if ((dsi->serversock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
257 port = htons(ipport);
258 else if ((service = getservbyname("afpovertcp", "tcp")))
259 port = service->s_port;
261 port = htons(DSI_AFPOVERTCP_PORT);
265 dsi->server.sin_addr.s_addr = htonl(INADDR_ANY);
266 else if (inet_aton(address, &dsi->server.sin_addr) == 0) {
267 LOG(log_info, logtype_default, "dsi_tcp: invalid address (%s)", address);
271 dsi->server.sin_family = AF_INET;
272 dsi->server.sin_port = port;
275 /* this deals w/ quick close/opens */
278 setsockopt(dsi->serversock, SOL_SOCKET, SO_REUSEADDR, &port, sizeof(port));
281 #ifdef USE_TCP_NODELAY
284 #define SOL_TCP IPPROTO_TCP
288 setsockopt(dsi->serversock, SOL_TCP, TCP_NODELAY, &port, sizeof(port));
289 #endif /* USE_TCP_NODELAY */
291 /* now, bind the socket and set it up for listening */
292 if ((bind(dsi->serversock, (struct sockaddr *) &dsi->server,
293 sizeof(dsi->server)) < 0) ||
294 (listen(dsi->serversock, DSI_TCPMAXPEND) < 0)) {
295 close(dsi->serversock);
300 /* Point protocol specific functions to tcp versions */
301 dsi->proto_open = dsi_tcp_open;
302 dsi->proto_close = dsi_tcp_close;
304 /* get real address for GetStatus. we'll go through the list of
305 * interfaces if necessary. */
308 /* address is a parameter, use it 'as is' */
312 if (!(host = gethostbyname(hostname)) ) { /* we can't resolve the name */
314 LOG(log_info, logtype_default, "dsi_tcp: cannot resolve hostname '%s'", hostname);
316 /* give up we have nothing to advertise */
321 if (( ((struct in_addr *) host->h_addr)->s_addr & htonl(0x7F000000) ) != htonl(0x7F000000)) { /* FIXME ugly check */
322 dsi->server.sin_addr.s_addr = ((struct in_addr *) host->h_addr)->s_addr;
325 LOG(log_info, logtype_default, "dsi_tcp: hostname '%s' resolves to loopback address", hostname);
328 char **start, **list;
331 /* get it from the interface list */
332 start = list = getifacelist();
333 while (list && *list) {
334 strlcpy(ifr.ifr_name, *list, sizeof(ifr.ifr_name));
341 if (ioctl(dsi->serversock, SIOCGIFFLAGS, &ifr) < 0)
344 if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_SLAVE))
347 if (!(ifr.ifr_flags & (IFF_UP | IFF_RUNNING)) )
350 if (ioctl(dsi->serversock, SIOCGIFADDR, &ifr) < 0)
353 dsi->server.sin_addr.s_addr =
354 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr;
355 LOG(log_info, logtype_default, "dsi_tcp: '%s' on interface '%s' will be used instead.",
356 inet_ntoa(dsi->server.sin_addr), ifr.ifr_name);
359 LOG(log_info, logtype_default, "dsi_tcp (Chooser will not select afp/tcp) \
360 Check to make sure %s is in /etc/hosts and the correct domain is in \
361 /etc/resolv.conf: %s", hostname, strerror(errno));
365 freeifacelist(start);