2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
12 #include <sys/param.h>
14 #include <sys/sysctl.h>
18 #include <netatalk/at.h>
19 #include <netatalk/endian.h>
20 #include <atalk/dsi.h>
21 #include <atalk/atp.h>
22 #include <atalk/asp.h>
23 #include <atalk/nbp.h>
25 #include "globals.h" /* includes <netdb.h> */
30 static void status_flags(char *data, const int ipok,
31 const unsigned char passwdbits)
35 status = AFPSRVRINFO_COPY;
36 if (passwdbits & PASSWD_SET) /* some uams may not allow this. */
37 status |= AFPSRVRINFO_PASSWD;
38 if (passwdbits & PASSWD_NOSAVE)
39 status |= AFPSRVRINFO_NOSAVEPASSWD;
40 status |= AFPSRVRINFO_SRVSIGNATURE;
41 /* only advertise tcp/ip if we have a valid address */
43 status |= AFPSRVRINFO_TCPIP;
44 status |= AFPSRVRINFO_SRVMSGS;
45 status |= AFPSRVRINFO_SRVNOTIFY;
46 status |= AFPSRVRINFO_FASTBOZO;
47 status = htons(status);
48 memcpy(data + AFPSTATUS_FLAGOFF, &status, sizeof(status));
51 static int status_server(char *data, const char *server)
54 char *Obj, *Type, *Zone;
58 /* make room for all offsets before server name */
59 data += AFPSTATUS_PRELEN;
61 /* extract the obj part of the server */
62 Obj = (char *) server;
63 nbp_name(server, &Obj, &Type, &Zone);
66 memcpy( data, Obj, len );
67 if ((len + 1) & 1) /* pad server name and length byte to even boundary */
71 /* make room for signature and net address offset. save location of
74 * NOTE: technically, we don't need to reserve space for the
75 * signature and net address offsets if they're not going to be
76 * used. as there are no offsets after them, it doesn't hurt to
77 * have them specified though. so, we just do that to simplify
81 status = htons(len + AFPSTATUS_POSTLEN);
82 memcpy(start + AFPSTATUS_MACHOFF, &status, sizeof(status));
83 return len; /* return the offset to beginning of signature offset */
86 static void status_machine(char *data)
92 const char *machine = "afs";
94 const char *machine = "unix";
97 memcpy(&status, start + AFPSTATUS_MACHOFF, sizeof(status));
98 data += ntohs( status );
99 len = strlen( machine );
101 memcpy( data, machine, len );
103 status = htons(data - start);
104 memcpy(start + AFPSTATUS_VERSOFF, &status, sizeof(status));
108 /* server signature is a 16-byte quantity */
109 static u_int16_t status_signature(char *data, int *servoffset, DSI *dsi,
110 const char *hostname)
114 u_int16_t offset, sigoff;
124 /* get server signature offset */
125 memcpy(&offset, data + *servoffset, sizeof(offset));
126 sigoff = offset = ntohs(offset);
128 /* jump to server signature offset */
131 /* 16-byte signature consists of copies of the hostid */
132 #if defined(BSD4_4) && defined(USE_GETHOSTID)
134 mib[1] = KERN_HOSTID;
135 len = sizeof(hostid);
136 sysctl(mib, 2, &hostid, &len, NULL, 0);
138 hostid = gethostid();
142 hostid = dsi->server.sin_addr.s_addr;
144 struct hostent *host;
146 if ((host = gethostbyname(hostname)))
147 hostid = ((struct in_addr *) host->h_addr)->s_addr;
151 /* it turns out that a server signature screws up separate
152 * servers running on the same machine. to work around that,
153 * i add in an increment */
156 for (i = 0; i < 16; i += sizeof(hostid)) {
157 memcpy(data, &hostid, sizeof(hostid));
158 data += sizeof(hostid);
161 /* calculate net address offset */
162 *servoffset += sizeof(offset);
163 offset = htons(data - status);
164 memcpy(status + *servoffset, &offset, sizeof(offset));
168 static int status_netaddress(char *data, const int servoffset,
169 const ASP asp, const DSI *dsi,
177 /* get net address offset */
178 memcpy(&offset, data + servoffset, sizeof(offset));
179 data += ntohs(offset);
183 len (byte = sizeof(length + address type + address)
184 address type (byte, ip address = 0x01, ip + port = 0x02,
185 ddp address = 0x03, fqdn = 0x04)
186 address (up to 254 bytes, ip = 4, ip + port = 6, ddp = 4)
189 /* number of addresses. this currently screws up if we have a dsi
190 connection, but we don't have the ip address. to get around this,
191 we turn off the status flag for tcp/ip. */
192 *data++ = (fqdn ? 1 : 0) + (dsi ? 1 : 0) + (asp ? 1 : 0);
194 /* handle DNS names */
196 int len = strlen(fqdn) + 2;
199 memcpy(data, fqdn, len);
205 const struct sockaddr_in *inaddr = &dsi->server;
207 if (inaddr->sin_port == htons(DSI_AFPOVERTCP_PORT)) {
208 *data++ = 6; /* length */
209 *data++ = 0x01; /* basic ip address */
210 memcpy(data, &inaddr->sin_addr.s_addr,
211 sizeof(inaddr->sin_addr.s_addr));
212 data += sizeof(inaddr->sin_addr.s_addr);
214 /* ip address + port */
216 *data++ = 0x02; /* ip address with port */
217 memcpy(data, &inaddr->sin_addr.s_addr,
218 sizeof(inaddr->sin_addr.s_addr));
219 data += sizeof(inaddr->sin_addr.s_addr);
220 memcpy(data, &inaddr->sin_port, sizeof(inaddr->sin_port));
221 data += sizeof(inaddr->sin_port);
227 const struct sockaddr_at *ddpaddr = atp_sockaddr(asp->asp_atp);
231 *data++ = 0x03; /* ddp address */
232 memcpy(data, &ddpaddr->sat_addr.s_net, sizeof(ddpaddr->sat_addr.s_net));
233 data += sizeof(ddpaddr->sat_addr.s_net);
234 memcpy(data, &ddpaddr->sat_addr.s_node,
235 sizeof(ddpaddr->sat_addr.s_node));
236 data += sizeof(ddpaddr->sat_addr.s_node);
237 memcpy(data, &ddpaddr->sat_port, sizeof(ddpaddr->sat_port));
238 data += sizeof(ddpaddr->sat_port);
242 /* return length of buffer */
243 return (data - begin);
246 /* returns actual offset to signature */
247 static void status_icon(char *data, const char *icondata,
248 const int iconlen, const int sigoffset)
251 char *sigdata = data + sigoffset;
252 u_int16_t ret, status;
254 memcpy(&status, start + AFPSTATUS_ICONOFF, sizeof(status));
255 if ( icondata == NULL ) {
257 memset(start + AFPSTATUS_ICONOFF, 0, sizeof(status));
259 data += ntohs( status );
260 memcpy( data, icondata, iconlen);
262 ret = htons(data - start);
265 /* put in signature offset */
267 memcpy(sigdata, &ret, sizeof(ret));
270 const void status_init(AFPConfig *aspconfig, AFPConfig *dsiconfig,
271 const struct afp_options *options)
278 if (!(aspconfig || dsiconfig) || !options)
282 asp = aspconfig->obj.handle;
283 status = aspconfig->status;
288 dsi = dsiconfig->obj.handle;
290 status = dsiconfig->status;
295 * These routines must be called in order -- earlier calls
296 * set the offsets for later calls.
298 * using structs is a bad idea, but that's what the original code
299 * does. solaris, in fact, will segfault with them. so, now
300 * we just use the powers of #defines and memcpy.
302 * reply block layout (offsets are 16-bit quantities):
303 * machine type offset -> AFP version count offset ->
304 * UAM count offset -> vol icon/mask offset -> flags ->
306 * server name [padded to even boundary] -> signature offset ->
307 * network address offset ->
309 * at the appropriate offsets:
310 * machine type, afp versions, uams, server signature
311 * (16-bytes), network addresses, volume icon/mask
314 status_flags(status, options->fqdn ||
315 (dsiconfig && dsi->server.sin_addr.s_addr),
316 options->passwdbits);
317 /* returns offset to signature offset */
318 c = status_server(status, options->server ? options->server :
320 status_machine(status);
321 status_versions(status);
322 status_uams(status, options->uamlist);
323 if (options->flags & OPTION_CUSTOMICON)
324 status_icon(status, icon, sizeof(icon), c);
326 status_icon(status, apple_atalk_icon, sizeof(apple_atalk_icon), c);
328 sigoff = status_signature(status, &c, dsi, options->hostname);
331 c = status_netaddress(status, c, asp, dsi, options->fqdn);
336 asp_setstatus(asp, status, c);
337 aspconfig->signature = status + sigoff;
338 aspconfig->statuslen = c;
343 if (aspconfig) { /* copy to dsiconfig */
344 memcpy(dsiconfig->status, status, ATP_MAXDATA);
345 status = dsiconfig->status;
348 if ((options->flags & OPTION_CUSTOMICON) == 0) {
349 status_icon(status, apple_tcp_icon, sizeof(apple_tcp_icon), 0);
351 dsi_setstatus(dsi, status, c);
352 dsiconfig->signature = status + sigoff;
353 dsiconfig->statuslen = c;
357 /* this is the same as asp/dsi_getstatus */
358 int afp_getsrvrinfo(obj, ibuf, ibuflen, rbuf, rbuflen )
361 int ibuflen, *rbuflen;
363 AFPConfig *config = obj->config;
365 memcpy(rbuf, config->status, config->statuslen);
366 *rbuflen = config->statuslen;