diff options
Diffstat (limited to 'toxcore/TCP_server.c')
-rw-r--r-- | toxcore/TCP_server.c | 81 |
1 files changed, 41 insertions, 40 deletions
diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c index 250d6c44..e85a506d 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c | |||
@@ -812,6 +812,7 @@ static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, const uint | |||
812 | source.port = 0; // dummy initialise | 812 | source.port = 0; // dummy initialise |
813 | source.ip.family = TCP_ONION_FAMILY; | 813 | source.ip.family = TCP_ONION_FAMILY; |
814 | source.ip.ip6.uint32[0] = con_id; | 814 | source.ip.ip6.uint32[0] = con_id; |
815 | source.ip.ip6.uint32[1] = 0; | ||
815 | source.ip.ip6.uint64[1] = con->identifier; | 816 | source.ip.ip6.uint64[1] = con->identifier; |
816 | onion_send_1(TCP_server->onion, data + 1 + crypto_box_NONCEBYTES, length - (1 + crypto_box_NONCEBYTES), source, | 817 | onion_send_1(TCP_server->onion, data + 1 + crypto_box_NONCEBYTES, length - (1 + crypto_box_NONCEBYTES), source, |
817 | data + 1); | 818 | data + 1); |
@@ -862,36 +863,42 @@ static int confirm_TCP_connection(TCP_Server *TCP_server, TCP_Secure_Connection | |||
862 | { | 863 | { |
863 | int index = add_accepted(TCP_server, con); | 864 | int index = add_accepted(TCP_server, con); |
864 | 865 | ||
865 | if (index == -1) | 866 | if (index == -1) { |
867 | kill_TCP_connection(con); | ||
866 | return -1; | 868 | return -1; |
869 | } | ||
870 | |||
871 | memset(con, 0, sizeof(TCP_Secure_Connection)); | ||
867 | 872 | ||
868 | if (handle_TCP_packet(TCP_server, index, data, length) == -1) { | 873 | if (handle_TCP_packet(TCP_server, index, data, length) == -1) { |
869 | kill_accepted(TCP_server, index); | 874 | kill_accepted(TCP_server, index); |
875 | return -1; | ||
870 | } | 876 | } |
871 | 877 | ||
872 | return index; | 878 | return index; |
873 | } | 879 | } |
874 | 880 | ||
875 | /* return 1 on success | 881 | /* return index on success |
876 | * return 0 on failure | 882 | * return -1 on failure |
877 | */ | 883 | */ |
878 | static int accept_connection(TCP_Server *TCP_server, sock_t sock) | 884 | static int accept_connection(TCP_Server *TCP_server, sock_t sock) |
879 | { | 885 | { |
880 | if (!sock_valid(sock)) | 886 | if (!sock_valid(sock)) |
881 | return 0; | 887 | return -1; |
882 | 888 | ||
883 | if (!set_socket_nonblock(sock)) { | 889 | if (!set_socket_nonblock(sock)) { |
884 | kill_sock(sock); | 890 | kill_sock(sock); |
885 | return 0; | 891 | return -1; |
886 | } | 892 | } |
887 | 893 | ||
888 | if (!set_socket_nosigpipe(sock)) { | 894 | if (!set_socket_nosigpipe(sock)) { |
889 | kill_sock(sock); | 895 | kill_sock(sock); |
890 | return 0; | 896 | return -1; |
891 | } | 897 | } |
892 | 898 | ||
893 | TCP_Secure_Connection *conn = | 899 | uint16_t index = TCP_server->incomming_connection_queue_index % MAX_INCOMMING_CONNECTIONS; |
894 | &TCP_server->incomming_connection_queue[TCP_server->incomming_connection_queue_index % MAX_INCOMMING_CONNECTIONS]; | 900 | |
901 | TCP_Secure_Connection *conn = &TCP_server->incomming_connection_queue[index]; | ||
895 | 902 | ||
896 | if (conn->status != TCP_STATUS_NO_STATUS) | 903 | if (conn->status != TCP_STATUS_NO_STATUS) |
897 | kill_TCP_connection(conn); | 904 | kill_TCP_connection(conn); |
@@ -901,7 +908,7 @@ static int accept_connection(TCP_Server *TCP_server, sock_t sock) | |||
901 | conn->next_packet_length = 0; | 908 | conn->next_packet_length = 0; |
902 | 909 | ||
903 | ++TCP_server->incomming_connection_queue_index; | 910 | ++TCP_server->incomming_connection_queue_index; |
904 | return 1; | 911 | return index; |
905 | } | 912 | } |
906 | 913 | ||
907 | static sock_t new_listening_TCP_socket(int family, uint16_t port) | 914 | static sock_t new_listening_TCP_socket(int family, uint16_t port) |
@@ -912,11 +919,7 @@ static sock_t new_listening_TCP_socket(int family, uint16_t port) | |||
912 | return ~0; | 919 | return ~0; |
913 | } | 920 | } |
914 | 921 | ||
915 | #ifndef TCP_SERVER_USE_EPOLL | ||
916 | int ok = set_socket_nonblock(sock); | 922 | int ok = set_socket_nonblock(sock); |
917 | #else | ||
918 | int ok = 1; | ||
919 | #endif | ||
920 | 923 | ||
921 | if (ok && family == AF_INET6) { | 924 | if (ok && family == AF_INET6) { |
922 | ok = set_socket_dualstack(sock); | 925 | ok = set_socket_dualstack(sock); |
@@ -983,7 +986,7 @@ TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uin | |||
983 | 986 | ||
984 | if (sock_valid(sock)) { | 987 | if (sock_valid(sock)) { |
985 | #ifdef TCP_SERVER_USE_EPOLL | 988 | #ifdef TCP_SERVER_USE_EPOLL |
986 | ev.events = EPOLLIN; | 989 | ev.events = EPOLLIN | EPOLLET; |
987 | ev.data.u64 = sock | ((uint64_t)TCP_SOCKET_LISTENING << 32); | 990 | ev.data.u64 = sock | ((uint64_t)TCP_SOCKET_LISTENING << 32); |
988 | 991 | ||
989 | if (epoll_ctl(temp->efd, EPOLL_CTL_ADD, sock, &ev) == -1) { | 992 | if (epoll_ctl(temp->efd, EPOLL_CTL_ADD, sock, &ev) == -1) { |
@@ -1027,7 +1030,7 @@ static void do_TCP_accept_new(TCP_Server *TCP_server) | |||
1027 | 1030 | ||
1028 | do { | 1031 | do { |
1029 | sock = accept(TCP_server->socks_listening[i], (struct sockaddr *)&addr, &addrlen); | 1032 | sock = accept(TCP_server->socks_listening[i], (struct sockaddr *)&addr, &addrlen); |
1030 | } while (accept_connection(TCP_server, sock)); | 1033 | } while (accept_connection(TCP_server, sock) != -1); |
1031 | } | 1034 | } |
1032 | } | 1035 | } |
1033 | 1036 | ||
@@ -1075,15 +1078,7 @@ static int do_unconfirmed(TCP_Server *TCP_server, uint32_t i) | |||
1075 | kill_TCP_connection(conn); | 1078 | kill_TCP_connection(conn); |
1076 | return -1; | 1079 | return -1; |
1077 | } else { | 1080 | } else { |
1078 | int index_new; | 1081 | return confirm_TCP_connection(TCP_server, conn, packet, len); |
1079 | |||
1080 | if ((index_new = confirm_TCP_connection(TCP_server, conn, packet, len)) == -1) { | ||
1081 | kill_TCP_connection(conn); | ||
1082 | } else { | ||
1083 | memset(conn, 0, sizeof(TCP_Secure_Connection)); | ||
1084 | } | ||
1085 | |||
1086 | return index_new; | ||
1087 | } | 1082 | } |
1088 | } | 1083 | } |
1089 | 1084 | ||
@@ -1194,7 +1189,7 @@ static void do_TCP_epoll(TCP_Server *TCP_server) | |||
1194 | sock_t sock = events[n].data.u64 & 0xFFFFFFFF; | 1189 | sock_t sock = events[n].data.u64 & 0xFFFFFFFF; |
1195 | int status = (events[n].data.u64 >> 32) & 0xFFFF, index = (events[n].data.u64 >> 48); | 1190 | int status = (events[n].data.u64 >> 32) & 0xFFFF, index = (events[n].data.u64 >> 48); |
1196 | 1191 | ||
1197 | if ((events[n].events & EPOLLERR) || (events[n].events & EPOLLHUP)) { | 1192 | if ((events[n].events & EPOLLERR) || (events[n].events & EPOLLHUP) || (events[n].events & EPOLLRDHUP)) { |
1198 | switch (status) { | 1193 | switch (status) { |
1199 | case TCP_SOCKET_LISTENING: { | 1194 | case TCP_SOCKET_LISTENING: { |
1200 | //should never happen | 1195 | //should never happen |
@@ -1230,24 +1225,29 @@ static void do_TCP_epoll(TCP_Server *TCP_server) | |||
1230 | //socket is from socks_listening, accept connection | 1225 | //socket is from socks_listening, accept connection |
1231 | struct sockaddr_storage addr; | 1226 | struct sockaddr_storage addr; |
1232 | unsigned int addrlen = sizeof(addr); | 1227 | unsigned int addrlen = sizeof(addr); |
1233 | sock_t sock_new; | ||
1234 | 1228 | ||
1235 | sock_new = accept(sock, (struct sockaddr *)&addr, &addrlen); | 1229 | while (1) { |
1230 | sock_t sock_new = accept(sock, (struct sockaddr *)&addr, &addrlen); | ||
1236 | 1231 | ||
1237 | int index_new = TCP_server->incomming_connection_queue_index % MAX_INCOMMING_CONNECTIONS; | 1232 | if (!sock_valid(sock_new)) { |
1233 | break; | ||
1234 | } | ||
1238 | 1235 | ||
1239 | if (!accept_connection(TCP_server, sock_new)) { | 1236 | int index_new = accept_connection(TCP_server, sock_new); |
1240 | break; | ||
1241 | } | ||
1242 | 1237 | ||
1243 | struct epoll_event ev = { | 1238 | if (index_new == -1) { |
1244 | .events = EPOLLIN | EPOLLET, | 1239 | continue; |
1245 | .data.u64 = sock_new | ((uint64_t)TCP_SOCKET_INCOMING << 32) | ((uint64_t)index_new << 48) | 1240 | } |
1246 | }; | ||
1247 | 1241 | ||
1248 | if (epoll_ctl(TCP_server->efd, EPOLL_CTL_ADD, sock_new, &ev) == -1) { | 1242 | struct epoll_event ev = { |
1249 | kill_TCP_connection(&TCP_server->incomming_connection_queue[index_new]); | 1243 | .events = EPOLLIN | EPOLLET | EPOLLRDHUP, |
1250 | break; | 1244 | .data.u64 = sock_new | ((uint64_t)TCP_SOCKET_INCOMING << 32) | ((uint64_t)index_new << 48) |
1245 | }; | ||
1246 | |||
1247 | if (epoll_ctl(TCP_server->efd, EPOLL_CTL_ADD, sock_new, &ev) == -1) { | ||
1248 | kill_TCP_connection(&TCP_server->incomming_connection_queue[index_new]); | ||
1249 | continue; | ||
1250 | } | ||
1251 | } | 1251 | } |
1252 | 1252 | ||
1253 | break; | 1253 | break; |
@@ -1257,7 +1257,7 @@ static void do_TCP_epoll(TCP_Server *TCP_server) | |||
1257 | int index_new; | 1257 | int index_new; |
1258 | 1258 | ||
1259 | if ((index_new = do_incoming(TCP_server, index)) != -1) { | 1259 | if ((index_new = do_incoming(TCP_server, index)) != -1) { |
1260 | events[n].events = EPOLLIN | EPOLLET; | 1260 | events[n].events = EPOLLIN | EPOLLET | EPOLLRDHUP; |
1261 | events[n].data.u64 = sock | ((uint64_t)TCP_SOCKET_UNCONFIRMED << 32) | ((uint64_t)index_new << 48); | 1261 | events[n].data.u64 = sock | ((uint64_t)TCP_SOCKET_UNCONFIRMED << 32) | ((uint64_t)index_new << 48); |
1262 | 1262 | ||
1263 | if (epoll_ctl(TCP_server->efd, EPOLL_CTL_MOD, sock, &events[n]) == -1) { | 1263 | if (epoll_ctl(TCP_server->efd, EPOLL_CTL_MOD, sock, &events[n]) == -1) { |
@@ -1273,7 +1273,7 @@ static void do_TCP_epoll(TCP_Server *TCP_server) | |||
1273 | int index_new; | 1273 | int index_new; |
1274 | 1274 | ||
1275 | if ((index_new = do_unconfirmed(TCP_server, index)) != -1) { | 1275 | if ((index_new = do_unconfirmed(TCP_server, index)) != -1) { |
1276 | events[n].events = EPOLLIN | EPOLLET; | 1276 | events[n].events = EPOLLIN | EPOLLET | EPOLLRDHUP; |
1277 | events[n].data.u64 = sock | ((uint64_t)TCP_SOCKET_CONFIRMED << 32) | ((uint64_t)index_new << 48); | 1277 | events[n].data.u64 = sock | ((uint64_t)TCP_SOCKET_CONFIRMED << 32) | ((uint64_t)index_new << 48); |
1278 | 1278 | ||
1279 | if (epoll_ctl(TCP_server->efd, EPOLL_CTL_MOD, sock, &events[n]) == -1) { | 1279 | if (epoll_ctl(TCP_server->efd, EPOLL_CTL_MOD, sock, &events[n]) == -1) { |
@@ -1333,5 +1333,6 @@ void kill_TCP_server(TCP_Server *TCP_server) | |||
1333 | #endif | 1333 | #endif |
1334 | 1334 | ||
1335 | free(TCP_server->socks_listening); | 1335 | free(TCP_server->socks_listening); |
1336 | free(TCP_server->accepted_connection_array); | ||
1336 | free(TCP_server); | 1337 | free(TCP_server); |
1337 | } | 1338 | } |