]> arthur.barton.de Git - netatalk.git/blob - etc/atalkd/main.c
fixed erroneous printf format arguments enclosed in 's instead "s.
[netatalk.git] / etc / atalkd / main.c
1 /*
2  * $Id: main.c,v 1.10 2001-09-12 19:13:17 uhees Exp $
3  *
4  * Copyright (c) 1990,1993 Regents of The University of Michigan.
5  * All Rights Reserved. See COPYRIGHT.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif /* HAVE_CONFIG_H */
11
12 #include <sys/param.h>
13 #include <sys/socket.h>
14 #if defined( sun ) && defined( __svr4__ )
15 #include </usr/ucbinclude/sys/file.h>
16 #else /* sun __svr4__ */
17 #include <sys/file.h>
18 #endif /* sun __svr4__ */
19 #include <sys/time.h>
20 #include <sys/resource.h>
21 #include <sys/ioctl.h>
22
23 /* POSIX.1 check */
24 #include <sys/types.h>
25 #ifdef HAVE_SYS_WAIT_H
26 #include <sys/wait.h>
27 #endif /* HAVE_SYS_WAIT_H */
28 #ifndef WEXITSTATUS 
29 #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
30 #endif /* ! WEXITSTATUS */
31 #ifndef WIFEXITED
32 #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
33 #endif /* ! WIFEXITED */
34
35 #include <errno.h>
36 #ifdef TRU64
37 #include <sys/mbuf.h>
38 #include <net/route.h>
39 #endif /* TRU64 */
40 #include <net/if.h>
41 #include <net/route.h>
42
43 #include <signal.h>
44 #include <syslog.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <netdb.h>
49 #include <fcntl.h>
50 #include <unistd.h>
51
52 #include <netatalk/endian.h>
53 #include <netatalk/at.h>
54 #include <atalk/compat.h>
55 #include <atalk/zip.h>
56 #include <atalk/rtmp.h>
57 #include <atalk/ddp.h>
58 #include <atalk/atp.h>
59 #include <atalk/paths.h>
60 #include <atalk/util.h>
61
62 #ifdef __svr4__
63 #include <sys/sockio.h>
64 #include <termios.h>
65 #endif /* __svr4__ */
66
67 #include "interface.h"
68 #include "gate.h"
69 #include "list.h"
70 #include "rtmp.h"
71 #include "zip.h"
72 #include "atserv.h"
73 #include "main.h"
74
75 /* Forward Declarations */
76 int ifconfig(const char *iname, unsigned long cmd, struct sockaddr_at *sa);
77
78 /* FIXME/SOCKLEN_T: socklen_t is a unix98 feature */
79 #ifndef SOCKLEN_T
80 #define SOCKLEN_T unsigned int
81 #endif /* SOCKLEN_T */
82
83 #ifndef WEXITSTATUS
84 #define WEXITSTATUS(x)  ((x).w_retcode)
85 #endif /* WEXITSTATUS */
86
87 /* linux has a special ioctl for appletalk device destruction.  as of
88  * 2.1.57, SIOCDIFADDR works w/ linux. okay, we need to deal with the
89  * fact that SIOCDIFADDR may be defined on linux despite the fact that
90  * it doesn't work. */
91 #if !defined(SIOCDIFADDR) && defined(SIOCATALKDIFADDR)
92 #define SIOCDIFADDR SIOCATALKDIFADDR
93 #endif
94
95 #define elements(a)     (sizeof(a)/sizeof((a)[0]))
96
97 #define PKTSZ   1024
98
99 extern int      rtmp_packet();
100 extern int      nbp_packet();
101 extern int      aep_packet();
102 extern int      zip_packet();
103
104 int             rtfd;
105
106 struct atserv   atserv[] = {
107     { "rtmp",           1,      rtmp_packet },          /* 0 */
108     { "nbp",            2,      nbp_packet },           /* 1 */
109     { "echo",           4,      aep_packet },           /* 2 */
110     { "zip",            6,      zip_packet },           /* 3 */
111 };
112 int             atservNATSERV = elements( atserv );
113
114 struct interface        *interfaces = NULL, *ciface = NULL;
115
116 int             debug = 0, quiet = 0, chatty = 0;
117 char            *configfile = NULL;
118 int             ziptimeout = 0, transition = 0;
119 int             stabletimer, stable = 0, newrtmpdata = 0, noparent = 0;
120 static int      ninterfaces;
121 int             defphase = IFACE_PHASE2;
122 int             nfds = 0;
123 fd_set          fds;
124 char            Packet[ PKTSZ ];
125 char            *version = VERSION;
126 static char     *pidfile = _PATH_ATALKDLOCK;
127
128
129 /* from config.c */
130
131 int readconf( char * );
132 int getifconf( void );
133 int writeconf( char * );
134
135 /* this is the messiest of the bunch as atalkd can exit pretty much
136  * everywhere. we delete interfaces here instead of in as_down. */
137 static void atalkd_exit(const int i)
138 {
139 #ifdef SIOCDIFADDR
140   struct interface *iface;
141
142   for (iface = interfaces; iface; iface = iface->i_next) {
143     if (ifconfig( iface->i_name, SIOCDIFADDR, &iface->i_addr)) {
144 #ifdef SIOCATALKDIFADDR
145 #if (SIOCDIFADDR != SIOCATALKDIFADDR)
146       if (!ifconfig(iface->i_name, SIOCATALKDIFADDR, &iface->i_addr)) 
147         continue;
148 #endif /* SIOCDIFADDR != SIOCATALKDIFADDR */
149 #endif /* SIOCATALKIFADDR */
150       syslog( LOG_ERR, "difaddr(%u.%u): %m", 
151               ntohs(iface->i_addr.sat_addr.s_net), 
152               iface->i_addr.sat_addr.s_node);
153     }
154   }
155 #endif /* SOPCDOFADDR */
156
157   server_unlock(pidfile);
158   exit(i);
159 }
160
161
162 #if !defined( ibm032 ) && !defined( _IBMR2 )
163     void
164 #endif /* ibm032 _IBMR2 */
165 as_timer()
166 {
167     struct sockaddr_at  sat;
168     struct ziphdr       zh;
169     struct rtmp_head    rh;
170     struct rtmp_tuple   rt;
171     struct atport       *ap, *zap, *rap;
172     struct interface    *iface, *iface2;
173     struct gate         *gate, *fgate = NULL;
174     struct rtmptab      *rtmp, *frtmp;
175     struct ziptab       *zt;
176     char                *data, *end, packet[ ATP_BUFSIZ ];
177     int                 sentzipq = 0;
178     int                 n, cc;
179
180     memset(&sat, 0, sizeof( struct sockaddr_at ));
181     for ( iface = interfaces; iface; iface = iface->i_next ) {
182         if ( iface->i_flags & IFACE_LOOPBACK ) {
183             continue;
184         }
185         for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
186             if ( ap->ap_packet == zip_packet ) {
187                 zap = ap;
188             }
189             if ( ap->ap_packet == rtmp_packet ) {
190                 rap = ap;
191             }
192         }
193
194         if (( iface->i_flags & ( IFACE_ADDR|IFACE_CONFIG|IFACE_NOROUTER )) ==
195                 IFACE_ADDR ) {
196             if ( iface->i_time < 3 ) {
197                 if ( iface->i_flags & IFACE_PHASE1 ) {
198                   if (rtmp_request( iface ) < 0) {
199                       syslog(LOG_ERR, "rtmp_request: %m");
200                       atalkd_exit(1);
201                   }
202                     newrtmpdata = 1;
203                 } else {
204                   if (zip_getnetinfo( iface ) < 0) {
205                     syslog(LOG_ERR, "zip_getnetinfo: %m");
206                     atalkd_exit(1);
207                   }
208                   sentzipq = 1;
209                 }
210                 iface->i_time++;
211             } else {
212                 iface->i_flags |= IFACE_NOROUTER;
213                 if ((iface->i_flags & IFACE_ISROUTER)) {
214                     if (( iface->i_flags & IFACE_SEED ) == 0 ) {
215                         /*
216                          * No seed info, and we've got multiple interfaces.
217                          * Wait forever.
218                          */
219                         syslog( LOG_INFO,
220                                 "as_timer multiple interfaces, no seed" );
221                         syslog( LOG_INFO, "as_timer can't configure %s",
222                                 iface->i_name );
223                         syslog( LOG_INFO, "as_timer waiting for router" );
224                         iface->i_time = 0;
225                         continue;
226                     } else {
227                         /*
228                          * Complete configuration for iface, and boot next
229                          * interface.
230                          */
231                         iface->i_flags |= IFACE_CONFIG;
232                         for ( zt = iface->i_czt; zt; zt = zt->zt_next ) {
233                             if (addzone( iface->i_rt, zt->zt_len, 
234                                          zt->zt_name) < 0) {
235                               syslog(LOG_ERR, "addzone: %m");
236                               atalkd_exit(1);
237                             }
238                         }
239                         if ( iface->i_rt->rt_zt ) {
240                             iface->i_rt->rt_flags &= ~RTMPTAB_ZIPQUERY;
241                             iface->i_rt->rt_flags |= RTMPTAB_HASZONES;
242                         }
243                         if ( iface->i_flags & IFACE_PHASE1 ) {
244                             syslog( LOG_INFO,
245                                     "as_timer configured %s phase 1 from seed",
246                                     iface->i_name );
247                             setaddr( iface, IFACE_PHASE1,
248                                     iface->i_caddr.sat_addr.s_net,
249                                     iface->i_addr.sat_addr.s_node,
250                                     iface->i_caddr.sat_addr.s_net,
251                                     iface->i_caddr.sat_addr.s_net );
252                         } else {
253                             syslog( LOG_INFO,
254                                     "as_timer configured %s phase 2 from seed",
255                                     iface->i_name );
256                         }
257
258                         if ( looproute( iface, RTMP_ADD )) { /* -1 or 1 */
259                             syslog( LOG_ERR,
260                                     "as_timer: can't route %u.%u to loop: %m",
261                                     ntohs( iface->i_addr.sat_addr.s_net ),
262                                     iface->i_addr.sat_addr.s_node );
263                             atalkd_exit( 1 );
264                         }
265                         if ( iface == ciface ) {
266                             ciface = ciface->i_next;
267                             bootaddr( ciface );
268                         }
269                     }
270                 } else {
271                     /*
272                      * Configure for no router operation.  Wait for a route
273                      * to become available in rtmp_packet().
274                      */
275                     syslog( LOG_INFO, "config for no router" );
276                       
277                     if ( iface->i_flags & IFACE_PHASE2 ) {
278                         iface->i_rt->rt_firstnet = 0;
279                         iface->i_rt->rt_lastnet = htons( STARTUP_LASTNET );
280                         setaddr( iface, IFACE_PHASE2,
281                                 iface->i_addr.sat_addr.s_net,
282                                 iface->i_addr.sat_addr.s_node,
283                                 0, htons( STARTUP_LASTNET ));
284                     }
285                     if ( looproute( iface, RTMP_ADD ) ) { /* -1 or 1 */
286                         syslog( LOG_ERR,
287                                 "as_timer: can't route %u.%u to loopback: %m",
288                                 ntohs( iface->i_addr.sat_addr.s_net ),
289                                 iface->i_addr.sat_addr.s_node );
290                         atalkd_exit( 1 );
291                     }
292
293                     if ( iface == ciface ) {
294                       ciface = ciface->i_next;
295                       bootaddr( ciface );
296                     }
297                 }
298             }
299         }
300
301         for ( gate = iface->i_gate; gate; gate = gate->g_next ) {
302             if ( fgate ) {
303                 free( (caddr_t)fgate );
304                 fgate = NULL;
305             }
306
307             n = 0;
308             data = packet + 1 + sizeof( struct ziphdr );
309             end = packet + sizeof( packet );
310
311             sat = gate->g_sat;
312             sat.sat_port = zap->ap_port;
313
314             /*
315              * Perform timeouts on routers.  If we've only got one
316              * interface, we'll use these timeouts to decide that
317              * our zone has gone away.
318              */
319             if ( ++gate->g_state >= RTMPTAB_BAD ) {
320                 syslog( LOG_INFO, "as_timer gateway %u.%u down",
321                         ntohs( gate->g_sat.sat_addr.s_net ),
322                         gate->g_sat.sat_addr.s_node );
323                 rtmp = gate->g_rt;
324                 while ( rtmp ) {
325                     frtmp = rtmp->rt_next;
326                     if ( rtmp->rt_hops == RTMPHOPS_POISON ||
327                             rtmp->rt_iprev == 0 ) {
328                         rtmp_free( rtmp );
329                     } else {
330                         rtmp->rt_hops = RTMPHOPS_POISON;
331                         if ((cc = rtmp_replace( rtmp )) < 0) {
332                           syslog(LOG_ERR, "rtmp_replace: %m");
333                           atalkd_exit(1);
334                         }
335                         if (cc) {
336                             gate->g_state = rtmp->rt_state = RTMPTAB_GOOD;
337                         }
338                     }
339                     rtmp = frtmp;
340                 }
341                 if ( gate->g_rt == 0 ) {
342                     if ( gate->g_prev == 0 ) {
343                         gate->g_iface->i_gate = gate->g_next;
344                     } else {
345                         gate->g_prev->g_next = gate->g_next;
346                     }
347                     if ( gate->g_next != 0 ) {
348                         gate->g_next->g_prev = gate->g_prev;
349                     }
350                     fgate = gate;       /* can't free here, just mark it */
351                 }
352                 /*
353                  * If this is the last router on the only interface,
354                  * reconfigure our netrange.  By marking the interface
355                  * as having no router, we will notice when a router
356                  * comes back up.
357                  *
358                  * XXX: actually, we always reconfigure an interface
359                  * if we're not a seed router.
360                  */
361
362                 if ( gate->g_iface->i_gate == 0 && 
363                      ((iface->i_flags & IFACE_SEED) == 0)) {
364                     gate->g_iface->i_flags |= IFACE_NOROUTER;
365                     gate->g_iface->i_flags &= ~IFACE_CONFIG;
366
367                     /* get rid of any zones associated with this iface */
368                     if (gate->g_iface->i_rt->rt_zt) {
369                       rtmp_delzonemap(gate->g_iface->i_rt);
370                       gate->g_iface->i_rt->rt_flags &= ~RTMPTAB_HASZONES;
371                     }
372
373                     syslog( LOG_INFO, "as_timer last gateway down" );
374
375                     /* Set netrange to 0-fffe.  */
376                     if ( gate->g_iface->i_flags & IFACE_PHASE2 ) {
377                         gate->g_iface->i_rt->rt_firstnet = 0;
378                         gate->g_iface->i_rt->rt_lastnet =
379                                 htons( STARTUP_LASTNET );
380                         setaddr( iface, IFACE_PHASE2,
381                                 iface->i_addr.sat_addr.s_net,
382                                 iface->i_addr.sat_addr.s_node,
383                                 0, htons( STARTUP_LASTNET ));
384                     }
385                 }
386                 continue;
387             }
388
389             /*
390              * If we don't have a zone for our interface yet, ask for
391              * it from any router (all routers) on the interface.
392              */
393             if (( iface->i_rt->rt_flags & RTMPTAB_HASZONES ) == 0 ) {
394                 iface->i_rt->rt_flags |= RTMPTAB_ZIPQUERY;
395                 memcpy( data, &iface->i_rt->rt_firstnet, sizeof( u_short ));
396                 data += sizeof( u_short );
397                 n++;
398             }
399
400             rtmp = gate->g_rt;
401             while ( rtmp ) {
402                 /*
403                  * Delete old routing tuples.
404                  */
405                 if ( rtmp->rt_state != RTMPTAB_PERM ) {
406                     rtmp->rt_state++;
407                 }
408
409                 /*
410                  * We've not been updated for this route in a while.  If
411                  * it's not in use, go ahead and remove it.  If it is in
412                  * use, mark the route as down (POISON), and look for a
413                  * better route.  If one is found, delete this route and use
414                  * the new one.  If it's not found, mark the route as GOOD
415                  * (so we'll propogate our poison) and delete it the next
416                  * time it becomes BAD.
417                  */
418                 if ( rtmp->rt_state >= RTMPTAB_BAD ) {
419                     frtmp = rtmp->rt_next;
420                     if ( rtmp->rt_iprev == 0 ) {        /* not in use */
421                         rtmp_free( rtmp );
422                     } else {                            /* in use */
423                         if ( rtmp->rt_hops == RTMPHOPS_POISON ) {
424                             rtmp_free( rtmp );
425                         } else {
426                             rtmp->rt_hops = RTMPHOPS_POISON;
427                             if ((cc = rtmp_replace( rtmp )) < 0) {
428                                 syslog(LOG_ERR, "rtmp_replace: %m");
429                                 atalkd_exit(1);
430                             }
431                             if (cc)
432                                 rtmp->rt_state = RTMPTAB_GOOD;
433                         }
434                     }
435                     rtmp = frtmp;
436                     continue;
437                 }
438
439                 /*
440                  * Do ZIP lookups.
441                  */
442                 if ( rtmp->rt_iprev &&
443                         ( rtmp->rt_flags & RTMPTAB_HASZONES ) == 0 ) {
444                     if ( data + sizeof( u_short ) > end || n == 255 ) {
445                         /* send what we've got */
446                         zh.zh_op = ZIPOP_QUERY;
447                         zh.zh_count = n;
448                         cc = data - packet;
449                         data = packet;
450                         *data++ = DDPTYPE_ZIP;
451                         memcpy( data, &zh, sizeof( struct ziphdr ));
452
453                         if ( sendto( zap->ap_fd, packet, cc, 0,
454                                 (struct sockaddr *)&sat,
455                                 sizeof( struct sockaddr_at )) < 0 ) {
456                             syslog( LOG_ERR, "as_timer sendto: %m" );
457                         }
458                         sentzipq = 1;
459
460                         n = 0;
461                         data = packet + 1 + sizeof( struct ziphdr );
462                         end = packet + sizeof( packet );
463                     }
464
465                     /*
466                      * rt_nzq is number of ZIP Queries we've issued for a
467                      * given netrange.  If we've got ziptimeout on, we
468                      * will only ask 3 times for any given netrange.
469                      * Interestingly enough, since rt_nzq is a u_char,
470                      * it will overflow after a while.  This means we will
471                      * periodically ask for nets that we've decided not to
472                      * ask about, and warn that we can't get it's zone.
473                      */
474                     if ( rtmp->rt_nzq++ == 3 ) {
475                         syslog( LOG_INFO, "as_timer can't get zone for %u",
476                                 ntohs( rtmp->rt_firstnet ));
477                     }
478                     if ( rtmp->rt_nzq > 3 ) {
479                         if ( ziptimeout ) {
480                             rtmp = rtmp->rt_next;
481                             continue;
482                         }
483                     } else {
484                         sentzipq = 1;
485                     }
486                     rtmp->rt_flags |= RTMPTAB_ZIPQUERY;
487                     memcpy( data, &rtmp->rt_firstnet, sizeof( u_short ));
488                     data += sizeof( u_short );
489                     n++;
490                 }
491                 rtmp = rtmp->rt_next;
492             }
493
494             /* send what we've got */
495             if ( n > 0 ) {
496                 zh.zh_op = ZIPOP_QUERY;
497                 zh.zh_count = n;
498                 cc = data - packet;
499                 data = packet;
500                 *data++ = DDPTYPE_ZIP;
501                 memcpy( data, &zh, sizeof( struct ziphdr ));
502
503                 if ( sendto( zap->ap_fd, packet, cc, 0, (struct sockaddr *)&sat,
504                         sizeof( struct sockaddr_at )) < 0 ) {
505                     syslog( LOG_ERR, "as_timer sendto: %m" );
506                 }
507             }
508         }
509         if ( fgate ) {
510             free( (caddr_t)fgate );
511             fgate = NULL;
512         }
513
514         /*
515          * Send RTMP broadcasts if we have multiple interfaces or our 
516          * interface is configured as a router.  
517          */
518         if ((iface->i_flags & IFACE_ISROUTER)) {
519 #ifdef BSD4_4
520             sat.sat_len = sizeof( struct sockaddr_at );
521 #endif /* BSD4_4 */
522             sat.sat_family = AF_APPLETALK;
523             sat.sat_addr.s_net = ATADDR_ANYNET;
524             sat.sat_addr.s_node = ATADDR_BCAST;
525             sat.sat_port = rap->ap_port;
526
527             data = packet;
528             end = data + sizeof( packet );
529             *data++ = DDPTYPE_RTMPRD;
530             rh.rh_net = iface->i_addr.sat_addr.s_net;
531             rh.rh_nodelen = 8;
532             rh.rh_node = iface->i_addr.sat_addr.s_node;
533             memcpy( data, &rh, sizeof( struct rtmp_head ));
534             data += sizeof( struct rtmp_head );
535             n = 0;
536
537
538             if ( iface->i_flags & IFACE_PHASE1 ) {
539                 rt.rt_net = 0;
540                 rt.rt_dist = 0x82;
541                 memcpy( data, &rt, SZ_RTMPTUPLE );
542                 data += SZ_RTMPTUPLE;
543             } else {
544                 rt.rt_net = iface->i_rt->rt_firstnet;
545                 rt.rt_dist = 0x80;
546                 memcpy( data, &rt, SZ_RTMPTUPLE );
547                 data += SZ_RTMPTUPLE;
548
549                 rt.rt_net = iface->i_rt->rt_lastnet;
550                 rt.rt_dist = 0x82;
551                 memcpy( data, &rt, SZ_RTMPTUPLE );
552                 data += SZ_RTMPTUPLE;
553             }
554
555             for ( iface2 = interfaces; iface2; iface2 = iface2->i_next ) {
556
557               /* XXX: there used to be a bit checking against iface ==
558                  iface2. also, we don't want to send an rtmp broadcast
559                  to an interface that doesn't want it.  */
560                 if ((( iface2->i_flags & IFACE_CONFIG ) == 0) ||
561                     ((iface2->i_flags & IFACE_ISROUTER) == 0)) {
562                     continue;
563                 }
564                 /*
565                  * Fill in tuples.  Always send the same thing, regardless
566                  * of the phase of the destination.  Routers who don't
567                  * understand extended rtmp packets will toss extended
568                  * tuples because their distance will have the high bit set.
569                  */
570                 for ( rtmp = iface2->i_rt; rtmp; rtmp = rtmp->rt_inext ) {
571                     /* don't broadcast routes we have no zone for */
572                     if ( rtmp->rt_zt == 0 ||
573                             ( rtmp->rt_flags & RTMPTAB_ZIPQUERY ) ||
574                             ( rtmp->rt_flags & RTMPTAB_HASZONES ) == 0 ) {
575                         continue;
576                     }
577
578                     if ((( rtmp->rt_flags & RTMPTAB_EXTENDED ) &&
579                             data + 2 * SZ_RTMPTUPLE > end ) ||
580                             data + SZ_RTMPTUPLE > end ) {
581                         if ( sendto( rap->ap_fd, packet, data - packet, 0,
582                                 (struct sockaddr *)&sat,
583                                 sizeof( struct sockaddr_at )) < 0 ) {
584                             syslog( LOG_ERR, "as_timer sendto %u.%u (%u): %m",
585                                     ntohs( sat.sat_addr.s_net ),
586                                     sat.sat_addr.s_node,
587                                     ntohs( iface->i_rt->rt_firstnet ));
588                         }
589
590                         if ( iface->i_flags & IFACE_PHASE2 ) {
591                             data = packet + 1 + sizeof( struct rtmp_head ) +
592                                     2 * SZ_RTMPTUPLE;
593                         } else {
594                             data = packet + 1 + sizeof( struct rtmp_head ) +
595                                     SZ_RTMPTUPLE;
596                         }
597                         n = 0;
598                     }
599
600                     rt.rt_net = rtmp->rt_firstnet;
601                     rt.rt_dist = rtmp->rt_hops;
602                     if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
603                         rt.rt_dist |= 0x80;
604                     }
605                     memcpy( data, &rt, SZ_RTMPTUPLE );
606                     data += SZ_RTMPTUPLE;
607
608                     if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
609                         rt.rt_net = rtmp->rt_lastnet;
610                         rt.rt_dist = 0x82;
611                         memcpy( data, &rt, SZ_RTMPTUPLE );
612                         data += SZ_RTMPTUPLE;
613                     }
614                     n++;
615                 }
616             }
617
618             /* send rest */
619             if ( n ) {
620                 if ( sendto( rap->ap_fd, packet, data - packet, 0,
621                         (struct sockaddr *)&sat,
622                         sizeof( struct sockaddr_at )) < 0 ) {
623                     syslog( LOG_ERR, "as_timer sendto %u.%u (%u): %m",
624                             ntohs( sat.sat_addr.s_net ),
625                             sat.sat_addr.s_node,
626                             ntohs( iface->i_rt->rt_firstnet ));
627                 }
628             }
629         }
630     }
631
632     /*
633      * Check if we're stable.  Each time we configure an interface, we
634      * sent stabletimer to UNSTABLE.  If stabletimer ever gets to
635      * STABLEANYWAY, we give up and decide to "be" stable anyway.
636      * Normally, we wait for stabletimer get <= STABLE with no new rtmp
637      * data and all zip data complete.
638      */
639     if ( !stable ) {
640         if ( stabletimer <= STABLE && !newrtmpdata && !sentzipq ) {
641             /* write out config file */
642             stable = 1;
643             writeconf( configfile );
644         } else {
645             if ( stabletimer-- <= STABLEANYWAY ) {
646                 stable = 1;
647             }
648         }
649         newrtmpdata = 0;
650
651         if ( stable && !noparent ) {
652             noparent = 1;
653             syslog( LOG_INFO, "ready %d/%d/%d", stabletimer, newrtmpdata,
654                     sentzipq );
655             if ( !debug ) {
656                 /*
657                  * Seems like we could get here more than once...
658                  */
659                 if ( kill( getpid(), SIGSTOP ) < 0 ) {
660                     syslog( LOG_ERR, "as_timer: kill-self failed!" );
661                     atalkd_exit( 1 );
662                 }
663             }
664         }
665     }
666
667 #ifdef DEBUG
668     consistency();
669 #endif /* DEBUG */
670 }
671
672 #ifdef DEBUG
673 /*
674 * Consistency check...
675 */
676 consistency()
677 {
678     struct rtmptab      *rtmp;
679     struct list         *lr, *lz;
680     struct ziptab       *zt;
681
682     for ( zt = ziptab; zt; zt = zt->zt_next ) {
683         for ( lr = zt->zt_rt; lr; lr = lr->l_next ) {
684             rtmp = (struct rtmptab *)lr->l_data;
685             if ( rtmp->rt_iprev == 0 && rtmp->rt_gate != 0 ) {
686                 syslog( LOG_ERR, "%.*s has %u-%u (unused)\n",
687                         zt->zt_len, zt->zt_name, ntohs( rtmp->rt_firstnet ),
688                         ntohs( rtmp->rt_lastnet ));
689                 atalkd_exit(1);
690             }
691             for ( lz = rtmp->rt_zt; lz; lz = lz->l_next ) {
692                 if ( zt == (struct ziptab *)lz->l_data ) {
693                     break;
694                 }
695             }
696             if ( lz == 0 ) {
697                 syslog( LOG_ERR, "no map from %u-%u to %.*s\n", 
698                         ntohs( rtmp->rt_firstnet ),
699                         ntohs( rtmp->rt_lastnet ),
700                         zt->zt_len, zt->zt_name );
701                 atalkd_exit(1);
702             }
703         }
704     }
705 }
706 #endif /* DEBUG */
707
708 #if !defined( ibm032 ) && !defined( _IBMR2 )
709     void
710 #endif /* ! ibm032 && ! _IBMR2 */
711 as_debug()
712 {
713     struct interface    *iface;
714     struct list         *l;
715     struct ziptab       *zt;
716     struct gate         *gate;
717     struct rtmptab      *rt;
718     FILE                *rtmpdebug;
719
720     if (( rtmpdebug = fopen( _PATH_ATALKDEBUG, "w" )) == NULL ) {
721         syslog( LOG_ERR, "rtmp: %m" );
722     }
723
724     for ( iface = interfaces; iface; iface = iface->i_next ) {
725         fprintf( rtmpdebug, "interface %s %u.%u ", iface->i_name,
726                 ntohs( iface->i_addr.sat_addr.s_net ),
727                 iface->i_addr.sat_addr.s_node );
728         if ( iface->i_flags & IFACE_PHASE1 ) {
729             putc( '1', rtmpdebug );
730         }
731         if ( iface->i_flags & IFACE_PHASE2 ) {
732             putc( '2', rtmpdebug );
733         }
734         if ( iface->i_flags & IFACE_RSEED ) {
735             putc( 'R', rtmpdebug );
736         }
737         if ( iface->i_flags & IFACE_SEED ) {
738             putc( 'S', rtmpdebug );
739         }
740         if ( iface->i_flags & IFACE_DONTROUTE ) {
741             putc( 'D', rtmpdebug );
742         }
743         if ( iface->i_flags & IFACE_ADDR ) {
744             putc( 'A', rtmpdebug );
745         }
746         if ( iface->i_flags & IFACE_CONFIG ) {
747             putc( 'C', rtmpdebug );
748         }
749         if ( iface->i_flags & IFACE_NOROUTER ) {
750             putc( 'N', rtmpdebug );
751         }
752         if ( iface->i_flags & IFACE_LOOP ) {
753             putc( 'L', rtmpdebug );
754         }
755         putc( '\n', rtmpdebug );
756
757         if ( iface->i_rt ) {
758             fprintf( rtmpdebug, "\t%u-%u ",
759                     ntohs( iface->i_rt->rt_firstnet ),
760                     ntohs( iface->i_rt->rt_lastnet ));
761             if ( iface->i_rt->rt_flags & RTMPTAB_ZIPQUERY ) {
762                 putc( 'q', rtmpdebug );
763             }
764             if ( iface->i_rt->rt_flags & RTMPTAB_HASZONES ) {
765                 putc( 'z', rtmpdebug );
766             }
767             if ( iface->i_rt->rt_flags & RTMPTAB_EXTENDED ) {
768                 putc( 'x', rtmpdebug );
769             }
770             putc( 'i', rtmpdebug );
771             for ( l = iface->i_rt->rt_zt; l; l = l->l_next ) {
772                 zt = (struct ziptab *)l->l_data;
773                 fprintf( rtmpdebug, " '%.*s'", zt->zt_len, zt->zt_name );
774             }
775             fprintf( rtmpdebug, "\n" );
776         }
777
778         for ( gate = iface->i_gate; gate; gate = gate->g_next ) {
779             fprintf( rtmpdebug, "gate %u.%u %X\n",
780                     ntohs( gate->g_sat.sat_addr.s_net ),
781                     gate->g_sat.sat_addr.s_node, gate->g_state );
782             for ( rt = gate->g_rt; rt; rt = rt->rt_next ) {
783                 fprintf( rtmpdebug, "\t%u-%u ", ntohs( rt->rt_firstnet ),
784                         ntohs( rt->rt_lastnet ));
785                 if ( rt->rt_flags & RTMPTAB_ZIPQUERY ) {
786                     putc( 'q', rtmpdebug );
787                 }
788                 if ( rt->rt_flags & RTMPTAB_HASZONES ) {
789                     putc( 'z', rtmpdebug );
790                 }
791                 if ( rt->rt_flags & RTMPTAB_EXTENDED ) {
792                     putc( 'x', rtmpdebug );
793                 }
794                 if ( rt->rt_iprev ) {
795                     putc( 'i', rtmpdebug );
796                 }
797                 for ( l = rt->rt_zt; l; l = l->l_next ) {
798                     zt = (struct ziptab *)l->l_data;
799                     fprintf( rtmpdebug, " '%.*s'", zt->zt_len, zt->zt_name );
800                 }
801                 fprintf( rtmpdebug, "\n" );
802             }
803         }
804     }
805
806     fclose( rtmpdebug );
807 }
808
809 /*
810  * Called when SIGTERM is recieved.  Remove all routes and then exit.
811  */
812 #if !defined( ibm032 ) && !defined( _IBMR2 )
813     void
814 #endif /* ! ibm032 && ! _IBMR2 */
815 as_down()
816 {
817     struct interface    *iface;
818     struct gate         *gate;
819     struct rtmptab      *rt;
820
821     for ( iface = interfaces; iface; iface = iface->i_next ) {
822         for ( gate = iface->i_gate; gate; gate = gate->g_next ) {
823             for ( rt = gate->g_rt; rt; rt = rt->rt_next ) {
824                 if ( rt->rt_iprev ) {
825                     if ( gateroute( RTMP_DEL, rt ) < 0 ) {
826                         syslog( LOG_ERR, "as_down remove %u-%u failed: %m",
827                                 ntohs( rt->rt_firstnet ),
828                                 ntohs( rt->rt_lastnet ));
829                     }
830                 }
831             }
832         }
833         if ( iface->i_flags & IFACE_LOOP ) {
834           if (looproute( iface, RTMP_DEL )) {
835             syslog( LOG_ERR, "as_down remove %s %u.%u failed: %m",
836                     iface->i_name, ntohs( iface->i_addr.sat_addr.s_net ),
837                     iface->i_addr.sat_addr.s_node );
838           }
839         }
840     }
841
842     syslog( LOG_INFO, "done" );
843     atalkd_exit( 0 );
844 }
845
846 int main( ac, av )
847     int         ac;
848     char        **av;
849 {
850     extern char         *optarg;
851     extern int          optind;
852
853     struct sockaddr_at  sat;
854     struct sigaction    sv;
855     struct itimerval    it;
856     struct interface    *iface;
857     int                 status;
858     struct atport       *ap;
859     fd_set              readfds;
860     int                 i, mask, c;
861     SOCKLEN_T           fromlen;
862     char                *prog;
863 ;
864
865     while (( c = getopt( ac, av, "12qsdtf:P:v" )) != EOF ) {
866         switch ( c ) {
867         case '1' :
868             defphase = IFACE_PHASE1;
869             break;
870
871         case '2' :
872             defphase = IFACE_PHASE2;
873             break;
874
875         case 'd' :
876             debug++;
877             break;
878
879         case 'f' :
880             configfile = optarg;
881             break;
882
883         case 'q' :      /* don't seed */
884             quiet++;
885             break;
886
887         case 's' :      /* seed */
888             chatty++;
889             break;
890
891         case 't' :      /* transition */
892             transition++;
893             break;
894
895         case 'P' :      /* pid file */
896             pidfile = optarg;
897             break;
898
899         case 'v' :      /* version */
900             printf( "atalkd (version %s)\n", version );
901             exit ( 1 );
902             break;
903
904         default :
905             fprintf( stderr, "Unknown option -- '%c'\n", c );
906             exit( 1 );
907         }
908     }
909     if ( optind != ac ) {
910         fprintf( stderr, "Too many arguments.\n" );
911         exit( 1 );
912     }
913
914     if (( prog = strrchr( av[ 0 ], '/' )) == NULL ) {
915         prog = av[ 0 ];
916     } else {
917         prog++;
918     }
919
920     /*
921      * Configure loop back address first, so appearances of "lo0" in
922      * the config file fail.  Also insures that lo0 gets configured,
923      * even if there's some hangup during configuration of some
924      * other interface.
925      */
926     if (( interfaces = newiface( LOOPIFACE )) == NULL ) {
927         perror( "newiface" );
928         exit( 1 );
929     }
930     interfaces->i_flags |= IFACE_PHASE2 | IFACE_LOOPBACK;
931
932     /*
933      * Check our initial configuration before we fork. This way we can
934      * complain about syntax errors on stdout.
935      *
936      * Basically, if we're going to read our config file, we should read
937      * it and initialize our data structures. If we're not going to read
938      * our config file, use GIFCONF to initialize our data structures.
939      */
940     if ( readconf( configfile ) < 0 && getifconf() < 0 ) {
941         fprintf( stderr, "%s: can't get interfaces, exiting.\n", prog );
942         exit( 1 );
943     }
944
945     /* we need to count up our interfaces so that we can simplify things
946      * later. we also need to figure out if we have more than one interface
947      * that is routing. */
948     for (i = 0, ninterfaces = 0, iface = interfaces; iface;
949          iface=iface->i_next) {
950       if (iface->i_flags & IFACE_DONTROUTE)
951         i++;
952       ninterfaces++;
953     }
954     i = ninterfaces - i; /* number of routable interfaces */
955
956     /*
957      * At this point, we have (at least partially) initialized data
958      * structures. Fill in what we can and verify that nothing is obviously
959      * broken.
960      */
961     for (iface = interfaces; iface; iface = iface->i_next) {
962         /* Apply the default phase */
963         if (( iface->i_flags & IFACE_PHASE1 ) == 0 &&
964                 ( iface->i_flags & IFACE_PHASE2 ) == 0 ) {
965             iface->i_flags |= defphase;
966         }
967
968         /* set up router flag information. if we have multiple interfaces
969          * and DONTROUTE isn't set, set up ROUTER. i is the number of 
970          * interfaces that don't have the DONTROUTE flag set. */
971         if ((i > IFBASE) && ((iface->i_flags & IFACE_DONTROUTE) == 0)) {
972           iface->i_flags |= IFACE_ISROUTER;
973         }
974
975         /* Set default addresses */
976         if ( iface->i_rt == NULL ) {
977             if (( iface->i_rt = newrt(iface)) == NULL ) {
978                 perror( "newrt" );
979                 exit( 1 );
980             }
981
982             if ( iface->i_flags & IFACE_PHASE1 ) {
983                 iface->i_rt->rt_firstnet = iface->i_rt->rt_lastnet =
984                         iface->i_caddr.sat_addr.s_net;
985             } else {
986                 if ( iface->i_caddr.sat_addr.s_net != ATADDR_ANYNET ||
987                         ( iface->i_flags & IFACE_LOOPBACK )) {
988                     iface->i_rt->rt_firstnet = iface->i_rt->rt_lastnet =
989                             iface->i_caddr.sat_addr.s_net;
990                 } else {
991                     iface->i_rt->rt_firstnet = htons( STARTUP_FIRSTNET );
992                     iface->i_rt->rt_lastnet = htons( STARTUP_LASTNET );
993                 }
994             }
995         }
996         
997         if (( iface->i_flags & IFACE_PHASE1 ) == 0 ) {
998             iface->i_rt->rt_flags |= RTMPTAB_EXTENDED;
999         }
1000
1001         if ( iface->i_caddr.sat_addr.s_net == ATADDR_ANYNET ) {
1002             iface->i_caddr.sat_addr.s_net = iface->i_rt->rt_firstnet;
1003         }
1004
1005         if ( debug ) {
1006             dumpconfig( iface );        /* probably needs args */
1007         }
1008     }
1009
1010     /*
1011      * A little consistency check...
1012      */
1013     if ( ninterfaces < IFBASE ) {
1014         fprintf( stderr, "%s: zero interfaces, exiting.\n", prog );
1015         exit( 1 );
1016     }
1017
1018     /* do this here so that we can use ifconfig */
1019 #ifdef __svr4__
1020     if ( plumb() < 0 ) {
1021         fprintf(stderr, "can't establish STREAMS plumbing, exiting.\n" );
1022         atalkd_exit( 1 );
1023     }
1024 #endif /* __svr4__ */
1025
1026     /* delete pre-existing interface addresses. */
1027 #ifdef SIOCDIFADDR
1028     for (iface = interfaces; iface; iface = iface->i_next) {
1029       if (ifconfig(iface->i_name, SIOCDIFADDR, &iface->i_addr)) {
1030 #ifdef SIOCATALKDIFADDR
1031 #if (SIOCDIFADDR != SIOCATALKDIFADDR)
1032         ifconfig(iface->i_name, SIOCATALKDIFADDR, &iface->i_addr);
1033 #endif /* SIOCDIFADDR != SIOCATALKDIFADDR */
1034 #endif /* SIOCATALKDIFADDR */
1035       }
1036     }
1037 #endif /* SIOCDIFADDR */
1038
1039     /*
1040      * Disassociate. The child will send itself a signal when it is
1041      * stable. This indicates that other processes may begin using
1042      * AppleTalk.
1043      */
1044     switch (i = server_lock("atalkd", pidfile, debug)) {
1045     case -1:
1046       exit(1);
1047     case 0: /* child */
1048       break;
1049     default: /* parent */
1050       /*
1051        * Wait for the child to send itself a SIGSTOP, after which
1052        * we send it a SIGCONT and exit ourself.
1053        */
1054       if ( wait3( &status, WUNTRACED, (struct rusage *)0 ) != i) {
1055         perror( "wait3" );      /* Child died? */
1056         atalkd_exit( 1 );
1057       }
1058       if ( !WIFSTOPPED( status )) {
1059         fprintf( stderr, "AppleTalk not up! Check your syslog for the reason." );
1060         if ( WIFEXITED( status )) {
1061           fprintf( stderr, " Child exited with %d.\n",
1062                    WEXITSTATUS( status ));
1063         } else {
1064           fprintf( stderr, " Child died.\n" );
1065         }
1066         atalkd_exit( 1 );
1067       }
1068       if ( kill(i, SIGCONT ) < 0 ) {
1069         perror( "kill" );
1070         atalkd_exit( 1 );
1071       }
1072       exit( 0 );
1073     }
1074
1075 #ifdef ultrix
1076     openlog( prog, LOG_PID );
1077 #else /* ultrix */
1078     openlog( prog, LOG_PID, LOG_DAEMON );
1079 #endif /* ultrix */
1080
1081     syslog( LOG_INFO, "restart (%s)", version );
1082
1083     /*
1084      * Socket for use in routing ioctl()s. Can't add routes to our
1085      * interfaces until we have our routing socket.
1086      */
1087 #ifdef BSD4_4
1088     if (( rtfd = socket( PF_ROUTE, SOCK_RAW, AF_APPLETALK )) < 0 ) {
1089         syslog( LOG_ERR, "route socket: %m" );
1090         atalkd_exit( 1 );
1091     }
1092     if ( shutdown( rtfd, 0 ) < 0 ) {
1093         syslog( LOG_ERR, "route shutdown: %m" );
1094         atalkd_exit( 1 );
1095     }
1096 #else /* BSD4_4 */
1097     if (( rtfd = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
1098         syslog( LOG_ERR, "route socket: %m" );
1099         atalkd_exit( 1 );
1100     }
1101 #endif /* BSD4_4 */
1102
1103     memset(&sv, 0, sizeof(sv));
1104     sv.sa_handler = as_down;
1105     sigemptyset( &sv.sa_mask );
1106     sigaddset( &sv.sa_mask, SIGUSR1 );
1107     sigaddset( &sv.sa_mask, SIGALRM );
1108     sigaddset( &sv.sa_mask, SIGTERM );
1109     sv.sa_flags = SA_RESTART;
1110     if ( sigaction( SIGTERM, &sv, NULL) < 0 ) {
1111         syslog( LOG_ERR, "sigterm: %m" );
1112         atalkd_exit( 1 );
1113     }
1114
1115     sv.sa_handler = as_debug;
1116     sigemptyset( &sv.sa_mask );
1117     sigaddset( &sv.sa_mask, SIGUSR1 );
1118     sigaddset( &sv.sa_mask, SIGALRM );
1119     sigaddset( &sv.sa_mask, SIGTERM );
1120     sv.sa_flags = SA_RESTART;
1121     if ( sigaction( SIGUSR1, &sv, NULL) < 0 ) {
1122         syslog( LOG_ERR, "sigusr1: %m" );
1123         atalkd_exit( 1 );
1124     }
1125
1126     sv.sa_handler = as_timer;
1127     sigemptyset( &sv.sa_mask );
1128     sigaddset( &sv.sa_mask, SIGUSR1 );
1129     sigaddset( &sv.sa_mask, SIGALRM );
1130     sigaddset( &sv.sa_mask, SIGTERM );
1131     sv.sa_flags = SA_RESTART;
1132     if ( sigaction( SIGALRM, &sv, NULL) < 0 ) {
1133         syslog( LOG_ERR, "sigalrm: %m" );
1134         atalkd_exit( 1 );
1135     }
1136
1137     it.it_interval.tv_sec = 10L;
1138     it.it_interval.tv_usec = 0L;
1139     it.it_value.tv_sec = 10L;
1140     it.it_value.tv_usec = 0L;
1141     if ( setitimer( ITIMER_REAL, &it, NULL) < 0 ) {
1142         syslog( LOG_ERR, "setitimer: %m" );
1143         atalkd_exit( 1 );
1144     }
1145
1146     ciface = interfaces;
1147     bootaddr( ciface );
1148     for (;;) {
1149         readfds = fds;
1150         if ( select( nfds, &readfds, NULL, NULL, NULL) < 0 ) {
1151             if ( errno == EINTR ) {
1152                 errno = 0;
1153                 continue;
1154             } else {
1155                 syslog( LOG_ERR, "select: %m" );
1156                 atalkd_exit( 1 );
1157             }
1158         }
1159
1160         for ( iface = interfaces; iface; iface = iface->i_next ) {
1161             for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
1162                 if ( FD_ISSET( ap->ap_fd, &readfds )) {
1163                     if ( ap->ap_packet ) {
1164                         fromlen = sizeof( struct sockaddr_at );
1165                         if (( c = recvfrom( ap->ap_fd, Packet, sizeof( Packet ),
1166                                 0, (struct sockaddr *)&sat, &fromlen )) < 0 ) {
1167                             syslog( LOG_ERR, "recvfrom: %m" );
1168                             continue;
1169                         }
1170 #ifdef DEBUG
1171                         if ( debug ) {
1172                             printf( "packet from %u.%u on %s (%x) %d (%d)\n",
1173                                     ntohs( sat.sat_addr.s_net ),
1174                                     sat.sat_addr.s_node, iface->i_name,
1175                                     iface->i_flags, ap->ap_port, ap->ap_fd );
1176                             bprint( Packet, c );
1177                         }
1178 #endif /* DEBUG */
1179 #ifdef __svr4__
1180                         if ( sighold( SIGALRM ) || sighold( SIGUSR1 )) {
1181                             syslog( LOG_ERR, "sighold: %m" );
1182                             atalkd_exit( 1 );
1183                         }
1184 #else /* __svr4__ */
1185                         mask = sigsetmask( sigmask( SIGALRM ) |
1186                                 sigmask( SIGUSR1 ));
1187 #endif /* __svr4__ */
1188                         if (( *ap->ap_packet )( ap, &sat, Packet, c ) < 0) {
1189                           syslog(LOG_ERR, "ap->ap_packet: %m");
1190                           atalkd_exit(1);
1191                         }
1192
1193 #ifdef DEBUG
1194                         consistency();
1195 #endif /* DEBUG */
1196 #ifdef __svr4__
1197                         if ( sigrelse( SIGUSR1 ) || sigrelse( SIGALRM )) {
1198                             syslog( LOG_ERR, "sigrelse: %m" );
1199                             atalkd_exit( 1 );
1200                         }
1201 #else /* __svr4__ */
1202                         sigsetmask( mask );
1203 #endif /* __svr4__ */
1204                     }
1205                 }
1206             }
1207         }
1208     }
1209 }
1210
1211 /*
1212  * This code is called (from main(), as_timer(), zip_packet(),
1213  * and rtmp_packet()) to set the initial "bootstrapping" address
1214  * on an interface.
1215  */
1216 void bootaddr( iface )
1217     struct interface    *iface;
1218 {
1219     if ( iface == 0 ) {
1220         return;
1221     }
1222
1223     /* consistency */
1224     if ( iface->i_flags & IFACE_ADDR ) {
1225         syslog( LOG_ERR, "bootaddr OOPS!" );
1226         atalkd_exit(1);
1227     }
1228
1229     if ( iface->i_flags & IFACE_PHASE1 ) {
1230         setaddr( iface, IFACE_PHASE1, 0,
1231                 iface->i_caddr.sat_addr.s_node, 0, 0 );
1232
1233         if ( iface->i_flags & IFACE_LOOPBACK ) {
1234             iface->i_flags |= IFACE_CONFIG | IFACE_ADDR;
1235             if ( ciface == iface ) {
1236                 ciface = ciface->i_next;
1237                 bootaddr( ciface );
1238             }
1239
1240         } else if (rtmp_request( iface ) < 0) {
1241           syslog(LOG_ERR, "bootaddr (rtmp_request): %m");
1242           atalkd_exit(1);
1243         }
1244
1245     } else {
1246         setaddr( iface, IFACE_PHASE2, iface->i_caddr.sat_addr.s_net,
1247                 iface->i_caddr.sat_addr.s_node,
1248                 iface->i_rt->rt_firstnet, iface->i_rt->rt_lastnet );
1249
1250         if ( iface->i_flags & IFACE_LOOPBACK ) {
1251             iface->i_flags |= IFACE_CONFIG | IFACE_ADDR;
1252             if ( ciface == iface ) {
1253                 ciface = ciface->i_next;
1254                 bootaddr( ciface );
1255             }
1256             
1257         } else if (zip_getnetinfo( iface ) < 0) {
1258           syslog(LOG_ERR, "bootaddr (zip_getnetinfo): %m");
1259           atalkd_exit(1);
1260         }
1261     }
1262     ++iface->i_time;
1263     iface->i_flags |= IFACE_ADDR;
1264     stabletimer = UNSTABLE;
1265 }
1266
1267
1268 /*
1269  * Change setaddr()
1270  * to manage the i_ports field and the fds for select().
1271  */
1272 void setaddr( iface, phase, net, node, first, last )
1273     struct interface    *iface;
1274     u_int8_t            phase;
1275     u_int16_t           net;
1276     u_int8_t            node;
1277     u_int16_t           first, last;
1278 {
1279     int                 i;
1280     struct atserv       *as;
1281     struct atport       *ap;
1282     struct servent      *se;
1283     struct sockaddr_at  sat;
1284     struct netrange     nr;
1285
1286     if ( iface->i_ports == NULL ) {     /* allocate port structures */
1287         for ( i = 0, as = atserv; i < atservNATSERV; i++, as++ ) {
1288             if (( se = getservbyname( as->as_name, "ddp" )) == NULL ) {
1289                 syslog( LOG_INFO, "%s: service unknown", as->as_name );
1290             } else {
1291                 as->as_port = ntohs( se->s_port );
1292             }
1293             if (( ap = (struct atport *)malloc( sizeof( struct atport ))) ==
1294                     NULL ) {
1295                 syslog( LOG_ERR, "malloc: %m" );
1296                 atalkd_exit( 1 );
1297             }
1298             ap->ap_fd = 0;
1299             ap->ap_next = iface->i_ports;
1300             ap->ap_iface = iface;
1301             ap->ap_port = as->as_port;
1302             ap->ap_packet = as->as_packet;
1303
1304             iface->i_ports = ap;
1305         }
1306     } else {                            /* close ports */
1307         for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
1308             (void)close( ap->ap_fd );
1309         }
1310     }
1311
1312 #ifdef BSD4_4
1313     iface->i_addr.sat_len = sizeof( struct sockaddr_at );
1314 #endif /* BSD4_4 */
1315     iface->i_addr.sat_family = AF_APPLETALK;
1316     iface->i_addr.sat_addr.s_net = net;
1317     iface->i_addr.sat_addr.s_node = node;
1318
1319     nr.nr_phase = phase;
1320     nr.nr_firstnet = first;
1321     nr.nr_lastnet = last;
1322     memcpy( iface->i_addr.sat_zero, &nr, sizeof( struct netrange ));
1323
1324     if ( ifconfig( iface->i_name, SIOCSIFADDR, &iface->i_addr )) {
1325       syslog( LOG_ERR, "setifaddr: %s (%u-%u): %m. try specifying a \
1326 smaller net range.", iface->i_name, ntohs(first), ntohs(last));
1327         atalkd_exit( 1 );
1328     }
1329     if ( ifconfig( iface->i_name, SIOCGIFADDR, &iface->i_addr )) {
1330         syslog( LOG_ERR, "getifaddr: %s: %m", iface->i_name );
1331         atalkd_exit( 1 );
1332     }
1333
1334     /* open ports */
1335     i = 1; /* enable broadcasts */
1336 #ifdef __svr4__ 
1337     syslog(LOG_INFO, "setsockopt incompatible w/ Solaris STREAMS module.");
1338 #endif /* __svr4__ */
1339     for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
1340         if (( ap->ap_fd = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
1341             syslog( LOG_ERR, "socket: %m" );
1342             atalkd_exit( 1 );
1343         }
1344 #ifndef __svr4__
1345         setsockopt(ap->ap_fd, SOL_SOCKET, SO_BROADCAST, &i, sizeof(i));
1346 #endif /* ! __svr4 */
1347
1348         memset( &sat, 0, sizeof( struct sockaddr_at ));
1349 #ifdef BSD4_4
1350         sat.sat_len = sizeof( struct sockaddr_at );
1351 #endif /* BSD4_4 */
1352         sat.sat_family = AF_APPLETALK;
1353         sat.sat_addr.s_net = iface->i_addr.sat_addr.s_net;
1354         sat.sat_addr.s_node = iface->i_addr.sat_addr.s_node;
1355         sat.sat_port = ap->ap_port;
1356
1357         if ( bind( ap->ap_fd, (struct sockaddr *)&sat,
1358                 sizeof( struct sockaddr_at )) < 0 ) {
1359             syslog( LOG_ERR, "bind %u.%u:%u: %m",
1360                     ntohs( sat.sat_addr.s_net ),
1361                     sat.sat_addr.s_node, sat.sat_port );
1362 #ifdef SIOCDIFADDR
1363             /* remove all interfaces if we have a problem with bind */
1364             for (iface = interfaces; iface; iface = iface->i_next) {
1365               if (ifconfig( iface->i_name, SIOCDIFADDR, &iface->i_addr )) {
1366 #ifdef SIOCATALKDIFADDR
1367 #if (SIOCDIFADDR != SIOCATALKDIFADDR)
1368                 ifconfig( iface->i_name, SIOCATALKDIFADDR, &iface->i_addr );
1369 #endif /* SIOCDIFADDR != SIOCATALKDIFADDR */
1370 #endif /* SIOCATALKDIFADDR */
1371               }
1372             }
1373 #endif /* SIOCDIFADDR */
1374             atalkd_exit( 1 );
1375         }
1376     }
1377
1378     /* recalculate nfds and fds */
1379     FD_ZERO( &fds );
1380     for ( nfds = 0, iface = interfaces; iface; iface = iface->i_next ) {
1381         for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
1382             FD_SET( ap->ap_fd, &fds );
1383             if ( ap->ap_fd > nfds ) {
1384                 nfds = ap->ap_fd;
1385             }
1386         }
1387     }
1388     nfds++;
1389 }
1390
1391 int ifconfig( iname, cmd, sa )
1392     const char          *iname;
1393     unsigned long       cmd;
1394     struct sockaddr_at  *sa;
1395 {
1396     struct ifreq        ifr;
1397     int                 s;
1398
1399     memset(&ifr, 0, sizeof(ifr));
1400     strcpy( ifr.ifr_name, iname );
1401     ifr.ifr_addr = *(struct sockaddr *)sa;
1402
1403     if (( s = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
1404         return( 1 );
1405     }
1406     if ( ioctl( s, cmd, &ifr ) < 0 ) {
1407         close(s);
1408         return( 1 );
1409     }
1410     close( s );
1411     if ( cmd == SIOCGIFADDR ) {
1412         *(struct sockaddr *)sa = ifr.ifr_addr;
1413     }
1414     return( 0 );
1415 }
1416
1417 void dumpconfig( iface )
1418     struct interface    *iface;
1419 {
1420     struct list         *l;
1421
1422     printf( "%s", iface->i_name );
1423     if ( iface->i_flags & IFACE_RSEED ) {
1424         printf( " -router" );
1425     } else if ( iface->i_flags & IFACE_SEED ) {
1426         printf( " -seed" );
1427     }
1428
1429     if ( iface->i_flags & IFACE_DONTROUTE) 
1430         printf( " -dontroute");
1431
1432     printf( " -phase" );
1433     if ( iface->i_flags & IFACE_PHASE1 ) {
1434         printf( " 1" );
1435     } else {
1436         printf( " 2" );
1437     }
1438     printf( " -net %d", ntohs( iface->i_rt->rt_firstnet ));
1439     if ( iface->i_rt->rt_lastnet != iface->i_rt->rt_firstnet ) {
1440         printf( "-%d", ntohs( iface->i_rt->rt_lastnet ));
1441     }
1442     printf( " -addr %u.%u", ntohs( iface->i_addr.sat_addr.s_net ),
1443             iface->i_addr.sat_addr.s_node );
1444     printf( " -caddr %u.%u", ntohs( iface->i_caddr.sat_addr.s_net ),
1445             iface->i_caddr.sat_addr.s_node );
1446     for ( l = iface->i_rt->rt_zt; l; l = l->l_next ) {
1447         printf( " -zone %.*s", ((struct ziptab *)l->l_data)->zt_len,
1448                 ((struct ziptab *)l->l_data)->zt_name );
1449     }
1450     printf( "\n" );
1451 }
1452
1453 #ifdef DEBUG
1454 void dumproutes()
1455 {
1456     struct interface    *iface;
1457     struct rtmptab      *rtmp;
1458     struct list         *l;
1459     struct ziptab       *zt;
1460
1461     for ( iface = interfaces; iface; iface = iface->i_next ) {
1462         for ( rtmp = iface->i_rt; rtmp; rtmp = rtmp->rt_inext ) {
1463             if ( rtmp->rt_gate == 0 ) {
1464                 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
1465                     printf( "%u-%u", ntohs( rtmp->rt_firstnet ),
1466                             ntohs( rtmp->rt_lastnet ));
1467                 } else {
1468                     printf( "%u", ntohs( rtmp->rt_firstnet ));
1469                 }
1470             } else {
1471                 if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
1472                     printf( "%u.%u for %u-%u",
1473                             ntohs( rtmp->rt_gate->g_sat.sat_addr.s_net ),
1474                             rtmp->rt_gate->g_sat.sat_addr.s_node,
1475                             ntohs( rtmp->rt_firstnet ),
1476                             ntohs( rtmp->rt_lastnet ));
1477                 } else {
1478                     printf( "%u.%u for %u",
1479                             ntohs( rtmp->rt_gate->g_sat.sat_addr.s_net ),
1480                             rtmp->rt_gate->g_sat.sat_addr.s_node,
1481                             ntohs( rtmp->rt_firstnet ));
1482                 }
1483             }
1484
1485             if ( rtmp->rt_iprev == 0 && rtmp != iface->i_rt ) {
1486                 printf( " *" );
1487             }
1488
1489             for ( l = rtmp->rt_zt; l; l = l->l_next ) {
1490                 zt = (struct ziptab *)l->l_data;
1491                 printf( " %.*s", zt->zt_len, zt->zt_name );
1492             }
1493
1494             printf( "\n" );
1495         }
1496     }
1497
1498     printf( "\n" );
1499     fflush( stdout );
1500 }
1501
1502 void dumpzones()
1503 {
1504     struct interface    *iface;
1505     struct rtmptab      *rtmp;
1506     struct list         *l;
1507     struct ziptab       *zt;
1508
1509     for ( zt = ziptab; zt; zt = zt->zt_next ) {
1510         printf( "%.*s", zt->zt_len, zt->zt_name );
1511         for ( l = zt->zt_rt; l; l = l->l_next ) {
1512             rtmp = (struct rtmptab *)l->l_data;
1513             if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
1514                 printf( " %u-%u", ntohs( rtmp->rt_firstnet ),
1515                         ntohs( rtmp->rt_lastnet ));
1516             } else {
1517                 printf( " %u", ntohs( rtmp->rt_firstnet ));
1518             }
1519             if ( rtmp->rt_iprev == 0 && rtmp->rt_gate != 0 ) {
1520                 printf( "*" );
1521             }
1522         }
1523         printf( "\n" );
1524     }
1525
1526     printf( "\n" );
1527     fflush( stdout );
1528 }
1529 #endif /* DEBUG */