Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Air pollution is really making us pay through the nose.


devel / comp.unix.programmer / Re: Berkeley Sockets -- Reply to UDP Broadcast

SubjectAuthor
o Re: Berkeley Sockets -- Reply to UDP BroadcastLew Pitcher

1
Re: Berkeley Sockets -- Reply to UDP Broadcast

<sjn6k8$lbf$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=16709&group=comp.unix.programmer#16709

  copy link   Newsgroups: comp.os.linux.networking comp.unix.programmer
Path: rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: lew.pitcher@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.os.linux.networking, comp.unix.programmer
Subject: Re: Berkeley Sockets -- Reply to UDP Broadcast
Date: Thu, 7 Oct 2021 16:18:16 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 49
Message-ID: <sjn6k8$lbf$1@dont-email.me>
References: <140867bc-9f6f-4875-8861-264c403c74a3n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 7 Oct 2021 16:18:16 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="a465bab7c8bf1477e104bc9132df5780";
logging-data="21871"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ym+f1X4kKl4JJlMKas8T6odk0Y47eYXQ="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:lV5Yrqd5F/T/+F7DCHSblFUECYw=
 by: Lew Pitcher - Thu, 7 Oct 2021 16:18 UTC

On Wed, 06 Oct 2021 11:33:20 -0700, Frederick Gotham wrote:

> I'm programming an embedded Linux device to send a reply to an incoming
> UDP broadcast packet. I spent a few hours today trying to figure this
> out, and it looks like I might be resorting to raw sockets with custom
> headers that I've written by myself byte by byte.
>
> Let's say the incoming UDP broadcast datagram has source port 5432, and
> destination port 1011.
>
> I want the reply packet I send to have source port 1011 and destination
> port 5432. I thought this would be possible by simply taking the address
> structure from the 'recvfrom' function and passing it verbatim to the
> 'sendto' function, however this doesn't work (it doesn't send the packet
> out).
>
> It seems as though I would have to create a new UDP socket and then send
> the reply packet on that new socket, however the problem here is that
> the source port will be an ephemeral port (e.g. 17654 instead of 1011).
>
> I played with all the settings like SO_BROADCAST but no joy.

In your server,
- bind() to port 1011, which allows the server to receive datagrams sent
to port 1011, and to send datagrams from port 1011
- recvfrom(), to populate a buffer with the received datagram, and
populate a struct sockaddr with the sender's address
- sendto(), with your server's reply buffer, and the struct sockaddr
populated by recvfrom(). This sends the reply datagram to the
sender's address

Your client will bind() to it's port (in your example, 5432) or use an
ephimeral port, and send() or sendto() the broadcast address, specifying
the same port as the server is listening on (in your example, port 1011).

Your server will bind() to it's port (your example was 1011), and
recvfrom() that socket to receive datagrams sent to that port. This will
include those datagrams sent "broadcast", as well as datagrams that were
specifically addressed to your server.

The server's recvfrom() will, in the case of received UDP datagrams,
populate the struct sockaddr with the client's source IP address and
source port number. If your client uses port 5432 as it's port number,
the struct sockaddr will contain port 5432, and the client's IP address.

HTH
--
Lew Pitcher
"In Skills, We Trust"

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor