Added 'inet_to_int' function, and 'char_to_socktype' function; started working on domain name support for remote address
This commit is contained in:
		
							
								
								
									
										51
									
								
								easysock.c
									
									
									
									
									
								
							
							
						
						
									
										51
									
								
								easysock.c
									
									
									
									
									
								
							| @@ -77,12 +77,41 @@ 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); | ||||
| 	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); | ||||
| @@ -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); | ||||
| 	} | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user