From 2d640466f7307fb2f557f11cd5276a6bdf95a172 Mon Sep 17 00:00:00 2001 From: Rockingcool Date: Wed, 22 Feb 2023 08:15:11 -0600 Subject: [PATCH] Added separate file for helper functions. --- easysock.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 easysock.h diff --git a/easysock.h b/easysock.h new file mode 100644 index 0000000..98927f4 --- /dev/null +++ b/easysock.h @@ -0,0 +1,44 @@ +#include +#include +#include +#include + +/* This function takes: +a layer 3 - network layer - integer, which must be '4' for IPv4 +and 6 for IPv6; and +a layer 4 - transport layer - character, which must be 'T' for +TCP or 'U' for UDP. + +It returns the created socket, or -1 if the socket creation failed.*/ + +int create_socket(int network, char transport); + + +/* This function fills in the sockaddr struct 'dest' based on the given information. +'network' is an integer that contains '4' for IPv4 or '6' for IPv6; +'address' is the address that is filled into the struct; +port is self-explanatory; +and dest is a pointer to the sockaddr struct that will be filled in. + +The function exits with error code -2 if the network parameter contained neither '4' +nor '6'. */ + +void create_addr(int network, char* address, int port,struct sockaddr* dest); + + + +/* This function utilizes the above two functions; it creates the socket and +_binds_ the addresses. It is used for local sockets (server sockets). Parameters are +same as above. + +It prints the error returned by 'bind' if something went wrong, and exits with error code '-3'.*/ + +int create_local (int network, char transport, char* address, int port,struct sockaddr* addr_struct); + + +/* This function utilizes the same functions as 'create_local' but _connects_ to the +requested address. It is used for remoet sockets (client sockets). The paramters are same +as above. + +It prints the error returned by 'connect' if something went wrong, and exits with error code '-3'.*/ +int create_remote (int network,char transport,char* address,int port);