From owner-freebsd-questions@FreeBSD.ORG Thu Mar 13 18:11:31 2008 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 5AE961065674 for ; Thu, 13 Mar 2008 18:11:31 +0000 (UTC) (envelope-from af300wsm@gmail.com) Received: from ug-out-1314.google.com (ug-out-1314.google.com [66.249.92.170]) by mx1.freebsd.org (Postfix) with ESMTP id D93C88FC19 for ; Thu, 13 Mar 2008 18:11:30 +0000 (UTC) (envelope-from af300wsm@gmail.com) Received: by ug-out-1314.google.com with SMTP id y2so558733uge.37 for ; Thu, 13 Mar 2008 11:11:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; bh=H+k7LWoDF8h8CRN6LkfH5dZ1G3Yx1/91fQ8u0ssJY44=; b=gRTJBztFJw/IkywttC8lq2lVC2CdLe845y3/5I4WiNwXGZ01pWMdpJUR59lvpg+Ai4pxK+IPRLHF7DVKJA6m6Q/z5S4wPvWfJ0Ga8ujPWft09GX/F3+4bZZJyt5RyJS3HTsRNvulkpQHTX5eIvOJjM03daeJRf3wNss+7TFv5Nc= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=L+VvZ7KIhJHk7t5hRV2q2lELDnWhwv3NS9LxMSIheGx2kkoh+LoqiKf5AYJCL9c1kRMUu7HTWfQ4ISO8JFffTMLX2nyb30JgxuVYDP0OBeJmVRvALvemjNuT4iZXgn0qXwVzFzK5h4qjfUnDFhdmJEpDhQElFX5dppeFrCuxMzY= Received: by 10.114.156.1 with SMTP id d1mr9940808wae.120.1205431888343; Thu, 13 Mar 2008 11:11:28 -0700 (PDT) Received: by 10.114.191.15 with HTTP; Thu, 13 Mar 2008 11:11:28 -0700 (PDT) Message-ID: <340a29540803131111g20315740n629ee146bc2f8602@mail.gmail.com> Date: Thu, 13 Mar 2008 12:11:28 -0600 From: "Andrew Falanga" To: "Patrick Mahan" In-Reply-To: <47D9682B.5060402@mahan.org> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <340a29540803130910l2a5badacxe50cd81ace87e1f7@mail.gmail.com> <47D9682B.5060402@mahan.org> Cc: FreeBSD Questions Subject: Re: Network programming question X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 13 Mar 2008 18:11:31 -0000 On Thu, Mar 13, 2008 at 11:45 AM, Patrick Mahan wrote: > > > Andrew Falanga presented these words - circa 3/13/08 9:10 AM-> > > > Hi, > > See man inet_pton . . . for details. > > Briefly, inet_pton() doesn't understand sockaddr structures. Instead, > it only understands in_addr or in6_addr structures which are included > inside the sockaddr structure. So your above example should be changed > to > Ok, I should have thought of that when reading the manual. > > if ((res = inet_pton(AF_INET, "192.168.0.1", &sa.sin_addr)) < 0) > perror("inet_pton"); > > > Because it is treating the sockaddr_in structure as an in_addr structure > which is clobbering the sin_family field. > If this is true, then why are my packets sent at all? The definition of sockaddr_in (from /usr/include/netinet/in.h): struct sockaddr_in { uint8_t sin_len; sa_family_t sin_family; in_port_t sin_port; struct in_addr sin_addr; char sin_zero[8]; }; The definition of in_addr (from /usr/include/netinet/in.h): struct in_addr { in_addr_t s_addr; }; The definition of in_addr_t (from /usr/include/netinet/in.h): typedef uint32_t in_addr_t; Passing in what I have, the address should indeed (as you've pointed out) clobber the sin_family member. However, since in_addr is basically an unsigned integer, i.e. 4 bytes wide, shouldn't inet_pton(3) clobber sin_len, sin_family & sin_port before ever reaching sin_addr? The sin_len & sin_family are 8 bit quantities, the sin_port is 16 bits, that's 32. If inet_pton(3) is expecting only an in_addr I would think that a call to sendto(2) would fail because the address in sin_addr is not filled, correct? Andy -- A: Because it messes up the order in which people normally read text. Q: Why is it such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail?