Date: Fri, 14 Mar 2008 09:23:59 -0600 From: "Andrew Falanga" <af300wsm@gmail.com> To: "Patrick Mahan" <mahan@mahan.org> Cc: FreeBSD Questions <freebsd-questions@freebsd.org> Subject: Re: Network programming question Message-ID: <340a29540803140823n6f838fdaj7692571f69003fb5@mail.gmail.com> In-Reply-To: <47DA13AF.5090804@mahan.org> References: <340a29540803130910l2a5badacxe50cd81ace87e1f7@mail.gmail.com> <47D9682B.5060402@mahan.org> <340a29540803131111g20315740n629ee146bc2f8602@mail.gmail.com> <47DA13AF.5090804@mahan.org>
next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, Mar 13, 2008 at 11:57 PM, Patrick Mahan <mahan@mahan.org> wrote: > > > > inet_pton() clobbered the fields you pointed out. In fact the sin_family > field was being set to 0x01 which caused your initial EADDRNOTSUPPORT error > you were seeing. You quick change fixed that problem. However, (depending > on how sockaddr_in structure is actually allocated) the sin_addr field was > 0.0.0.0. This is actually an accepted form of the broadcast address for UDP > packets. I forget exactly who the culprit was (Sun comes to mind) but there > was a need to allow broadcasts to 0.0.0.0 (which is also know as INADDR_ANY). > So, therefore, sendto() succeeded, just not in the way you expected. Looking > at in_pcbconnect_setup() in the kernel shows that actually the packet is sent > to the local primary interface address. > > Let's look at what really happen to that packet - > > "192.168.0.1" after being mangled by inet_pton() gives > the field sin_addr.s_addr of 0x0100A8C0. This should make > your sockaddr_in structure look like - > > sa.sin_len = 0x01 > sa.sin_family = 0x00 > sa.sin_port = 0xA8C0 (which is port 49320) > sa.sin_addr.s_addr = 0x00000000 > > So the sendto() call was sending a packet to your local interface for port 49320. > And since UDP is a connectionless protocol, you don't have a way (unless it is > builtin to your application protocol) to determine an error. For example, TFTP > sends back notification for every dgram received. > > I hope this helps with your understanding. I highly recommend if you are going > to do more network programming that you obtain at least some books on the subject. > > > Patrick > Thanks much for this explanation. Books would be good, yes. I guys got to learn somehow. Thanks for taking the time to explain it. That's interesting that a broadcast may be sent to 0.0.0.0. I knew that 0.0.0.0 is equal to INADDR_ANY. However, I thought it wasn't possible to send to that address, only to bind to it locally for a server application. 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?
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?340a29540803140823n6f838fdaj7692571f69003fb5>