httpd: reduce ifdef forest. comment out redundant PATH setting
[oweals/busybox.git] / networking / dnsd.c
index 433b124430c696ba4b7b09e679a22686116e21be..6f9dc5d67129df7d97401536a7d5252571ebe5ea 100644 (file)
@@ -1,9 +1,11 @@
+/* vi: set sw=4 ts=4: */
 /*
  * Mini DNS server implementation for busybox
  *
  * Copyright (C) 2005 Roberto A. Foglietta (me@roberto.foglietta.name)
  * Copyright (C) 2005 Odd Arild Olsen (oao at fibula dot no)
  * Copyright (C) 2003 Paul Sheer
+ *
  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  *
  * Odd Arild Olsen started out with the sheerdns [1] of Paul Sheer and rewrote
  * the first porting of oao' scdns to busybox also.
  */
 
-#include <unistd.h>
-#include <string.h>
-#include <signal.h>
-#include <arpa/inet.h>
-#include <sys/socket.h>
-#include <ctype.h>
-#include "libbb.h"
+#include "busybox.h"
 
 static char *fileconf = "/etc/dnsd.conf";
 #define LOCK_FILE       "/var/run/dnsd.lock"
 #define LOG_FILE        "/var/log/dnsd.log"
 
-#define  MAX_HOST_LEN 16        // longest host name allowed is 15
-#define IP_STRING_LEN 18        // .xxx.xxx.xxx.xxx\0
+// Must matct getopt32 call
+#define OPT_daemon  (option_mask32 & 0x10)
+#define OPT_verbose (option_mask32 & 0x20)
+
+//#define DEBUG 1
+
+enum {
+       MAX_HOST_LEN = 16,      // longest host name allowed is 15
+       IP_STRING_LEN = 18,     // .xxx.xxx.xxx.xxx\0
 
 //must be strlen('.in-addr.arpa') larger than IP_STRING_LEN
-static const int MAX_NAME_LEN = (IP_STRING_LEN + 13);
+       MAX_NAME_LEN = (IP_STRING_LEN + 13),
 
 /* Cannot get bigger packets than 512 per RFC1035
    In practice this can be set considerably smaller:
@@ -39,12 +42,13 @@ static const int MAX_NAME_LEN = (IP_STRING_LEN + 13);
    ttl(4B) + rlen(2B) + r (MAX_NAME_LEN =21B) +
    2*querystring (2 MAX_NAME_LEN= 42B), all together 90 Byte
 */
-static const int MAX_PACK_LEN = 512 + 1;
+       MAX_PACK_LEN = 512 + 1,
 
-#define  DEFAULT_TTL 30;        // increase this when not testing?
+       DEFAULT_TTL = 30,       // increase this when not testing?
 
-static const int REQ_A = 1;
-static const int REQ_PTR = 12;
+       REQ_A = 1,
+       REQ_PTR = 12
+};
 
 struct dns_repl {              // resource record, add 0 or 1 to accepted dns_msg in resp
        uint16_t rlen;
@@ -72,32 +76,32 @@ struct dns_entry {          // element of known name, ip address and reversed ip address
 };
 
 static struct dns_entry *dnsentry = NULL;
-static int daemonmode = 0;
+// FIXME! unused! :(
+static int daemonmode;
 static uint32_t ttl = DEFAULT_TTL;
 
 /*
  * Convert host name from C-string to dns length/string.
  */
-static void
-convname(char *a, uint8_t *q)
+static void convname(char *a, uint8_t *q)
 {
        int i = (q[0] == '.') ? 0 : 1;
-       for(; i < MAX_HOST_LEN-1 && *q; i++, q++)
+       for (; i < MAX_HOST_LEN-1 && *q; i++, q++)
                a[i] = tolower(*q);
        a[0] = i - 1;
        a[i] = 0;
 }
 
 /*
- * Insert length of substrings insetad of dots
+ * Insert length of substrings instead of dots
  */
-static void
-undot(uint8_t * rip)
+static void undot(uint8_t * rip)
 {
        int i = 0, s = 0;
-       while(rip[i]) i++;
-       for(--i; i >= 0; i--) {
-               if(rip[i] == '.') {
+       while (rip[i])
+               i++;
+       for (--i; i >= 0; i--) {
+               if (rip[i] == '.') {
                        rip[i] = s;
                        s = 0;
                } else s++;
@@ -107,8 +111,7 @@ undot(uint8_t * rip)
 /*
  * Append message to log file
  */
-static void
-log_message(char *filename, char *message)
+static void log_message(char *filename, char *message)
 {
        FILE *logfile;
        if (!daemonmode)
@@ -129,57 +132,54 @@ log_message(char *filename, char *message)
  * converting to a length/string substring for that label.
  */
 
-static int
-getfileentry(FILE * fp, struct dns_entry *s, int verb)
+static int getfileentry(FILE * fp, struct dns_entry *s)
 {
        unsigned int a,b,c,d;
        char *r, *name;
 
-restart:
-       if(!(r = bb_get_line_from_file(fp)))
+ restart:
+       r = xmalloc_fgets(fp);
+       if (!r)
                return -1;
-       while(*r == ' ' || *r == '\t') {
+       while (*r == ' ' || *r == '\t') {
                r++;
-               if(!*r || *r == '#' || *r == '\n')
+               if (!*r || *r == '#' || *r == '\n')
                        goto restart; /* skipping empty/blank and commented lines  */
        }
        name = r;
-       while(*r != ' ' && *r != '\t')
+       while (*r != ' ' && *r != '\t')
                r++;
        *r++ = 0;
-       if(sscanf(r,"%u.%u.%u.%u",&a,&b,&c,&d) != 4)
-                       goto restart; /* skipping wrong lines */
+       if (sscanf(r, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
+               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);
+       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);
 
-       if(verb)
-               fprintf(stderr,"\tname:%s, ip:%s\n",&(s->name[1]),s->ip);
+       if (OPT_verbose)
+               fprintf(stderr, "\tname:%s, ip:%s\n", &(s->name[1]),s->ip);
 
-       return 0; /* warningkiller */
+       return 0;
 }
 
 /*
  * Read hostname/IP records from file
  */
-static void
-dnsentryinit(int verb)
+static void dnsentryinit(void)
 {
        FILE *fp;
        struct dns_entry *m, *prev;
        prev = dnsentry = NULL;
 
-       if(!(fp = fopen(fileconf, "r")))
-               bb_perror_msg_and_die("open %s",fileconf);
+       fp = xfopen(fileconf, "r");
 
        while (1) {
-               if(!(m = (struct dns_entry *)malloc(sizeof(struct dns_entry))))
-                       bb_perror_msg_and_die("malloc dns_entry");
+               m = xmalloc(sizeof(struct dns_entry));
 
                m->next = NULL;
-               if (getfileentry(fp, m, verb))
+               if (getfileentry(fp, m))
                        break;
 
                if (prev == NULL)
@@ -195,15 +195,13 @@ dnsentryinit(int verb)
 /*
  * Set up UDP socket
  */
-static int
-listen_socket(char *iface_addr, int listen_port)
+static int listen_socket(char *iface_addr, int listen_port)
 {
        struct sockaddr_in a;
        char msg[100];
        int s;
        int yes = 1;
-       if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
-               bb_perror_msg_and_die("socket() failed");
+       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");
@@ -213,9 +211,8 @@ listen_socket(char *iface_addr, int listen_port)
        a.sin_family = AF_INET;
        if (!inet_aton(iface_addr, &a.sin_addr))
                bb_perror_msg_and_die("bad iface address");
-       if (bind(s, (struct sockaddr *)&a, sizeof(a)) < 0)
-               bb_perror_msg_and_die("bind() failed");
-       listen(s, 50);
+       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);
@@ -226,8 +223,7 @@ listen_socket(char *iface_addr, int listen_port)
  * Look query up in dns records and return answer if found
  * qs is the query string, first byte the string length
  */
-static int
-table_lookup(uint16_t type, uint8_t * as, uint8_t * qs)
+static int table_lookup(uint16_t type, uint8_t * as, uint8_t * qs)
 {
        int i;
        struct dns_entry *d=dnsentry;
@@ -237,28 +233,32 @@ table_lookup(uint16_t type, uint8_t * as, uint8_t * qs)
                char *p,*q;
                q = (char *)&(qs[1]);
                p = &(d->name[1]);
-               fprintf(stderr, "\ntest: %d <%s> <%s> %d", strlen(p), p, q, strlen(q));
+               fprintf(stderr, "\n%s: %d/%d p:%s q:%s %d", 
+                       __FUNCTION__, strlen(p), (int)(d->name[0]), p, q, 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])
-                                       continue;
+                       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
-                       fprintf(stderr, " OK");
+                               fprintf(stderr, " OK");
 #endif
-                       strcpy((char *)as, d->ip);
+                               strcpy((char *)as, d->ip);
 #ifdef DEBUG
-                       fprintf(stderr, " %s ", as);
+                               fprintf(stderr, " as:%s\n", as);
 #endif
                                        return 0;
-                               }
-               else if (type == REQ_PTR) { /* search by IP-address */
+                       }
+               } 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);
                                return 0;
                        }
                }
-       } while ((d = d->next) != NULL);
+               d = d->next;
+       } while (d);
        return -1;
 }
 
@@ -266,9 +266,8 @@ table_lookup(uint16_t type, uint8_t * as, uint8_t * qs)
 /*
  * Decode message and generate answer
  */
-#define eret(s) do { fprintf (stderr, "%s\n", s); return -1; } while (0)
-static int
-process_packet(uint8_t * buf)
+#define eret(s) do { fputs(s, stderr); return -1; } while (0)
+static int process_packet(uint8_t * buf)
 {
        struct dns_head *head;
        struct dns_prop *qprop;
@@ -283,13 +282,13 @@ process_packet(uint8_t * buf)
 
        head = (struct dns_head *)buf;
        if (head->nquer == 0)
-               eret("no queries");
+               eret("no queries\n");
 
-       if ((head->flags & 0x8000))
-               eret("ignoring response packet");
+       if (head->flags & 0x8000)
+               eret("ignoring response packet\n");
 
        from = (void *)&head[1];        //  start of query string
-       next = answb = from + strlen((char *)&head[1]) + 1 + sizeof(struct dns_prop);   // where to append answer block
+       next = answb = from + strlen((char *)from) + 1 + sizeof(struct dns_prop);   // where to append answer block
 
        outr.rlen = 0;                  // may change later
        outr.r = NULL;
@@ -313,9 +312,8 @@ process_packet(uint8_t * buf)
                goto empty_packet;
 
        // We have a standard query
-
-       log_message(LOG_FILE, (char *)head);
-       lookup_result = table_lookup(type, answstr, (uint8_t*)(&head[1]));
+       log_message(LOG_FILE, (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
                goto empty_packet;
@@ -349,7 +347,7 @@ process_packet(uint8_t * buf)
        memcpy(next, (void *)answstr, outr.rlen);
        next += outr.rlen;
 
     empty_packet:
+ empty_packet:
 
        flags = ntohs(head->flags);
        // clear rcode and RA, set responsebit and our new flags
@@ -365,53 +363,46 @@ process_packet(uint8_t * buf)
 /*
  * Exit on signal
  */
-static void
-interrupt(int x)
+static void interrupt(int x)
 {
        unlink(LOCK_FILE);
        write(2, "interrupt exiting\n", 18);
        exit(2);
 }
 
-#define is_daemon()  (flags&16)
-#define is_verbose() (flags&32)
-//#define DEBUG 1
-
 int dnsd_main(int argc, char **argv)
 {
        int udps;
        uint16_t port = 53;
        uint8_t buf[MAX_PACK_LEN];
-       unsigned long flags = 0;
        char *listen_interface = "0.0.0.0";
-       char *sttl=NULL, *sport=NULL;
+       char *sttl, *sport;
 
-       if(argc > 1)
-               flags = bb_getopt_ulflags(argc, argv, "i:c:t:p:dv", &listen_interface, &fileconf, &sttl, &sport);
-       if(sttl)
-               if(!(ttl = atol(sttl)))
+       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();
-       if(sport)
-               if(!(port = atol(sport)))
+       if (option_mask32 & 0x8) // -p
+               if (!(port = atol(sport)))
                        bb_show_usage();
 
-       if(is_verbose()) {
-               fprintf(stderr,"listen_interface: %s\n", listen_interface);
-               fprintf(stderr,"ttl: %d, port: %d\n", ttl, port);
-               fprintf(stderr,"fileconf: %s\n", fileconf);
+       if (OPT_verbose) {
+               bb_info_msg("listen_interface: %s", listen_interface);
+               bb_info_msg("ttl: %d, port: %d", ttl, port);
+               bb_info_msg("fileconf: %s", fileconf);
        }
 
-       if(is_daemon())
-#if defined(__uClinux__)
+       if (OPT_daemon)
+#ifdef BB_NOMMU
                /* reexec for vfork() do continue parent */
                vfork_daemon_rexec(1, 0, argc, argv, "-d");
-#else                                                  /* uClinux */
-               if (daemon(1, 0) < 0) {
-                       bb_perror_msg_and_die("daemon");
-               }
-#endif                                                 /* uClinuvx */
+#else
+               xdaemon(1, 0);
+#endif
 
-       dnsentryinit(is_verbose());
+       dnsentryinit();
 
        signal(SIGINT, interrupt);
        signal(SIGPIPE, SIG_IGN);
@@ -434,20 +425,20 @@ int dnsd_main(int argc, char **argv)
                FD_ZERO(&fdset);
                FD_SET(udps, &fdset);
                // Block until a message arrives
-               if((r = select(udps + 1, &fdset, NULL, NULL, NULL)) < 0)
+               r = select(udps + 1, &fdset, NULL, NULL, NULL);
+               if (r < 0)
                        bb_perror_msg_and_die("select error");
-               else
-               if(r == 0)
+               if (r == 0)
                        bb_perror_msg_and_die("select spurious return");
 
-               /* Can this test ever be false? */
+               /* 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);
-                       if(is_verbose())
+                       if (OPT_verbose)
                                fprintf(stderr, "\n--- Got UDP  ");
                        log_message(LOG_FILE, "\n--- Got UDP  ");
 
@@ -462,9 +453,7 @@ int dnsd_main(int argc, char **argv)
                                               r, 0, (struct sockaddr *)&from,
                                               fromlen);
                        }
-               }               // end if
-       }                       // end while
+               } // end if
+       } // end while
        return 0;
 }
-
-