Added 'inet_to_int' function, and 'char_to_socktype' function; started working on domain name support for remote address

master
Aadhavan Srinivasan 2 years ago
parent 25bbcb620e
commit 66150609c3

@ -77,13 +77,42 @@ int create_local (int network, char transport, char* address, int port,struct so
int create_remote (int network,char transport,char* address,int port,struct sockaddr* remote_addr_struct) {
struct addrinfo hints; /* Used to tell getaddrinfo what kind of address we want */
struct addrinfo* results; /* Used by getaddrinfo to store the addresses */
if (check_ip_ver(address) < 0) { /* If the address is a domain name */
int err_code;
char* port_str = malloc(10 * sizeof(char));
sprintf(port_str,"%d",port); /* getaddrinfo expects a string for its port */
memset(&hints,'\0',sizeof(hints));
// hints.ai_family = int_to_inet(network); /* AF_INET if network is 4, AF_INET6 if it is 6 */
hints.ai_socktype = char_to_socktype(transport);
err_code = getaddrinfo(address,port_str,&hints,&results);
if (err_code != 0) {
exit(err_code);
}
remote_addr_struct = results->ai_addr;
network = inet_to_int(results->ai_family);
} else {
create_addr(network,address,port,remote_addr_struct);
}
int socket = create_socket(network,transport);
if (socket < 0) {
exit(errno);
}
create_addr(network,address,port,remote_addr_struct);
int addrlen;
unsigned char ip[100]= "";
inet_ntop(int_to_inet(network), &results->ai_addr->sa_data[2], ip, sizeof(ip));
printf ("IP address: %s\n\n", ip);
int addrlen;
if (network == 4) {
addrlen = sizeof(struct sockaddr_in);
} else if (network == 6) {
@ -105,14 +134,14 @@ int create_remote (int network,char transport,char* address,int port,struct sock
int check_ip_ver(char* address) {
char buffer[16]; /* 16 chars - 128 bits - is enough to hold an ipv6 address */
if (inet_pton(AF_INET,address,buffer) == 1) {
return 4;
} else if (inet_pton(AF_INET6,address,buffer) == 1) {
return 6;
} else {
return -1;
}
char buffer[16]; /* 16 chars - 128 bits - is enough to hold an ipv6 address */
if (inet_pton(AF_INET,address,buffer) == 1) {
return 4;
} else if (inet_pton(AF_INET6,address,buffer) == 1) {
return 6;
} else {
return -1;
}
}
int int_to_inet(int network) {
@ -124,3 +153,23 @@ int int_to_inet(int network) {
exit(207);
}
}
int inet_to_int(int af_type) {
if (af_type == AF_INET) {
return 4;
} else if (af_type == AF_INET6) {
return 6;
} else {
exit(207);
}
}
int char_to_socktype(char transport) {
if (transport == 'T') {
return SOCK_STREAM;
} else if (transport == 'U') {
return SOCK_DGRAM;
} else {
exit(250);
}
}

Loading…
Cancel
Save