Connecting a socket, using getaddrinfo, times out - c

I spent hours trying to find out my issue, just found the solution as I was writing my question (it always help when you need to formalize your issue and explain it). I post it, hopefully it helps someone.
Using getaddrinfo, if I try to connect a socket to my server, doing (what I thought was) exactly what is being explained on tons of website aswell as in the man page sample code of getaddrinfo, it FAILS with a "connection timed out" error message:
(Simplifying the code to be more concise)
void connect_UsingGetAddrInfo_Wrong (std::string host, unsigned short int port, int& socketfd)
{
//simplified loops & error handling for concision
int x;
int domain = AF_INET; // IP_v4
int socketType = SOCK_STREAM; // Sequenced, reliable, connection-based byte streams.
addrinfo hints, *addr;
//fine-tune hints according to which socket you want to open
hints.ai_family = domain;
hints.ai_socktype = socketType;
hints.ai_protocol = 0; // no enum : possible value can be read in /etc/protocols
hints.ai_flags = AI_CANONNAME | AI_ALL | AI_ADDRCONFIG;
x = getaddrinfo(hostname, NULL, &hints, &addr);
//shall rather loop on addr linked list, but this is not the topic here.
socketfd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
x = connect(socketfd, addr->ai_addr, addr->ai_addrlen);
}
However, I was able to connect a socket to the same server, using gethostbyname method.
void connect_UsingGetHostByName_Deprecated (std::string host, unsigned short int port, int& socketfd)
{
//simplified loops & error handling for concision
int x;
int domain = AF_INET; // IP_v4
int socketType = SOCK_STREAM; // Sequenced, reliable, connection-based byte streams.
struct hostent DNS, *r;
char buf[1024];
x = gethostbyname_r(hostname.c_str(), & DNS, buf, sizeof(buf), & r, & err));
socketfd = socket(domain, socketType, 0);
//server.
sockaddr_in server;
memset(&server, 0x00, sizeof(server));
server.sin_family=domain;
server.sin_port=htons(port);
memcpy(& server.sin_addr.s_addr, DNS.h_addr, (size_t) DNS.h_length);
x = connect(socketfd, (struct sockaddr *) & server, sizeof(server));
}
Running code shows that both version correctly retrieve the valid IP address of the server. Still the first one won't connect and will time out.
Why ?

The reason why it kept failing was : to retrieve the addrinfo, I had left the field 'service' equals to NULL. It will still return success and provide you an address (which you can map with getnameinfo to the right IP address). Still the address won't be usable to connect your socket !
I had found an hybrid version of both methods here :
https://codereview.stackexchange.com/questions/17863/socket-connect-realization-gethostbyname-or-getnameinfo
This one is functional, but i don't buy the casting
void connect_UsingGetAddrInfo_HYBRID (std::string host, unsigned short int port, int& socketfd)
{
//simplified loops & error handling for concision
int x;
int domain = AF_INET; // IP_v4
int socketType = SOCK_STREAM; // Sequenced, reliable, connection-based byte streams.
addrinfo hints, *addr;
//fine-tune hints according to which socket you want to open
hints.ai_family = domain;
hints.ai_socktype = socketType;
hints.ai_protocol = 0; // no enum : possible value can be read in /etc/protocols
hints.ai_flags = AI_CANONNAME | AI_ALL | AI_ADDRCONFIG;
x = getaddrinfo(host, NULL, &hints, &addr);
socketfd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
//here is the hybrid part
sockaddr_in servAddr;
memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family = addr->ai_family;
servAddr.sin_addr.s_addr = *((uint32_t*) & (((sockaddr_in*)addr->ai_addr)->sin_addr));
servAddr.sin_port = htons(port);
x=connect(socketfd, (struct sockaddr*) &servAddr, sizeof(servAddr));
}
In the end, it helped me find the rootcause :
void connect_UsingGetAddrInfo_FIXED (std::string host, unsigned short int port, int& socketfd)
{
//simplified loops & error handling for concision
int x;
int domain = AF_INET; // IP_v4
int socketType = SOCK_STREAM; // Sequenced, reliable, connection-based byte streams.
addrinfo hints, *addr;
//fine-tune hints according to which socket you want to open
hints.ai_family = domain;
hints.ai_socktype = socketType;
hints.ai_protocol = 0; // no enum : possible value can be read in /etc/protocols
hints.ai_flags = AI_CANONNAME | AI_ALL | AI_ADDRCONFIG;
//Precise here the port !
const char* service = std::to_string(port).c_str();
x = getaddrinfo(host, service, &hints, &addr);
socketfd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
x = connect(socketfd, addr->ai_addr, addr->ai_addrlen);
}
Hope this will help someone one day !

