ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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;
};
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.