]> arthur.barton.de Git - netatalk.git/blobdiff - etc/atalkd/main.c
remove most sparse warning 'Using plain integer as NULL pointer'
[netatalk.git] / etc / atalkd / main.c
index 34d6f9d1ef84a2632a4b101be661d13221c483e8..b8080c29150a56646de93a5f2b167911a9e1578e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: main.c,v 1.16 2002-09-29 23:24:47 sibaz Exp $
+ * $Id: main.c,v 1.22 2009-10-14 02:24:05 didg Exp $
  *
  * Copyright (c) 1990,1993 Regents of The University of Michigan.
  * All Rights Reserved. See COPYRIGHT.
 
 #include <sys/param.h>
 #include <sys/socket.h>
-#if defined( sun ) && defined( __svr4__ )
-#include </usr/ucbinclude/sys/file.h>
-#else /* sun __svr4__ */
 #include <sys/file.h>
-#endif /* sun __svr4__ */
 #include <sys/time.h>
 #include <sys/resource.h>
 #include <sys/ioctl.h>
@@ -31,6 +27,9 @@
 #ifndef WIFEXITED
 #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
 #endif /* ! WIFEXITED */
+#ifndef WIFSTOPPED
+#define WIFSTOPPED(status) (((status) & 0xff) == 0x7f)
+#endif
 
 #include <errno.h>
 #ifdef TRU64
@@ -56,6 +55,7 @@
 #include <atalk/compat.h>
 #include <atalk/zip.h>
 #include <atalk/rtmp.h>
+#include <atalk/nbp.h>
 #include <atalk/ddp.h>
 #include <atalk/atp.h>
 #include <atalk/paths.h>
@@ -71,6 +71,7 @@
 #include "list.h"
 #include "rtmp.h"
 #include "zip.h"
+#include "nbp.h"
 #include "atserv.h"
 #include "main.h"
 
@@ -98,34 +99,33 @@ int ifconfig(const char *iname, unsigned long cmd, struct sockaddr_at *sa);
 
 #define PKTSZ  1024
 
-extern int     rtmp_packet();
-extern int     nbp_packet();
-extern int     aep_packet();
-extern int     zip_packet();
+extern int aep_packet(struct atport *ap, struct sockaddr_at *from, char *data, int len);
 
 int            rtfd;
+int            transition = 0;
+int            stabletimer, newrtmpdata = 0;
 
-struct atserv  atserv[] = {
+static struct atserv   atserv[] = {
     { "rtmp",          1,      rtmp_packet },          /* 0 */
     { "nbp",           2,      nbp_packet },           /* 1 */
     { "echo",          4,      aep_packet },           /* 2 */
     { "zip",           6,      zip_packet },           /* 3 */
 };
-int            atservNATSERV = elements( atserv );
+static int             atservNATSERV = elements( atserv );
 
 struct interface       *interfaces = NULL, *ciface = NULL;
 
-int            debug = 0, quiet = 0, chatty = 0;
-char           *configfile = NULL;
-int            ziptimeout = 0, transition = 0;
-int            stabletimer, stable = 0, newrtmpdata = 0, noparent = 0;
-static int     ninterfaces;
-int            defphase = IFACE_PHASE2;
-int            nfds = 0;
-fd_set         fds;
-char           Packet[ PKTSZ ];
-char           *version = VERSION;
-static char     *pidfile = _PATH_ATALKDLOCK;
+static int             debug = 0, quiet = 0, chatty = 0;
+static char            *configfile = NULL;
+static int             ziptimeout = 0;
+static int             stable = 0, noparent = 0;
+static int             ninterfaces;
+static int             defphase = IFACE_PHASE2;
+static int             nfds = 0;
+static fd_set          fds;
+static char            Packet[ PKTSZ ];
+static char            *version = VERSION;
+static char            *pidfile = _PATH_ATALKDLOCK;
 
 
 /* from config.c */
@@ -153,6 +153,10 @@ static void atalkd_exit(const int i)
              ntohs(iface->i_addr.sat_addr.s_net), 
              iface->i_addr.sat_addr.s_node, strerror(errno));
     }
