[ale] UDP Broadcasts

Horkan Smith ale at horkan.net
Thu Dec 12 18:35:57 EST 2013


Yup, netcat's a very good cli for UDP and TCP.

Debian carries both netcat-openbsd and netcat-traditional, w/ netcat-openbsd having quite a bit more functionality.  od, hexdump and tcpdump will also come in handy, but aren't required.

You can 'google' (or other preferred search engine) for "stevens udp server example" to find sources, including class homework from a college course or two.

I *think* I generated these years (decades?) ago by reading the Stevens Unix networking book....  Feel free to email w/ questions; commenting is non-existent.  I would consider these educational in nature, not production ready, btw.

udpserv.c - udp server, notice the 'INADDR_ANY' on the bind, that means I'll bind to any/all interfaces and not care who sent it.  You also get an example of select() for free.... 8-)  (select() is not required, you can use the recvfrom() w/out it.)
--start---
/* sample udp server code */

#include        <sys/types.h>
#include        <sys/socket.h>
#include        <sys/file.h>
#include        <netinet/in.h>
 
#include        <stdio.h>
#include        <netdb.h>
#include        <fcntl.h>
#include        <pwd.h>
#include        <time.h>
#include        <ctype.h>
#include        <unistd.h>
#include        <signal.h>
#include        <errno.h>
#include        <sys/wait.h>
 
#include        <sys/time.h>    /* gettimeofday() */
#include        <pwd.h>
#include        <memory.h>
#include        <string.h>
 
#include        <shadow.h>

void main(int argc, char *argv[])
  {
   int sockfd;
   int lport = 32722;
   int result;
   int salen;
   int status;

   struct sockaddr salocal;
   struct sockaddr saremote;
   struct sockaddr_in *sin;
   fd_set readfds;

   char recv_buffer[10240];


   sockfd = socket(AF_INET, SOCK_DGRAM, 0);
   if (sockfd < 0)
     {
      perror ("Error creating socket");
      exit (-1);
     }

   sin = (struct sockaddr_in *) & salocal;
   memset ((char *) sin, '\0', sizeof (salocal));
   sin->sin_family = AF_INET;
   sin->sin_addr.s_addr = INADDR_ANY;
   sin->sin_port = lport;

   result = bind (sockfd, & salocal, sizeof (*sin));
   if (result < 0)
     {
      perror ("auth bind");
      exit(-1);
     }
 
   for(;;)
     {
      FD_ZERO(&readfds);

      if(sockfd >= 0)
        {
         FD_SET(sockfd, &readfds);
        }

      status = select(32, &readfds, NULL, NULL, NULL);

      if(status == -1)
        {
         if (errno == EINTR) continue;
         break;
        }

      if(sockfd >= 0 && FD_ISSET(sockfd, &readfds))
        {
         salen = sizeof (saremote);
         result = recvfrom (sockfd,
                            (char *) recv_buffer,
                            (int) sizeof(recv_buffer),
                            (int) 0,
                            & saremote,
                            & salen);
 
         if(result > 0)
           {
            printf ("Got a message!\n");
            printf ("result: %d, salen: %d\n", result, salen);

            sin = (struct sockaddr_in *) &saremote;
            printf ("saremote: sin_port %d sin_addr %08x\n",
                    sin->sin_port, *((int*)(&(sin->sin_addr))));

            printf ("Message: %s\n", recv_buffer);
/*
            authreq = radrecv(ntohl(sin->sin_addr.s_addr),
                              ntohs(sin->sin_port),
                              recv_buffer, result);
                              radrespond(authreq, sockfd);
*/
            /* hexdump recv bytes */
            /* hexdump_buffer (recv_buffer, result); */
           }
          else if(result < 0 && errno == EINTR)
           {
            result = 0;
           }

        }

     }  /* for (;;) */

   close(sockfd);
  }
--end---

udpclient.c - also generated by me from info in the Stevens book.
--start---
/* sample udp client prog */

