phcaze
phcaze

Reputation: 1777

UDP Socket broadcast and ifaddrstruct

I have a problem with the client side of an application I have to develop.

The first phase I have to implement is a "scan" of the network, searching for other users. I decided to use UDP sockets, and they work properly. If I use the broadcast ip address (getnamebyhost("192.168.1.255")) it works fine. The problem is that the application have to work on different networks, and I don't know the ip address (192.168 or 10.0 or else). I have to learn the address, so I found on the net to use getifaddr(). Here's the code:

#include <stdio.h>      
#include <sys/types.h>
#include <ifaddrs.h>
#include <netinet/in.h> 
#include <string.h> 
#include <arpa/inet.h>
#include <string.h>


int FindMyIp();
int IsConnected();

char *myIpAddr, *myMask, *myBroad;

int main (int argc, const char * argv[]) {

    int checkConnection=0;
    checkConnection = IsConnected();
    while (checkConnection == 0) {
        printf("Checking again..\n");
        checkConnection = IsConnected();
        sleep(2);
    }
    checkConnection=0;
    printf("\nConnected to a new network..\n");
    while (checkConnection == 0) {
        checkConnection = FindMyIp();
        if (checkConnection==0) { sleep(2); }
    }
    printf("My IP is %s\n", myIpAddr);
    printf("My Mask is %s\n", myMask);
    printf("The Broadcast IP is %s\n", myBroad);
    return 0;
}


int IsConnected()
{
    if (system("ping -c 1 www.google.com")){
        return 0;
    }
    else {  
        return 1;
    }
}

int FindMyIp()
{
    struct ifaddrs * ifAddrStruct=NULL;
    struct ifaddrs * ifa=NULL;
    void * tmpAddrPtr=NULL;
    printf("Acquiring interface information.. ");
    getifaddrs(&ifAddrStruct);
    printf("Checking for wanted interface..\n");
    int i=0, connected=0;
    ifa = ifAddrStruct;
    while (i!=1) {
        if ((ifa ->ifa_addr->sa_family==AF_INET)&&(ifa->ifa_name[0]=='e')&&(ifa->ifa_name[1]=='n')&&(ifa->ifa_name[2]=='1')) {
            i=1;
            connected=1;
        }
        else {
            if (ifa->ifa_next == NULL) {
                printf("error");
                return connected;
            }
            ifa = ifa->ifa_next;
        }
    }
    char addressBuffer1[INET_ADDRSTRLEN];
    char addressBuffer2[INET_ADDRSTRLEN];
    char addressBuffer3[INET_ADDRSTRLEN];
    tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
    inet_ntop(AF_INET, tmpAddrPtr, addressBuffer1, INET_ADDRSTRLEN);
    myIpAddr = addressBuffer1;
    tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_netmask)->sin_addr;
    inet_ntop(AF_INET, tmpAddrPtr, addressBuffer2, INET_ADDRSTRLEN);
    myMask = addressBuffer2;
    tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_broadaddr)->sin_addr;
    inet_ntop(AF_INET, tmpAddrPtr, addressBuffer3, INET_ADDRSTRLEN);
    myBroad = addressBuffer3;
    return connected;
}

So in myBroad I should have saved the broadcast ip address, and it's, I can print it with printf, but when I pass it to the socket by: struct hostent *hptr = gethostbyname(myBroad); it won't work! Where's the error?

Thanks

Upvotes: 0

Views: 370

Answers (2)

priteshbaviskar
priteshbaviskar

Reputation: 2327

char *myBroad is just an address holder which looses its value after the end of function. You actually need to create a string array and pass it to further functions of socket API. Hope this helps!

Upvotes: 0

nos
nos

Reputation: 229058

myIpAddr = addressBuffer1;

Here you're setting a global pointer to point to a local buffer inside the FindMyIP function. That array is gone when the function ends.

Just make all your global char *myIpAddr, *myMask, *myBroad; an array, and place the strings directly into those, instead of the local addressBuffer1/2/3

Upvotes: 1

Related Questions