Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 30 Aug 2004 20:35:07 -0500
From:      "Lucas (a.k.a T-Bird or bsdfan3)" <tbird-contact@cox.net>
To:        <freebsd-questions@freebsd.org>
Subject:   Variable length packets?
Message-ID:  <000301c48efa$bfa00500$c022fc18@yourxu5v9frokn>

next in thread | raw e-mail | index | archive | help
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 




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?000301c48efa$bfa00500$c022fc18>