#include        <sys/types.h>
#include        <sys/socket.h>
#include        <netinet/in.h>
#include        <arpa/inet.h>
#include        <sys/param.h>
#include        <sys/time.h>
#include        <sys/select.h>
 
#include        <errno.h>
#include        <sys/errno.h>
#include        <netdb.h>
#include        <stdio.h>
#include        <stdlib.h>
#include        <syslog.h>
#include        <time.h>
#include        <unistd.h>
 

void main(int argv, char *argc[])
  {
   int sockfd;
   int length;
   struct sockaddr salocal;
   struct sockaddr saremote;
   struct sockaddr_in *sin;
   int remote_ipaddr = 0x7f000001;  /* localhost */
   int remote_port = 32722 ;  /* localhost */
   char msg[4096];


   sockfd = socket (AF_INET, SOCK_DGRAM, 0);
   if (sockfd < 0)
     {
      perror ("socket:");
      return;
     }

   length = sizeof (salocal);
   sin = (struct sockaddr_in *) & salocal;
   memset ((char *) sin, '\0', length);
   sin->sin_family = AF_INET;
   sin->sin_addr.s_addr = INADDR_ANY;
   sin->sin_port = htons (0);

   if (bind (sockfd, (struct sockaddr *) sin, length) < 0
    || getsockname (sockfd, (struct sockaddr *) sin, &length) < 0)
     {
      close (sockfd);
      perror ("bind:");
      return;
     }

   printf ("salocal: sin_port %d sin_addr %08x\n",
           sin->sin_port, *((int*)(&(sin->sin_addr))));
 
   sin = (struct sockaddr_in *) & saremote;
   memset ((char *) sin, '\0', sizeof (saremote));
   sin->sin_family = AF_INET;
   sin->sin_addr.s_addr = htonl (remote_ipaddr);
   sin->sin_port = htons (remote_port);

   sprintf (msg, "This is a test message\n");
   length = strlen(msg);
   for (;;)
     {
      sendto (sockfd, (char *) msg, (int) length, (int) 0,
              (struct sockaddr *) sin, sizeof (struct sockaddr_in));

      break;
     }

   close(sockfd);
  }
--end---


On Thu, Dec 12, 2013 at 07:17:01AM -0500, Ed Cashin wrote:
> It's Go, but I have a few examples in the "paxos" area of my go-getting
> github repo:
> 
> 
> https://github.com/ecashin/go-getting/blob/master/paxos/udptest-reciprocal.go
> 
> For the Linux half, maybe I'm reading too fast, but would tcpdump (can
> display payloads) plus netcat (can send and receive arbitrary packets) do
> the trick?
> 
> 
> 
> On Thu, Dec 12, 2013 at 12:55 AM, Alex Carver <agcarver+ale at acarver.net>wrote:
> 
> > Ok, a networking and a Linux question.
> >
> > First, does anyone have a reference (URL or source code) that shows how
> > to receive UDP broadcast packets from multiple sending devices?  In
> > other words, if I have two net devices each broadcasting UDP packets, I
> > want to build a listener that will capture the packets from both devices
> > (or more as I add devices).
> >
> > The Linux half of the question:  is there a quick CLI way of playing
> > with UDP similar to utilities like tcpconnect?  I want to be able to
> > examine the payloads before I dive into writing a C program although
> > maybe writing the C program that just echos the payload is the way to go
> > to figure out the base network stack.
> > _______________________________________________
> > Ale mailing list
> > Ale at ale.org
> > http://mail.ale.org/mailman/listinfo/ale
> > See JOBS, ANNOUNCE and SCHOOLS lists at
> > http://mail.ale.org/mailman/listinfo
> >
> 
> 
> 
> -- 
>   Ed Cashin <ecashin at noserose.net>
>   http://noserose.net/e/
>   http://www.coraid.com/

> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://mail.ale.org/mailman/listinfo/ale
> See JOBS, ANNOUNCE and SCHOOLS lists at
> http://mail.ale.org/mailman/listinfo


-- 
Horkan Smith
678-777-3263 cell, ale at horkan.net


More information about the Ale mailing list