2 * $Id: status.c,v 1.7 2002-02-06 21:58:50 jmarcus Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
16 #include <sys/types.h>
19 #include <sys/param.h>
21 #include <sys/sysctl.h>
22 #endif /* USE_GETHOSTID */
25 #include <netatalk/at.h>
26 #include <netatalk/endian.h>
27 #include <atalk/dsi.h>
28 #include <atalk/atp.h>
29 #include <atalk/asp.h>
30 #include <atalk/nbp.h>
32 #include "globals.h" /* includes <netdb.h> */
34 #include "afp_config.h"
37 static void status_flags(char *data, const int notif, const int ipok,
38 const unsigned char passwdbits)
42 status = AFPSRVRINFO_COPY;
43 if (passwdbits & PASSWD_SET) /* some uams may not allow this. */
44 status |= AFPSRVRINFO_PASSWD;
45 if (passwdbits & PASSWD_NOSAVE)
46 status |= AFPSRVRINFO_NOSAVEPASSWD;
47 status |= AFPSRVRINFO_SRVSIGNATURE;
48 /* only advertise tcp/ip if we have a valid address */
50 status |= AFPSRVRINFO_TCPIP;
51 status |= AFPSRVRINFO_SRVMSGS;
52 /* Allow the user to decide if we should support server notifications.
53 * With this turned off, the clients will poll for directory changes every
54 * 10 seconds. This might be too costly to network resources, so make
55 * this an optional thing. Default will be to _not_ support server
58 status |= AFPSRVRINFO_SRVNOTIFY;
60 status |= AFPSRVRINFO_FASTBOZO;
61 status = htons(status);
62 memcpy(data + AFPSTATUS_FLAGOFF, &status, sizeof(status));
65 static int status_server(char *data, const char *server)
68 char *Obj, *Type, *Zone;
72 /* make room for all offsets before server name */
73 data += AFPSTATUS_PRELEN;
75 /* extract the obj part of the server */
76 Obj = (char *) server;
77 nbp_name(server, &Obj, &Type, &Zone);
80 memcpy( data, Obj, len );
81 if ((len + 1) & 1) /* pad server name and length byte to even boundary */
85 /* make room for signature and net address offset. save location of
88 * NOTE: technically, we don't need to reserve space for the
89 * signature and net address offsets if they're not going to be
90 * used. as there are no offsets after them, it doesn't hurt to
91 * have them specified though. so, we just do that to simplify
95 status = htons(len + AFPSTATUS_POSTLEN);
96 memcpy(start + AFPSTATUS_MACHOFF, &status, sizeof(status));
97 return len; /* return the offset to beginning of signature offset */
100 static void status_machine(char *data)
106 const char *machine = "afs";
108 const char *machine = "unix";
111 memcpy(&status, start + AFPSTATUS_MACHOFF, sizeof(status));
112 data += ntohs( status );
113 len = strlen( machine );
115 memcpy( data, machine, len );
117 status = htons(data - start);
118 memcpy(start + AFPSTATUS_VERSOFF, &status, sizeof(status));
122 /* server signature is a 16-byte quantity */
123 static u_int16_t status_signature(char *data, int *servoffset, DSI *dsi,
124 const char *hostname)
128 u_int16_t offset, sigoff;
138 /* get server signature offset */
139 memcpy(&offset, data + *servoffset, sizeof(offset));
140 sigoff = offset = ntohs(offset);
142 /* jump to server signature offset */
145 /* 16-byte signature consists of copies of the hostid */
146 #if defined(BSD4_4) && defined(USE_GETHOSTID)
148 mib[1] = KERN_HOSTID;
149 len = sizeof(hostid);
150 sysctl(mib, 2, &hostid, &len, NULL, 0);
151 #else /* BSD4_4 && USE_GETHOSTID */
152 hostid = gethostid();
153 #endif /* BSD4_4 && USE_GETHOSTID */
156 hostid = dsi->server.sin_addr.s_addr;
158 struct hostent *host;
160 if ((host = gethostbyname(hostname)))
161 hostid = ((struct in_addr *) host->h_addr)->s_addr;
165 /* it turns out that a server signature screws up separate
166 * servers running on the same machine. to work around that,
167 * i add in an increment */
170 for (i = 0; i < 16; i += sizeof(hostid)) {
171 memcpy(data, &hostid, sizeof(hostid));
172 data += sizeof(hostid);
175 /* calculate net address offset */
176 *servoffset += sizeof(offset);
177 offset = htons(data - status);
178 memcpy(status + *servoffset, &offset, sizeof(offset));
182 static int status_netaddress(char *data, const int servoffset,
183 const ASP asp, const DSI *dsi,
191 /* get net address offset */
192 memcpy(&offset, data + servoffset, sizeof(offset));
193 data += ntohs(offset);
197 len (byte = sizeof(length + address type + address)
198 address type (byte, ip address = 0x01, ip + port = 0x02,
199 ddp address = 0x03, fqdn = 0x04)
200 address (up to 254 bytes, ip = 4, ip + port = 6, ddp = 4)
203 /* number of addresses. this currently screws up if we have a dsi
204 connection, but we don't have the ip address. to get around this,
205 we turn off the status flag for tcp/ip. */
206 *data++ = (fqdn ? 1 : 0) + (dsi ? 1 : 0) + (asp ? 1 : 0);
208 /* handle DNS names */
210 int len = strlen(fqdn) + 2;
213 memcpy(data, fqdn, len);
219 const struct sockaddr_in *inaddr = &dsi->server;
221 if (inaddr->sin_port == htons(DSI_AFPOVERTCP_PORT)) {
222 *data++ = 6; /* length */
223 *data++ = 0x01; /* basic ip address */
224 memcpy(data, &inaddr->sin_addr.s_addr,
225 sizeof(inaddr->sin_addr.s_addr));
226 data += sizeof(inaddr->sin_addr.s_addr);
228 /* ip address + port */
230 *data++ = 0x02; /* ip address with port */
231 memcpy(data, &inaddr->sin_addr.s_addr,
232 sizeof(inaddr->sin_addr.s_addr));
233 data += sizeof(inaddr->sin_addr.s_addr);
234 memcpy(data, &inaddr->sin_port, sizeof(inaddr->sin_port));
235 data += sizeof(inaddr->sin_port);
241 const struct sockaddr_at *ddpaddr = atp_sockaddr(asp->asp_atp);
245 *data++ = 0x03; /* ddp address */
246 memcpy(data, &ddpaddr->sat_addr.s_net, sizeof(ddpaddr->sat_addr.s_net));
247 data += sizeof(ddpaddr->sat_addr.s_net);
248 memcpy(data, &ddpaddr->sat_addr.s_node,
249 sizeof(ddpaddr->sat_addr.s_node));
250 data += sizeof(ddpaddr->sat_addr.s_node);
251 memcpy(data, &ddpaddr->sat_port, sizeof(ddpaddr->sat_port));
252 data += sizeof(ddpaddr->sat_port);
254 #endif /* ! NO_DDP */
256 /* return length of buffer */
257 return (data - begin);
260 /* returns actual offset to signature */
261 static void status_icon(char *data, const unsigned char *icondata,
262 const int iconlen, const int sigoffset)
265 char *sigdata = data + sigoffset;
266 u_int16_t ret, status;
268 memcpy(&status, start + AFPSTATUS_ICONOFF, sizeof(status));
269 if ( icondata == NULL ) {
271 memset(start + AFPSTATUS_ICONOFF, 0, sizeof(status));
273 data += ntohs( status );
274 memcpy( data, icondata, iconlen);
276 ret = htons(data - start);
279 /* put in signature offset */
281 memcpy(sigdata, &ret, sizeof(ret));
284 void status_init(AFPConfig *aspconfig, AFPConfig *dsiconfig,
285 const struct afp_options *options)
292 if (!(aspconfig || dsiconfig) || !options)
296 asp = aspconfig->obj.handle;
297 status = aspconfig->status;
302 dsi = dsiconfig->obj.handle;
304 status = dsiconfig->status;
309 * These routines must be called in order -- earlier calls
310 * set the offsets for later calls.
312 * using structs is a bad idea, but that's what the original code
313 * does. solaris, in fact, will segfault with them. so, now
314 * we just use the powers of #defines and memcpy.
316 * reply block layout (offsets are 16-bit quantities):
317 * machine type offset -> AFP version count offset ->
318 * UAM count offset -> vol icon/mask offset -> flags ->
320 * server name [padded to even boundary] -> signature offset ->
321 * network address offset ->
323 * at the appropriate offsets:
324 * machine type, afp versions, uams, server signature
325 * (16-bytes), network addresses, volume icon/mask
328 status_flags(status, options->server_notif, options->fqdn ||
329 (dsiconfig && dsi->server.sin_addr.s_addr),
330 options->passwdbits);
331 /* returns offset to signature offset */
332 c = status_server(status, options->server ? options->server :
334 status_machine(status);
335 status_versions(status);
336 status_uams(status, options->uamlist);
337 if (options->flags & OPTION_CUSTOMICON)
338 status_icon(status, icon, sizeof(icon), c);
340 status_icon(status, apple_atalk_icon, sizeof(apple_atalk_icon), c);
342 sigoff = status_signature(status, &c, dsi, options->hostname);
345 c = status_netaddress(status, c, asp, dsi, options->fqdn);
350 asp_setstatus(asp, status, c);
351 aspconfig->signature = status + sigoff;
352 aspconfig->statuslen = c;
354 #endif /* ! NO_DDP */
357 if (aspconfig) { /* copy to dsiconfig */
358 memcpy(dsiconfig->status, status, ATP_MAXDATA);
359 status = dsiconfig->status;
362 if ((options->flags & OPTION_CUSTOMICON) == 0) {
363 status_icon(status, apple_tcp_icon, sizeof(apple_tcp_icon), 0);
365 dsi_setstatus(dsi, status, c);
366 dsiconfig->signature = status + sigoff;
367 dsiconfig->statuslen = c;
371 /* this is the same as asp/dsi_getstatus */
372 int afp_getsrvrinfo(obj, ibuf, ibuflen, rbuf, rbuflen )
375 int ibuflen, *rbuflen;
377 AFPConfig *config = obj->config;
379 memcpy(rbuf, config->status, config->statuslen);
380 *rbuflen = config->statuslen;