Updated easysock library files

master
Aadhavan Srinivasan 2 years ago
parent 69da015727
commit 4d606df24d

@ -1,5 +1,6 @@
#include "easysock.h" #include "easysock.h"
int create_socket(int network, char transport) { int create_socket(int network, char transport) {
int domain; int domain;
int type; int type;
@ -25,7 +26,7 @@ int create_socket(int network, char transport) {
} }
void create_addr(int network, char* address, int port,struct sockaddr* dest) { int create_addr(int network, char* address, int port,struct sockaddr* dest) {
if (network == 4) { if (network == 4) {
struct sockaddr_in listen_address; struct sockaddr_in listen_address;
@ -33,7 +34,7 @@ void create_addr(int network, char* address, int port,struct sockaddr* dest) {
listen_address.sin_port = htons(port); listen_address.sin_port = htons(port);
inet_pton(AF_INET,address,&listen_address.sin_addr); inet_pton(AF_INET,address,&listen_address.sin_addr);
memcpy(dest,&listen_address,sizeof(listen_address)); memcpy(dest,&listen_address,sizeof(listen_address));
return; return 0;
} else if (network == 6) { } else if (network == 6) {
struct sockaddr_in6 listen_ipv6; struct sockaddr_in6 listen_ipv6;
@ -41,10 +42,10 @@ void create_addr(int network, char* address, int port,struct sockaddr* dest) {
listen_ipv6.sin6_port = htons(port); listen_ipv6.sin6_port = htons(port);
inet_pton(AF_INET6,address,&listen_ipv6.sin6_addr); inet_pton(AF_INET6,address,&listen_ipv6.sin6_addr);
memcpy(dest,&listen_ipv6,sizeof(listen_ipv6)); memcpy(dest,&listen_ipv6,sizeof(listen_ipv6));
return; return 0;
} else { } else {
exit(202); return -202;
} }
} }
@ -52,7 +53,7 @@ void create_addr(int network, char* address, int port,struct sockaddr* dest) {
int create_local (int network, char transport, char* address, int port,struct sockaddr* addr_struct) { int create_local (int network, char transport, char* address, int port,struct sockaddr* addr_struct) {
int socket = create_socket(network,transport); int socket = create_socket(network,transport);
if (socket < 0) { if (socket < 0) {
exit(errno); return (-1 * errno);
} }
create_addr(network,address,port,addr_struct); create_addr(network,address,port,addr_struct);
int addrlen; int addrlen;
@ -61,7 +62,7 @@ int create_local (int network, char transport, char* address, int port,struct so
} else if (network == 6) { } else if (network == 6) {
addrlen = sizeof(struct sockaddr_in6); addrlen = sizeof(struct sockaddr_in6);
} else { } else {
exit(207); return -207;
} }
/* The value of addrlen should be the size of the 'sockaddr'. /* The value of addrlen should be the size of the 'sockaddr'.
@ -70,7 +71,7 @@ int create_local (int network, char transport, char* address, int port,struct so
int i = bind (socket,addr_struct,(socklen_t)addrlen); int i = bind (socket,addr_struct,(socklen_t)addrlen);
if (i < 0) { if (i < 0) {
exit(errno); return (-1 * errno);
} }
return socket; return socket;
} }
@ -103,7 +104,7 @@ int create_remote (int network,char transport,char* address,int port,struct sock
int socket = create_socket(network,transport); int socket = create_socket(network,transport);
if (socket < 0) { if (socket < 0) {
exit(errno); return (-1 * errno);
} }
int addrlen; int addrlen;
@ -112,7 +113,7 @@ int create_remote (int network,char transport,char* address,int port,struct sock
} else if (network == 6) { } else if (network == 6) {
addrlen = sizeof(struct sockaddr_in6); addrlen = sizeof(struct sockaddr_in6);
} else { } else {
exit(207); return (-1 * errno);
} }
/* The value of addrlen should be the size of the 'sockaddr'. /* The value of addrlen should be the size of the 'sockaddr'.
@ -121,7 +122,7 @@ int create_remote (int network,char transport,char* address,int port,struct sock
int i = connect(socket,remote_addr_struct,(socklen_t)addrlen); int i = connect(socket,remote_addr_struct,(socklen_t)addrlen);
if (i < 0) { if (i < 0) {
exit(errno); return (-1 * errno);
} }
return socket; return socket;
} }
@ -144,7 +145,7 @@ int int_to_inet(int network) {
} else if (network == 6) { } else if (network == 6) {
return AF_INET6; return AF_INET6;
} else { } else {
exit(207); return -207;
} }
} }
@ -154,7 +155,7 @@ int inet_to_int(int af_type) {
} else if (af_type == AF_INET6) { } else if (af_type == AF_INET6) {
return 6; return 6;
} else { } else {
exit(207); return -207;
} }
} }
@ -164,6 +165,6 @@ int char_to_socktype(char transport) {
} else if (transport == 'U') { } else if (transport == 'U') {
return SOCK_DGRAM; return SOCK_DGRAM;
} else { } else {
exit(250); return -250;
} }
} }

@ -28,10 +28,10 @@ int create_socket(int network, char transport);
port is self-explanatory; port is self-explanatory;
and dest is a pointer to the sockaddr struct that will be filled in. and dest is a pointer to the sockaddr struct that will be filled in.
The function exits with error code 202 if the network parameter contained neither '4' The function returns with -202 if the network parameter contained neither '4'
nor '6'. */ nor '6'. */
void create_addr(int network, char* address, int port,struct sockaddr* dest); int create_addr(int network, char* address, int port,struct sockaddr* dest);
@ -39,7 +39,7 @@ void create_addr(int network, char* address, int port,struct sockaddr* dest);
_binds_ the addresses. It is used for local sockets (server sockets). Parameters are _binds_ the addresses. It is used for local sockets (server sockets). Parameters are
same as above. same as above.
It prints the error returned by 'bind' if something went wrong, and exits with errno.*/ It prints the error returned by 'bind' if something went wrong, and returns ( -1 * errno ).*/
int create_local (int network, char transport, char* address, int port,struct sockaddr* addr_struct); int create_local (int network, char transport, char* address, int port,struct sockaddr* addr_struct);
@ -48,7 +48,7 @@ int create_local (int network, char transport, char* address, int port,struct so
requested address. It is used for remote sockets (client sockets). The paramters are same requested address. It is used for remote sockets (client sockets). The paramters are same
as above. This function needs an empty 'sockaddr *' structure passed to it, which it will fill. as above. This function needs an empty 'sockaddr *' structure passed to it, which it will fill.
It prints the error returned by 'connect' if something went wrong, and exits with errno.*/ If something goes wrong, this function returns with ( -1 * errno ). */
int create_remote (int network,char transport,char* address,int port,struct sockaddr* remote_addr_struct); int create_remote (int network,char transport,char* address,int port,struct sockaddr* remote_addr_struct);
@ -63,7 +63,7 @@ AF_INET6 respectively. */
int int_to_inet(int network); int int_to_inet(int network);
/* char_to_socktype - Takes a character that represents a transport-layer protocol /* 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 (currently only supports 'T' for TCP or 'U' for UDP - it returns -250 if
the given characters is neither of these) and return the appropriate SOCKTYPE value. */ the given characters is neither of these) and return the appropriate SOCKTYPE value. */
int char_to_socktype(char transport); int char_to_socktype(char transport);
@ -72,4 +72,5 @@ int char_to_socktype(char transport);
and returns the appropriate int value. */ and returns the appropriate int value. */
int inet_to_int(int af_type); int inet_to_int(int af_type);
#endif #endif

Loading…
Cancel
Save