From owner-freebsd-questions@FreeBSD.ORG Tue Aug 31 01:35:06 2004 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id DD0E316A4CE for ; Tue, 31 Aug 2004 01:35:06 +0000 (GMT) Received: from lakermmtao12.cox.net (lakermmtao12.cox.net [68.230.240.27]) by mx1.FreeBSD.org (Postfix) with ESMTP id 57F6E43D1D for ; Tue, 31 Aug 2004 01:35:06 +0000 (GMT) (envelope-from tbird-contact@cox.net) Received: from yourxu5v9frokn ([24.252.34.192]) by lakermmtao12.cox.net (InterMail vM.6.01.03.02.01 201-2131-111-104-103-20040709) with SMTP id <20040831013501.ILFG10626.lakermmtao12.cox.net@yourxu5v9frokn> for ; Mon, 30 Aug 2004 21:35:01 -0400 Message-ID: <000301c48efa$bfa00500$c022fc18@yourxu5v9frokn> From: "Lucas (a.k.a T-Bird or bsdfan3)" To: Date: Mon, 30 Aug 2004 20:35:07 -0500 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2900.2180 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 Subject: Variable length packets? X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 31 Aug 2004 01:35:07 -0000 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