library routine for determing broadcat address

library routine for determing broadcat address

Post by Paul Mackl » Sat, 11 Jun 1994 22:10:27



Hi,

Does anybody know of a library routine for determining
the TCP/IP broadcast address under SunOS 4.1.3? I guess
I am looking for something along the lines of gethostbyaddr(),
but any information would be much appreciated. BTW, not
all environments use the default x.x.x.255 broadcast
address as I had once thought myself.
--
-------------------------------------

Downtown Digital, NYC

 
 
 

library routine for determing broadcat address

Post by Oliver Lauma » Sat, 11 Jun 1994 23:40:50


Quote:> Does anybody know of a library routine for determining
> the TCP/IP broadcast address under SunOS 4.1.3?

I don't think such a routine exists in the C library.  Also, note that
your machine may have more than one network interface, each with its
own broadcast address.  Here is a small program that prints the
interface name and broadcast address for each interface that has a
broadcast address.  You may want to use this code when writing your own
library function.

-----------------------------------------------------------------------------
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>

main(ac,av) char **av; {
    struct sockaddr_in sin;
    struct ifconf ifc;
    struct ifreq ifs[10];
    int s, i, n;

    if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
        perror("socket"); return 1;
    }
    ifc.ifc_len = sizeof(ifs);
    ifc.ifc_req = ifs;
    if (ioctl(s, SIOCGIFCONF, &ifc) == -1) {
        perror("ioctl SIOCGIFCONF"); return 1;
    }
    for (n = ifc.ifc_len / sizeof(struct ifreq), i = 0; n > 0; n--, i++)  {
        if (ioctl(s, SIOCGIFBRDADDR, (char *)&ifs[i]) != -1) {
            bcopy((char *)&ifs[i].ifr_addr, (char *)&sin, sizeof(sin));
            printf("%s:\t%s\n", ifs[i].ifr_name, inet_ntoa(sin.sin_addr));
        }
    }
    return 0;

Quote:}