Related

C open second connection on socket

I need to program a distributed hash table with P2P. The peer which gets the request from the client needs to have an open connection to send the response back and also needs to open a connection to its predecessor to receive the information. I tried using accept a second time on the same socket but it didn't work.
I read a few solutions about forking but I'm not sure if it's suitable for my needs. (And I'm not really sure how to implement it correctly here)
My basic setup:
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if ((gai_status = getaddrinfo(NULL, port, &hints, &md_srvinfo)) != 0) {
fprintf(stderr, "error: %s\n", gai_strerror(gai_status));
exit(gai_status);
}
int md_listen_socket = socket(md_srvinfo->ai_family, md_srvinfo->ai_socktype, md_srvinfo->ai_protocol);
int yes;
setsockopt(md_listen_socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(1));
bind(md_listen_socket, md_srvinfo->ai_addr, md_srvinfo->ai_addrlen);
listen(md_listen_socket, 2);
while(1) {
// accept connection
struct sockaddr_storage client_address;
socklen_t addr_size = sizeof(client_address);
int md_active_socket;
md_active_socket = accept(md_listen_socket, (struct sockaddr *) &client_address, &addr_size);
.
.
.
.
recv(stuff from predecessor peer)
send(stuff to md_active_socket)
}
Edit:
struct addrinfo *pred_srvinfo;
getaddrinfo(NULL, pred_port, &hints, &pred_srvinfo);
int pred_listen_socket = socket(pred_srvinfo->ai_family, pred_srvinfo->ai_socktype, pred_srvinfo->ai_protocol);
struct sockaddr_storage pred_address;
socklen_t pred_addr_size = sizeof(pred_address);
int pred_active_socket = accept(pred_listen_socket, (struct sockaddr *) &pred_address, &pred_addr_size);
connect(pred_active_socket, pred_srvinfo->ai_addr, pred_srvinfo->ai_addrlen);
Can someone lead me to the right path?

gethostbyname() alternatives in c

I am currently doing a project on networking game where I need to design a game where maximum of 3 clients can connect to the server and the game is played between all the clients and server. I am using the "sockaddr_in" structure at both the server and client side.
In my game, anyone can become the server and the clients should give the correct IP address and port number to be able to connect to the server. When I hard code the values of the IP address of server and port number in "server_address.sin_addr.s_addr" and "server_address.sin_port" respectively the game works fine. But hard coded will not solve my problem of anyone being a server and asking the clients to enter the server's address and port number. So, I used "gethostbyname()" function call on the client's side. But it did not solve my problem. (may the reason is that behaviour of gethostbyname() when passed a numeric string is unspecified. (Source : link) .
Below is the code used by me at server side :
struct sockaddr_in serv_addr, client_addr;
/* open a socket */
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
err_ret = errno;
return err_ret;
}
/* set initial values */
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
serv_addr.sin_addr.s_addr = inet_addr(IP);
memset(&(serv_addr.sin_zero), 0, 8);
/* bind address with socket */
if(bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) == -1)
{
err_ret = errno;
return err_ret;
}
and at client side
struct sockaddr_in serv_addr;
struct hostent *to;
/* generate address */
if((to = gethostbyname(IP))==NULL)
{
err_ret = h_errno;
fprintf(stderr, "gethostbyname() error...\n");
return err_ret;
}
/* open a socket */
if((newfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
err_ret = errno;
fprintf(stderr, "socket() error...\n");
return err_ret;
}
/* set initial values */
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
memcpy(&serv_addr.sin_addr, to->h_addr_list[0], to->h_length);
memset(&(serv_addr.sin_zero), 0, 8);
Can anyone here tell an efficient way to carry out the above process?
Any help would be appreciated.
getaddrinfo has superseded gethostbyname. That should make it easier to create sockaddr_in structs from IP address strings.
Sample code to convert either a string in numeric form or as a hostname to a sockaddr_in.
struct addrinfo hints = {};
addrinfo* pResultList = NULL;
struct sockaddr_in addr = {};
char* hostname = "1.2.3.4";
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
// hints.ai_flags = AI_NUMERICHOST; // if you know hostname is a numeric stirng, you can skip the DNS lookup by setting this flag
result = getaddrinfo(hostname , NULL, &hints, &pResultList);
if (result)
memcpy(&addr, pResultList->ai_addr, sizeof(addr));
if (pResultList != NULL)
{
::freeaddrinfo(pResultList);
}

Getting an address IP and using a socket to connect to it

I'm writing an HTTP client using UNIX sockets (as part of a homework assignment). I currently have this working code to connect to a given IP Address:
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
char *server_address = "127.0.0.1";
struct sockaddr_in address;
if (sockfd < 0) {
printf("Unable to open socket\n");
exit(1);
}
// Try to connect to server_address on port PORT
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr(server_address);
address.sin_port = htons(PORT);
if (connect(sockfd, (struct sockaddr*) &address, sizeof(address)) < 0) {
printf("Unable to connect to host\n");
exit(1);
}
However, I now want to modify it so that server_address could also be something that is not an IP, such as "google.com". I've been trying to figure out how to do this using gethostbyname, but I am having trouble.
Will gethostbyname accept both an IP Address OR an address like "google.com" and have it work correctly? (or should I try and run a regex on the address first and do something else if it is an IP Address)?
I have tried the following code to try to get it working with something like "google.com", but I am getting a warning warning: assignment makes integer from pointer without a cast
struct hostent *host_entity = gethostbyname(server_address);
address.sin_addr.s_addr = host_entity->h_addr_list[0];
I know I am doing-it-wrong, but the gethostbyname documentation is atrocious.
What you want is maybe getaddrinfo(3):
#include <sys/socket.h>
#include <netdb.h>
static int
resolve(const char *host, const char *port)
{
struct addrinfo *aires;
struct addrinfo hints = {0};
int s = -1;
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = 0;
#if defined AI_ADDRCONFIG
hints.ai_flags |= AI_ADDRCONFIG;
#endif /* AI_ADDRCONFIG */
#if defined AI_V4MAPPED
hints.ai_flags |= AI_V4MAPPED;
#endif /* AI_V4MAPPED */
hints.ai_protocol = 0;
if (getaddrinfo(host, port, &hints, &aires) < 0) {
goto out;
}
/* now try them all */
for (const struct addrinfo *ai = aires;
ai != NULL &&
((s = socket(ai->ai_family, ai->ai_socktype, 0)) < 0 ||
connect(s, ai->ai_addr, ai->ai_addrlen) < 0);
close(s), s = -1, ai = ai->ai_next);
out:
freeaddrinfo(aires);
return s;
}
This version gets you a socket from a host/port pair. It also takes IP addresses for host and service strings for port. It will, however, connect to the host in question already.

UDP Socket in C - Setting it up wrong?

I'm working on a project that involves sending various requests to a server through UDP. However, I seem to be setting up the socket entirely wrong, as the server does not respond to any of my requests. We were provided with a server binary to test against, and the code below ellicits no response. Am I setting up the UDP socket correctly? If so, am I somehow using sendto wrong? I have confirmed that I am sending the correct number of bits.
The input for the program is: ./client [URL] [port] [username], and I always test with ./client localhost 8080 user. Here is the struct I am sending and the code.
struct request_login {
int req_type; /* = REQ_LOGIN */
char req_username[32];
} packed;
Code:
struct sockaddr_in sa;
int sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(sockfd == -1){
printf("Could not create socket.");
exit(EXIT_FAILURE);
}
// Prepare the socket address
memset(&sa, 0, sizeof sa);
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = inet_addr(argv[1]);
// Convert to network order
sa.sin_port = htonl(atoi(argv[2]));
// Assemble and send login request
struct request_login * reqlogin = (struct request_login *) malloc(sizeof(struct request_login));
reqlogin->req_type = REQ_LOGIN;
strcpy(reqlogin->req_username, argv[3]);
int res = sendto(sockfd, reqlogin, sizeof (struct request_login), 0, (struct sockaddr*)&sa, sizeof sa);
free(reqlogin)
Huh?
This:
sa.sin_addr.s_addr = inet_addr(argv[1]);
certainly won't do the right thing if, as you say, argv[1] is typically "localhost". You need to look up the host name, so that you get an IP address. You can only use inet_addr() if the input is a dotted IP address, not a host name.
Look at getaddrinfo().
After re-reading your code a couple of times, I think I know what one cause of the error may be:
sa.sin_port = htonl(atoi(argv[2]));
The port number is a short so you should use htons instead. It's very small and easy to miss.
try this instead:
struct addrinfo hint;
memset(&chk,0,sizeof(chk));
hint.ai_family = AF_INET;
hint.ai_socktype = SOCK_DGRAM;
hint.ai_protocol = IPPROTO_UDP;
struct addrinfo* servAddr = NULL;
int ret = getaddrinfo(argv[1],atoi(argv[2]),&hint,&servAddr);
if (-1 == ret)
{
perror("getaddrinfo failed");
exit(EXIT_FAILURE);
}
int sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(sockfd == -1){
printf("Could not create socket.");
exit(EXIT_FAILURE);
}
// Assemble and send login request
struct request_login reqlogin;
reqlogin.req_type = REQ_LOGIN;
strcpy(reqlogin.req_username, argv[3]);
int res = sendto(sockfd, &reqlogin, sizeof (struct request_login), 0, servAddr->ai_addr, servAddr->ai_addrlen);

C open socket on a specific IP

My website can be accessed using any of 2 different static IPs (IPv4).
Is it possible to open a TCP connection to another server, specifying which of the 2 IPs to use as a return address ?
x.x.x.x (my server) => z.z.z.z (destination server)
y.y.y.y (my server) => z.z.z.z (destination server)
Error-checking, etc. in this example has been omitted for simplicity sakes :
struct addrinfo hints, *result;
hints.ai_flags = 0;
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_addrlen = 0;
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
getaddrinfo(domain, "80", &hints, &result);
socket(result->ai_family, result->ai_socktype, result->ai_protocol);
Like #hochl says, you use the bind system call:
struct sockaddr_in sin = { 0 };
int sock;
/* Create a socket address, with a specific port and (local) ipnumber */
sin.sin_family = AF_INET;
sin.sin_port = htons(80);
inet_aton("1.2.3.4", &sin.sin_addr);
/* Create socket */
sock = socket(AF_INET, SOCK_STREAM, 0);
/* Bind socket to the local address */
bind(sock, (struct sockaddr *) &sin, sizeof(sin));
/* Now connect to remote server... */
/* connect(...) */
It should be noted that use of inet_aton is normally discouraged, in favor of inet_pton.
Not sure, but can't you bind your socket to one of your local addresses before you connect? In this case you may choose which of your IPs is used for the connection.

Resources