+#ifdef linux
+    if (!(iface->i_flags & IFACE_WASALLMULTI) && (iface->i_flags & IFACE_ALLMULTI))
+        ifsetallmulti(iface->i_name, 0);
+#endif /* linux */
   }
 #endif /* SOPCDOFADDR */
 
@@ -161,7 +165,7 @@ static void atalkd_exit(const int i)
 }
 
 
-static void as_timer(int sig)
+static void as_timer(int sig _U_)
 {
     struct sockaddr_at sat;
     struct ziphdr      zh;
@@ -176,6 +180,8 @@ static void as_timer(int sig)
     int                        sentzipq = 0;
     int                        n, cc;
 
+    ap=zap=rap=NULL;
+
     memset(&sat, 0, sizeof( struct sockaddr_at ));
     for ( iface = interfaces; iface; iface = iface->i_next ) {
        if ( iface->i_flags & IFACE_LOOPBACK ) {
@@ -325,7 +331,7 @@ static void as_timer(int sig)
                while ( rtmp ) {
                    frtmp = rtmp->rt_next;
                    if ( rtmp->rt_hops == RTMPHOPS_POISON ||
-                           rtmp->rt_iprev == 0 ) {
+                           rtmp->rt_iprev == NULL ) {
                        rtmp_free( rtmp );
                    } else {
                        rtmp->rt_hops = RTMPHOPS_POISON;
@@ -339,13 +345,13 @@ static void as_timer(int sig)
                    }
                    rtmp = frtmp;
                }
-               if ( gate->g_rt == 0 ) {
-                   if ( gate->g_prev == 0 ) {
+               if ( gate->g_rt == NULL ) {
+                   if ( gate->g_prev == NULL ) {
                        gate->g_iface->i_gate = gate->g_next;
                    } else {
                        gate->g_prev->g_next = gate->g_next;
                    }
-                   if ( gate->g_next != 0 ) {
+                   if ( gate->g_next != NULL ) {
                        gate->g_next->g_prev = gate->g_prev;
                    }
                    fgate = gate;       /* can't free here, just mark it */
@@ -360,7 +366,7 @@ static void as_timer(int sig)
                 * if we're not a seed router.
                 */
 
-               if ( gate->g_iface->i_gate == 0 && 
+               if ( gate->g_iface->i_gate == NULL && 
                     ((iface->i_flags & IFACE_SEED) == 0)) {
                    gate->g_iface->i_flags |= IFACE_NOROUTER;
                    gate->g_iface->i_flags &= ~IFACE_CONFIG;
@@ -418,7 +424,7 @@ static void as_timer(int sig)
                 */
                if ( rtmp->rt_state >= RTMPTAB_BAD ) {
                    frtmp = rtmp->rt_next;
-                   if ( rtmp->rt_iprev == 0 ) {        /* not in use */
+                   if ( rtmp->rt_iprev == NULL ) {     /* not in use */
                        rtmp_free( rtmp );
                    } else {                            /* in use */
                        if ( rtmp->rt_hops == RTMPHOPS_POISON ) {
@@ -570,7 +576,7 @@ static void as_timer(int sig)
                 */
                for ( rtmp = iface2->i_rt; rtmp; rtmp = rtmp->rt_inext ) {
                    /* don't broadcast routes we have no zone for */
-                   if ( rtmp->rt_zt == 0 ||
+                   if ( rtmp->rt_zt == NULL ||
                            ( rtmp->rt_flags & RTMPTAB_ZIPQUERY ) ||
                            ( rtmp->rt_flags & RTMPTAB_HASZONES ) == 0 ) {
                        continue;
@@ -686,7 +692,7 @@ consistency()
        for ( lr = zt->zt_rt; lr; lr = lr->l_next ) {
            rtmp = (struct rtmptab *)lr->l_data;
            if ( rtmp->rt_iprev == 0 && rtmp->rt_gate != 0 ) {
-               LOG(log_error, logtype_atalkd, "%.*s has %u-%u (unused)\n",
+               LOG(log_error, logtype_atalkd, "%.*s has %u-%u (unused)",
                        zt->zt_len, zt->zt_name, ntohs( rtmp->rt_firstnet ),
                        ntohs( rtmp->rt_lastnet ));
                atalkd_exit(1);
@@ -697,7 +703,7 @@ consistency()
                }
            }
            if ( lz == 0 ) {
-               LOG(log_error, logtype_atalkd, "no map from %u-%u to %.*s\n", 
+               LOG(log_error, logtype_atalkd, "no map from %u-%u to %.*s", 
                        ntohs( rtmp->rt_firstnet ),
                        ntohs( rtmp->rt_lastnet ),
                        zt->zt_len, zt->zt_name );
@@ -708,10 +714,8 @@ consistency()
 }
 #endif /* DEBUG */
 
-#if !defined( ibm032 ) && !defined( _IBMR2 )
-    void
-#endif /* ! ibm032 && ! _IBMR2 */
-as_debug()
+static void
+as_debug(int sig _U_)
 {
     struct interface   *iface;
     struct list                *l;
@@ -812,10 +816,8 @@ as_debug()
 /*
  * Called when SIGTERM is recieved.  Remove all routes and then exit.
  */
-#if !defined( ibm032 ) && !defined( _IBMR2 )
-    void
-#endif /* ! ibm032 && ! _IBMR2 */
-as_down()
+static void
+as_down(int sig _U_)
 {
     struct interface   *iface;
     struct gate                *gate;
@@ -848,9 +850,7 @@ as_down()
     atalkd_exit( 0 );
 }
 
-int main( ac, av )
-    int                ac;
-    char       **av;
+int main( int ac, char **av)
 {
     extern char         *optarg;
     extern int          optind;
@@ -858,14 +858,15 @@ int main( ac, av )
     struct sockaddr_at sat;
     struct sigaction   sv;
     struct itimerval   it;
+    sigset_t            signal_set, old_set;
+    
     struct interface   *iface;
     int                        status;
     struct atport      *ap;
     fd_set             readfds;
-    int                        i, mask, c;
+    int                        i, c;
     SOCKLEN_T          fromlen;
     char               *prog;
-;
 
     while (( c = getopt( ac, av, "12qsdtf:P:v" )) != EOF ) {
        switch ( c ) {
@@ -1020,6 +1021,12 @@ int main( ac, av )
        exit( 1 );
     }
 
+    /*
+     * Set process name for logging
+     */
+
+    set_processname("atalkd");
+
     /* do this here so that we can use ifconfig */
 #ifdef __svr4__
     if ( plumb() < 0 ) {
@@ -1106,6 +1113,9 @@ int main( ac, av )
     }
 #endif /* BSD4_4 */
 
+    ciface = interfaces;
+    bootaddr( ciface );
+
     memset(&sv, 0, sizeof(sv));
     sv.sa_handler = as_down;
     sigemptyset( &sv.sa_mask );
@@ -1149,8 +1159,14 @@ int main( ac, av )
        atalkd_exit( 1 );
     }
 
-    ciface = interfaces;
-    bootaddr( ciface );
+    sigemptyset( &signal_set );
+    sigaddset(&signal_set, SIGALRM);
+#if 0
+    /* don't block SIGTERM */
+    sigaddset(&signal_set, SIGTERM);
+#endif
+    sigaddset(&signal_set, SIGUSR1);
+
     for (;;) {
        readfds = fds;
        if ( select( nfds, &readfds, NULL, NULL, NULL) < 0 ) {
@@ -1173,7 +1189,7 @@ int main( ac, av )
                            LOG(log_error, logtype_atalkd, "recvfrom: %s", strerror(errno) );
                            continue;
                        }
-#ifdef DEBUG
+#ifdef DEBUG1
                        if ( debug ) {
                            printf( "packet from %u.%u on %s (%x) %d (%d)\n",
                                    ntohs( sat.sat_addr.s_net ),
@@ -1181,16 +1197,12 @@ int main( ac, av )
                                    iface->i_flags, ap->ap_port, ap->ap_fd );
                            bprint( Packet, c );
                        }
-#endif /* DEBUG */
-#ifdef __svr4__
-                       if ( sighold( SIGALRM ) || sighold( SIGUSR1 )) {
-                           LOG(log_error, logtype_atalkd, "sighold: %s", strerror(errno) );
+#endif 
+                       if (sigprocmask(SIG_BLOCK, &signal_set, &old_set) < 0) {
+                           LOG(log_error, logtype_atalkd, "sigprocmask: %s", strerror(errno) );
                            atalkd_exit( 1 );
                        }
-#else /* __svr4__ */
-                       mask = sigsetmask( sigmask( SIGALRM ) |
-                               sigmask( SIGUSR1 ));
-#endif /* __svr4__ */
+
                        if (( *ap->ap_packet )( ap, &sat, Packet, c ) < 0) {
                          LOG(log_error, logtype_atalkd, "ap->ap_packet: %s", strerror(errno));
                          atalkd_exit(1);
@@ -1198,15 +1210,12 @@ int main( ac, av )
 
 #ifdef DEBUG
                        consistency();
-#endif /* DEBUG */
-#ifdef __svr4__
-                       if ( sigrelse( SIGUSR1 ) || sigrelse( SIGALRM )) {
-                           LOG(log_error, logtype_atalkd, "sigrelse: %s", strerror(errno) );
+#endif 
+                       if (sigprocmask(SIG_SETMASK, &old_set, NULL) < 0) {
+                           LOG(log_error, logtype_atalkd, "sigprocmask old set: %s", strerror(errno) );
                            atalkd_exit( 1 );
                        }
-#else /* __svr4__ */
-                       sigsetmask( mask );
-#endif /* __svr4__ */
+
                    }
                }
            }
@@ -1219,10 +1228,9 @@ int main( ac, av )
  * and rtmp_packet()) to set the initial "bootstrapping" address
  * on an interface.
  */
-void bootaddr( iface )
-    struct interface   *iface;
+void bootaddr(struct interface *iface)
 {
-    if ( iface == 0 ) {
+    if ( iface == NULL ) {
        return;
     }
 
@@ -1336,7 +1344,8 @@ smaller net range.", iface->i_name, ntohs(first), ntohs(last), strerror(errno));
 
     /* open ports */
     i = 1; /* enable broadcasts */
-#ifdef __svr4__ 
+#if 0
+    /* useless message, no? */
     LOG(log_info, logtype_atalkd, "setsockopt incompatible w/ Solaris STREAMS module.");
 #endif /* __svr4__ */
     for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
@@ -1391,10 +1400,42 @@ smaller net range.", iface->i_name, ntohs(first), ntohs(last), strerror(errno));
     nfds++;
 }
 
-int ifconfig( iname, cmd, sa )
-    const char         *iname;
-    unsigned long      cmd;
-    struct sockaddr_at *sa;
+int ifsetallmulti (const char *iname, int set)
+{
+    int sock;
+    struct ifreq ifr;
+
+    memset(&ifr, 0, sizeof(ifr));
+
+    if (( sock = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
+        return( -1 );
+    }
+
+    /* get interface config */
+    strlcpy(ifr.ifr_name, iname, sizeof(ifr.ifr_name));
+    if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
+        close(sock);
+        return (-1);
+    }
+
+    /* should we set or unset IFF_ALLMULTI */
+    if (set)
+           ifr.ifr_flags |= IFF_ALLMULTI;
+    else
+           ifr.ifr_flags &= ~IFF_ALLMULTI;
+
+    /* set interface config */
+    strlcpy(ifr.ifr_name, iname, sizeof(ifr.ifr_name));
+    if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0) {
+        close(sock);   
+        return -1;
+    }
+
+    close(sock);
+    return (0);
+}
+
+int ifconfig( const char *iname, unsigned long cmd, struct sockaddr_at *sa)
 {
     struct ifreq       ifr;
     int                        s;
@@ -1417,8 +1458,7 @@ int ifconfig( iname, cmd, sa )
     return( 0 );
 }
 
-void dumpconfig( iface )
-    struct interface   *iface;
+void dumpconfig( struct interface *iface)
 {
     struct list                *l;
 
@@ -1454,7 +1494,7 @@ void dumpconfig( iface )
 }
 
 #ifdef DEBUG
-void dumproutes()
+void dumproutes(void)
 {
     struct interface   *iface;
     struct rtmptab     *rtmp;
@@ -1502,7 +1542,7 @@ void dumproutes()
     fflush( stdout );
 }
 
-void dumpzones()
+void dumpzones(void)
 {
     struct interface   *iface;
     struct rtmptab     *rtmp;