I am trying to implement a custom protocol that sends and receives variable-length packets on top of TCP/IPv4. The problem is that the length field of the packet is silently being mangled first becoming 0 and then getting turned into a very large number (about 2-3 billion). The length field is a u_int32_t and I am using the byteorder routines. Source code snippets follow:

--decl of struct packet_t--
struct packet_t
 u_int16_t num;
 u_int32_t len;
 char data[0];
--receive code--
     if (read (s, auth_header, sizeof (struct packet_t)))
         perror ("srvrmond read: could not read from socket");
         exit (2);
     auth_header -> num = ntohs (auth_header -> num);

auth_header -> len = ntohl (auth_header -> len);
--send code--
struct packet_t *auth_data2 = malloc (sizeof (struct packet_t) + AUTHCOOKIE_S\
IZE + sizeof (time_t));
auth_data2 -> num = htons (100);
auth_data2 -> len = htonl (AUTHCOOKIE_SIZE + sizeof (time_t));
fprintf (stderr, "Packet payload length: %lu", ntohl (auth_data2 -> len));
long temp = htonl ((long)time (0) & 0xffffff00);
memcpy (auth_data2 -> data, cookie, AUTHCOOKIE_SIZE);
memcpy (auth_data2 -> data + AUTHCOOKIE_SIZE, &temp, sizeof (time_t));
if (write (s, auth_data2, AUTHCOOKIE_SIZE + sizeof (struct packet_t) + sizeof\
(time_t)) == -1)
perror ("srvrmon write");
free (auth_data2);
exit (1);

P.S. Please CC me, I am not subscribed to the list

_______________________________________________ [EMAIL PROTECTED] mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"

Reply via email to