Commits

Markus Tzoe committed ec07991

update client: use getaddrinfo().

Comments (0)

Files changed (1)

 
 #ifndef HAVE_RPRT
 #define HAVE_RPRT
+#define RPRT_PORT_S "5335"
 #endif
 #include "dnsmasq.h"
 
     return 0;
 }
 
+static int makesocket(const char* host, const char* port, struct sockaddr* sa) {
+    struct addrinfo hints;
+    struct addrinfo *result, *rp;
+    int s, sockfd;
+
+    memset(&hints, 0, sizeof(struct addrinfo));
+    #ifdef HAVE_IPV6
+    hints.ai_family = AF_UNSPEC;
+    #else
+    hints.ai_family = AF_INET;
+    #endif
+    hints.ai_socktype = SOCK_DGRAM;
+    hints.ai_flags = AI_NUMERICSERV;
+
+    s = getaddrinfo(host, port, &hints, &result);
+    if (s != 0) {
+        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
+        return -1;
+    }
+
+    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(rp == NULL){
+        fprintf(stderr, "make socket failed.\n");
+        return -1;
+    }
+    if(sa != NULL){
+        *sa = *(rp->ai_addr);
+    }
+    freeaddrinfo(result);
+    return sockfd;
+}
+
 int main(int argc, char const *argv[])
 {
     register char *cp;
             if (ret == -1)
             {
                 perror("gethostname");
-                exit(-1);
+                abort();
             }
             // get short version of host name;
             char *ch = strchr(hostname, '.');
         if (ret == -1)
         {
             perror("getifaddrs");
-            exit(-1);
+            abort();
         }
 
         uint16_t mark = 0;
                     {
                         if(inet_ntop(AF_INET6, &((struct sockaddr_in6*)iap->ifa_addr)->sin6_addr, buffer, ADDRSTRLEN) == NULL) {
                             perror("inet6_ntop");
-                            exit(-1);
+                            abort();
                         }
                         mark = ((struct sockaddr_in6 *)iap->ifa_addr)->sin6_addr._v6_ad_.__u6_addr16[0] & 0xFFFF;
                         if(mark != 0x0000 && mark != 0x80fe){
                     default: {
                         if(inet_ntop(AF_INET, &((struct sockaddr_in *)iap->ifa_addr)->sin_addr, buffer, ADDRSTRLEN) == NULL) {
                             perror("inet_ntop");
-                            exit(-1);
+                            abort();
                         }
                         mark = ((struct sockaddr_in *)iap->ifa_addr)->sin_addr.s_addr & 0xFF;
                         if(mark != 0x7F && mark != 0xc0){
         {
             int sockfd;
             struct sockaddr_in s;
-            struct hostent *host;
 
-            sockfd = socket(AF_INET, SOCK_DGRAM, 0);
-            if (sockfd < 0)
-            {
-                perror("socket");
-                exit(1);
+            sockfd = makesocket(server, RPRT_PORT_S, (struct sockaddr*)&s);
+            if(sockfd == -1){
+                abort();
             }
-
-            if (!(host = gethostbyname(server)))
-            {
-                perror("gethostbyname");
-                exit(1);
-            }
-
-            memset(&s, 0, sizeof(s));
-            s.sin_family = AF_INET;
-            s.sin_addr.s_addr = ((struct in_addr *)(host->h_addr))->s_addr;
-            s.sin_port = htons(RPRT_PORT);
             socklen_t len = sizeof(s);
 
             if(sendto(sockfd, &report, sizeof(struct report), 0, (struct sockaddr*)&s, len) == -1){
                 perror("sendto");
-                exit(1);
+                abort();
             }
 
             if (flag & RPRT_ACKN) {
                 int ret = poll(&pfd, 1, 1500);
                 if (ret == -1) {
                     perror("poll");
-                    exit(1);
+                    abort();
                 }
                 if (ret == 0) {
                     fprintf(stderr, "timeout.\n");
-                    exit(0);
+                    abort();
                 }
                 if(pfd.revents & (POLLIN | POLLPRI)){
                     ret = recvfrom(sockfd, &report, sizeof(struct report), 0, (struct sockaddr*)&s, &len);
                     if (ret == -1) {
                         perror("recvfrom");
-                        exit(1);
+                        abort();
                     }
                     if (checkhdr(&report) == -1) {
                         perror("checkhdr");
-                        exit(1);
+                        abort();
                     }
 
                     if(report.flags & RPRT_IPV6)