I am trying to build a HTTP request using sockets in c.
So, in order to navigate the socket to the correct site ip.
I need to get the site ip.
I have managed to get the host ip but that not always work.
The following code gets the host ip:
host = gethostbyname(host_name);
if (host != NULL) {
memcpy(&inp, host->h_addr_list[0], host->h_length);
sprintf(ip, "%s", inet_ntoa(inp));
}
But that not always work, for example if I want to send the socket to stackoverflow.com and get his HTML content.I used this code and the output was: "198.252.206.16".
And if you enter that ip you can see that it is a wrong ip,so what can I do?
Please help.
P.S that this all my code:
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <errno.h>
#include <assert.h>
int main(int argc, char **argv) {
if (argc < 2) {
fprintf(stderr, "usage: %s domain_name\nE.g. %s www.yahoo.com/lalal.html\n", argv[0], argv[0]);
return(0);
}
struct protoent *pr;
struct in_addr inp;
int x = 1;
int ret;
char buf[4192];
char ip[16];
struct hostent *host;
int sock, bytes_recieved;
struct sockaddr_in server_addr;
char url[strlen(argv[1])];
strcpy(url,argv[1]);
char *index_page = strstr(argv[1], "/");
char *host_name = strtok(url,"/");
char message[4000];
sprintf(message,"GET %s HTTP/1.1\r\nHost: %s\r\n\r\n",index_page,host_name);
printf("%s",message);
host = gethostbyname(host_name);
if (host != NULL) {
memcpy(&inp, host->h_addr_list[0], host->h_length);
sprintf(ip, "%s", inet_ntoa(inp));
}
else {
printf("ERROR - Host ip was not found.\n\n");
exit(1);
}
printf("%s\n",ip);
pr = getprotobyname("tcp");
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Socket");
exit(1);
}
printf("%s\n",message);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(80);
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
bzero(&(server_addr.sin_zero),8);
if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
perror("Connect");
exit(1);
}
write(sock, message, strlen(message));
while ((ret = read(sock, buf, 4192)) != 0) {
buf[ret]='\0';
fwrite(buf, ret, sizeof(char), stdout);
x++;
}
if (close(sock) == -1)
printf("Close socket error\n");
return 0;
}
StackExchange hosts multiple sites on the same server. 198.252.206.16 is the IP address of that server, and that is the correct IP address you need to connect your socket to.
When requesting an HTTP resource from a site that resides on a shared server, you must provide an HTTP Host header to specify the site's hostname so the server knows which site you are trying to access.
For example, if you go to http://198.252.206.16, the request would look like this:
(connect to 198.252.206.16)
GET / HTTP 1.1
Host: 198.252.206.16
...
If you go to http://www.stackoverflow.com, the request looks like this:
(connect to 198.252.206.16)
GET / HTTP 1.1
Host: www.stackoverflow.com
...
If you go to http://www.stackexchange.com, the request looks like this:
(connect to 198.252.206.16)
GET / HTTP 1.1
Host: www.stackexchange.com
...
Notice that they all connect to the same IP address.
There is no site associated with the 198.252.206.16 host, which is why you get the error message.
The Host header is required for all HTTP 1.1 requests, and is optional for HTTP 1.0 requests (but an HTTP 1.0 request will fail in this situation if the Host header is missing). It was designed specifically to support multiple sites on a shared server.
Related
I am trying to establish a connection between a local client program and a server program running on an AWS EC2 instance. When I'm directly connecting through the public IP address of the EC2 instance, I face no problem and the connection is established as expected (I am printing out the IP address of the connected server to check whether the connection proceeds correctly or not)
However, when I add the EC2 instance to a Network Load Balancer and pass the NLB's IP while running the client program, then the connection that is being established is between the client program and the Load Balancer, and not the server program (as suggested by the IP address printed). I was expecting that the Load Balancer would forward the connection to the server and the connection would be established, but apparently, it's not the case
Why is it so? And how to solve this and get the desired result (i.e. the connection to be between the client and server program as it was happening without the load balancer)
EDIT: This does not seem to be a problem with load balancer configuration or permissions, as it works well when I run a simple Web Application on an Apache server on the EC2 instances (instead of the C server program). I guess the client and server programs are not establishing the connection in the desired manner. What needs to be done?
This is my client program
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <malloc.h>
#include <string.h>
#include <sys/socket.h>
#include <resolv.h>
#include <netdb.h>
#include <arpa/inet.h>
#include "openssl/ssl.h"
#include "openssl/err.h"
int OpenConnection(const char *hostname, int port) {
int sd;
struct hostent *host;
struct sockaddr_in addr;
if ( (host = gethostbyname(hostname)) == NULL )
{
printf("Could not find host\n");
perror(hostname);
abort();
}
else {
printf("Succesfully found host - %s\n", host->h_name);
}
sd = socket(PF_INET, SOCK_STREAM, 0);
bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = *(long*)(host->h_addr);
if ( connect(sd, (struct sockaddr*)&addr, sizeof(addr)) != 0 )
{
printf("Connection Failed\n");
close(sd);
perror(hostname);
abort();
}
else {
char* address = inet_ntoa(addr.sin_addr);
printf("Connection Succesful at socket %d\n", sd);
printf("%s\n", address);
printf("Port is: %d\n", ntohs(addr.sin_port));
}
return sd;
}
int main(int args, char* argv[]) {
char* hostname = argv[1];
char* port = argv[2];
int portnum = atoi(port);
int server;
server = OpenConnection(hostname, portnum);
close(server);
return 0;
}
And this is my server program
#include <errno.h>
#include <unistd.h>
#include <malloc.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <resolv.h>
#include "openssl/ssl.h"
#include "openssl/err.h"
#define FAIL -1
int OpenListener(int port) {
int sd;
struct sockaddr_in addr;
sd = socket(PF_INET, SOCK_STREAM, 0);
bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_ANY;
if (bind(sd, (struct sockaddr*)&addr, sizeof(addr)) != 0 ) {
perror("can't bind port");
printf("Cannot bind port\n");
abort();
}
else printf("Port bind Succesful\n");
if (listen(sd, 10) != 0 ) {
perror("Can't configure listening port");
abort();
}
else printf("Configured listening port\n");
return sd;
}
int isRoot() {
if (getuid() != 0) return 0;
else return 1;
}
int main(int args, char *argv[]) {
SSL_CTX *ctx;
int server;
char *portnum;
if(!isRoot()) {
printf("This program must be run as root/sudo user!!");
exit(0);
}
if (args != 2 ) {
printf("Usage: %s <portnum>\n", argv[0]);
exit(0);
}
portnum = argv[1];
int port = atoi(portnum);
server = OpenListener(port);
while(1) {
struct sockaddr_in addr;
socklen_t len = sizeof(addr);
int client = accept(server, (struct sockaddr*)&addr, &len);
printf("Connection: %s:%d\n",inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
}
close(server);
return 0;
}
It's hard to understand what's going on, I'll try to suggest that you need to use Application Load Balancer, because:
Network Load Balancer — this is the distribution of traffic based on network variables, such as IP address and destination ports. It is layer 4 (TCP) and below and is not designed to take into consideration anything at the application layer such as content type, cookie data, custom headers, user location, or the application behavior.
Why does this header file go into a freeze state or it does not output anything even if the server is accepting connection form other scripts/tools
tcpclient.h
#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#pragma once
int socket_desc;
struct sockaddr_in server_addr;
int init_client()
{
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (socket_desc < 0 )
{
printf("Failed to create socket\n");
return -1;
}
return 0;
}
int connection(char *host,int port )
{
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr.s_addr = inet_addr(host);
if (connect(socket_desc, (struct sockaddr*)&server_addr, sizeof(server_addr))<0)
{
printf("Failed to connect # %s:%d\n",host,port);
return -1;
}
printf("Connected to [%s:%d]\n",host,port);
return 0;
}
using a main.c like so
int main()
{
int soc = init_client();
int con = connection("192.168.0.12",6666);
return 0;
}
the only warning I get is that the variable soc is not used
Your program works correctly in my computer.... It's not clear what do you claim on.
Anyway, after tweaking it a bit, I have some points to comment on it. I changed the ip address to 127.0.0.1 and port to 80 and started an apache server locally to check that the server is, indeed ready to accept connections.
First I checked that the connection opened locally with a telnet(1) command (you will probably have to install it in your machine as it is not installed now by default)
$ telnet localhost 80
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
^]
telnet> close
Connection closed.
$ _
then I changed several things in your code (they should not affect the run, but they will make you trouble in the future if you don't take this path)
I first created a socket.h file to put prototypes for both functions, so you don't run in trouble if you later change the prototypes.
socket.h
#ifndef _SOCKET_H
#define _SOCKET_H
int init_client(void);
int connection(const char *host, int port);
#endif /* _SOCKET_H */
As you see, I changed the parameter `host` into a `const char *` type, so the compiler knows that the string is not changed inside the function and can optimize calls, based on that.
Second, I #included the file in both modules, so if you change the interface, both modules (provider and consumer) know about the interface change.
socket.c
#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
I used the last two includes to use strerror(errno), that gives you the reason for the connection error (mainly being a connection refused error)
#include "socket.h"
int socket_desc = -1;
struct sockaddr_in server_addr;
this is not good practice. A better solution would be to return the socket and pass it as parameter to the connection call below. Using global variables will run you into trouble later if you want to use these functions in different threads, as the socket_desc global variable is common to all the threads you will have.
int init_client(void)
{
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (socket_desc < 0 )
{
printf("Failed to create socket, %s\n",
strerror(errno));
return -1;
}
return 0;
}
int connection(const char *host, int port)
{
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr.s_addr = inet_addr(host);
printf("trying connect to %s:%d\n", host, port);
if (connect(socket_desc, (struct sockaddr*) &server_addr, sizeof(server_addr))<0)
{
printf("Failed to connect # %s:%d, %s\n",
host, port, strerror(errno));
return -1;
printing here the reason of the non connection is a good idea to know what is happening.
}
printf("Connected to [%s:%d]\n",host,port);
return 0;
}
A better solution would be the following:
socket.c
#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
#include "socket.h"
int init_client(void)
{
int socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (socket_desc < 0 )
{
printf("Failed to create socket: %s\n",
strerror(errno));
}
return socket_desc;
}
int connection(int sd, const char *host, int port)
{
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr.s_addr = inet_addr(host);
printf("trying connect to %s:%d\n", host, port);
if (connect(sd, (struct sockaddr*) &server_addr, sizeof(server_addr)) < 0) {
printf("Failed to connect # %s:%d, %s\n",
host, port, strerror(errno));
return -1;
}
printf("Connected to [%s:%d]\n", host, port);
return 0;
}
where socket.h becomes:
socket.h
#ifndef _SOCKET_H
#define _SOCKET_H
int init_client(void);
int connection(int socket_descriptor, const char *host, int port);
#endif /* _SOCKET_H */
this modifications, made that calling the program to localhost:80 resulted in:
$ a.out
trying connect to 127.0.0.1:80
Connected to [127.0.0.1:80]
$ _
while using 79 as the port resulted in:
$ a.out
trying connect to 127.0.0.1:79
Failed to connect # 127.0.0.1:79, Connection refused
Failed connection(), Connection refused
$ _
Spending way too much time trying to figure out why inet_ntop is always returning the same IP address of 2.0.19.86 inside of my barebones C UDP socket program.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#define SERVERPORT "4950" // the port users will be connecting to
int main(int argc, char *argv[])
{
int sock;
struct addrinfo addr_type, *server_info, *p;
int err;
int numbytes;
if (argc != 3) {
fprintf(stderr,"usage: talker hostname message\n");
exit(1);
}
//Specify type of response we want to git
memset(&addr_type, 0, sizeof addr_type);
addr_type.ai_family = AF_INET; // set to AF_INET to use IPv4
addr_type.ai_socktype = SOCK_DGRAM;
//Get the address info (like IP address) and store in server_info struct
if ((err = getaddrinfo(argv[1], SERVERPORT, &addr_type, &server_info)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(err));
return 1;
}
// There might be multiple IP addresses...loop through and use the first one that works
for(p = server_info; p != NULL; p = p->ai_next) {
if ((sock = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) {
perror("Error when creating socket");
continue;
}
break;
}
if (p == NULL) {
fprintf(stderr, "Client failed to create socket\n");
return 2;
}
char s[INET_ADDRSTRLEN];
inet_ntop(AF_INET,(struct sockaddr_in *)p->ai_addr,s, sizeof s);
printf("sending to %s....\n",s);
if ((numbytes = sendto(sock, argv[2], strlen(argv[2]), 0,
p->ai_addr, p->ai_addrlen)) == -1) {
perror("Error sending message");
exit(1);
}
printf("client sent %d bytes to %s\n", numbytes, argv[1]);
freeaddrinfo(server_info);
close(sock);
return 0;
}
The lines I am particularly stuck on is:
char s[INET_ADDRSTRLEN];
inet_ntop(AF_INET,(struct sockaddr_in *)p->ai_addr,s, sizeof s);
printf("sending to %s....\n",s);
For example I run the program with ./client www.google.com hello and get the following:
sending to 2.0.19.86....
client sent 5 bytes to www.google.com
I run the program again with ./client localhost hello and inet_ntop still returns the same IP.
sending to 2.0.19.86....
client sent 5 bytes to localhost
No errors are being thrown when I am creating the socket, and the message sends successfully when I send it to the receiving program over localhost, why is inet_ntop still outputting this weird address?
In your call to inet_ntop:
inet_ntop(AF_INET,(struct sockaddr_in *)p->ai_addr,s, sizeof s);
You're not passing in the correct structure. When AF_INET is passed as the first argument, the second argument should have type struct in_addr *, not struct sockaddr_in *.
You need to call out the sin_addr member which is of this type.
inet_ntop(AF_INET, &((struct sockaddr_in *)p->ai_addr)->sin_addr, s, sizeof s);
I am having a serious problem transferring my HTTP connection socket program over to HTTPS connection socket code, how do I make only an HTTPS connection in pure C?
I am working on a package manager and am rewriting the connection.c file, the only thing this file contains is the code used to make the initial connection to the server containg packages, it does nothing else. I had this working 100% with an HTTP connection, however I need to move to an HTTPS connection and need to use LibreSSL; at the moment I am trying to use OpenSSL as I can't find anything on LibreSSL. The HTTP code I had is as follows:
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include "repos.h"
#include "resolv.h"
short connection()
{
short socket_desc;
socket_desc = socket(AF_INET, SOCK_STREAM, 0); /* create socket with IPv4 and TCP protocol */
char host[17];
if (socket_desc == -1)
printf("could not create socket\n");
struct sockaddr_in *serv_addr = calloc(1, sizeof(struct sockaddr_in));
serv_addr->sin_family = AF_INET;
serv_addr->sin_port = htons(80);
resolv(DEFAULT_HOST, host); /* set repository to use */
if (inet_pton(AF_INET, host, &serv_addr->sin_addr) <= 0) {
printf("error");
free(serv_addr);
return -1;
}
if (connect(socket_desc, (struct sockaddr *)serv_addr, sizeof(*serv_addr)) < 0) {
printf("connection failed\n");
return 1;
}
else {
printf("connection initialized\n");
return 0;
}
/* close the connection */
free(serv_addr);
close(socket_desc);
return 0;
}
This works 100% and I want to just port this over to HTTPS. After looking at the horribly formatted OpenSSL client.c example (see here: https://wiki.openssl.org/index.php/SSL/TLS_Client) I got that code working on my system (had to make some changes to it), and then went off to port over my HTTP code to HTTPS. I worked on it for a bit and thought I got it working, I have been debugging it but can't figure out why it keeps failing. The code is as follows:
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <openssl/bio.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include "repos.h"
#include "resolv.h"
SSL *cSSL;
void initssl()
{
SSL_load_error_strings();
SSL_library_init();
OpenSSL_add_all_algorithms();
}
void destroyssl()
{
ERR_free_strings();
EVP_cleanup();
}
void shutdownssl()
{
SSL_shutdown(cSSL);
SSL_free(cSSL);
}
int main()
{
short socket_desc;
short socket_ssl;
char host[17];
socklen_t sock_size;
SSL_CTX *sslctx;
initssl();
socket_desc = socket(AF_INET, SOCK_STREAM, 0); /* create socket with IPv4 and TCP protocol */
if (socket_desc == -1)
printf("could not create socket\n");
struct sockaddr_in *serv_addr = calloc(1, sizeof(struct sockaddr_in));
serv_addr->sin_family = AF_INET;
serv_addr->sin_port = htons(443);
resolv(DEFAULT_HOST, host); /* resolve DEFAULT_HOST and store the ip in host */
if (inet_pton(AF_INET, host, &serv_addr->sin_addr) <= 0) {
printf("error");
free(serv_addr);
return -1;
}
bind(socket_desc, (struct sockaddr *)serv_addr, sizeof(struct sockaddr_in));
listen(socket_desc, 5);
sock_size = sizeof(struct sockaddr_in);
socket_ssl = accept(socket_desc, (struct sockaddr *)serv_addr, &sock_size); /* this is where hang occurs, however I am usnure why. I am reading docs and such and if I figure this out I will post the fix; however I would love some advice/help if anyone sees my error */
sslctx = SSL_CTX_new(SSLv23_server_method());
SSL_CTX_set_options(sslctx, SSL_OP_SINGLE_DH_USE);
short use_cert = SSL_CTX_use_certificate_file(sslctx, "/serverCertificate.pem" , SSL_FILETYPE_PEM);
short use_prv = SSL_CTX_use_PrivateKey_file(sslctx, "/serverCertificate.pem", SSL_FILETYPE_PEM);
cSSL = SSL_new(sslctx);
SSL_set_fd(cSSL, socket_ssl);
char ssl_err = SSL_accept(cSSL);
if(ssl_err <= 0) {
printf("connection failed\n");
shutdownssl();
}
else
printf("connected\n");
return 0;
}
Now I know it is missing some obvious things such as writing my own initssl (I am unsure why that isn't already in the lib, but I am starting to see why OpenBSD decided to fork). I left those out as I am more interested in this working with LibreSSL and don't believe you need them with LibreSSL. I tried using print statements to debug but they never get printed even when given at the top of main(). I am unsure why this isn't working and need some help getting this ported. The other files I wrote, repos.h and resolv.c can be seen below:
/* repos.h */
char DEFAULT_HOST[11] = "gitlab.com";
char DEFAULT_PAGE[24] = "Puffles_the_Dragon/core";
/* resolv.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h>
short resolv(char *host, char *ip)
{
struct hostent *hp = calloc(1, sizeof(struct hostent));
hp = gethostbyname(host);
if (hp == NULL) {
fprintf(stderr, "gethostbyname() failed\n");
exit(1);
}
else {
short i = 0;
while (hp->h_addr_list[i] != NULL) {
inet_ntoa(*(struct in_addr *)(hp->h_addr_list[i]));
i++;
}
strlcpy(ip, inet_ntoa(*(struct in_addr *)(hp->h_addr_list[0])), 16);
}
return 0;
}
I know some of these calls are outdated due to IPv6, but I am going to add for IPv6 after I get this all working and port from BSD libc to musl libc.
I expected the HTTPS code to run and connect to the server thus printing connected, but it just runs and doesn't fail or print anything.
I'm trying to access the website https://www.000webhost.com with C sockets:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char** argv) {
struct sockaddr_in servaddr;
struct hostent *hp;
int sock_id;
char message[1024*1024];
char request[] = "GET / HTTP/1.1\n" "From: ...\n";
//get a socket
if((sock_id = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
fprintf(stderr,"Couldn't get a socket.\n");
exit(EXIT_FAILURE);
}else {
fprintf(stderr,"Got a socket.\n");
}
memset(&servaddr,0,sizeof(servaddr));
//get address
if((hp = gethostbyname("000webhost.com")) == NULL) {
fprintf(stderr,"Couldn't get an address.\n");
exit(EXIT_FAILURE);
}else {
fprintf(stderr,"Got an address.\n");
}
memcpy((char *)&servaddr.sin_addr.s_addr, (char *)hp->h_addr, hp->h_length);
//port number and type
servaddr.sin_port = htons(80);
servaddr.sin_family = AF_INET;
//connect
if(connect(sock_id, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0) {
fprintf(stderr, "Couldn't connect.\n");
}else {
fprintf(stderr,"Got a connection.\n");
}
//request
write(sock_id,request,strlen(request));
//response
read(sock_id,message,1024*1024);
fprintf(stdout,"%s",message);
return 0;
}
If I change the request[] array from "GET / HTTP/1.1\n" "From: ...\n" to "GET / HTTP/1.1\n" "Host: https://www.000webhost.com" "From: ...\n" (therefore removing the direct-IP adress from the request), I still get the error Error 1003. Direct IP access not allowed. Is there some other part of the request that I need to modify? What else do I need to do?
Your request is malformed:
Each line needs to be terminated by \r\n, not just \n. (Some web servers will let you get away with \n, but I have no idea whether that'll work on 000webhost.)
Every header needs a \r\n after it. The modified code you mention in the last paragraph is missing a \r\n at the end of the Host header.
The Host header needs to just be a hostname (e.g, "Host: example.com"). Don't include http://.