Can someone help me to get the IPs of google.com?
I can't find any good resource on this.
Most tutorials on C network programming create a client socket and a server socket on the same computer. (I don't know in which situation that would make sense)
And none of the codes on beej.us worked for me.
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
int main(void)
{
struct addrinfo* res = NULL;
getaddrinfo("google.com", "443", 0, &res);
struct addrinfo* i;
for(i=res; i!=0; res=res->ai_next)
{
printf("%s\n", res->ai_addr->sa_data);
}
}
Output:
�
�
�
���$u
���$u
���$u
���"u
���"u
���"u
��� u
��� u
��� u
���&u
���&u
���&u
Segmentation fault
First, you never modify i inside of the loop so you end up with an infinite loop. You need to change to loop statement to:
for(i=res; i!=NULL; i=i->ai_next)
Regarding the output, the ai_addr field is of type struct sockaddr *, which is a pointer to a generic socket address struct, and its sa_data field is just a binary blob of the data for that address, not a printable string.
To print these values correctly, you need to inet_ntop function. First, you need to inspect ai_addr->sa_family to see if it is an IPv4 address or an IPv6 address. Then you'll need to cast ai_addr to either a struct sockaddr_in * for IPv4 or struct sockaddr_in6 * for IPv6, then pass the sin_addr or sin6_addr field respectively to inet_ntop along with the address family to convert it to a string.
for(i=res; i!=NULL; i=i->ai_next)
{
char str[INET6_ADDRSTRLEN];
if (i->ai_addr->sa_family == AF_INET) {
struct sockaddr_in *p = (struct sockaddr_in *)i->ai_addr;
printf("%s\n", inet_ntop(AF_INET, &p->sin_addr, str, sizeof(str)));
} else if (i->ai_addr->sa_family == AF_INET6) {
struct sockaddr_in6 *p = (struct sockaddr_in6 *)i->ai_addr;
printf("%s\n", inet_ntop(AF_INET6, &p->sin6_addr, str, sizeof(str)));
}
}
Output:
172.217.10.238
172.217.10.238
172.217.10.238
2607:f8b0:4006:813::200e
2607:f8b0:4006:813::200e
2607:f8b0:4006:813::200e
In This statement i is never being modified in loop body, and because it is not initialized, it never even enters the loop:
for(i=res; i!=0; res=res->ai_next)
{
printf("%s\n", res->ai_addr->sa_data);
}
Additionally, you are missing a few other parts. The following steps are part of a full example linked below:
Create the following instances of struct addrinfo:
struct addrinfo *result = NULL;
struct addrinfo *ptr = NULL;
struct addrinfo hints;
Then initialize Winsock
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
Setup the hints address info structure
ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
Call getaddrinfo
dwRetval = getaddrinfo(argv[1], argv[2], &hints, &result);
if ( dwRetval != 0 ){//handle error}
Now, for example based on above, your loop would look like:
for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {//
Entering "www.google.com" 0 on the command line for the complete example (linked below) will look similar to this:
This full Windows example is here. Note: Bug in code, i.e. ptr->ai_cannonname can be null with "www.google.com" so change this line to test before calling:
if(ptr->ai_canonname) printf("\tCanonical name: %s\n", ptr->ai_canonname);
A full Linux example is here.
Related
I found this piece of C code:
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
int main(int argc, char *argv[])
{
struct addrinfo hints, *res, *p;
int status;
char ipstr[INET6_ADDRSTRLEN];
if (argc != 2) {
fprintf(stderr,"usage: showip hostname\n");
return 1;
}
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
hints.ai_socktype = SOCK_STREAM;
if ((status = getaddrinfo(argv[1], NULL, &hints, &res)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
return 2;
}
printf("IP addresses for %s:\n\n", argv[1]);
for(p = res;p != NULL; p = p->ai_next) {
void *addr;
char *ipver;
// get the pointer to the address itself,
// different fields in IPv4 and IPv6:
if (p->ai_family == AF_INET) { // IPv4
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
addr = &(ipv4->sin_addr);
ipver = "IPv4";
} else { // IPv6
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
addr = &(ipv6->sin6_addr);
ipver = "IPv6";
}
// convert the IP to a string and print it:
inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
printf(" %s: %s\n", ipver, ipstr);
}
freeaddrinfo(res); // free the linked list
return 0;
}
There are two lines that I don't know how to interpret:
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
Why does the author of this program use parentheses?
I tried following a wonderful guide about interpreting complex declarations but I still don't understand.
It's called casting. It converts from one type to another, but the theory behind it is a lot more complicated. Casting doesn't mean you have an object describing a Dog (for example a struct) and you can cast it to a Human. Obviously you can't.
Casting means having identical memory blocks that can map on another block. A simpler explanation is when you have a complex structure and you cast from a pointer to the structure or from the data structure to the pointer.
Also note, that a data structure doesn't necessarily mean a struct. It can also mean an array. And, an array is a concept so it can mean something like int some_data[20]; or int** some_data;.
Now, as for why the author uses the parenthesis, it's because this is the syntax. You can't use any character.
Along with further reading about casting, please look up static_cast, dynamic_cast and reinterpret_cast. These 3 functions (well, almost, but I'm trying to not complicate it even more) use native casting. static_cast is similar to (type_a*)type_b.
For some examples, look up casting from int to enum and vice-versa to get an idea.
If you want to keep it simple, don't check any C++ concepts about casting just yet until you get a hang of the concepts in C first. They are common, but C++ will fork a lot from C because of classes, polymorphism and inheritance in general.
I am reading Beej's guide to network programming and in chapter 5.1, in the showip.c program I see the following line of code:
memset(&hints, 0, sizeof hints);
After a discussion on the ##c channel on freenode I deducted that the reasoning of that memset call could be to set the value of hints.ai_flags to 0(note that the program works fine I remove that line and I explicitly initialize hints.ai_flags to 0). If this is true, why does he need to set the whole struct to 0?
This is the full source:
/*
** showip.c -- show IP addresses for a host given on the command line
*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
int main(int argc, char *argv[])
{
struct addrinfo hints, *res, *p;
int status;
char ipstr[INET6_ADDRSTRLEN];
if (argc != 2) {
fprintf(stderr,"usage: showip hostname\n");
return 1;
}
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
hints.ai_socktype = SOCK_STREAM;
if ((status = getaddrinfo(argv[1], NULL, &hints, &res)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
return 2;
}
printf("IP addresses for %s:\n\n", argv[1]);
for(p = res;p != NULL; p = p->ai_next) {
void *addr;
char *ipver;
// get the pointer to the address itself,
// different fields in IPv4 and IPv6:
if (p->ai_family == AF_INET) { // IPv4
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
addr = &(ipv4->sin_addr);
ipver = "IPv4";
} else { // IPv6
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
addr = &(ipv6->sin6_addr);
ipver = "IPv6";
}
// convert the IP to a string and print it:
inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
printf(" %s: %s\n", ipver, ipstr);
}
freeaddrinfo(res); // free the linked list
return 0;
}
It's required by getaddrinfo() function documentation (where you pass your hints variable as parameter). From man getaddrinfo:
All the other fields in the structure pointed to by hints must contain either 0 or a NULL pointer, as appropriate.
It's because you are only going to fill/use/initialize some of the fields of the struct, giving 0 to the other fields prevents reading an uninitialzied variable, and sometimes 0 is the default value for those variables.
note that the program works fine I remove that line and I explicitly initialize hints.ai_flags to 0
Not necessarily, if you are on linux, I suggest using valgrind to detect reads to uninitialized variables, since that causes undefined behavior, the behavior could be that nothing wierd happens.
/* program to print the IP address of the Host*/
------------------------------------------------
I am trying to print the Host IP address. when I execute the following program
I am getting loop back address that is 127.0.0.1. What should I change to get the
actual IP address.
# include <stdio.h>
# include <stdlib.h>
# include <arpa/inet.h>
# include <string.h>
# include <sys/types.h>
# include <sys/socket.h>
# include <netdb.h>
# include <netinet/in.h>
int main () {
void *addr;
char ipstr[INET6_ADDRSTRLEN];
int rv;
struct addrinfo hints, *res, *p;
memset ( &hints, 0, sizeof hints );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
//hints.ai_flags = AI_ADDRCONFIG;
hints.ai_flags = AI_PASSIVE;
if ((rv = getaddrinfo(NULL ,"3490" , &hints, &res)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
for(p = res;p != NULL; p = p->ai_next) {
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
addr = &(ipv4->sin_addr);
inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
printf(" %s\n", ipstr);
}
}
As long as your development machine is not connected to a network you'll get the loopback address (which is perfectly valid for local network, of course). As soon as you connect the machine to the network you can determine the "right" address.
ipconfig, btw, behaves the same.
If you call getaddrinfo with NULL for the node name (first parameter) it is the same as when you call it with "localhost". You'll get the loopback IP. However, if you use the "real" hostname, you'll get the "real" ip.
Hi I am trying to make a simple server that takes in an IP address from getaddrinfo() and binds to it. Using ifconfig, I've found that I have an ip address of wlan0 192.168.2.10 which I would like to bind to. Unfortunately the address I seem to be binding to is my lo device. For some reason when I initialize getaddrinfo("192.168.2.10","3490",&hings,&res); res gets returned to a NULL pointer. I will show off my code bellow.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <signal.h>
#define MAXDATASIZE 500;
int main(int argc, char *argv[]){
// dealing with client socket
struct sockaddr_storage their_addr;
socklen_t addr_size;
// server socket
struct addrinfo serverSide,*serverInfo,*sortIP;
int optValRet;
int listenSock, newSock;
// this is for reading in information
char buf[501];
char point[INET6_ADDRSTRLEN];
char compare[INET6_ADDRSTRLEN] = "192.168.2.10";
// this is for handeling child processes and signals
struct sigaction sa;
sa.sa_handler = NULL;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if(sigaction(SIGCHLD, &sa, NULL) == -1){
printf("We have a problem, sigaction is not working.\n");
perror("\n");
exit(1);
}
// this sets up addrinfo
memset(&serverSide, 0, sizeof serverSide);
serverSide.ai_family = AF_UNSPEC;
serverSide.ai_socktype = SOCK_STREAM;
serverSide.ai_flags = INADDR_ANY;
// set up the address
if(getaddrinfo("192.168.2.10","3490",&serverSide,&serverInfo)!=0){
printf("get addr not success\n");
perror("\n");
return 1;
}
printf("Got address lists\n");
for(sortIP = serverInfo; sortIP = sortIP->ai_next; sortIP != NULL){
if((listenSock = socket(sortIP->ai_family, sortIP->ai_socktype, sortIP->ai_protocol))==-1){
continue;
}
if(setsockopt(listenSock,SOL_SOCKET,SO_REUSEADDR,&optValRet,sizeof(int))==-1){
perror("\n");
exit(1);
}
if(bind(listenSock,sortIP->ai_addr,sortIP->ai_addrlen) == -1 ){
perror("\n");
close(listenSock);
continue;
}
break;
}
if(sortIP == NULL){printf("sort ip is null.");}
inet_ntop(sortIP->ai_family,sortIP->ai_addr,point,sizeof point);
printf("Tell the clients connect to ip address %s on port 3490\n",point);
listen(listenSock, 10);
addr_size = sizeof their_addr;
newSock = accept(listenSock,(struct sockaddr *)&their_addr,&addr_size);
recv(newSock, buf, 500, 0);
printf("%s\n",buf);
close(listenSock);
close(newSock);
freeaddrinfo(serverInfo);
return 0;
}
Now I have some other questions beside the fact that I'm returning null. Since the wifi router has assigned me the ip address 192.168.2.10 for my subnet, how do I find out what my ip address is if I'm outside the network and trying to contact my server? I'm assuming the inside network ip is different from the outside network ip ... am I wrong? Anyways those are my two questions.
Thanks for any help!
This is wrong and is your immediate problem:
for (sortIP = serverInfo; sortIP = sortIP->ai_next; sortIP != NULL)
You want something like:
for (sortIP = serverInfo; sortIP != NULL; sortIP = sortIP->ai_next)
but I would go with a while loop personally.
To your main question, you should just bind to INADDR_ANY. That avoids that whole mess. Also:
recv(newSock, buf, 500, 0);
printf("%s\n",buf);
The %s format specifier is only for C-style strings, it's not for arbitrary binary data. Also, you throw away the return value from recv. There is no other way to know how many bytes you received.
As for finding your dynamic IP address from outside your network, use any of the dozens of IP posting services that assign you a host name and map it to your dynamic IP address.
Am trying my hand at sockets programming and I need some help because I can't build any of the programs I write. Whenever I try to build any sockets application, the compiler reports the following error:
error: expected specifier-qualifier-list before '_uid32_t'
This variable is inside a struct, which in turn is located in the header file "socket.h", and looks like this:
struct ucred {
pid_t pid;
__uid32_t uid;
__gid32_t gid;
};
There aren't any other errors in the other files and this is the only error the compiler returns.
I don't know if this is of any consequence but the header file is the one that comes with cygwin because the guides and tutorials am using are on Unix sockets and am running Windows XP. Am also using Code::Blocks running a gcc compiler.
I really hope that it's possible to run a program using the Unix sockets API on Windows because I'd really hate to confine myself to winsock only. Also, most of the freely available and comprehensive tutorials on sockets and network programming use Unix sockets.
Here's the code, though I don't think it really matters because I get the exact same error no matter program as long as it's using socket.h.
#include <stdio.h>
#include <string.h>
#include <sys\types.h>
#include <sys\socket.h>
#include <netdb.h>
#include <arpa\inet.h>
int main(int argc, char *argv[])
{
struct addrinfo hints, *res, *p;
int status;
char ipstr[INET6_ADDRSTRLEN];
if (argc != 2)
{
fprintf(stderr, "Usage: showip hostname\n");
return 1;
}
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;//AF_INET or AF_INET6 to force version
hints.ai_socktype = SOCK_STREAM;
if ((status = getaddrinfo(argv[1], NULL, &hints, &res)) != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
return 2;
}
printf("IP addresses for %s:\n\n", argv[1]);
for (p = res; p != NULL; p = p ->ai_next)
{
void *addr;
char *ipver;
//get the pointer to the address itself
//different fields in IPv4 and IPv6:
if (p ->ai_family == AF_INET)
{
//IPv4
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p ->ai_addr;
addr = &(ipv4 ->sin_addr);
ipver = "IPv4";
}
else//IPv6
{
struct sockaddr_in *ipv6 = (struct sockaddr_in6 *)p ->ai_addr;
ipver = "IPv6";
}
//convert the IP to a string and print it;
inet_ntop(p ->ai_family, addr, ipstr, sizeof ipstr);
printf(" %s: %s\n", ipver, ipstr);
}
freeaddrinfo(res);//free linked list
return 0;
}
There's also other source code from other programs I could have added but I chose this one instead because I got it from the internet and not a textbook so anyone can look it up here.
Try including <unistd.h> and <sys/types.h> before your socket.h.