Use offsetof macro where possible to defined header size;
[tedtools.git] / udp.c
diff --git a/udp.c b/udp.c
index 6e127da..9c212af 100644 (file)
--- a/udp.c
+++ b/udp.c
@@ -110,6 +110,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 +136,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;
 
@@ -182,7 +190,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;