Commits

Markus Tzoe committed 55e3cf1

eliminate address pushing
- change protocol as well.
- update client.

Comments (0)

Files changed (5)

 }
 
 #ifdef HAVE_RPRT
-void write_record(struct report* rt, struct sockaddr_in* src) {
-
-    if (daemon->rprt_cond == 1) { // overwrite address to src addr in restrict mode.
-        my_syslog(LOG_INFO, _("restrict mode: overwrite record address from %X to %X"), rt->payload.address.v4.sin_addr, src->sin_addr);
-        rt->payload.address.v4 = *src;
-    }
-
+void write_record(struct report* rt) {
+    // ignore daemon->rprt_altr currently.
     struct crec* cache = NULL;
     struct all_addr addr;
     addr.addr.addr4 = rt->payload.address.v4.sin_addr;
   char *addrbuff;
 
   #ifdef HAVE_RPRT
-  int rprt_cond;
+  int rprt_altr;
   #endif
 } *daemon;
 
         char hostname[32];
         union
         {
+            char altname[32];
             struct sockaddr v0;
             struct sockaddr_in v4;
             struct sockaddr_in6 v6;
         uint64_t size;
     } payload;
 };
-void write_record(struct report*, struct sockaddr_in*);
+void write_record(struct report*);
 void report_cache(struct report**, size_t size);
 void read_cache();
 void save_cache();
 #endif
 #include "dnsmasq.h"
 
-#ifdef __APPLE__
-#define _v6_ad_ __u6_addr
-#else
-#define _v6_ad_ __in6_u
-#endif
-
 const char *prog = NULL;
 
 void print_help(void)
     fprintf(stderr, " -f, --fetch <count>               Fetch stored [hostname:address]s in server.\n");
     fprintf(stderr, " -s, --server <host>               Override default server.\n");
     fprintf(stderr, "     --name <hostname>             Override hostname.\n");
-    fprintf(stderr, "     --device <etherno>            Specify ether device to report.\n");
     fprintf(stderr, "     --ack                         Ask server to send an ack after the primary action.\n");
     fprintf(stderr, " -h, --help                        Print this help.\n");
     fprintf(stderr, " -v, --version                     Print version infomation.\n\n");
     return 0;
 }
 
