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