Getting Broadcast information. - Unix

This is a discussion on Getting Broadcast information. - Unix ; So I'm trying to get the broadcast address for my machines current network interface. I'm using this site http://ou800doc.caldera.com/en/SDK_n...t_netconf.html and Unix Network Programming by R. Stevens. I'm kind of stumped now. Here's my code that fully compiles. The errors I ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: Getting Broadcast information.

  1. Getting Broadcast information.

    So I'm trying to get the broadcast address for my machines current
    network interface. I'm using this site
    http://ou800doc.caldera.com/en/SDK_n...t_netconf.html
    and Unix Network Programming by R. Stevens. I'm kind of stumped now.
    Here's my code that fully compiles. The errors I get are

    Help:: No such device
    Help:: Address family not supported by protocol

    I've ran this code threw gdb and it's not finding a device in the for
    loop. It find 2 devices but neither are of AF_INET related. IN fact
    the last device is just full of Null's. Not sure what's happening. The
    system has a network card because I programming this threw ssh on it.

    Any suggestions? Thanks in advance.



    #include
    #include
    #include
    #include
    #include


    int main()
    {
    int s, on = 1;
    struct sockaddr_in sin, dst;
    struct ifconf ifc;
    struct ifreq *ifr;
    char buf[2048];
    int n;

    s = socket(AF_INET, SOCK_DGRAM, 0);
    if(s < 0)
    perror("Help:");

    if(setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0)
    perror("Help:");

    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = htonl(INADDR_ANY);
    sin.sin_port = htons(2190);

    if(bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)
    perror("Help:");

    ifc.ifc_len = sizeof(buf);
    ifc.ifc_buf = buf;
    if(ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0)
    perror("Help:");

    for (n=ifc.ifc_len/sizeof(struct ifreq); --n >= 0; ifr++)
    {
    if(ifr->ifr_addr.sa_family != AF_INET)
    continue;
    if(ioctl(s, SIOCGIFFLAGS, (char *) ifr) < 0)
    perror("Help:");
    if((ifr->ifr_flags & IFF_UP) == 0 || (ifr->ifr_flags &
    IFF_LOOPBACK) || (ifr->ifr_flags & (IFF_BROADCAST
    continue;
    }
    if(ifr->ifr_flags & IFF_POINTOPOINT)
    {
    if(ioctl(s, SIOCGIFDSTADDR, (char *) ifr) < 0)
    perror("Help:");
    memcpy((char *) &dst, (char *) &ifr->ifr_dstaddr,
    sizeof(ifr->ifr_dstaddr));
    }
    else if (ifr->ifr_flags & IFF_BROADCAST)
    {
    if(ioctl(s, SIOCGIFBRDADDR, (char *) ifr) < 0)
    perror("Help:");
    memcpy((char *) &dst, (char *) &ifr->ifr_broadaddr,
    sizeof(ifr->ifr_broadaddr));
    }
    if(sendto(s, "HELLO MOTHER", 13, 0, (struct sockaddr *)&dst,
    sizeof(dst)) < 0)
    perror("Help:");

    return 0;
    }

  2. Re: Getting Broadcast information.

    In article <13l1phseu4ac583@corp.supernews.com>,
    Ryan Knopp wrote:

    > So I'm trying to get the broadcast address for my machines current
    > network interface. I'm using this site
    > http://ou800doc.caldera.com/en/SDK_n...t_netconf.html
    > and Unix Network Programming by R. Stevens. I'm kind of stumped now.
    > Here's my code that fully compiles. The errors I get are
    >
    > Help:: No such device
    > Help:: Address family not supported by protocol
    >
    > I've ran this code threw gdb and it's not finding a device in the for
    > loop. It find 2 devices but neither are of AF_INET related. IN fact
    > the last device is just full of Null's. Not sure what's happening. The
    > system has a network card because I programming this threw ssh on it.
    >
    > Any suggestions? Thanks in advance.
    >
    >
    >
    > #include
    > #include
    > #include
    > #include
    > #include
    >
    >
    > int main()
    > {
    > int s, on = 1;
    > struct sockaddr_in sin, dst;
    > struct ifconf ifc;
    > struct ifreq *ifr;
    > char buf[2048];
    > int n;
    >
    > s = socket(AF_INET, SOCK_DGRAM, 0);
    > if(s < 0)
    > perror("Help:");
    >
    > if(setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0)
    > perror("Help:");
    >
    > sin.sin_family = AF_INET;
    > sin.sin_addr.s_addr = htonl(INADDR_ANY);
    > sin.sin_port = htons(2190);
    >
    > if(bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)
    > perror("Help:");
    >
    > ifc.ifc_len = sizeof(buf);
    > ifc.ifc_buf = buf;
    > if(ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0)
    > perror("Help:");
    >
    > for (n=ifc.ifc_len/sizeof(struct ifreq); --n >= 0; ifr++)
    > {
    > if(ifr->ifr_addr.sa_family != AF_INET)


    You never assign ifr, and now you're indirecting through it as if it
    contained the result of the ioctl.

    > continue;
    > if(ioctl(s, SIOCGIFFLAGS, (char *) ifr) < 0)
    > perror("Help:");
    > if((ifr->ifr_flags & IFF_UP) == 0 || (ifr->ifr_flags &
    > IFF_LOOPBACK) || (ifr->ifr_flags & (IFF_BROADCAST
    > continue;
    > }
    > if(ifr->ifr_flags & IFF_POINTOPOINT)
    > {
    > if(ioctl(s, SIOCGIFDSTADDR, (char *) ifr) < 0)
    > perror("Help:");
    > memcpy((char *) &dst, (char *) &ifr->ifr_dstaddr,
    > sizeof(ifr->ifr_dstaddr));
    > }
    > else if (ifr->ifr_flags & IFF_BROADCAST)
    > {
    > if(ioctl(s, SIOCGIFBRDADDR, (char *) ifr) < 0)
    > perror("Help:");
    > memcpy((char *) &dst, (char *) &ifr->ifr_broadaddr,
    > sizeof(ifr->ifr_broadaddr));
    > }
    > if(sendto(s, "HELLO MOTHER", 13, 0, (struct sockaddr *)&dst,
    > sizeof(dst)) < 0)
    > perror("Help:");
    >
    > return 0;
    > }

    --
    Barry Margolin
    Arlington, MA

  3. Re: Getting Broadcast information.

    On Nov 30, 8:42 pm, Ryan Knopp
    wrote:
    > So I'm trying to get the broadcast address for my machines current
    > network interface. I'm using this sitehttp://ou800doc.caldera.com/en/SDK_netapi/sockC.bcast_det_netconf.html
    > and Unix Network Programming by R. Stevens. I'm kind of stumped now.
    > Here's my code that fully compiles. The errors I get are
    >
    > Help:: No such device
    > Help:: Address family not supported by protocol

    [...]
    > s = socket(AF_INET, SOCK_DGRAM, 0);
    > if(s < 0)
    > perror("Help:");
    >
    > if(setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0)
    > perror("Help:");

    [...]

    In addition to other suggestions, it would probably be easier if you
    had perror() print a more descriptive message that would tell you what
    function failed. E.g.

    if (socket(...) < 0)
    perror("socket");
    if (setsockopt(...) < 0)
    perror("setsockopt");

    etc. If more than a fixed string is needed, try something like

    fprintf("Failed to write %d bytes to %s: %s\n", bytes, filename,
    strerror(errno));

  4. Re: Getting Broadcast information.

    Thanks for the help, I didn't notice that.

    Now I've updated my code and did some pruning and I'm having a problem
    with the sendto function. When I run this code I get this as an output.

    192.168.0.255
    sendto:: Invalid argument

    dst seems to have the correct information and I can't seem to find or
    understand why sendto has an Invalid argument.

    Thanks for your help!
    Ryan


    #include
    #include
    #include
    #include
    #include
    #include
    #include

    int main()
    {
    int s, on = 1;
    struct sockaddr_in sin;
    struct sockaddr_in dst;
    struct ifconf ifc;
    struct ifreq *ifr;
    char buf[2048];
    char buf2[] = "HELLO MOTHER";
    int n;

    s = socket(AF_INET, SOCK_DGRAM, 0);
    if(s < 0)
    perror("socket:");

    if(setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0)
    perror("setsockopt:");

    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = htonl(INADDR_ANY);
    sin.sin_port = htons(2190);

    if(bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)
    perror("bind:");

    ifc.ifc_len = sizeof(buf);
    ifc.ifc_buf = buf;
    if(ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0)
    perror("ioctl-conf:");

    ifr = ifc.ifc_req;
    for (n=ifc.ifc_len/sizeof(struct ifreq); --n >= 0; ifr++)
    {
    if(!strcmp(ifr->ifr_name, "eth0"))
    {
    if(ioctl(s, SIOCGIFFLAGS, (char *)ifr) < 0)
    perror("ioctl-flag:");
    break;
    }
    }

    if (ifr->ifr_flags & IFF_BROADCAST)
    {
    if(ioctl(s, SIOCGIFBRDADDR, (char *) ifr) < 0)
    perror("ioctl-addr:");
    memcpy((char *) &dst, (char *) &ifr->ifr_broadaddr,
    sizeof(ifr->ifr_broadaddr));
    } else
    printf("Not able to broadcast");

    printf("%s\n", inet_ntoa(dst.sin_addr));

    if(sendto(s, buf2, strlen(buf2), 0, (struct sockaddr *)&dst,
    sizeof(dst)) < 0)
    perror("sendto:");

    return 0;
    }

  5. Re: Getting Broadcast information.

    On Dec 1, 6:39 pm, Ryan Knopp
    wrote:
    > Thanks for the help, I didn't notice that.
    >
    > Now I've updated my code and did some pruning and I'm having a problem
    > with the sendto function. When I run this code I get this as an output.
    >
    > 192.168.0.255
    > sendto:: Invalid argument
    >
    > dst seems to have the correct information and I can't seem to find or
    > understand why sendto has an Invalid argument.


    What UDP port did you intend to send the broadcast to?
    0, which is what dst.sin_port contains after the memcpy()
    from ifr->ifr_broadaddr, is not a valid destination port.

    OttoM.
    __
    ottomeister (more often at gmail than mail.com)

    Disclaimer: These are my opinions. I do not speak for my employer.

+ Reply to Thread