2 * $Id: dsi_tcp.c,v 1.6 2002-01-04 04:45:48 sibaz 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 /* alarm handler for tcp_open */
90 static void timeout_handler()
92 LOG(log_error, logtype_default, "dsi_tcp_open: connection timed out");
96 /* accept the socket and do a little sanity checking */
97 static int dsi_tcp_open(DSI *dsi)
102 len = sizeof(dsi->client);
103 dsi->socket = accept(dsi->serversock, (struct sockaddr *) &dsi->client,
108 struct request_info req;
109 request_init(&req, RQ_DAEMON, dsi->program, RQ_FILE, dsi->socket, NULL);
111 if (!hosts_access(&req)) {
112 LOG(deny_severity, "refused connect from %s", eval_client(&req));
114 errno = ECONNREFUSED;
123 if ((pid = fork()) == 0) { /* child */
124 static const struct itimerval timer = {{0, 0}, {DSI_TCPTIMEOUT, 0}};
125 struct sigaction newact, oldact;
126 u_int8_t block[DSI_BLOCKSIZ];
129 /* reset a couple signals */
130 signal(SIGTERM, SIG_DFL);
131 signal(SIGHUP, SIG_DFL);
133 /* install an alarm to deal with non-responsive connections */
134 memset(&newact, 0, sizeof(newact));
135 newact.sa_handler = timeout_handler;
136 if ((sigaction(SIGALRM, &newact, &oldact) < 0) ||
137 (setitimer(ITIMER_REAL, &timer, NULL) < 0)) {
138 LOG(log_error, logtype_default, "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 dsi_stream_read(dsi, block, 2);
148 if ((block[0] > DSIFL_MAX) || (block[1] > DSIFUNC_MAX)) {
149 LOG(log_error, logtype_default, "dsi_tcp_open: invalid header");
153 /* read in the rest of the header */
155 while (stored < DSI_BLOCKSIZ) {
156 len = dsi_stream_read(dsi, block + stored, sizeof(block) - stored);
160 LOG(log_error, logtype_default, "dsi_tcp_open: stream_read: %s", strerror(errno));
165 dsi->header.dsi_flags = block[0];
166 dsi->header.dsi_command = block[1];
167 memcpy(&dsi->header.dsi_requestID, block + 2,
168 sizeof(dsi->header.dsi_requestID));
169 memcpy(&dsi->header.dsi_code, block + 4, sizeof(dsi->header.dsi_code));
170 memcpy(&dsi->header.dsi_len, block + 8, sizeof(dsi->header.dsi_len));
171 memcpy(&dsi->header.dsi_reserved, block + 12,
172 sizeof(dsi->header.dsi_reserved));
173 dsi->clientID = ntohs(dsi->header.dsi_requestID);
175 /* make sure we don't over-write our buffers. */
176 dsi->cmdlen = min(ntohl(dsi->header.dsi_len), DSI_CMDSIZ);
179 while (stored < dsi->cmdlen) {
180 len = dsi_stream_read(dsi, dsi->commands + stored, dsi->cmdlen - stored);
184 LOG(log_error, logtype_default, "dsi_tcp_open: stream_read: %s", strerror(errno));
190 sigaction(SIGALRM, &oldact, NULL);
192 LOG(log_info, logtype_default,"ASIP session:%u(%d) from %s:%u(%d)",
193 ntohs(dsi->server.sin_port), dsi->serversock,
194 inet_ntoa(dsi->client.sin_addr), ntohs(dsi->client.sin_port),
198 /* send back our pid */
202 /* this needs to accept passed in addresses */
203 int dsi_tcp_init(DSI *dsi, const char *hostname, const char *address,
204 const u_int16_t ipport, const int proxy)
206 struct servent *service;
207 struct hostent *host;
210 dsi->protocol = DSI_TCPIP;
212 /* create a socket */
214 dsi->serversock = -1;
215 else if ((dsi->serversock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
220 port = htons(ipport);
221 else if ((service = getservbyname("afpovertcp", "tcp")))
222 port = service->s_port;
224 port = htons(DSI_AFPOVERTCP_PORT);
228 dsi->server.sin_addr.s_addr = htonl(INADDR_ANY);
229 else if (inet_aton(address, &dsi->server.sin_addr) == 0) {
230 LOG(log_info, logtype_default, "dsi_tcp: invalid address (%s)", address);
234 dsi->server.sin_family = AF_INET;
235 dsi->server.sin_port = port;
238 /* this deals w/ quick close/opens */
241 setsockopt(dsi->serversock, SOL_SOCKET, SO_REUSEADDR, &port, sizeof(port));
242 #endif /* SO_REUSEADDR */
244 #ifdef USE_TCP_NODELAY
246 #define SOL_TCP IPPROTO_TCP
247 #endif /* ! SOL_TCP */
249 setsockopt(dsi->serversock, SOL_TCP, TCP_NODELAY, &port, sizeof(port));
250 #endif /* USE_TCP_NODELAY */
252 /* now, bind the socket and set it up for listening */
253 if ((bind(dsi->serversock, (struct sockaddr *) &dsi->server,
254 sizeof(dsi->server)) < 0) ||
255 (listen(dsi->serversock, DSI_TCPMAXPEND) < 0)) {
256 close(dsi->serversock);
261 /* get real address for GetStatus. we'll go through the list of
262 * interfaces if necessary. */
264 if ((host = gethostbyname(hostname))) /* we can resolve the name */
265 dsi->server.sin_addr.s_addr = ((struct in_addr *) host->h_addr)->s_addr;
267 char **start, **list;
270 /* get it from the interface list */
271 start = list = getifacelist();
272 while (list && *list) {
273 strncpy(ifr.ifr_name, *list, sizeof(ifr.ifr_name));
278 #endif /* ! IFF_SLAVE */
279 if (ioctl(dsi->serversock, SIOCGIFFLAGS, &ifr) < 0)
282 if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_SLAVE))
285 if ((ifr.ifr_flags & IFF_UP) == 0)
288 if (ioctl(dsi->serversock, SIOCGIFADDR, &ifr) < 0)
291 dsi->server.sin_addr.s_addr =
292 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr;
293 LOG(log_info, logtype_default, "dsi_tcp: Can't resolve hostname (%s).\n"
294 "%s on interface %s will be used instead.", hostname,
295 inet_ntoa(dsi->server.sin_addr), ifr.ifr_name);
299 LOG(log_info, logtype_default, "dsi_tcp (Chooser will not select afp/tcp)\n\
300 Check to make sure %s is in /etc/hosts and the correct domain is in\n\
301 /etc/resolv.conf: %s", hostname, strerror(errno));
305 freeifacelist(start);
309 /* everything's set up. now point protocol specific functions to
311 dsi->proto_open = dsi_tcp_open;
312 dsi->proto_close = dsi_tcp_close;