@ -1,3 +1,4 @@
# include <iostream>
# include <string>
# include <string>
# include <vector>
# include <vector>
# include <iomanip>
# include <iomanip>
@ -5,6 +6,15 @@
# include <cstdint>
# include <cstdint>
# include "includes/connect_code.hpp"
# include "includes/connect_code.hpp"
# include "includes/numeric_base.hpp"
# include "includes/numeric_base.hpp"
# include "includes/easysock.h"
# if defined(_WIN32)
# include <In6addr.h>
# include <Ws2tcpip.h>
# else
# include <sys/socket.h>
# include <arpa/inet.h>
# endif
namespace connect_code {
namespace connect_code {
@ -45,11 +55,65 @@ namespace connect_code {
return std : : to_string ( addr_val ) ;
return std : : to_string ( addr_val ) ;
}
}
/* Expand an IPv6 address (expand '::' into ':0000:', for example).
This is done by first converting the address into a binary representation ,
and then printing every character of the binary representation into a string . */
std : : string expand_ip6_addr ( std : : string addr ) {
char ip6_string [ 40 ] ; // 32 characters + 7 colons
struct in6_addr * ip6_s_ptr = ( struct in6_addr * ) malloc ( sizeof ( in6_addr ) ) ; // Struct pointer, to store the binary representation of the address
inet_pton ( AF_INET6 , addr . data ( ) , ip6_s_ptr ) ; // Convert the string representation into a binary form
/* This abomination, converts the binary representation into a string.
It uses sprintf to print every byte in the binary representation into a string .
The bytes are formatted as 2 - character hexadecimal values . */
sprintf ( ip6_string ,
" %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x " ,
ip6_s_ptr - > s6_addr [ 0 ] , ip6_s_ptr - > s6_addr [ 1 ] ,
ip6_s_ptr - > s6_addr [ 2 ] , ip6_s_ptr - > s6_addr [ 3 ] ,
ip6_s_ptr - > s6_addr [ 4 ] , ip6_s_ptr - > s6_addr [ 5 ] ,
ip6_s_ptr - > s6_addr [ 6 ] , ip6_s_ptr - > s6_addr [ 7 ] ,
ip6_s_ptr - > s6_addr [ 8 ] , ip6_s_ptr - > s6_addr [ 9 ] ,
ip6_s_ptr - > s6_addr [ 10 ] , ip6_s_ptr - > s6_addr [ 11 ] ,
ip6_s_ptr - > s6_addr [ 12 ] , ip6_s_ptr - > s6_addr [ 13 ] ,
ip6_s_ptr - > s6_addr [ 14 ] , ip6_s_ptr - > s6_addr [ 15 ] ) ;
return std : : string ( ip6_string ) ;
}
std : : string encode ( std : : string address , std : : string port ) {
std : : string encode ( std : : string address , std : : string port ) {
std : : string addr_coded ;
/* Convert the address to decimal, and convert that to hex */
/* Convert the address to decimal, and convert that to hex */
std : : string addr_coded = dotted_dec_to_dec ( address ) ;
if ( check_ip_ver ( address . data ( ) ) = = 4 ) {
addr_coded = base_convert ( addr_coded , 10 , 32 ) ;
/* I don't really have a reason to use my own function (dotted_dec_to_dec()
and dec_to_dotted_dec ( ) ) , to convert the IP address from text to binary .
The inet_pton ( ) and inet_ntop ( ) functions can do this just fine , and also
take care of edge cases . Maybe someday , I might change this code . I could probably
repurpose the functions for something else , though . */
/* First, convert the address into a 32-bit integer (the integer is stored as a string).
Then , convert the address into base - 32. */
addr_coded = dotted_dec_to_dec ( address ) ;
addr_coded = base_convert ( addr_coded , 10 , 32 ) ;
}
if ( check_ip_ver ( address . data ( ) ) = = 6 ) {
/* First, expand the address into the full 39-character format (32 hex values + 7 colons).
Then , tokenize the string , using colons as the delimiters .
Finally , take each token in the string , and convert it from base - 16 to base - 32 , appending a ' - ' as a delimiter . */
std : : string addr_expanded = expand_ip6_addr ( address ) ;
std : : string addr_coded = " " ;
std : : vector < std : : string > addr_tokenized = tokenize_str ( addr_expanded , " : " ) ;
for ( int i = 0 ; i < addr_tokenized . size ( ) ; i + + ) {
addr_coded + = base_convert ( addr_tokenized [ i ] , 16 , 32 ) ;
addr_coded + = " - " ;
}
/* TODO - Check if the IP address is actually converted properly, and test if the server socket is created correctly.
Also do the same for client side , and check client - server connection . */
std : : cout < < addr_coded < < std : : endl ;
abort ( ) ;
}
/* Convert the port to hex */
/* Convert the port to hex */
std : : string port_coded = base_convert ( port , 10 , 32 ) ;
std : : string port_coded = base_convert ( port , 10 , 32 ) ;