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. */
9 #define USE_TCP_NODELAY
17 #include <sys/types.h>
19 #include <sys/socket.h>
21 #include <sys/ioctl.h>
23 #include <netinet/tcp.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
31 #include <sys/sockio.h>
36 int allow_severity = LOG_INFO;
37 int deny_severity = LOG_WARNING;
40 #include <atalk/dsi.h>
41 #include <atalk/compat.h>
42 #include <atalk/util.h>
43 #include <netatalk/endian.h>
44 #include "dsi_private.h"
46 #define min(a,b) ((a) < (b) ? (a) : (b))
48 #ifndef DSI_TCPMAXPEND
49 #define DSI_TCPMAXPEND 20 /* max # of pending connections */
52 #ifndef DSI_TCPTIMEOUT
53 #define DSI_TCPTIMEOUT 120 /* timeout in seconds for connections */
57 /* FIXME/SOCKLEN_T: socklen_t is a unix98 feature. */
59 #define SOCKLEN_T unsigned int
62 static void dsi_tcp_close(DSI *dsi)
64 if (dsi->socket == -1)
71 /* alarm handler for tcp_open */
72 static void timeout_handler()
74 syslog(LOG_ERR, "dsi_tcp_open: connection timed out");
78 /* accept the socket and do a little sanity checking */
79 static int dsi_tcp_open(DSI *dsi)
84 len = sizeof(dsi->client);
85 dsi->socket = accept(dsi->serversock, (struct sockaddr *) &dsi->client,
90 struct request_info req;
91 request_init(&req, RQ_DAEMON, dsi->program, RQ_FILE, dsi->socket, NULL);
93 if (!hosts_access(&req)) {
94 syslog(deny_severity, "refused connect from %s", eval_client(&req));
105 if ((pid = fork()) == 0) { /* child */
106 static const struct itimerval timer = {{0, 0}, {DSI_TCPTIMEOUT, 0}};
107 struct sigaction newact, oldact;
108 u_int8_t block[DSI_BLOCKSIZ];
111 /* reset a couple signals */
112 signal(SIGTERM, SIG_DFL);
113 signal(SIGHUP, SIG_DFL);
115 /* install an alarm to deal with non-responsive connections */
116 memset(&newact, 0, sizeof(newact));
117 newact.sa_handler = timeout_handler;
118 if ((sigaction(SIGALRM, &newact, &oldact) < 0) ||
119 (setitimer(ITIMER_REAL, &timer, NULL) < 0)) {
120 syslog(LOG_ERR, "dsi_tcp_open: %m");
124 /* read in commands. this is similar to dsi_receive except
125 * for the fact that we do some sanity checking to prevent
126 * delinquent connections from causing mischief. */
128 /* read in the first two bytes */
129 dsi_stream_read(dsi, block, 2);
130 if ((block[0] > DSIFL_MAX) || (block[1] > DSIFUNC_MAX)) {
131 syslog(LOG_ERR, "dsi_tcp_open: invalid header");
135 /* read in the rest of the header */
137 while (stored < DSI_BLOCKSIZ) {
138 len = dsi_stream_read(dsi, block + stored, sizeof(block) - stored);
142 syslog(LOG_ERR, "dsi_tcp_open: stream_read: %m");
147 dsi->header.dsi_flags = block[0];
148 dsi->header.dsi_command = block[1];
149 memcpy(&dsi->header.dsi_requestID, block + 2,
150 sizeof(dsi->header.dsi_requestID));
151 memcpy(&dsi->header.dsi_code, block + 4, sizeof(dsi->header.dsi_code));
152 memcpy(&dsi->header.dsi_len, block + 8, sizeof(dsi->header.dsi_len));
153 memcpy(&dsi->header.dsi_reserved, block + 12,
154 sizeof(dsi->header.dsi_reserved));
155 dsi->clientID = ntohs(dsi->header.dsi_requestID);
157 /* make sure we don't over-write our buffers. */
158 dsi->cmdlen = min(ntohl(dsi->header.dsi_len), DSI_CMDSIZ);
161 while (stored < dsi->cmdlen) {
162 len = dsi_stream_read(dsi, dsi->commands + stored, dsi->cmdlen - stored);
166 syslog(LOG_ERR, "dsi_tcp_open: stream_read: %m");
172 sigaction(SIGALRM, &oldact, NULL);
174 syslog(LOG_INFO,"ASIP session:%u(%d) from %s:%u(%d)",
175 ntohs(dsi->server.sin_port), dsi->serversock,
176 inet_ntoa(dsi->client.sin_addr), ntohs(dsi->client.sin_port),
180 /* send back our pid */
184 /* this needs to accept passed in addresses */
185 int dsi_tcp_init(DSI *dsi, const char *hostname, const char *address,
186 const u_int16_t ipport, const int proxy)
188 struct servent *service;
189 struct hostent *host;
192 dsi->protocol = DSI_TCPIP;
194 /* create a socket */
196 dsi->serversock = -1;
197 else if ((dsi->serversock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
202 port = htons(ipport);
203 else if ((service = getservbyname("afpovertcp", "tcp")))
204 port = service->s_port;
206 port = htons(DSI_AFPOVERTCP_PORT);
210 dsi->server.sin_addr.s_addr = htonl(INADDR_ANY);
211 else if (inet_aton(address, &dsi->server.sin_addr) == 0) {
212 syslog(LOG_INFO, "dsi_tcp: invalid address (%s)", address);
216 dsi->server.sin_family = AF_INET;
217 dsi->server.sin_port = port;
220 /* this deals w/ quick close/opens */
223 setsockopt(dsi->serversock, SOL_SOCKET, SO_REUSEADDR, &port, sizeof(port));
226 #ifdef USE_TCP_NODELAY
228 #define SOL_TCP IPPROTO_TCP
231 setsockopt(dsi->serversock, SOL_TCP, TCP_NODELAY, &port, sizeof(port));
234 /* now, bind the socket and set it up for listening */
235 if ((bind(dsi->serversock, (struct sockaddr *) &dsi->server,
236 sizeof(dsi->server)) < 0) ||
237 (listen(dsi->serversock, DSI_TCPMAXPEND) < 0)) {
238 close(dsi->serversock);
243 /* get real address for GetStatus. we'll go through the list of
244 * interfaces if necessary. */
246 if ((host = gethostbyname(hostname))) /* we can resolve the name */
247 dsi->server.sin_addr.s_addr = ((struct in_addr *) host->h_addr)->s_addr;
249 char **start, **list;
252 /* get it from the interface list */
253 start = list = getifacelist();
254 while (list && *list) {
255 strncpy(ifr.ifr_name, *list, sizeof(ifr.ifr_name));
261 if (ioctl(dsi->serversock, SIOCGIFFLAGS, &ifr) < 0)
264 if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_SLAVE))
267 if ((ifr.ifr_flags & IFF_UP) == 0)
270 if (ioctl(dsi->serversock, SIOCGIFADDR, &ifr) < 0)
273 dsi->server.sin_addr.s_addr =
274 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr;
275 syslog(LOG_INFO, "dsi_tcp: Can't resolve hostname (%s).\n"
276 "%s on interface %s will be used instead.", hostname,
277 inet_ntoa(dsi->server.sin_addr), ifr.ifr_name);
281 syslog(LOG_INFO, "dsi_tcp (Chooser will not select afp/tcp)\n\
282 Check to make sure %s is in /etc/hosts and the correct domain is in\n\
283 /etc/resolv.conf: %m", hostname);
287 freeifacelist(start);
291 /* everything's set up. now point protocol specific functions to
293 dsi->proto_open = dsi_tcp_open;
294 dsi->proto_close = dsi_tcp_close;