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. */
13 #define USE_TCP_NODELAY
21 #include <sys/types.h>
23 #include <sys/socket.h>
25 #include <sys/ioctl.h>
27 #include <netinet/tcp.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
35 #include <sys/sockio.h>
40 int allow_severity = LOG_INFO;
41 int deny_severity = LOG_WARNING;
44 #include <atalk/dsi.h>
45 #include <atalk/compat.h>
46 #include <atalk/util.h>
47 #include <netatalk/endian.h>
48 #include "dsi_private.h"
50 #define min(a,b) ((a) < (b) ? (a) : (b))
52 #ifndef DSI_TCPMAXPEND
53 #define DSI_TCPMAXPEND 20 /* max # of pending connections */
56 #ifndef DSI_TCPTIMEOUT
57 #define DSI_TCPTIMEOUT 120 /* timeout in seconds for connections */
61 /* FIXME/SOCKLEN_T: socklen_t is a unix98 feature. */
63 #define SOCKLEN_T unsigned int
66 static void dsi_tcp_close(DSI *dsi)
68 if (dsi->socket == -1)
75 /* alarm handler for tcp_open */
76 static void timeout_handler()
78 syslog(LOG_ERR, "dsi_tcp_open: connection timed out");
82 /* accept the socket and do a little sanity checking */
83 static int dsi_tcp_open(DSI *dsi)
88 len = sizeof(dsi->client);
89 dsi->socket = accept(dsi->serversock, (struct sockaddr *) &dsi->client,
94 struct request_info req;
95 request_init(&req, RQ_DAEMON, dsi->program, RQ_FILE, dsi->socket, NULL);
97 if (!hosts_access(&req)) {
98 syslog(deny_severity, "refused connect from %s", eval_client(&req));
100 errno = ECONNREFUSED;
109 if ((pid = fork()) == 0) { /* child */
110 static const struct itimerval timer = {{0, 0}, {DSI_TCPTIMEOUT, 0}};
111 struct sigaction newact, oldact;
112 u_int8_t block[DSI_BLOCKSIZ];
115 /* reset a couple signals */
116 signal(SIGTERM, SIG_DFL);
117 signal(SIGHUP, SIG_DFL);
119 /* install an alarm to deal with non-responsive connections */
120 memset(&newact, 0, sizeof(newact));
121 newact.sa_handler = timeout_handler;
122 if ((sigaction(SIGALRM, &newact, &oldact) < 0) ||
123 (setitimer(ITIMER_REAL, &timer, NULL) < 0)) {
124 syslog(LOG_ERR, "dsi_tcp_open: %m");
128 /* read in commands. this is similar to dsi_receive except
129 * for the fact that we do some sanity checking to prevent
130 * delinquent connections from causing mischief. */
132 /* read in the first two bytes */
133 dsi_stream_read(dsi, block, 2);
134 if ((block[0] > DSIFL_MAX) || (block[1] > DSIFUNC_MAX)) {
135 syslog(LOG_ERR, "dsi_tcp_open: invalid header");
139 /* read in the rest of the header */
141 while (stored < DSI_BLOCKSIZ) {
142 len = dsi_stream_read(dsi, block + stored, sizeof(block) - stored);
146 syslog(LOG_ERR, "dsi_tcp_open: stream_read: %m");
151 dsi->header.dsi_flags = block[0];
152 dsi->header.dsi_command = block[1];
153 memcpy(&dsi->header.dsi_requestID, block + 2,
154 sizeof(dsi->header.dsi_requestID));
155 memcpy(&dsi->header.dsi_code, block + 4, sizeof(dsi->header.dsi_code));
156 memcpy(&dsi->header.dsi_len, block + 8, sizeof(dsi->header.dsi_len));
157 memcpy(&dsi->header.dsi_reserved, block + 12,
158 sizeof(dsi->header.dsi_reserved));
159 dsi->clientID = ntohs(dsi->header.dsi_requestID);
161 /* make sure we don't over-write our buffers. */
162 dsi->cmdlen = min(ntohl(dsi->header.dsi_len), DSI_CMDSIZ);
165 while (stored < dsi->cmdlen) {
166 len = dsi_stream_read(dsi, dsi->commands + stored, dsi->cmdlen - stored);
170 syslog(LOG_ERR, "dsi_tcp_open: stream_read: %m");
176 sigaction(SIGALRM, &oldact, NULL);
178 syslog(LOG_INFO,"ASIP session:%u(%d) from %s:%u(%d)",
179 ntohs(dsi->server.sin_port), dsi->serversock,
180 inet_ntoa(dsi->client.sin_addr), ntohs(dsi->client.sin_port),
184 /* send back our pid */
188 /* this needs to accept passed in addresses */
189 int dsi_tcp_init(DSI *dsi, const char *hostname, const char *address,
190 const u_int16_t ipport, const int proxy)
192 struct servent *service;
193 struct hostent *host;
196 dsi->protocol = DSI_TCPIP;
198 /* create a socket */
200 dsi->serversock = -1;
201 else if ((dsi->serversock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
206 port = htons(ipport);
207 else if ((service = getservbyname("afpovertcp", "tcp")))
208 port = service->s_port;
210 port = htons(DSI_AFPOVERTCP_PORT);
214 dsi->server.sin_addr.s_addr = htonl(INADDR_ANY);
215 else if (inet_aton(address, &dsi->server.sin_addr) == 0) {
216 syslog(LOG_INFO, "dsi_tcp: invalid address (%s)", address);
220 dsi->server.sin_family = AF_INET;
221 dsi->server.sin_port = port;
224 /* this deals w/ quick close/opens */
227 setsockopt(dsi->serversock, SOL_SOCKET, SO_REUSEADDR, &port, sizeof(port));
230 #ifdef USE_TCP_NODELAY
232 #define SOL_TCP IPPROTO_TCP
235 setsockopt(dsi->serversock, SOL_TCP, TCP_NODELAY, &port, sizeof(port));
238 /* now, bind the socket and set it up for listening */
239 if ((bind(dsi->serversock, (struct sockaddr *) &dsi->server,
240 sizeof(dsi->server)) < 0) ||
241 (listen(dsi->serversock, DSI_TCPMAXPEND) < 0)) {
242 close(dsi->serversock);
247 /* get real address for GetStatus. we'll go through the list of
248 * interfaces if necessary. */
250 if ((host = gethostbyname(hostname))) /* we can resolve the name */
251 dsi->server.sin_addr.s_addr = ((struct in_addr *) host->h_addr)->s_addr;
253 char **start, **list;
256 /* get it from the interface list */
257 start = list = getifacelist();
258 while (list && *list) {
259 strncpy(ifr.ifr_name, *list, sizeof(ifr.ifr_name));
265 if (ioctl(dsi->serversock, SIOCGIFFLAGS, &ifr) < 0)
268 if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_SLAVE))
271 if ((ifr.ifr_flags & IFF_UP) == 0)
274 if (ioctl(dsi->serversock, SIOCGIFADDR, &ifr) < 0)
277 dsi->server.sin_addr.s_addr =
278 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr;
279 syslog(LOG_INFO, "dsi_tcp: Can't resolve hostname (%s).\n"
280 "%s on interface %s will be used instead.", hostname,
281 inet_ntoa(dsi->server.sin_addr), ifr.ifr_name);
285 syslog(LOG_INFO, "dsi_tcp (Chooser will not select afp/tcp)\n\
286 Check to make sure %s is in /etc/hosts and the correct domain is in\n\
287 /etc/resolv.conf: %m", hostname);
291 freeifacelist(start);
295 /* everything's set up. now point protocol specific functions to
297 dsi->proto_open = dsi_tcp_open;
298 dsi->proto_close = dsi_tcp_close;