From owner-freebsd-questions@FreeBSD.ORG Thu Mar 13 17:45:17 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 3AF561065673 for ; Thu, 13 Mar 2008 17:45:17 +0000 (UTC) (envelope-from mahan@mahan.org) Received: from ns.mahan.org (ns.mahan.org [67.116.10.138]) by mx1.freebsd.org (Postfix) with ESMTP id DE9E18FC24 for ; Thu, 13 Mar 2008 17:45:16 +0000 (UTC) (envelope-from mahan@mahan.org) Received: from widowmaker.local (crowTrobot [67.116.10.140]) by ns.mahan.org (8.13.6/8.13.6) with ESMTP id m2DHjWtH069328; Thu, 13 Mar 2008 10:45:33 -0700 (PDT) (envelope-from mahan@mahan.org) Message-ID: <47D9682B.5060402@mahan.org> Date: Thu, 13 Mar 2008 10:45:15 -0700 From: Patrick Mahan User-Agent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.8.1.12) Gecko/20080213 Thunderbird/2.0.0.12 Mnenhy/0.7.5.0 MIME-Version: 1.0 To: Andrew Falanga References: <340a29540803130910l2a5badacxe50cd81ace87e1f7@mail.gmail.com> In-Reply-To: <340a29540803130910l2a5badacxe50cd81ace87e1f7@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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 17:45:17 -0000 Andrew Falanga presented these words - circa 3/13/08 9:10 AM-> > Hi, > > I'd like to know why the inet_pton(3) doesn't fill in the address > family of the proper structure passed into it. I'm at a complete loss > for why. Here's the prototype: > > int inet_pton(int af, const char * restrict src, void * restrict dst); > > > Three arguments only. The address family, hm, I'm passing it in; the > address string in printable ASCII text, and a void pointer to the > address structure to put the address into, presumably one of the > sockaddr_* family structures for AF_INET or AF_INET6 (further, the man > page says that this function is only valid for these two families now > anyway). > >>From some coding for a program, I did find that this function, > inet_pton(3), *does* in fact mangle the sin_family member of the > sockaddr_in structure, so why not "mangle" it to what it should be? I > was doing something like this: > > // valid code above > sockaddr_in sa; > > sa.sin_family = AF_INET; > sa.sin_port = htons(3252); > > inet_pton(AF_INET, "192.168.0.1", &sa); > > sendto(sa, msg, strlen(msg), 0, (struct sockaddr*)&sa, sizeof(sa)); > 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 // valid code above sockaddr_in sa; int res; sa.sin_family = AF_INET; sa.sin_port = htons(3252); if ((res = inet_pton(AF_INET, "192.168.0.1", &sa.sin_addr)) < 0) perror("inet_pton"); if (!res) // error occurred fprintf(stderr, "Address notation incorrect for AF_INET address\n"); > > The call to sendto is wrapped in an if an was failing for errno code > 47, Address family not supported by protocol (I was using UDP). I > changed the assignment of AF_INET to the sa.sin_family member to > *after* the call to inet_pton(3) and suddenly everything worked. Why? > Since the address family was used by inet_pton(3) to figure out how > to read the address and assign it to sa.sin_addr.s_addr, why not > simply assign AF_INET to the address family member in inet_pton(3)? > Because it is treating the sockaddr_in structure as an in_addr structure which is clobbering the sin_family field. > I'm not trying to be argumentative. I'm just curious. It seems like > redundancy. I've used the address family to tell inet_pton(3) how to > operate, and then this function can't assign it to the sockaddr_in > structure passed to it? This makes little sense. In case it's > because I'm using older FBSD libraries that had a flaw fixed, I'm > using FreeBSD 6.2-RELEASE-p4. Is this because that's how POSIX > defined it to work? Is this the right venue or should I try one of > the other mailing lists? > RTM, Patrick