[comp.protocols.tcp-ip] Internet broadcast: INADDR_BROADCAST

benny@vlss.amdahl.com (Benny Schnaider) (04/11/91)

Hi,

I am trying to broadcast a message over the internet by using the
INADDR_BROADCAST shorthand notation. I tried this on SunOS 4.01
and got: Network is unreachable... On other system that I tried
the same code it worked.
Code is enclosed.

1. What is the problem ?
2. How common is it to use INADDR_BROADCAST ?

Thanks,
Benny.

____________________________________________________________________________

  int sd, on;
  struct sockaddr_in sockAddr;

  /*
   * zero out the socket structures
   */
  bzero(&sockAddr, sizeof(sockAddr));

  /*
   * Open the socket
   */
  if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
    close(sd);
    BPCerror(LOG_WARNING, "in socket creation (request)");
    return(ERROR);
  }
 
  /*
   * Mark the socket to allow broadcast.
   */
  on = 1;
  if ((setsockopt(sd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on))) == -1) {
    close(sd);
    BPCerror(LOG_WARNING, "in SO_BROADCAST");
    return(ERROR);
  }

  /*
   * Fill sockAddrAs
   */
  sockAddr.sin_family       = AF_INET;
  sockAddr.sin_port         = IPPORT_BOOTPS;
  sockAddr.sin_addr.s_addr  = INADDR_ANY;

  /*
   * Bind the socket
   */
  if (bind(sd, &sockAddr, sizeof(sockAddr)) == -1) {
    close(sd);
    BPCerror(LOG_WARNING, "in socket binding (request)");
    return(ERROR);
  }

  /*
   * Broadcast the request
   */
  sockAddr.sin_addr.s_addr  = INADDR_BROADCAST;
  if (sendto(sd, msg, sizeof(*msg), 0, &sockAddr, sizeof(sockAddr)) == -1) {
    close(sd);
    BPCerror(LOG_WARNING, "sendto");
    return(ERROR);
  } /* sendto */

  close(sd);
  return(OK);

} /* BPCrequestIP() */

-----------------------------------------------------------------
Benny Schnaider    benny@vlss.amdahl.com
                   Amdahl Corporation, 1250 EAST Arques Avenue,
                   M/S 246, Sunnyvale CA, 94088-3470
                   (408) 296 - 0596, (408) 746-3440
-----------------------------------------------------------------
--
-----------------------------------------------------------------
Benny Schnaider    benny@vlss.amdahl.com
                   Amdahl Corporation, 1250 EAST Arques Avenue,
                   M/S 246, Sunnyvale CA, 94088-3470
                   (408) 296 - 0596, (408) 746-3440
-----------------------------------------------------------------

ken@racerx.UUCP (Ken Hardy) (04/12/91)

In article <BENNY.91Apr10110956@dimona.vlss.amdahl.com>, benny@vlss.amdahl.com (Benny Schnaider) writes:
> Hi,
> 
> I am trying to broadcast a message over the internet by using the
> INADDR_BROADCAST shorthand notation. I tried this on SunOS 4.01
> and got: Network is unreachable... On other system that I tried
> the same code it worked.
> Code is enclosed.
> 
> 1. What is the problem ?
> 2. How common is it to use INADDR_BROADCAST ?
> 
> Thanks,
> Benny.


You need to have a valid network in the network portion of
the internet address.  This is a routine I use on Suns to
build a broadcast address.  May not be cleanest code, but
it works on Sun 4.0 & 4.1.  The print statements yield:

		Name = [racerx]
		Host ID 80420006
		Net  ID 00008042
		Bcast ID 8042ffff

This get physically resolved on our system to a broadcast ethernet
address.  This may or may not be passed by bridges.  IP routers
may not pass it based on the network portion of the IP address,
and even if a bridge or router passes it, other IP hosts on another
network may not pay attention to it if they have another network
i.d.


----------------------------------------------------------------

int     bcast_address ()
/*
 *
 */
{
    struct in_addr bcip;
    unsigned ipad,
            netp;
    extern struct hostent *getmyhostent () ;


    if (my_he == (struct hostent *)0)
    {
        my_he = getmyhostent () ;
        memcpy (&myip.s_addr, my_he->h_addr, sizeof (myip.s_addr));
        memcpy (&my_ipaddr[0], my_he->h_addr, sizeof (myip.s_addr) ) ;
    }
    netp = inet_netof (myip);
    bcip = inet_makeaddr (netp, INADDR_BROADCAST);
    if (bug)
    {
        printf ("Name = [%s]\n", my_he->h_name);
        printf ("Host ID %08lx\n", htonl (myip.s_addr));
        printf ("Net  ID %08lx\n", netp);
        printf ("Bcast ID %08lx\n", htonl (bcip.s_addr));
    }

    RETURN (bcip.s_addr);
}

----------------------------------------------------------------


-- 
Ken Hardy		uunet!racerx!ken		ken@racerx.UUCP