LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-02-2006, 05:58 AM   #1
rino.caldelli
Member
 
Registered: Apr 2005
Location: perugia
Distribution: ubuntu
Posts: 181

Rep: Reputation: 31
ip server and client: grab interface address


Can you help me with this simple server? it simply returns the eth0 address.. unfortunately I can't compile it for my embedded router so I must run it from a client machine on the lan (namely 192.168.0.69)..

But this way it isn't able to grab the real ip but only the mask 255.255.255.255

below I post the code

ipserv.c

Code:
// IPSERV server - serves to clients the IP address of any given network interface
//                 to which the server is connected to.

// Compile with:
//   gcc -o ipserv ipserv.c

// Send the 'ipserv' binary to your Linux router and run it like so:
//   ./ipserv ppp0 &>/dev/null &

// NOTE:  'ppp0' is the Point-to-Point Protocol interface name that you want
// to grab the IP address from.  This also works with eth0, eth1, or any
// other network interface.  If an invalid network interface is passed, the
// server will return 255.255.255.255 to any client that connects.

//   This is simply an incredibly simple server that sits on port 31602
// and listens for connections.  When it receives a connection, it will
// query the interface ppp0 and send the IP address to the client and then
// close the connection and wait for more connections. It is safe to simply
// kill the process of this server.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>

unsigned int get_ip_address(char *iface) {
	struct ifreq	if_ipaddr;
    unsigned char 	*ipaddr;
	int 			skfd = -1;

    if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        perror("socket");
        return -1;
    }

    strncpy(if_ipaddr.ifr_name, iface, IFNAMSIZ);

	if (ioctl(skfd, SIOCGIFFLAGS, (caddr_t) &if_ipaddr) < 0) {
        perror("Error on device");
        return -1;
    }

    if (ioctl(skfd, SIOCGIFADDR, (caddr_t) &if_ipaddr) != -1)
        ioctl(skfd, SIOCDIFADDR, (caddr_t) &if_ipaddr);

	close(skfd);

    return *((unsigned int *)(if_ipaddr.ifr_addr.sa_data + 2));
};

int main(int argc, char **argv) {

	unsigned int		ipaddr;
    int					sock;
    int					newsd, clilen;
    struct sockaddr_in	my_addr, client_addr;

	if (argc <= 1) {
		fprintf(stderr, "Specify a network interface (such as eth0, eth1, ppp0).\n");
        exit(-1);
    }

    if (get_ip_address(argv[1]) == -1) {
		fprintf(stderr, "%s does not seem to be up.\n", argv[1]);
        exit(-1);
    }

    // Open socket for the 'server':
	if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
		perror("socket");
        exit(-1);
    }

    // Set the port:
    bzero( (char *)&my_addr, sizeof(my_addr) );
    my_addr.sin_family = AF_INET;
    my_addr.sin_port = htons(31602);
    my_addr.sin_addr.s_addr = htons(INADDR_ANY);

	if (bind(sock, (struct sockaddr *)&my_addr, sizeof(my_addr)) < 0) {
		perror("bind");
        exit(-1);
    };

    if (listen(sock, 5) < 0) {
        perror("listen");
        exit(-1);
    }

    while (1 == 1) {
    	// Accept a connection:
        if ((newsd = accept(sock, (struct sockaddr *)&client_addr, &clilen)) < 0) {
        	perror("accept");
            exit(-1);
        }

        // Grab the IP address of our interface passed on the commandline:
	    ipaddr = get_ip_address(argv[1]);

        // Send it across the link:
		write(newsd, &ipaddr, sizeof(int));

		// Close the link:
        close(newsd);
    };

	close(sock);

	return 0;
};
 
Old 03-02-2006, 06:00 AM   #2
rino.caldelli
Member
 
Registered: Apr 2005
Location: perugia
Distribution: ubuntu
Posts: 181

Original Poster
Rep: Reputation: 31
to test it use this client: you may need to edit the ip addresse and port!!!

Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/un.h>
#include <sys/utsname.h>
#include <netdb.h>
#include <signal.h>
#include <unistd.h>

// Calls up 192.168.0.69 on port 31602 and asks for his ppp0 IP address:
int get_ip_address() {
    struct sockaddr_in	sin;
    socklen_t			addrlen;
    int					sock, ipaddr;

	ipaddr = 0;

    if (!inet_aton("192.168.0.69", &sin.sin_addr)) {
        struct hostent *hp;
        if ( !(hp = gethostbyname("192.168.0.69")) ) {
            perror("gethostbyname");
            return -1;
        }
        memset(&sin, 0, sizeof(struct sockaddr_in));
        memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length);
        sin.sin_family = hp->h_addrtype;
    } else {
	    sin.sin_family = AF_INET;
    }

    sin.sin_port = htons(31602);
    addrlen = sizeof(sin);

    if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
      perror("socket");
      return -1;
    }

    if (connect(sock, (struct sockaddr *)(&sin), addrlen) < 0) {
        if ((errno == EINPROGRESS) || (errno == EINTR)) {
            perror("connect");
        } else {
            perror("connect");
            close(sock);
            return -1;
        }
	};

    // Read the IP address:
	read(sock, &ipaddr, sizeof(int));

    close(sock);

    // Return the IP address:
    return ipaddr;
}

int main(void) {
    int   	ipaddr;

    ipaddr = get_ip_address();

    // Okay!  We got an IP address.... just gotta send it back to whomever asked for it!
	char	*msg = (char *)malloc(64);
    printf("%d.%d.%d.%d\n", ((unsigned char *)&ipaddr)[0],
                            ((unsigned char *)&ipaddr)[1],
                            ((unsigned char *)&ipaddr)[2],
                            ((unsigned char *)&ipaddr)[3]);

    free(msg);

    return 0;
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get ip address associated with an interface ro0tless Linux - Networking 4 10-03-2011 12:18 PM
How to get IP address of client from server.c program nazsarwat Programming 1 04-22-2005 03:57 PM
how to get client ip address at udp server cranium2004 Programming 2 03-21-2005 11:35 PM
Network client not receiving address of DNS server arohl74 Linux - Networking 1 03-03-2005 06:58 AM
Getting IP address from an interface in C dravya Programming 2 05-25-2004 02:48 PM


All times are GMT -5. The time now is 03:16 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration