Commits

Markus Tzoe committed 8a982d3

ack impled.
- fix: wait till data on fd is available;
- move restrict_mode to write_record, update its interface;
- client use poll();

  • Participants
  • Parent commits f1d980d

Comments (0)

Files changed (4)

 }
 
 #ifdef HAVE_RPRT
-void write_record(struct report* rt) {
+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.record.addr.addr.addr4, src->sin_addr);
+        rt->payload.record.addr.addr.addr4 = src->sin_addr;
+    }
+
     struct crec* cache = NULL;
     struct all_addr addr;
     addr.addr = rt->payload.record.addr.addr;

File src/dnsmasq.h

         uint64_t size;
     } payload;
 };
-void write_record(struct report*);
+void write_record(struct report*, struct sockaddr_in*);
 void report_cache(struct report**, size_t size);
 void read_cache();
 void save_cache();

File src/nsreporter.c

 #include <locale.h>
 #include <sys/types.h>
 #include <sys/socket.h>
+#include <sys/poll.h>
 #include <ifaddrs.h>
 #include <arpa/inet.h>
 #include <netdb.h>
                 exit(1);
             }
 
+            if (flag & RPRT_ACKN) {
+                fprintf(stderr, "receiving ack... ");
+                struct pollfd pfd;
+                pfd.fd = sockfd;
+                pfd.events = POLLIN;
+                int ret = poll(&pfd, 1, 1500);
+                if (ret == -1) {
+                    perror("poll");
+                    exit(1);
+                }
+                if (ret == 0) {
+                    fprintf(stderr, "timeout.\n");
+                    exit(0);
+                }
+                if(pfd.revents & POLLIN){
+                    ret = recv(sockfd, &report, sizeof(struct report), 0);
+                    if (ret == -1) {
+                        perror("recv");
+                        exit(1);
+                    }
+
+                    if(report.flags & RPRT_IPV6)
+                    {
+                        inet_ntop(AF_INET6, &(report.payload.record.addr.addr.addr6), buffer, ADDRSTRLEN);
+                    }
+                    else 
+                    {
+                        inet_ntop(AF_INET, &(report.payload.record.addr.addr.addr4), buffer, ADDRSTRLEN);
+                    }
+                    fprintf(stderr, "ok.\ndnsmasq server update cache [%s %s].\n", report.payload.record.hostname, buffer);
+                }
+            }
             close(sockfd);
         }
 

File src/reporter.c

 
 #ifdef HAVE_RPRT
 
+void reply_ack(int fd, struct report* rt) {
+    if(send(fd, rt, sizeof(struct report), 0) == -1){
+        my_syslog(LOG_ERR, _("reply_ack: error %d occurs."), errno);
+        return;
+    }
+    close(fd);
+
+}
+
 void accept_report(struct listener *listen, time_t now)
 {
-    int fd;
+    int fd, dd;
     struct sockaddr_in faddr;
     socklen_t len = sizeof(faddr);
     while ((fd = accept(listen->rprtfd, (struct sockaddr *)&faddr, &len)) == -1 && errno == EINTR);
     }
     struct report report;
     memset(&report, 0, sizeof(struct report));
-    if (recv(fd, &report, sizeof(struct report), 0) == -1)
+    while ((dd = recv(fd, &report, sizeof(struct report), 0) == -1) && errno == EAGAIN);
+    if (dd == -1)
     {
         my_syslog(LOG_ERR, _("%d: accept_report: unkonw error: %d"), now, errno);
         return;
     }
-    
-    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"), report.payload.record.addr.addr.addr4, faddr.sin_addr);
-        report.payload.record.addr.addr.addr4 = faddr.sin_addr;
-    }
 
     if(inet_ntop(faddr.sin_family, &faddr.sin_addr, daemon->addrbuff, ADDRSTRLEN) == NULL) {
         my_syslog(LOG_ERR, ("%d: inet_ntop() error. abort cache insertion."), now);
     }*/
     if (RPRT_PUSH & report.flags)
     {
-        write_record(&report);
+        write_record(&report, &faddr);
+    }
+    if (RPRT_ACKN & report.flags)
+    {
+        reply_ack(fd, &report);
     }
 
 }