| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297 |
- /**
- * @file udpgw.c
- * @author Ambroz Bizjak <ambrop7@gmail.com>
- *
- * @section LICENSE
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the author nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include <stdio.h>
- #include <string.h>
- #include <stdarg.h>
- #include <stdlib.h>
- #include <protocol/udpgw_proto.h>
- #include <misc/debug.h>
- #include <misc/version.h>
- #include <misc/loggers_string.h>
- #include <misc/loglevel.h>
- #include <misc/offset.h>
- #include <misc/byteorder.h>
- #include <misc/bsize.h>
- #include <misc/open_standard_streams.h>
- #include <misc/balloc.h>
- #include <misc/compare.h>
- #include <misc/print_macros.h>
- #include <structure/LinkedList1.h>
- #include <structure/BAVL.h>
- #include <base/BLog.h>
- #include <system/BReactor.h>
- #include <system/BNetwork.h>
- #include <system/BConnection.h>
- #include <system/BDatagram.h>
- #include <system/BSignal.h>
- #include <flow/PacketProtoDecoder.h>
- #include <flow/PacketPassFairQueue.h>
- #include <flow/PacketStreamSender.h>
- #include <flow/PacketProtoFlow.h>
- #include <flow/SinglePacketBuffer.h>
- #ifndef BADVPN_USE_WINAPI
- #include <base/BLog_syslog.h>
- #endif
- #include <udpgw/udpgw.h>
- #include <generated/blog_channel_udpgw.h>
- #define LOGGER_STDOUT 1
- #define LOGGER_SYSLOG 2
- struct client {
- BConnection con;
- BAddr addr;
- BTimer disconnect_timer;
- PacketProtoDecoder recv_decoder;
- PacketPassInterface recv_if;
- PacketPassFairQueue send_queue;
- PacketStreamSender send_sender;
- BAVL connections_tree;
- LinkedList1 connections_list;
- int num_connections;
- LinkedList1 closing_connections_list;
- LinkedList1Node clients_list_node;
- };
- struct connection {
- struct client *client;
- uint16_t conid;
- BAddr addr;
- const uint8_t *first_data;
- int first_data_len;
- btime_t last_use_time;
- int closing;
- BPending first_job;
- BufferWriter *send_if;
- PacketProtoFlow send_ppflow;
- PacketPassFairQueueFlow send_qflow;
- union {
- struct {
- BDatagram udp_dgram;
- int local_port_index;
- BufferWriter udp_send_writer;
- PacketBuffer udp_send_buffer;
- SinglePacketBuffer udp_recv_buffer;
- PacketPassInterface udp_recv_if;
- BAVLNode connections_tree_node;
- LinkedList1Node connections_list_node;
- };
- struct {
- LinkedList1Node closing_connections_list_node;
- };
- };
- };
- // command-line options
- struct {
- int help;
- int version;
- int logger;
- #ifndef BADVPN_USE_WINAPI
- char *logger_syslog_facility;
- char *logger_syslog_ident;
- #endif
- int loglevel;
- int loglevels[BLOG_NUM_CHANNELS];
- char *listen_addrs[MAX_LISTEN_ADDRS];
- int num_listen_addrs;
- int udp_mtu;
- int max_clients;
- int max_connections_for_client;
- int client_socket_sndbuf;
- int local_udp_num_ports;
- char *local_udp_addr;
- int unique_local_ports;
- } options;
- // MTUs
- int udpgw_mtu;
- int pp_mtu;
- // listen addresses
- BAddr listen_addrs[MAX_LISTEN_ADDRS];
- int num_listen_addrs;
- // local UDP port range, if options.local_udp_num_ports>=0
- BAddr local_udp_addr;
- // reactor
- BReactor ss;
- // listeners
- BListener listeners[MAX_LISTEN_ADDRS];
- int num_listeners;
- // clients
- LinkedList1 clients_list;
- int num_clients;
- static void print_help (const char *name);
- static void print_version (void);
- static int parse_arguments (int argc, char *argv[]);
- static int process_arguments (void);
- static void signal_handler (void *unused);
- static void listener_handler (BListener *listener);
- static void client_free (struct client *client);
- static void client_logfunc (struct client *client);
- static void client_log (struct client *client, int level, const char *fmt, ...);
- static void client_disconnect_timer_handler (struct client *client);
- static void client_connection_handler (struct client *client, int event);
- static void client_decoder_handler_error (struct client *client);
- static void client_recv_if_handler_send (struct client *client, uint8_t *data, int data_len);
- static uint8_t * build_port_usage_array_and_find_least_used_connection (BAddr remote_addr, struct connection **out_con);
- static void connection_init (struct client *client, uint16_t conid, BAddr addr, const uint8_t *data, int data_len);
- static void connection_free (struct connection *con);
- static void connection_logfunc (struct connection *con);
- static void connection_log (struct connection *con, int level, const char *fmt, ...);
- static void connection_free_udp (struct connection *con);
- static void connection_first_job_handler (struct connection *con);
- static int connection_send_to_client (struct connection *con, uint8_t flags, const uint8_t *data, int data_len);
- static int connection_send_to_udp (struct connection *con, const uint8_t *data, int data_len);
- static void connection_close (struct connection *con);
- static void connection_send_qflow_busy_handler (struct connection *con);
- static void connection_dgram_handler_event (struct connection *con, int event);
- static void connection_udp_recv_if_handler_send (struct connection *con, uint8_t *data, int data_len);
- static struct connection * find_connection (struct client *client, uint16_t conid);
- static int uint16_comparator (void *unused, uint16_t *v1, uint16_t *v2);
- int main (int argc, char **argv)
- {
- if (argc <= 0) {
- return 1;
- }
-
- // open standard streams
- open_standard_streams();
-
- // parse command-line arguments
- if (!parse_arguments(argc, argv)) {
- fprintf(stderr, "Failed to parse arguments\n");
- print_help(argv[0]);
- goto fail0;
- }
-
- // handle --help and --version
- if (options.help) {
- print_version();
- print_help(argv[0]);
- return 0;
- }
- if (options.version) {
- print_version();
- return 0;
- }
-
- // initialize logger
- switch (options.logger) {
- case LOGGER_STDOUT:
- BLog_InitStdout();
- break;
- #ifndef BADVPN_USE_WINAPI
- case LOGGER_SYSLOG:
- if (!BLog_InitSyslog(options.logger_syslog_ident, options.logger_syslog_facility)) {
- fprintf(stderr, "Failed to initialize syslog logger\n");
- goto fail0;
- }
- break;
- #endif
- default:
- ASSERT(0);
- }
-
- // configure logger channels
- for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
- if (options.loglevels[i] >= 0) {
- BLog_SetChannelLoglevel(i, options.loglevels[i]);
- }
- else if (options.loglevel >= 0) {
- BLog_SetChannelLoglevel(i, options.loglevel);
- }
- }
-
- BLog(BLOG_NOTICE, "initializing "GLOBAL_PRODUCT_NAME" "PROGRAM_NAME" "GLOBAL_VERSION);
-
- // initialize network
- if (!BNetwork_GlobalInit()) {
- BLog(BLOG_ERROR, "BNetwork_GlobalInit failed");
- goto fail1;
- }
-
- // process arguments
- if (!process_arguments()) {
- BLog(BLOG_ERROR, "Failed to process arguments");
- goto fail1;
- }
-
- // compute MTUs
- if ((udpgw_mtu = udpgw_compute_mtu(options.udp_mtu)) < 0 ||
- udpgw_mtu > PACKETPROTO_MAXPAYLOAD
- ) {
- BLog(BLOG_ERROR, "MTU is too big");
- goto fail1;
- }
- pp_mtu = udpgw_mtu + sizeof(struct packetproto_header);
-
- // init time
- BTime_Init();
-
- // init reactor
- if (!BReactor_Init(&ss)) {
- BLog(BLOG_ERROR, "BReactor_Init failed");
- goto fail1;
- }
-
- // setup signal handler
- if (!BSignal_Init(&ss, signal_handler, NULL)) {
- BLog(BLOG_ERROR, "BSignal_Init failed");
- goto fail2;
- }
-
- // initialize listeners
- num_listeners = 0;
- while (num_listeners < num_listen_addrs) {
- if (!BListener_Init(&listeners[num_listeners], listen_addrs[num_listeners], &ss, &listeners[num_listeners], (BListener_handler)listener_handler)) {
- BLog(BLOG_ERROR, "Listener_Init failed");
- goto fail3;
- }
- num_listeners++;
- }
-
- // init clients list
- LinkedList1_Init(&clients_list);
- num_clients = 0;
-
- // enter event loop
- BLog(BLOG_NOTICE, "entering event loop");
- BReactor_Exec(&ss);
-
- // free clients
- while (!LinkedList1_IsEmpty(&clients_list)) {
- struct client *client = UPPER_OBJECT(LinkedList1_GetFirst(&clients_list), struct client, clients_list_node);
- client_free(client);
- }
- fail3:
- // free listeners
- while (num_listeners > 0) {
- num_listeners--;
- BListener_Free(&listeners[num_listeners]);
- }
- // finish signal handling
- BSignal_Finish();
- fail2:
- // free reactor
- BReactor_Free(&ss);
- fail1:
- // free logger
- BLog(BLOG_NOTICE, "exiting");
- BLog_Free();
- fail0:
- // finish debug objects
- DebugObjectGlobal_Finish();
-
- return 1;
- }
- void print_help (const char *name)
- {
- printf(
- "Usage:\n"
- " %s\n"
- " [--help]\n"
- " [--version]\n"
- " [--logger <"LOGGERS_STRING">]\n"
- #ifndef BADVPN_USE_WINAPI
- " (logger=syslog?\n"
- " [--syslog-facility <string>]\n"
- " [--syslog-ident <string>]\n"
- " )\n"
- #endif
- " [--loglevel <0-5/none/error/warning/notice/info/debug>]\n"
- " [--channel-loglevel <channel-name> <0-5/none/error/warning/notice/info/debug>] ...\n"
- " [--listen-addr <addr>] ...\n"
- " [--udp-mtu <bytes>]\n"
- " [--max-clients <number>]\n"
- " [--max-connections-for-client <number>]\n"
- " [--client-socket-sndbuf <bytes / 0>]\n"
- " [--local-udp-addrs <addr> <num_ports>]\n"
- " [--unique-local-ports]\n"
- "Address format is a.b.c.d:port (IPv4) or [addr]:port (IPv6).\n",
- name
- );
- }
- void print_version (void)
- {
- printf(GLOBAL_PRODUCT_NAME" "PROGRAM_NAME" "GLOBAL_VERSION"\n"GLOBAL_COPYRIGHT_NOTICE"\n");
- }
- int parse_arguments (int argc, char *argv[])
- {
- if (argc <= 0) {
- return 0;
- }
-
- options.help = 0;
- options.version = 0;
- options.logger = LOGGER_STDOUT;
- #ifndef BADVPN_USE_WINAPI
- options.logger_syslog_facility = "daemon";
- options.logger_syslog_ident = argv[0];
- #endif
- options.loglevel = -1;
- for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
- options.loglevels[i] = -1;
- }
- options.num_listen_addrs = 0;
- options.udp_mtu = DEFAULT_UDP_MTU;
- options.max_clients = DEFAULT_MAX_CLIENTS;
- options.max_connections_for_client = DEFAULT_MAX_CONNECTIONS_FOR_CLIENT;
- options.client_socket_sndbuf = CLIENT_DEFAULT_SOCKET_SEND_BUFFER;
- options.local_udp_num_ports = -1;
- options.unique_local_ports = 0;
-
- int i;
- for (i = 1; i < argc; i++) {
- char *arg = argv[i];
- if (!strcmp(arg, "--help")) {
- options.help = 1;
- }
- else if (!strcmp(arg, "--version")) {
- options.version = 1;
- }
- else if (!strcmp(arg, "--logger")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- char *arg2 = argv[i + 1];
- if (!strcmp(arg2, "stdout")) {
- options.logger = LOGGER_STDOUT;
- }
- #ifndef BADVPN_USE_WINAPI
- else if (!strcmp(arg2, "syslog")) {
- options.logger = LOGGER_SYSLOG;
- }
- #endif
- else {
- fprintf(stderr, "%s: wrong argument\n", arg);
- return 0;
- }
- i++;
- }
- #ifndef BADVPN_USE_WINAPI
- else if (!strcmp(arg, "--syslog-facility")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- options.logger_syslog_facility = argv[i + 1];
- i++;
- }
- else if (!strcmp(arg, "--syslog-ident")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- options.logger_syslog_ident = argv[i + 1];
- i++;
- }
- #endif
- else if (!strcmp(arg, "--loglevel")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- if ((options.loglevel = parse_loglevel(argv[i + 1])) < 0) {
- fprintf(stderr, "%s: wrong argument\n", arg);
- return 0;
- }
- i++;
- }
- else if (!strcmp(arg, "--channel-loglevel")) {
- if (2 >= argc - i) {
- fprintf(stderr, "%s: requires two arguments\n", arg);
- return 0;
- }
- int channel = BLogGlobal_GetChannelByName(argv[i + 1]);
- if (channel < 0) {
- fprintf(stderr, "%s: wrong channel argument\n", arg);
- return 0;
- }
- int loglevel = parse_loglevel(argv[i + 2]);
- if (loglevel < 0) {
- fprintf(stderr, "%s: wrong loglevel argument\n", arg);
- return 0;
- }
- options.loglevels[channel] = loglevel;
- i += 2;
- }
- else if (!strcmp(arg, "--listen-addr")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- if (options.num_listen_addrs == MAX_LISTEN_ADDRS) {
- fprintf(stderr, "%s: too many\n", arg);
- return 0;
- }
- options.listen_addrs[options.num_listen_addrs] = argv[i + 1];
- options.num_listen_addrs++;
- i++;
- }
- else if (!strcmp(arg, "--udp-mtu")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- if ((options.udp_mtu = atoi(argv[i + 1])) < 0) {
- fprintf(stderr, "%s: wrong argument\n", arg);
- return 0;
- }
- i++;
- }
- else if (!strcmp(arg, "--max-clients")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- if ((options.max_clients = atoi(argv[i + 1])) <= 0) {
- fprintf(stderr, "%s: wrong argument\n", arg);
- return 0;
- }
- i++;
- }
- else if (!strcmp(arg, "--max-connections-for-client")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- if ((options.max_connections_for_client = atoi(argv[i + 1])) <= 0) {
- fprintf(stderr, "%s: wrong argument\n", arg);
- return 0;
- }
- i++;
- }
- else if (!strcmp(arg, "--client-socket-sndbuf")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- if ((options.client_socket_sndbuf = atoi(argv[i + 1])) < 0) {
- fprintf(stderr, "%s: wrong argument\n", arg);
- return 0;
- }
- i++;
- }
- else if (!strcmp(arg, "--local-udp-addrs")) {
- if (2 >= argc - i) {
- fprintf(stderr, "%s: requires two arguments\n", arg);
- return 0;
- }
- options.local_udp_addr = argv[i + 1];
- if ((options.local_udp_num_ports = atoi(argv[i + 2])) < 0) {
- fprintf(stderr, "%s: wrong argument\n", arg);
- return 0;
- }
- i += 2;
- }
- else if (!strcmp(arg, "--unique-local-ports")) {
- options.unique_local_ports = 1;
- }
- else {
- fprintf(stderr, "unknown option: %s\n", arg);
- return 0;
- }
- }
-
- if (options.help || options.version) {
- return 1;
- }
-
- return 1;
- }
- int process_arguments (void)
- {
- // resolve listen addresses
- num_listen_addrs = 0;
- while (num_listen_addrs < options.num_listen_addrs) {
- if (!BAddr_Parse(&listen_addrs[num_listen_addrs], options.listen_addrs[num_listen_addrs], NULL, 0)) {
- BLog(BLOG_ERROR, "listen addr: BAddr_Parse failed");
- return 0;
- }
- num_listen_addrs++;
- }
-
- // resolve local UDP address
- if (options.local_udp_num_ports >= 0) {
- if (!BAddr_Parse(&local_udp_addr, options.local_udp_addr, NULL, 0)) {
- BLog(BLOG_ERROR, "local udp addr: BAddr_Parse failed");
- return 0;
- }
-
- if (local_udp_addr.type != BADDR_TYPE_IPV4) {
- BLog(BLOG_ERROR, "local udp addr: must be an IPv4 address");
- return 0;
- }
- }
-
- return 1;
- }
- void signal_handler (void *unused)
- {
- BLog(BLOG_NOTICE, "termination requested");
-
- // exit event loop
- BReactor_Quit(&ss, 1);
- }
- void listener_handler (BListener *listener)
- {
- if (num_clients == options.max_clients) {
- BLog(BLOG_ERROR, "maximum number of clients reached");
- goto fail0;
- }
-
- // allocate structure
- struct client *client = (struct client *)malloc(sizeof(*client));
- if (!client) {
- BLog(BLOG_ERROR, "malloc failed");
- goto fail0;
- }
-
- // accept client
- if (!BConnection_Init(&client->con, BConnection_source_listener(listener, &client->addr), &ss, client, (BConnection_handler)client_connection_handler)) {
- BLog(BLOG_ERROR, "BConnection_Init failed");
- goto fail1;
- }
-
- // limit socket send buffer, else our scheduling is pointless
- if (options.client_socket_sndbuf > 0) {
- if (!BConnection_SetSendBuffer(&client->con, options.client_socket_sndbuf)) {
- BLog(BLOG_WARNING, "BConnection_SetSendBuffer failed");
- }
- }
-
- // init connection interfaces
- BConnection_SendAsync_Init(&client->con);
- BConnection_RecvAsync_Init(&client->con);
-
- // init disconnect timer
- BTimer_Init(&client->disconnect_timer, CLIENT_DISCONNECT_TIMEOUT, (BTimer_handler)client_disconnect_timer_handler, client);
- BReactor_SetTimer(&ss, &client->disconnect_timer);
-
- // init recv interface
- PacketPassInterface_Init(&client->recv_if, udpgw_mtu, (PacketPassInterface_handler_send)client_recv_if_handler_send, client, BReactor_PendingGroup(&ss));
-
- // init recv decoder
- if (!PacketProtoDecoder_Init(&client->recv_decoder, BConnection_RecvAsync_GetIf(&client->con), &client->recv_if, BReactor_PendingGroup(&ss), client,
- (PacketProtoDecoder_handler_error)client_decoder_handler_error
- )) {
- BLog(BLOG_ERROR, "PacketProtoDecoder_Init failed");
- goto fail2;
- }
-
- // init send sender
- PacketStreamSender_Init(&client->send_sender, BConnection_SendAsync_GetIf(&client->con), pp_mtu, BReactor_PendingGroup(&ss));
-
- // init send queue
- if (!PacketPassFairQueue_Init(&client->send_queue, PacketStreamSender_GetInput(&client->send_sender), BReactor_PendingGroup(&ss), 0, 1)) {
- BLog(BLOG_ERROR, "PacketPassFairQueue_Init failed");
- goto fail3;
- }
-
- // init connections tree
- BAVL_Init(&client->connections_tree, OFFSET_DIFF(struct connection, conid, connections_tree_node), (BAVL_comparator)uint16_comparator, NULL);
-
- // init connections list
- LinkedList1_Init(&client->connections_list);
-
- // set zero connections
- client->num_connections = 0;
-
- // init closing connections list
- LinkedList1_Init(&client->closing_connections_list);
-
- // insert to clients list
- LinkedList1_Append(&clients_list, &client->clients_list_node);
- num_clients++;
-
- client_log(client, BLOG_INFO, "connected");
-
- return;
-
- fail3:
- PacketStreamSender_Free(&client->send_sender);
- PacketProtoDecoder_Free(&client->recv_decoder);
- fail2:
- PacketPassInterface_Free(&client->recv_if);
- BReactor_RemoveTimer(&ss, &client->disconnect_timer);
- BConnection_RecvAsync_Free(&client->con);
- BConnection_SendAsync_Free(&client->con);
- BConnection_Free(&client->con);
- fail1:
- free(client);
- fail0:
- return;
- }
- void client_free (struct client *client)
- {
- // allow freeing send queue flows
- PacketPassFairQueue_PrepareFree(&client->send_queue);
-
- // free connections
- while (!LinkedList1_IsEmpty(&client->connections_list)) {
- struct connection *con = UPPER_OBJECT(LinkedList1_GetFirst(&client->connections_list), struct connection, connections_list_node);
- connection_free(con);
- }
-
- // free closing connections
- while (!LinkedList1_IsEmpty(&client->closing_connections_list)) {
- struct connection *con = UPPER_OBJECT(LinkedList1_GetFirst(&client->closing_connections_list), struct connection, closing_connections_list_node);
- connection_free(con);
- }
-
- // remove from clients list
- LinkedList1_Remove(&clients_list, &client->clients_list_node);
- num_clients--;
-
- // free send queue
- PacketPassFairQueue_Free(&client->send_queue);
-
- // free send sender
- PacketStreamSender_Free(&client->send_sender);
-
- // free recv decoder
- PacketProtoDecoder_Free(&client->recv_decoder);
-
- // free recv interface
- PacketPassInterface_Free(&client->recv_if);
-
- // free disconnect timer
- BReactor_RemoveTimer(&ss, &client->disconnect_timer);
-
- // free connection interfaces
- BConnection_RecvAsync_Free(&client->con);
- BConnection_SendAsync_Free(&client->con);
-
- // free connection
- BConnection_Free(&client->con);
-
- // free structure
- free(client);
- }
- void client_logfunc (struct client *client)
- {
- char addr[BADDR_MAX_PRINT_LEN];
- BAddr_Print(&client->addr, addr);
-
- BLog_Append("client (%s): ", addr);
- }
- void client_log (struct client *client, int level, const char *fmt, ...)
- {
- va_list vl;
- va_start(vl, fmt);
- BLog_LogViaFuncVarArg((BLog_logfunc)client_logfunc, client, BLOG_CURRENT_CHANNEL, level, fmt, vl);
- va_end(vl);
- }
- void client_disconnect_timer_handler (struct client *client)
- {
- client_log(client, BLOG_INFO, "timed out, disconnecting");
-
- // free client
- client_free(client);
- }
- void client_connection_handler (struct client *client, int event)
- {
- if (event == BCONNECTION_EVENT_RECVCLOSED) {
- client_log(client, BLOG_INFO, "client closed");
- } else {
- client_log(client, BLOG_INFO, "client error");
- }
-
- // free client
- client_free(client);
- }
- void client_decoder_handler_error (struct client *client)
- {
- client_log(client, BLOG_ERROR, "decoder error");
-
- // free client
- client_free(client);
- }
- void client_recv_if_handler_send (struct client *client, uint8_t *data, int data_len)
- {
- ASSERT(data_len >= 0)
- ASSERT(data_len <= udpgw_mtu)
-
- // accept packet
- PacketPassInterface_Done(&client->recv_if);
-
- // parse header
- if (data_len < sizeof(struct udpgw_header)) {
- client_log(client, BLOG_ERROR, "missing header");
- return;
- }
- struct udpgw_header header;
- memcpy(&header, data, sizeof(header));
- data += sizeof(header);
- data_len -= sizeof(header);
- uint8_t flags = ltoh8(header.flags);
- uint16_t conid = ltoh16(header.conid);
-
- // reset disconnect timer
- BReactor_SetTimer(&ss, &client->disconnect_timer);
-
- // if this is keepalive, ignore any payload
- if ((flags & UDPGW_CLIENT_FLAG_KEEPALIVE)) {
- client_log(client, BLOG_DEBUG, "received keepalive");
- return;
- }
-
- // check payload length
- if (data_len > options.udp_mtu) {
- client_log(client, BLOG_ERROR, "too much data");
- return;
- }
-
- // find connection
- struct connection *con = find_connection(client, conid);
- ASSERT(!con || !con->closing)
-
- // if connection exists, close it if needed
- if (con && ((flags & UDPGW_CLIENT_FLAG_REBIND) || con->addr.ipv4.ip != header.addr_ip || con->addr.ipv4.port != header.addr_port)) {
- connection_log(con, BLOG_DEBUG, "close old");
-
- connection_close(con);
- con = NULL;
- }
-
- // if connection doesn't exists, create it
- if (!con) {
- // check number of connections
- if (client->num_connections == options.max_connections_for_client) {
- // close least recently used connection
- con = UPPER_OBJECT(LinkedList1_GetFirst(&client->connections_list), struct connection, connections_list_node);
- connection_close(con);
- }
-
- // read address
- BAddr addr;
- BAddr_InitIPv4(&addr, header.addr_ip, header.addr_port);
-
- // create new connection
- connection_init(client, conid, addr, data, data_len);
- } else {
- // submit packet to existing connection
- connection_send_to_udp(con, data, data_len);
- }
- }
- uint8_t * build_port_usage_array_and_find_least_used_connection (BAddr remote_addr, struct connection **out_con)
- {
- ASSERT(options.local_udp_num_ports >= 0)
- ASSERT(remote_addr.type == BADDR_TYPE_IPV4)
-
- // allocate port usage array
- uint8_t *port_usage = (uint8_t *)BAllocSize(bsize_fromint(options.local_udp_num_ports));
- if (!port_usage) {
- return NULL;
- }
-
- // zero array
- memset(port_usage, 0, options.local_udp_num_ports);
-
- struct connection *least_con = NULL;
-
- // flag inappropriate ports (those with the same remote address)
- for (LinkedList1Node *ln = LinkedList1_GetFirst(&clients_list); ln; ln = LinkedList1Node_Next(ln)) {
- struct client *client = UPPER_OBJECT(ln, struct client, clients_list_node);
-
- for (LinkedList1Node *ln2 = LinkedList1_GetFirst(&client->connections_list); ln2; ln2 = LinkedList1Node_Next(ln2)) {
- struct connection *con = UPPER_OBJECT(ln2, struct connection, connections_list_node);
- ASSERT(con->client == client)
- ASSERT(!con->closing)
- ASSERT(con->local_port_index < options.local_udp_num_ports)
-
- if (con->local_port_index < 0) {
- continue;
- }
-
- if (options.unique_local_ports) {
- if (con->addr.ipv4.ip != remote_addr.ipv4.ip) {
- continue;
- }
- } else {
- if (!BAddr_Compare(&con->addr, &remote_addr)) {
- continue;
- }
- }
-
- port_usage[con->local_port_index] = 1;
-
- if (!PacketPassFairQueueFlow_IsBusy(&con->send_qflow)) {
- if (!least_con || con->last_use_time < least_con->last_use_time) {
- least_con = con;
- }
- }
- }
- }
-
- *out_con = least_con;
- return port_usage;
- }
- void connection_init (struct client *client, uint16_t conid, BAddr addr, const uint8_t *data, int data_len)
- {
- ASSERT(client->num_connections < options.max_connections_for_client)
- ASSERT(!find_connection(client, conid))
- BAddr_Assert(&addr);
- ASSERT(addr.type == BADDR_TYPE_IPV4)
- ASSERT(data_len >= 0)
- ASSERT(data_len <= options.udp_mtu)
-
- // allocate structure
- struct connection *con = (struct connection *)malloc(sizeof(*con));
- if (!con) {
- client_log(client, BLOG_ERROR, "malloc failed");
- goto fail0;
- }
-
- // init arguments
- con->client = client;
- con->conid = conid;
- con->addr = addr;
- con->first_data = data;
- con->first_data_len = data_len;
-
- // set last use time
- con->last_use_time = btime_gettime();
-
- // set not closing
- con->closing = 0;
-
- // init first job
- BPending_Init(&con->first_job, BReactor_PendingGroup(&ss), (BPending_handler)connection_first_job_handler, con);
- BPending_Set(&con->first_job);
-
- // init send queue flow
- PacketPassFairQueueFlow_Init(&con->send_qflow, &client->send_queue);
-
- // init send PacketProtoFlow
- if (!PacketProtoFlow_Init(&con->send_ppflow, udpgw_mtu, CONNECTION_CLIENT_BUFFER_SIZE, PacketPassFairQueueFlow_GetInput(&con->send_qflow), BReactor_PendingGroup(&ss))) {
- client_log(client, BLOG_ERROR, "PacketProtoFlow_Init failed");
- goto fail1;
- }
- con->send_if = PacketProtoFlow_GetInput(&con->send_ppflow);
-
- // init UDP dgram
- if (!BDatagram_Init(&con->udp_dgram, addr.type, &ss, con, (BDatagram_handler)connection_dgram_handler_event)) {
- client_log(client, BLOG_ERROR, "BDatagram_Init failed");
- goto fail2;
- }
-
- con->local_port_index = -1;
-
- if (options.local_udp_num_ports >= 0) {
- // build port usage array, find least used connection
- struct connection *least_con;
- uint8_t *port_usage = build_port_usage_array_and_find_least_used_connection(addr, &least_con);
- if (!port_usage) {
- client_log(client, BLOG_ERROR, "build_port_usage_array failed");
- goto failed;
- }
-
- // set SO_REUSEADDR
- if (!BDatagram_SetReuseAddr(&con->udp_dgram, 1)) {
- client_log(client, BLOG_ERROR, "set SO_REUSEADDR failed");
- goto failed;
- }
-
- // try different ports
- for (int i = 0; i < options.local_udp_num_ports; i++) {
- // skip inappropriate ports
- if (port_usage[i]) {
- continue;
- }
-
- BAddr bind_addr = local_udp_addr;
- BAddr_SetPort(&bind_addr, hton16(ntoh16(BAddr_GetPort(&bind_addr)) + (uint16_t)i));
- if (BDatagram_Bind(&con->udp_dgram, bind_addr)) {
- // remember which port we're using
- con->local_port_index = i;
- goto cont;
- }
- }
-
- // try closing an unused connection with the same remote addr
- if (!least_con) {
- goto failed;
- }
-
- ASSERT(least_con->local_port_index >= 0)
- ASSERT(least_con->local_port_index < options.local_udp_num_ports)
- ASSERT(!PacketPassFairQueueFlow_IsBusy(&least_con->send_qflow))
-
- int i = least_con->local_port_index;
-
- BLog(BLOG_INFO, "closing connection for its remote address");
-
- // close the offending connection
- connection_close(least_con);
-
- // try binding to its port
- BAddr bind_addr = local_udp_addr;
- BAddr_SetPort(&bind_addr, hton16(ntoh16(BAddr_GetPort(&bind_addr)) + (uint16_t)i));
- if (BDatagram_Bind(&con->udp_dgram, bind_addr)) {
- // remember which port we're using
- con->local_port_index = i;
- goto cont;
- }
-
- failed:
- client_log(client, BLOG_WARNING, "failed to bind to any local address; proceeding regardless");
- cont:;
- BFree(port_usage);
- }
-
- // set UDP dgram send address
- BIPAddr ipaddr;
- BIPAddr_InitInvalid(&ipaddr);
- BDatagram_SetSendAddrs(&con->udp_dgram, addr, ipaddr);
-
- // init UDP dgram interfaces
- BDatagram_SendAsync_Init(&con->udp_dgram, options.udp_mtu);
- BDatagram_RecvAsync_Init(&con->udp_dgram, options.udp_mtu);
-
- // init UDP writer
- BufferWriter_Init(&con->udp_send_writer, options.udp_mtu, BReactor_PendingGroup(&ss));
-
- // init UDP buffer
- if (!PacketBuffer_Init(&con->udp_send_buffer, BufferWriter_GetOutput(&con->udp_send_writer), BDatagram_SendAsync_GetIf(&con->udp_dgram), CONNECTION_UDP_BUFFER_SIZE, BReactor_PendingGroup(&ss))) {
- client_log(client, BLOG_ERROR, "PacketBuffer_Init failed");
- goto fail4;
- }
-
- // init UDP recv interface
- PacketPassInterface_Init(&con->udp_recv_if, options.udp_mtu, (PacketPassInterface_handler_send)connection_udp_recv_if_handler_send, con, BReactor_PendingGroup(&ss));
-
- // init UDP recv buffer
- if (!SinglePacketBuffer_Init(&con->udp_recv_buffer, BDatagram_RecvAsync_GetIf(&con->udp_dgram), &con->udp_recv_if, BReactor_PendingGroup(&ss))) {
- client_log(client, BLOG_ERROR, "SinglePacketBuffer_Init failed");
- goto fail5;
- }
-
- // insert to client's connections tree
- ASSERT_EXECUTE(BAVL_Insert(&client->connections_tree, &con->connections_tree_node, NULL))
-
- // insert to client's connections list
- LinkedList1_Append(&client->connections_list, &con->connections_list_node);
-
- // increment number of connections
- client->num_connections++;
-
- connection_log(con, BLOG_DEBUG, "initialized");
-
- return;
-
- fail5:
- PacketPassInterface_Free(&con->udp_recv_if);
- PacketBuffer_Free(&con->udp_send_buffer);
- fail4:
- BufferWriter_Free(&con->udp_send_writer);
- BDatagram_RecvAsync_Free(&con->udp_dgram);
- BDatagram_SendAsync_Free(&con->udp_dgram);
- BDatagram_Free(&con->udp_dgram);
- fail2:
- PacketProtoFlow_Free(&con->send_ppflow);
- fail1:
- PacketPassFairQueueFlow_Free(&con->send_qflow);
- BPending_Free(&con->first_job);
- free(con);
- fail0:
- return;
- }
- void connection_free (struct connection *con)
- {
- struct client *client = con->client;
- PacketPassFairQueueFlow_AssertFree(&con->send_qflow);
-
- if (con->closing) {
- // remove from client's closing connections list
- LinkedList1_Remove(&client->closing_connections_list, &con->closing_connections_list_node);
- } else {
- // decrement number of connections
- client->num_connections--;
-
- // remove from client's connections list
- LinkedList1_Remove(&client->connections_list, &con->connections_list_node);
-
- // remove from client's connections tree
- BAVL_Remove(&client->connections_tree, &con->connections_tree_node);
-
- // free UDP
- connection_free_udp(con);
- }
-
- // free send PacketProtoFlow
- PacketProtoFlow_Free(&con->send_ppflow);
-
- // free send queue flow
- PacketPassFairQueueFlow_Free(&con->send_qflow);
-
- // free first job
- BPending_Free(&con->first_job);
-
- // free structure
- free(con);
- }
- void connection_logfunc (struct connection *con)
- {
- client_logfunc(con->client);
-
- if (con->closing) {
- BLog_Append("old connection %"PRIu16": ", con->conid);
- } else {
- BLog_Append("connection %"PRIu16": ", con->conid);
- }
- }
- void connection_log (struct connection *con, int level, const char *fmt, ...)
- {
- va_list vl;
- va_start(vl, fmt);
- BLog_LogViaFuncVarArg((BLog_logfunc)connection_logfunc, con, BLOG_CURRENT_CHANNEL, level, fmt, vl);
- va_end(vl);
- }
- void connection_free_udp (struct connection *con)
- {
- // free UDP receive buffer
- SinglePacketBuffer_Free(&con->udp_recv_buffer);
-
- // free UDP receive interface
- PacketPassInterface_Free(&con->udp_recv_if);
-
- // free UDP buffer
- PacketBuffer_Free(&con->udp_send_buffer);
-
- // free UDP writer
- BufferWriter_Free(&con->udp_send_writer);
-
- // free UDP dgram interfaces
- BDatagram_RecvAsync_Free(&con->udp_dgram);
- BDatagram_SendAsync_Free(&con->udp_dgram);
-
- // free UDP dgram
- BDatagram_Free(&con->udp_dgram);
- }
- void connection_first_job_handler (struct connection *con)
- {
- ASSERT(!con->closing)
-
- connection_send_to_udp(con, con->first_data, con->first_data_len);
- }
- int connection_send_to_client (struct connection *con, uint8_t flags, const uint8_t *data, int data_len)
- {
- ASSERT(data_len >= 0)
- ASSERT(data_len <= options.udp_mtu)
-
- // get buffer location
- uint8_t *out;
- if (!BufferWriter_StartPacket(con->send_if, &out)) {
- connection_log(con, BLOG_ERROR, "out of client buffer");
- return 0;
- }
-
- // write header
- struct udpgw_header header;
- header.flags = htol8(flags);
- header.conid = htol16(con->conid);
- header.addr_ip = con->addr.ipv4.ip;
- header.addr_port = con->addr.ipv4.port;
- memcpy(out, &header, sizeof(header));
-
- // write message
- memcpy(out + sizeof(header), data, data_len);
-
- // submit written message
- BufferWriter_EndPacket(con->send_if, sizeof(header) + data_len);
-
- return 1;
- }
- int connection_send_to_udp (struct connection *con, const uint8_t *data, int data_len)
- {
- struct client *client = con->client;
- ASSERT(!con->closing)
- ASSERT(data_len >= 0)
- ASSERT(data_len <= options.udp_mtu)
-
- connection_log(con, BLOG_DEBUG, "from client %d bytes", data_len);
-
- // set last use time
- con->last_use_time = btime_gettime();
-
- // move connection to front
- LinkedList1_Remove(&client->connections_list, &con->connections_list_node);
- LinkedList1_Append(&client->connections_list, &con->connections_list_node);
-
- // get buffer location
- uint8_t *out;
- if (!BufferWriter_StartPacket(&con->udp_send_writer, &out)) {
- connection_log(con, BLOG_ERROR, "out of UDP buffer");
- return 0;
- }
-
- // write message
- memcpy(out, data, data_len);
-
- // submit written message
- BufferWriter_EndPacket(&con->udp_send_writer, data_len);
-
- return 1;
- }
- void connection_close (struct connection *con)
- {
- struct client *client = con->client;
- ASSERT(!con->closing)
-
- // if possible, free connection immediately
- if (!PacketPassFairQueueFlow_IsBusy(&con->send_qflow)) {
- connection_free(con);
- return;
- }
-
- connection_log(con, BLOG_DEBUG, "closing later");
-
- // decrement number of connections
- client->num_connections--;
-
- // remove from client's connections list
- LinkedList1_Remove(&client->connections_list, &con->connections_list_node);
-
- // remove from client's connections tree
- BAVL_Remove(&client->connections_tree, &con->connections_tree_node);
-
- // free UDP
- connection_free_udp(con);
-
- // insert to client's closing connections list
- LinkedList1_Append(&client->closing_connections_list, &con->closing_connections_list_node);
-
- // set busy handler
- PacketPassFairQueueFlow_SetBusyHandler(&con->send_qflow, (PacketPassFairQueue_handler_busy)connection_send_qflow_busy_handler, con);
-
- // unset first job
- BPending_Unset(&con->first_job);
-
- // set closing
- con->closing = 1;
- }
- void connection_send_qflow_busy_handler (struct connection *con)
- {
- ASSERT(con->closing)
- PacketPassFairQueueFlow_AssertFree(&con->send_qflow);
-
- connection_log(con, BLOG_DEBUG, "closing finally");
-
- // free connection
- connection_free(con);
- }
- void connection_dgram_handler_event (struct connection *con, int event)
- {
- ASSERT(!con->closing)
-
- connection_log(con, BLOG_INFO, "UDP error");
-
- // close connection
- connection_close(con);
- }
- void connection_udp_recv_if_handler_send (struct connection *con, uint8_t *data, int data_len)
- {
- struct client *client = con->client;
- ASSERT(!con->closing)
- ASSERT(data_len >= 0)
- ASSERT(data_len <= options.udp_mtu)
-
- connection_log(con, BLOG_DEBUG, "from UDP %d bytes", data_len);
-
- // set last use time
- con->last_use_time = btime_gettime();
-
- // move connection to front
- LinkedList1_Remove(&client->connections_list, &con->connections_list_node);
- LinkedList1_Append(&client->connections_list, &con->connections_list_node);
-
- // accept packet
- PacketPassInterface_Done(&con->udp_recv_if);
-
- // send packet to client
- connection_send_to_client(con, 0, data, data_len);
- }
- struct connection * find_connection (struct client *client, uint16_t conid)
- {
- BAVLNode *tree_node = BAVL_LookupExact(&client->connections_tree, &conid);
- if (!tree_node) {
- return NULL;
- }
- struct connection *con = UPPER_OBJECT(tree_node, struct connection, connections_tree_node);
- ASSERT(con->conid == conid)
- ASSERT(!con->closing)
-
- return con;
- }
- int uint16_comparator (void *unused, uint16_t *v1, uint16_t *v2)
- {
- return B_COMPARE(*v1, *v2);
- }
|