summaryrefslogtreecommitdiff
path: root/toxcore/TCP_server.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_server.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_server.c')
-rw-r--r--toxcore/TCP_server.c37
1 files changed, 5 insertions, 32 deletions
diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c
index 10f64d38..d2b08206 100644
--- a/toxcore/TCP_server.c
+++ b/toxcore/TCP_server.c
@@ -69,39 +69,12 @@ size_t tcp_server_listen_count(const TCP_Server *tcp_server)
69 return tcp_server->num_listening_socks; 69 return tcp_server->num_listening_socks;
70} 70}
71 71
72/* this is needed to compile on Android below API 21 72/* This is needed to compile on Android below API 21
73*/ 73 */
74#ifndef EPOLLRDHUP 74#ifndef EPOLLRDHUP
75#define EPOLLRDHUP 0x2000 75#define EPOLLRDHUP 0x2000
76#endif 76#endif
77 77
78/* return 1 on success
79 * return 0 on failure
80 */
81static int bind_to_port(Socket sock, int family, uint16_t port)
82{
83 struct sockaddr_storage addr = {0};
84 size_t addrsize;
85
86 if (family == AF_INET) {
87 struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr;
88
89 addrsize = sizeof(struct sockaddr_in);
90 addr4->sin_family = AF_INET;
91 addr4->sin_port = htons(port);
92 } else if (family == AF_INET6) {
93 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr;
94
95 addrsize = sizeof(struct sockaddr_in6);
96 addr6->sin6_family = AF_INET6;
97 addr6->sin6_port = htons(port);
98 } else {
99 return 0;
100 }
101
102 return (bind(sock, (struct sockaddr *)&addr, addrsize) == 0);
103}
104
105/* Set the size of the connection list to numfriends. 78/* Set the size of the connection list to numfriends.
106 * 79 *
107 * return -1 if realloc fails. 80 * return -1 if realloc fails.
@@ -265,7 +238,7 @@ uint16_t read_TCP_length(Socket sock)
265 return 0; 238 return 0;
266 } 239 }
267 240
268 length = ntohs(length); 241 length = net_ntohs(length);
269 242
270 if (length > MAX_PACKET_SIZE) { 243 if (length > MAX_PACKET_SIZE) {
271 return ~0; 244 return ~0;
@@ -460,7 +433,7 @@ static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const
460 433
461 VLA(uint8_t, packet, sizeof(uint16_t) + length + CRYPTO_MAC_SIZE); 434 VLA(uint8_t, packet, sizeof(uint16_t) + length + CRYPTO_MAC_SIZE);
462 435
463 uint16_t c_length = htons(length + CRYPTO_MAC_SIZE); 436 uint16_t c_length = net_htons(length + CRYPTO_MAC_SIZE);
464 memcpy(packet, &c_length, sizeof(uint16_t)); 437 memcpy(packet, &c_length, sizeof(uint16_t));
465 int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t)); 438 int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t));
466 439
@@ -1005,7 +978,7 @@ static int accept_connection(TCP_Server *TCP_server, Socket sock)
1005 978
1006static Socket new_listening_TCP_socket(int family, uint16_t port) 979static Socket new_listening_TCP_socket(int family, uint16_t port)
1007{ 980{
1008 Socket sock = socket(family, SOCK_STREAM, IPPROTO_TCP); 981 Socket sock = net_socket(family, TOX_SOCK_STREAM, TOX_PROTO_TCP);
1009 982
1010 if (!sock_valid(sock)) { 983 if (!sock_valid(sock)) {
1011 return ~0; 984 return ~0;