-static int makesocket(const char* host, const char* port, struct sockaddr* sa) {
+static int makesocket(const char* host, const char* port, struct sockaddr *sa) {
     struct addrinfo hints;
     struct addrinfo *result, *rp;
     int s, sockfd;
 
     for(rp = result; rp != NULL; rp = rp->ai_next){
         sockfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
-        if (sockfd == -1) continue;
-        else break;
-        close(sockfd);
+        if (sockfd != -1) break;
     }
     if(rp == NULL){
-        fprintf(stderr, "make socket failed.\n");
+        fprintf(stderr, "makesocket failed.\n");
         return -1;
     }
     if(sa != NULL){
         { "server", required_argument, NULL, 's' },
         { "version", no_argument, NULL, 'v' },
         { "name", required_argument, NULL, 256 },
-        { "device", required_argument, NULL, 257 },
-        { "ack", no_argument, NULL, 258 },
+        { "ack", no_argument, NULL, 257 },
         #ifdef HAVE_IPV6
-        { "ipv6", no_argument, NULL, 259 },
+        { "ipv6", no_argument, NULL, 258 },
         #endif
         { NULL, 0, NULL, 0 }
     };
     extern int optind;
     extern char *optarg;
     char *hname = NULL;
-    char *device = NULL;
-    int done = 0;
 
     (void) setlocale(LC_ALL, "");
 
         }
         case 257:
         {
-            device = optarg;
-            break;
-        }
-        case 258:
-        {
             flag |= RPRT_ACKN;
             break;
         }
-        case 259:
+        case 258:
         {
             family = AF_INET6;
             flag |= RPRT_IPV6;
     report.flags = flag;
     char buffer[ADDRSTRLEN];
 
-
     if (flag & RPRT_PUSH)
     {
         int ret = 0;
             hname = hostname;
         }
         memcpy(report.payload.hostname, hname, strlen(hname));
-        fprintf(stderr, "hostname: %s\n", hname);
 
-        struct ifaddrs *ifaddr = NULL;
-        struct ifaddrs *iap = NULL;
+        int sockfd;
+        struct sockaddr_in s;
 
-        ret = getifaddrs(&ifaddr);
-        if (ret == -1)
-        {
-            perror("getifaddrs");
+        sockfd = makesocket(server, RPRT_PORT_S, (struct sockaddr *)&s);
+        if(sockfd == -1){
             abort();
         }
+        socklen_t len = sizeof(s);
 
-        uint16_t mark = 0;
-
-        for (iap = ifaddr; iap != NULL; iap = iap->ifa_next)
-        {
-            if (iap->ifa_addr->sa_family == family)
-            {
-                switch(family){
-                    case AF_INET6:
-                    {
-                        if(inet_ntop(AF_INET6, &((struct sockaddr_in6*)iap->ifa_addr)->sin6_addr, buffer, ADDRSTRLEN) == NULL) {
-                            perror("inet6_ntop");
-                            abort();
-                        }
-                        mark = ((struct sockaddr_in6 *)iap->ifa_addr)->sin6_addr._v6_ad_.__u6_addr16[0] & 0xFFFF;
-                        if(mark != 0x0000 && mark != 0x80fe){
-                            mark = 1;
-                            report.payload.address.v6 = *((struct sockaddr_in6 *)iap->ifa_addr);
-                        }
-                        else mark = 0;
-                        break;
-                    }
-                    case AF_INET:
-                    default: {
-                        if(inet_ntop(AF_INET, &((struct sockaddr_in *)iap->ifa_addr)->sin_addr, buffer, ADDRSTRLEN) == NULL) {
-                            perror("inet_ntop");
-                            abort();
-                        }
-                        mark = ((struct sockaddr_in *)iap->ifa_addr)->sin_addr.s_addr & 0xFF;
-                        if(mark != 0x7F && mark != 0xc0){
-                            mark = 1;
-                            report.payload.address.v4 = *((struct sockaddr_in *)iap->ifa_addr);
-                        }
-                        else mark = 0;
-                        break;
-                    }
-                }
-                if(mark == 0) continue;
-                if (device != NULL) // user specified.
-                {
-                    if (strcmp(device, iap->ifa_name) == 0)
-                    {
-                        fprintf(stderr, "read %s: %s\n", iap->ifa_name, buffer);
-                        done = 1;
-                        break;
-                    }
-                    continue;
-                }
-                else
-                {
-                    fprintf(stderr, "read %s: %s\n", iap->ifa_name, buffer);
-                    done = 1;
-                    break;
-                }
-            }
-
+        if(sendto(sockfd, &report, sizeof(struct report), 0, (struct sockaddr*)&s, len) == -1){
+            perror("sendto");
+            abort();
         }
 
-        if (ifaddr != NULL) freeifaddrs(ifaddr);
-        if (done == 0) fprintf(stderr, "No valid device found. Try with '--device' option properly.\n");
-        else
-        {
-            int sockfd;
-            struct sockaddr_in s;
-
-            sockfd = makesocket(server, RPRT_PORT_S, (struct sockaddr*)&s);
-            if(sockfd == -1){
+        if (flag & RPRT_ACKN) {
+            fprintf(stderr, "receiving ack... ");
+            struct pollfd pfd;
+            pfd.fd = sockfd;
+            pfd.events = POLLIN | POLLPRI;
+            int ret = poll(&pfd, 1, 1500);
+            if (ret == -1) {
+                perror("poll");
                 abort();
             }
-            socklen_t len = sizeof(s);
-
-            if(sendto(sockfd, &report, sizeof(struct report), 0, (struct sockaddr*)&s, len) == -1){
-                perror("sendto");
+            if (ret == 0) {
+                fprintf(stderr, "timeout.\n");
                 abort();
             }
-
-            if (flag & RPRT_ACKN) {
-                fprintf(stderr, "receiving ack... ");
-                struct pollfd pfd;
-                pfd.fd = sockfd;
-                pfd.events = POLLIN | POLLPRI;
-                int ret = poll(&pfd, 1, 1500);
+            if(pfd.revents & (POLLIN | POLLPRI)){
+                fprintf(stderr, "ok.\n");
+                ret = recvfrom(sockfd, &report, sizeof(struct report), 0, (struct sockaddr*)&s, &len);
                 if (ret == -1) {
-                    perror("poll");
+                    perror("recvfrom");
                     abort();
                 }
-                if (ret == 0) {
-                    fprintf(stderr, "timeout.\n");
+                if (checkhdr(&report) == -1) {
+                    perror("checkhdr");
                     abort();
                 }
-                if(pfd.revents & (POLLIN | POLLPRI)){
-                    ret = recvfrom(sockfd, &report, sizeof(struct report), 0, (struct sockaddr*)&s, &len);
-                    if (ret == -1) {
-                        perror("recvfrom");
-                        abort();
-                    }
-                    if (checkhdr(&report) == -1) {
-                        perror("checkhdr");
-                        abort();
-                    }
 
-                    if(report.flags & RPRT_IPV6)
-                    {
-                        inet_ntop(AF_INET6, &(report.payload.address.v6.sin6_addr), buffer, INET6_ADDRSTRLEN);
-                    }
-                    else 
-                    {
-                        inet_ntop(AF_INET, &(report.payload.address.v4.sin_addr), buffer, INET_ADDRSTRLEN);
-                    }
-                    fprintf(stderr, "ok.\ndnsmasq server update cache [%s %s].\n", report.payload.hostname, buffer);
+                if(report.flags & RPRT_IPV6)
+                {
+                    inet_ntop(AF_INET6, &(report.payload.address.v6.sin6_addr), buffer, INET6_ADDRSTRLEN);
                 }
+                else 
+                {
+                    inet_ntop(AF_INET, &(report.payload.address.v4.sin_addr), buffer, INET_ADDRSTRLEN);
+                }
+                fprintf(stderr, "dnsmasq server update cache [%s %s].\n", report.payload.hostname, buffer);
             }
-            close(sockfd);
         }
-
+        close(sockfd);
     }
 
     if (flag & RPRT_PULL)
 #define LOPT_DUID      307
 #define LOPT_HOST_REC  308
 #ifdef HAVE_RPRT
-#define LOPT_RPRT_COND  309
+#define LOPT_RPRT_ALTR  309
 #endif
 
 #ifdef HAVE_GETOPT_LONG
     { "dhcp-duid", 1, 0, LOPT_DUID },
     { "host-record", 1, 0, LOPT_HOST_REC },
 #ifdef HAVE_RPRT
-    { "report-cond", 1, 0, LOPT_RPRT_COND },
+    { "report-altr", 1, 0, LOPT_RPRT_ALTR },
 #endif
     { NULL, 0, 0, 0 }
   };
   { LOPT_DUID, ARG_ONE, "<enterprise>,<duid>", gettext_noop("Specify DUID_EN-type DHCPv6 server DUID"), NULL },
   { LOPT_HOST_REC, ARG_DUP, "<name>,<address>", gettext_noop("Specify host (A/AAAA and PTR) records"), NULL },
 #ifdef HAVE_RPRT
-  { LOPT_RPRT_COND, ARG_ONE, "<1/0>", gettext_noop("Restrict reporter condition or not"), NULL },
+  { LOPT_RPRT_ALTR, ARG_ONE, "<1/0>", gettext_noop("Accept client alternative name"), NULL },
 #endif
   { 0, 0, NULL, NULL, NULL }
 }; 
       }
 
 #ifdef HAVE_RPRT
-      case LOPT_RPRT_COND: 
+      case LOPT_RPRT_ALTR: 
       {
         int cond;
         if(!atoi_check(arg, &cond)){
           option = '?';
         }
         else {
-          daemon->rprt_cond = cond == 0 ? 0 : 1;
+          daemon->rprt_altr = cond == 0 ? 0 : 1;
         }
         break;
       }
   daemon->edns_pktsz = EDNS_PKTSZ;
   daemon->log_fac = -1;
 #ifdef HAVE_RPRT
-  daemon->rprt_cond = 1;
+  daemon->rprt_altr = 1;
 #endif
   add_txt("version.bind", "dnsmasq-" VERSION );
   add_txt("authors.bind", "Simon Kelley");
 
         if (RPRT_PUSH & report.flags)
         {
-            write_record(&report, &faddr);
+            report.payload.address.v4 = faddr;
+            write_record(&report);
         }
     }