add .gitignore
[tedtools.git] / udp.c
diff --git a/udp.c b/udp.c
index 53f6c12..424860b 100644 (file)
--- a/udp.c
+++ b/udp.c
@@ -57,7 +57,7 @@ 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);
+       serv_addr.sin_addr.s_addr = (host && *host!='*') ? inet_addr(host) : htonl(INADDR_ANY);
        if ( serv_addr.sin_addr.s_addr == INADDR_NONE ) {
                struct hostent *ip_host;
 
@@ -69,11 +69,7 @@ TC_AcceptUdp(char *host, int port) {
                        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;
                }
@@ -110,6 +106,12 @@ TC_getMsg( int sockfd, Msg *msg ) {
                tlog(TL_ALARM, "Got message %d bytes (should be al least %d)", n, TCMSGHDRSZ );
                return CS_AGAIN;
        }
+       
+       /*
+        * convert from network byteorder
+        */
+       pmsg->len = ntohl(pmsg->len);
+       pmsg->type = ntohl(pmsg->type);
 
        if ( pmsg->len > MSGMAXSIZE  ) {
                tlog(TL_ALARM, "Messages (%d bytes) is too big", pmsg->len);
@@ -130,6 +132,8 @@ TC_getMsg( int sockfd, Msg *msg ) {
 /* send */
 u_int32_t 
 TC_sendMsg( Msg *msg ) {
+       int msglen;
+
        if ( msg->msg == NULL || msg->msg->len <=0  )
                return CS_OK;
 
@@ -169,11 +173,7 @@ TC_sendMsg( Msg *msg ) {
                                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;
@@ -182,7 +182,14 @@ TC_sendMsg( Msg *msg ) {
                msg->host_addr.sin_port = htons(msg->port);
        }
 
-       if (sendto(msg->sockfd, (void*)msg->msg, msg->msg->len, 0, (struct sockaddr *) &(msg->host_addr), sizeof(msg->host_addr)) != msg->msg->len) {
+       /*
+        * convert to network byteorder
+        */
+       msglen = msg->msg->len;
+       msg->msg->len = htonl(msg->msg->len);
+       msg->msg->type = htonl(msg->msg->type);
+
+       if (sendto(msg->sockfd, (void*)msg->msg, msglen, 0, (struct sockaddr *) &(msg->host_addr), sizeof(msg->host_addr)) != msglen) {
                tlog(TL_CRIT,"Can't send message to %s:%d : %s", msg->host, msg->port, strerror(errno));
                close(msg->sockfd);
                msg->sockfd=-1;