accidentally applied wrong (old) patch, fixing up...
[oweals/busybox.git] / networking / dnsd.c
index 35f6c109658dd0a1902efaa48d08d981099ff237..6a866f1d719962ea19c405ed4a0c440a96df188f 100644 (file)
 
 #include "busybox.h"
 
-static char *fileconf = "/etc/dnsd.conf";
+static const char *fileconf = "/etc/dnsd.conf";
 #define LOCK_FILE       "/var/run/dnsd.lock"
-#define LOG_FILE        "/var/log/dnsd.log"
 
-// Must matct getopt32 call
+// Must match getopt32 call
 #define OPT_daemon  (option_mask32 & 0x10)
 #define OPT_verbose (option_mask32 & 0x20)
 
 //#define DEBUG 1
+#define DEBUG 0
 
 enum {
        MAX_HOST_LEN = 16,      // longest host name allowed is 15
@@ -76,8 +76,6 @@ struct dns_entry {            // element of known name, ip address and reversed ip address
 };
 
 static struct dns_entry *dnsentry = NULL;
-// FIXME! unused! :(
-static int daemonmode;
 static uint32_t ttl = DEFAULT_TTL;
 
 /*
@@ -108,26 +106,11 @@ static void undot(uint8_t * rip)
        }
 }
 
-/*
- * Append message to log file
- */
-static void log_message(char *filename, char *message)
-{
-       FILE *logfile;
-       if (!daemonmode)
-               return;
-       logfile = fopen(filename, "a");
-       if (!logfile)
-               return;
-       fprintf(logfile, "%s\n", message);
-       fclose(logfile);
-}
-
 /*
  * Read one line of hostname/IP from file
  * Returns 0 for each valid entry read, -1 at EOF
  * Assumes all host names are lower case only
- * Hostnames with more than one label is not handled correctly.
+ * Hostnames with more than one label are not handled correctly.
  * Presently the dot is copied into name without
  * converting to a length/string substring for that label.
  */
@@ -135,32 +118,37 @@ static void log_message(char *filename, char *message)
 static int getfileentry(FILE * fp, struct dns_entry *s)
 {
        unsigned int a,b,c,d;
-       char *r, *name;
+       char *line, *r, *name;
 
  restart:
-       r = bb_get_line_from_file(fp);
+       line = r = xmalloc_fgets(fp);
        if (!r)
                return -1;
        while (*r == ' ' || *r == '\t') {
                r++;
-               if (!*r || *r == '#' || *r == '\n')
+               if (!*r || *r == '#' || *r == '\n') {
+                       free(line);
                        goto restart; /* skipping empty/blank and commented lines  */
+               }
        }
        name = r;
        while (*r != ' ' && *r != '\t')
                r++;
-       *r++ = 0;
-       if (sscanf(r, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
+       *r++ = '\0';
+       if (sscanf(r, "%u.%u.%u.%u", &a, &b, &c, &d) != 4) {
+               free(line);
                goto restart; /* skipping wrong lines */
+       }
 
        sprintf(s->ip, "%u.%u.%u.%u", a, b, c, d);
        sprintf(s->rip, ".%u.%u.%u.%u", d, c, b, a);
        undot((uint8_t*)s->rip);
-       convname(s->name,(uint8_t*)name);
+       convname(s->name, (uint8_t*)name);
 
        if (OPT_verbose)
                fprintf(stderr, "\tname:%s, ip:%s\n", &(s->name[1]),s->ip);
 
+       free(line);
        return 0;
 }
 
@@ -171,14 +159,13 @@ static void dnsentryinit(void)
 {
        FILE *fp;
        struct dns_entry *m, *prev;
-       prev = dnsentry = NULL;
 
+       prev = dnsentry = NULL;
        fp = xfopen(fileconf, "r");
 
        while (1) {
-               m = xmalloc(sizeof(struct dns_entry));
-
-               m->next = NULL;
+               m = xzalloc(sizeof(*m));
+               /*m->next = NULL;*/
                if (getfileentry(fp, m))
                        break;
 
@@ -191,34 +178,6 @@ static void dnsentryinit(void)
        fclose(fp);
 }
 
-
-/*
- * Set up UDP socket
- */
-static int listen_socket(char *iface_addr, int listen_port)
-{
-       struct sockaddr_in a;
-       char msg[100];
-       int s;
-       int yes = 1;
-       s = xsocket(PF_INET, SOCK_DGRAM, 0);
-#ifdef SO_REUSEADDR
-       if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(yes)) < 0)
-               bb_perror_msg_and_die("setsockopt() failed");
-#endif
-       memset(&a, 0, sizeof(a));
-       a.sin_port = htons(listen_port);
-       a.sin_family = AF_INET;
-       if (!inet_aton(iface_addr, &a.sin_addr))
-               bb_perror_msg_and_die("bad iface address");
-       xbind(s, (struct sockaddr *)&a, sizeof(a));
-       xlisten(s, 50);
-       sprintf(msg, "accepting UDP packets on addr:port %s:%d\n",
-               iface_addr, (int)listen_port);
-       log_message(LOG_FILE, msg);
-       return s;
-}
-
 /*
  * Look query up in dns records and return answer if found
  * qs is the query string, first byte the string length
@@ -229,28 +188,29 @@ static int table_lookup(uint16_t type, uint8_t * as, uint8_t * qs)
        struct dns_entry *d=dnsentry;
 
        do {
-#ifdef DEBUG
+#if DEBUG
                char *p,*q;
                q = (char *)&(qs[1]);
                p = &(d->name[1]);
-               fprintf(stderr, "\n%s: %d/%d p:%s q:%s %d", 
-                       __FUNCTION__, strlen(p), (int)(d->name[0]), p, q, strlen(q));
+               fprintf(stderr, "\n%s: %d/%d p:%s q:%s %d",
+                       __FUNCTION__, (int)strlen(p), (int)(d->name[0]),
+                       p, q, (int)strlen(q));
 #endif
                if (type == REQ_A) { /* search by host name */
                        for (i = 1; i <= (int)(d->name[0]); i++)
                                if (tolower(qs[i]) != d->name[i])
                                        break;
                        if (i > (int)(d->name[0])) {
-#ifdef DEBUG
+#if DEBUG
                                fprintf(stderr, " OK");
 #endif
                                strcpy((char *)as, d->ip);
-#ifdef DEBUG
+#if DEBUG
                                fprintf(stderr, " as:%s\n", as);
 #endif
                                        return 0;
                        }
-               } else 
+               } else
                if (type == REQ_PTR) { /* search by IP-address */
                        if (!strncmp((char*)&d->rip[1], (char*)&qs[1], strlen(d->rip)-1)) {
                                strcpy((char *)as, d->name);
@@ -312,7 +272,7 @@ static int process_packet(uint8_t * buf)
                goto empty_packet;
 
        // We have a standard query
-       log_message(LOG_FILE, (char *)from);
+       bb_info_msg("%s", (char *)from);
        lookup_result = table_lookup(type, answstr, (uint8_t*)from);
        if (lookup_result != 0) {
                outr.flags = 3 | 0x0400;        //name do not exist and auth
@@ -366,27 +326,26 @@ static int process_packet(uint8_t * buf)
 static void interrupt(int x)
 {
        unlink(LOCK_FILE);
-       write(2, "interrupt exiting\n", 18);
+       bb_error_msg("interrupt, exiting\n");
        exit(2);
 }
 
 int dnsd_main(int argc, char **argv)
 {
+       char *listen_interface = NULL;
+       char *sttl, *sport;
+       len_and_sockaddr *lsa;
        int udps;
        uint16_t port = 53;
        uint8_t buf[MAX_PACK_LEN];
-       char *listen_interface = "0.0.0.0";
-       char *sttl, *sport;
 
        getopt32(argc, argv, "i:c:t:p:dv", &listen_interface, &fileconf, &sttl, &sport);
        //if (option_mask32 & 0x1) // -i
        //if (option_mask32 & 0x2) // -c
        if (option_mask32 & 0x4) // -t
-               if (!(ttl = atol(sttl)))
-                       bb_show_usage();
+               ttl = xatou_range(sttl, 1, 0xffffffff);
        if (option_mask32 & 0x8) // -p
-               if (!(port = atol(sport)))
-                       bb_show_usage();
+               port = xatou_range(sttl, 1, 0xffff);
 
        if (OPT_verbose) {
                bb_info_msg("listen_interface: %s", listen_interface);
@@ -394,13 +353,16 @@ int dnsd_main(int argc, char **argv)
                bb_info_msg("fileconf: %s", fileconf);
        }
 
-       if (OPT_daemon)
+       if (OPT_daemon) {
+//FIXME: NOMMU will NOT set LOGMODE_SYSLOG!
 #ifdef BB_NOMMU
                /* reexec for vfork() do continue parent */
                vfork_daemon_rexec(1, 0, argc, argv, "-d");
 #else
                xdaemon(1, 0);
 #endif
+               logmode = LOGMODE_SYSLOG;
+       }
 
        dnsentryinit();
 
@@ -414,9 +376,12 @@ int dnsd_main(int argc, char **argv)
        signal(SIGURG, SIG_IGN);
 #endif
 
-       udps = listen_socket(listen_interface, port);
-       if (udps < 0)
-               exit(1);
+       lsa = host2sockaddr(listen_interface, port);
+       udps = xsocket(lsa->sa.sa_family, SOCK_DGRAM, 0);
+       xbind(udps, &lsa->sa, lsa->len);
+       // xlisten(udps, 50); - ?!! DGRAM sockets are never listened on I think?
+       bb_info_msg("Accepting UDP packets on %s",
+                       xmalloc_sockaddr2dotted(&lsa->sa, lsa->len));
 
        while (1) {
                fd_set fdset;
@@ -425,6 +390,8 @@ int dnsd_main(int argc, char **argv)
                FD_ZERO(&fdset);
                FD_SET(udps, &fdset);
                // Block until a message arrives
+// FIXME: Fantastic. select'ing on just one fd??
+// Why no just block on it doing recvfrom() ?
                r = select(udps + 1, &fdset, NULL, NULL, NULL);
                if (r < 0)
                        bb_perror_msg_and_die("select error");
@@ -433,27 +400,26 @@ int dnsd_main(int argc, char **argv)
 
                /* Can this test ever be false? - yes */
                if (FD_ISSET(udps, &fdset)) {
-                       struct sockaddr_in from;
-                       int fromlen = sizeof(from);
-                       r = recvfrom(udps, buf, sizeof(buf), 0,
-                                    (struct sockaddr *)&from,
-                                    (void *)&fromlen);
+                       socklen_t fromlen = lsa->len;
+// FIXME: need to get *DEST* address (to which of our addresses
+// this query was directed), and reply from the same address.
+// Or else we can exhibit usual UDP ugliness:
+// [ip1.multihomed.ip2] <=  query to ip1  <= peer
+// [ip1.multihomed.ip2] => reply from ip2 => peer (confused)
+                       r = recvfrom(udps, buf, sizeof(buf), 0, &lsa->sa, &fromlen);
                        if (OPT_verbose)
-                               fprintf(stderr, "\n--- Got UDP  ");
-                       log_message(LOG_FILE, "\n--- Got UDP  ");
+                               bb_info_msg("Got UDP packet");
 
                        if (r < 12 || r > 512) {
                                bb_error_msg("invalid packet size");
                                continue;
                        }
-                       if (r > 0) {
-                               r = process_packet(buf);
-                               if (r > 0)
-                                       sendto(udps, buf,
-                                              r, 0, (struct sockaddr *)&from,
-                                              fromlen);
-                       }
-               } // end if
-       } // end while
-       return 0;
+                       if (r <= 0)
+                               continue;
+                       r = process_packet(buf);
+                       if (r <= 0)
+                               continue;
+                       sendto(udps, buf, r, 0, &lsa->sa, fromlen);
+               }
+       }
 }