Replaced all instances of struct sockaddr with struct sockaddr_storage, since it can fit v6 addresses as well. Cast values accordingly.

master
Aadhavan Srinivasan 7 months ago
parent 0a1934fdf9
commit 839efc3c44

@ -60,14 +60,14 @@ SOCKET create_socket(int network, char transport) {
} }
int create_addr(int network, const char* address, int port,struct sockaddr* dest) { int create_addr(int network, const char* address, int port,struct sockaddr_storage* dest) {
if (network == 4) { if (network == 4) {
struct sockaddr_in listen_address; struct sockaddr_in listen_address;
listen_address.sin_family = AF_INET; listen_address.sin_family = AF_INET;
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((struct sockaddr *)dest,&listen_address,sizeof(listen_address));
return 0; return 0;
} else if (network == 6) { } else if (network == 6) {
@ -75,7 +75,7 @@ int create_addr(int network, const char* address, int port,struct sockaddr* dest
listen_ipv6.sin6_family = AF_INET6; listen_ipv6.sin6_family = AF_INET6;
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((struct sockaddr_in6 *)dest,&listen_ipv6,sizeof(listen_ipv6));
return 0; return 0;
} else { } else {
@ -84,7 +84,7 @@ int create_addr(int network, const char* address, int port,struct sockaddr* dest
} }
SOCKET create_local (int network, char transport, const char* address, int port,struct sockaddr* addr_struct) { SOCKET create_local (int network, char transport, const char* address, int port,struct sockaddr_storage* addr_struct) {
int socket = create_socket(network,transport); int socket = create_socket(network,transport);
if (socket < 0) { if (socket < 0) {
return (-1 * errno); return (-1 * errno);
@ -103,14 +103,14 @@ SOCKET create_local (int network, char transport, const char* address, int port,
This should be set to the size of 'sockaddr_in' for IPv4, and 'sockaddr_in6' for IPv6. This should be set to the size of 'sockaddr_in' for IPv4, and 'sockaddr_in6' for IPv6.
See https://stackoverflow.com/questions/73707162/socket-bind-failed-with-invalid-argument-error-for-program-running-on-macos */ See https://stackoverflow.com/questions/73707162/socket-bind-failed-with-invalid-argument-error-for-program-running-on-macos */
int i = bind (socket,addr_struct,(socklen_t)addrlen); int i = bind (socket,(struct sockaddr *)addr_struct,(socklen_t)addrlen);
if (i < 0) { if (i < 0) {
return (-1 * errno); return (-1 * errno);
} }
return socket; return socket;
} }
SOCKET create_remote (int network,char transport, const char* address,int port,struct sockaddr* remote_addr_struct) { SOCKET create_remote (int network,char transport, const char* address,int port,struct sockaddr_storage* remote_addr_struct) {
struct addrinfo hints; /* Used to tell getaddrinfo what kind of address we want */ struct addrinfo hints; /* Used to tell getaddrinfo what kind of address we want */
struct addrinfo* results; /* Used by getaddrinfo to store the addresses */ struct addrinfo* results; /* Used by getaddrinfo to store the addresses */

@ -47,7 +47,7 @@ and dest is a pointer to the sockaddr struct that will be filled in.
The function returns with -202 if the network parameter contained neither '4' The function returns with -202 if the network parameter contained neither '4'
nor '6'. */ nor '6'. */
int create_addr(int network, const char* address, int port,struct sockaddr* dest); int create_addr(int network, const char* address, int port,struct sockaddr_storage* dest);
@ -57,7 +57,7 @@ same as above.
It prints the error returned by 'bind' if something went wrong, and returns ( -1 * errno ).*/ It prints the error returned by 'bind' if something went wrong, and returns ( -1 * errno ).*/
SOCKET create_local (int network, char transport, const char* address, int port,struct sockaddr* addr_struct); SOCKET create_local (int network, char transport, const char* address, int port,struct sockaddr_storage* addr_struct);
/* This function utilizes the same functions as 'create_local' but _connects_ to the /* This function utilizes the same functions as 'create_local' but _connects_ to the
@ -66,7 +66,7 @@ as above. This function needs an empty 'sockaddr *' structure passed to it, whic
If something goes wrong, this function returns with ( -1 * errno ). */ If something goes wrong, this function returns with ( -1 * errno ). */
SOCKET create_remote (int network,char transport, const char* address,int port,struct sockaddr* remote_addr_struct); SOCKET create_remote (int network,char transport, const char* address,int port,struct sockaddr_storage* remote_addr_struct);
/* check_ip_ver - This function checks if the given string is an IPv4 address (returns 4), /* check_ip_ver - This function checks if the given string is an IPv4 address (returns 4),
IPv6 address (returns 6) or neither (returns -1). */ IPv6 address (returns 6) or neither (returns -1). */

Loading…
Cancel
Save