Allow use hostname instead of ip address
authorteodor <teodor>
Thu, 29 Nov 2007 15:37:34 +0000 (15:37 +0000)
committerteodor <teodor>
Thu, 29 Nov 2007 15:37:34 +0000 (15:37 +0000)
tcp.c
udp.c

diff --git a/tcp.c b/tcp.c
index ecc3bf1..1616dc2 100644 (file)
--- a/tcp.c
+++ b/tcp.c
@@ -105,7 +105,7 @@ TC_ClientInitConnection(TC_Connection *cs, char *name, u_int32_t port) {
                        ntohs(cs->serv_addr.sin_port),strerror(errno));
 
         if (setsockopt(cs->fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0) {
-                tlog(TL_CRIT|TL_EXIT, "socketsockopt failed: %s (d%)", strerror(errno), errno);
+                tlog(TL_CRIT|TL_EXIT, "socketsockopt failed: %s (%d)", strerror(errno), errno);
                 return CS_ERROR;
         }
 
@@ -160,6 +160,27 @@ TC_fillConnection(TC_Connection *sc, char *name, u_int32_t port) {
        memset(sc, 0, sizeof(TC_Connection));
        sc->serv_addr.sin_family = AF_INET;
        sc->serv_addr.sin_addr.s_addr = (name) ? inet_addr(name) : htonl(INADDR_ANY);
+       if ( sc->serv_addr.sin_addr.s_addr == INADDR_NONE ) {
+               struct hostent *host;
+
+               /*
+                * Can't parse address: it's a DNS Name
+                */
+               host = gethostbyname(name);
+               if ( host && host->h_addrtype == AF_INET ) {
+                       memcpy(&sc->serv_addr.sin_addr.s_addr, host->h_addr_list[0], 
+                               sizeof(&sc->serv_addr.sin_addr.s_addr));
+               } else {
+#ifdef HAVE_HSTRERROR
+                       tlog(TL_CRIT,"gethostbyname: %s - %s", name, hstrerror(h_errno));
+#else
+                       tlog(TL_CRIT,"gethostbyname: %s - %s", name, strerror(errno));
+#endif
+                       sc->state = CS_ERROR;
+                       return sc;
+               }
+       }
+       
        sc->serv_addr.sin_port = htons(port);
        sc->state = CS_NOTINITED;
        return sc; 
diff --git a/udp.c b/udp.c
index 8450502..53f6c12 100644 (file)
--- a/udp.c
+++ b/udp.c
 #include <unistd.h>
 #include <fcntl.h>
 
+#ifdef HAVE_HSTRERROR
+#include <netdb.h>
+#endif
+
 #include "connection.h"
 #include "tlog.h"
 #include "tmalloc.h"
@@ -54,6 +58,26 @@ TC_AcceptUdp(char *host, int port) {
        memset(&serv_addr, 0, sizeof(serv_addr));
        serv_addr.sin_family = AF_INET;
        serv_addr.sin_addr.s_addr = inet_addr(host);
+       if ( serv_addr.sin_addr.s_addr == INADDR_NONE ) {
+               struct hostent *ip_host;
+
+               /*
+                * Can't parse address: it's a DNS Name
+                */
+               ip_host = gethostbyname(host);
+               if ( ip_host && ip_host->h_addrtype == AF_INET ) {
+                       memcpy(&serv_addr.sin_addr.s_addr, ip_host->h_addr_list[0],
+                               sizeof(&serv_addr.sin_addr.s_addr));
+               } else {
+#ifdef HAVE_HSTRERROR
+                               tlog(TL_CRIT,"gethostbyname: %s - %s", host, hstrerror(h_errno));
+#else
+                               tlog(TL_CRIT,"gethostbyname: %s - %s", host, strerror(errno));
+#endif
+                       close(sockfd);
+                       return -1;
+               }
+       }
        serv_addr.sin_port = htons(port);
 
        if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
@@ -114,6 +138,7 @@ TC_sendMsg( Msg *msg ) {
 
        if ( msg->sockfd <=0 ) {
                struct sockaddr_in cli_addr;
+
                if ( (msg->sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
                        tlog(TL_CRIT,"udp socket %s %d: %s", msg->host, msg->port, strerror(errno));
                        return CS_ERROR;
@@ -133,6 +158,27 @@ TC_sendMsg( Msg *msg ) {
                memset(&(msg->host_addr), 0, sizeof(msg->host_addr));
                msg->host_addr.sin_family = AF_INET;
                msg->host_addr.sin_addr.s_addr = inet_addr(msg->host);
+               if ( msg->host_addr.sin_addr.s_addr == INADDR_NONE ) {
+                       struct hostent *ip_host;
+
+                       /*
+                        * Can't parse address: it's a DNS Name
+                        */
+                       ip_host = gethostbyname(msg->host);
+                       if ( ip_host && ip_host->h_addrtype == AF_INET ) {
+                               memcpy(&msg->host_addr.sin_addr.s_addr, ip_host->h_addr_list[0],
+                                       sizeof(&msg->host_addr.sin_addr.s_addr));
+                       } else {
+#ifdef HAVE_HSTRERROR
+                               tlog(TL_CRIT,"gethostbyname: %s - %s", msg->host, hstrerror(h_errno));
+#else
+                               tlog(TL_CRIT,"gethostbyname: %s - %s", msg->host, strerror(errno));
+#endif
+                               close(msg->sockfd);
+                               msg->sockfd=-1;
+                               return CS_ERROR;
+                       }
+               }
                msg->host_addr.sin_port = htons(msg->port);
        }