summaryrefslogtreecommitdiff
path: root/toxcore/TCP_client.c
diff options
context:
space:
mode:
authorDiadlo <polsha3@gmail.com>2017-01-07 11:28:53 +0300
committerDiadlo <polsha3@gmail.com>2017-02-26 23:16:16 +0300
commitb19a9e54643eceaa52e71c0f38fa2ecae5a6809e (patch)
tree4c6fff0a086988ab21636e88dbf65c2e40a76a67 /toxcore/TCP_client.c
parentb2d04eae9d9b76523b877a9e5efd5989f83b143d (diff)
Add part of platform-independent network API implementation
socket -> net_socket htons -> net_htons htonl -> net_htonl connect -> net_connect sendto -> net_sendto_ip4 getaddrinfo -> net_getipport sa_family_t -> Family
Diffstat (limited to 'toxcore/TCP_client.c')
-rw-r--r--toxcore/TCP_client.c30
1 files changed, 4 insertions, 26 deletions
diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c
index 0910534d..3e6de14a 100644
--- a/toxcore/TCP_client.c
+++ b/toxcore/TCP_client.c
@@ -42,30 +42,8 @@ static int connect_sock_to(Socket sock, IP_Port ip_port, TCP_Proxy_Info *proxy_i
42 ip_port = proxy_info->ip_port; 42 ip_port = proxy_info->ip_port;
43 } 43 }
44 44
45 struct sockaddr_storage addr = {0};
46
47 size_t addrsize;
48
49 if (ip_port.ip.family == AF_INET) {
50 struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr;
51
52 addrsize = sizeof(struct sockaddr_in);
53 addr4->sin_family = AF_INET;
54 fill_addr4(ip_port.ip.ip4, &addr4->sin_addr);
55 addr4->sin_port = ip_port.port;
56 } else if (ip_port.ip.family == AF_INET6) {
57 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr;
58
59 addrsize = sizeof(struct sockaddr_in6);
60 addr6->sin6_family = AF_INET6;
61 fill_addr6(ip_port.ip.ip6, &addr6->sin6_addr);
62 addr6->sin6_port = ip_port.port;
63 } else {
64 return 0;
65 }
66
67 /* nonblocking socket, connect will never return success */ 45 /* nonblocking socket, connect will never return success */
68 connect(sock, (struct sockaddr *)&addr, addrsize); 46 net_connect(sock, ip_port);
69 return 1; 47 return 1;
70} 48}
71 49
@@ -84,7 +62,7 @@ static int proxy_http_generate_connection_request(TCP_Client_Connection *TCP_con
84 return 0; 62 return 0;
85 } 63 }
86 64
87 const uint16_t port = ntohs(TCP_conn->ip_port.port); 65 const uint16_t port = net_ntohs(TCP_conn->ip_port.port);
88 const int written = snprintf((char *)TCP_conn->last_packet, MAX_PACKET_SIZE, "%s%s:%hu%s%s:%hu%s", one, ip, port, two, 66 const int written = snprintf((char *)TCP_conn->last_packet, MAX_PACKET_SIZE, "%s%s:%hu%s%s:%hu%s", one, ip, port, two,
89 ip, port, three); 67 ip, port, three);
90 68
@@ -389,7 +367,7 @@ static int write_packet_TCP_secure_connection(TCP_Client_Connection *con, const
389 367
390 VLA(uint8_t, packet, sizeof(uint16_t) + length + CRYPTO_MAC_SIZE); 368 VLA(uint8_t, packet, sizeof(uint16_t) + length + CRYPTO_MAC_SIZE);
391 369
392 uint16_t c_length = htons(length + CRYPTO_MAC_SIZE); 370 uint16_t c_length = net_htons(length + CRYPTO_MAC_SIZE);
393 memcpy(packet, &c_length, sizeof(uint16_t)); 371 memcpy(packet, &c_length, sizeof(uint16_t));
394 int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t)); 372 int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t));
395 373
@@ -653,7 +631,7 @@ TCP_Client_Connection *new_TCP_connection(IP_Port ip_port, const uint8_t *public
653 family = proxy_info->ip_port.ip.family; 631 family = proxy_info->ip_port.ip.family;
654 } 632 }
655 633
656 Socket sock = socket(family, SOCK_STREAM, IPPROTO_TCP); 634 Socket sock = net_socket(family, TOX_SOCK_STREAM, TOX_PROTO_TCP);
657 635
658 if (!sock_valid(sock)) { 636 if (!sock_valid(sock)) {
659 return NULL; 637 return NULL;