Updated easysock files

master
Aadhavan Srinivasan 2 years ago
parent 7d3ef3a6c9
commit b85d83d0ea

@ -77,12 +77,35 @@ 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_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;
if (network == 4) {
addrlen = sizeof(struct sockaddr_in);
@ -102,3 +125,45 @@ int create_remote (int network,char transport,char* address,int port,struct sock
}
return socket;
}
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;
}
}
int int_to_inet(int network) {
if (network == 4) {
return AF_INET;
} else if (network == 6) {
return AF_INET6;
} else {
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);
}
}

@ -2,7 +2,9 @@
#define EASYSOCK_H_
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
@ -50,5 +52,24 @@ It prints the error returned by 'connect' if something went wrong, and exits wit
int create_remote (int network,char transport,char* address,int port,struct sockaddr* remote_addr_struct);
/* check_ip_ver - This function checks if the given string is an IPv4 address (returns 4),
IPv6 address (returns 6) or neither (returns -1). */
int check_ip_ver(char* address);
/* int_to_inet - Takes an int value (4 for IPv4, 6 for IPv6) and returns AF_INET or
AF_INET6 respectively. */
int int_to_inet(int network);
/* char_to_socktype - Takes a character that represents a transport-layer protocol
(currently only supports 'T' for TCP or 'U' for UDP - exits with error code 250 if
the given characters is neither of these) and return the appropriate SOCKTYPE value. */
int char_to_socktype(char transport);
/* inet_to_int - Takes an int value that corresponds to AF_INET or AF_INET6,
and returns the appropriate int value. */
int inet_to_int(int af_type);
#endif

Loading…
Cancel
Save