11/*
22 * Copyright (C) 2018 Jigsaw Operations LLC
3+ * Copyright (C) 2019 Ambroz Bizjak (modifications)
34 *
45 * Redistribution and use in source and binary forms, with or without
56 * modification, are permitted provided that the following conditions are met:
2728#ifndef BADVPN_SOCKS_UDP_CLIENT_SOCKSUDPCLIENT_H
2829#define BADVPN_SOCKS_UDP_CLIENT_SOCKSUDPCLIENT_H
2930
31+ #include <stddef.h>
3032#include <stdint.h>
3133
3234#include <base/BPending.h>
3335#include <base/DebugObject.h>
3436#include <flow/BufferWriter.h>
3537#include <flow/PacketBuffer.h>
3638#include <flow/SinglePacketBuffer.h>
39+ #include <flow/PacketPassInterface.h>
3740#include <flowextra/PacketPassInactivityMonitor.h>
38- #include <misc/debug.h>
39- #include <misc/socks_proto.h>
4041#include <socksclient/BSocksClient.h>
4142#include <structure/BAVL.h>
4243#include <system/BAddr.h>
4344#include <system/BDatagram.h>
4445#include <system/BReactor.h>
4546#include <system/BTime.h>
4647
47- // This sets the number of packets to accept while waiting for SOCKS server to authenticate and
48- // connect. A slow or far-away SOCKS server could require 300 ms to connect, and a chatty
49- // client (e.g. STUN) could send a packet every 20 ms, so a limit of 16 seems reasonable.
50- #define SOCKS_UDP_SEND_BUFFER_PACKETS 16
51-
52- typedef void (* SocksUdpClient_handler_received ) (void * user , BAddr local_addr , BAddr remote_addr , const uint8_t * data , int data_len );
48+ typedef void (* SocksUdpClient_handler_received ) (
49+ void * user , BAddr local_addr , BAddr remote_addr , const uint8_t * data , int data_len );
5350
5451typedef struct {
5552 BAddr server_addr ;
5653 const struct BSocksClient_auth_info * auth_info ;
5754 size_t num_auth_info ;
5855 int num_connections ;
5956 int max_connections ;
57+ int send_buf_size ;
6058 int udp_mtu ;
59+ int socks_mtu ;
6160 btime_t keepalive_time ;
6261 BReactor * reactor ;
6362 void * user ;
@@ -77,8 +76,8 @@ struct SocksUdpClient_connection {
7776 BDatagram socket ;
7877 PacketPassInterface recv_if ;
7978 SinglePacketBuffer recv_buffer ;
80- // The first_* members represent the initial packet, which has to be stored so it can wait for
81- // send_writer to become ready.
79+ // The first_* members represent the initial packet, which has to be stored so it can
80+ // wait for send_writer to become ready.
8281 uint8_t * first_data ;
8382 int first_data_len ;
8483 BAddr first_remote_addr ;
@@ -92,43 +91,59 @@ struct SocksUdpClient_connection {
9291
9392/**
9493 * Initializes the SOCKS5-UDP client object.
95- * This function does not perform network access, so it will always succeed if the arguments
96- * are valid .
94+ *
95+ * This function only initialzies the object and does not perform network access .
9796 *
9897 * Currently, this function only supports connection to a SOCKS5 server that is routable from
99- * localhost (i.e. running on the local machine). It may be possible to add support for remote
100- * servers, but SOCKS5 does not support UDP if there is a NAT or firewall between the client
101- * and the proxy.
98+ * localhost (i.e. running on the local machine). It may be possible to add support for
99+ * remote servers, but SOCKS5 does not support UDP if there is a NAT or firewall between the
100+ * client and the proxy.
102101 *
103102 * @param o the object
104103 * @param udp_mtu the maximum size of packets that will be sent through the tunnel
105104 * @param max_connections how many local ports to track before dropping packets
105+ * @param send_buf_size maximum number of buffered outgoing packets per connection
106106 * @param keepalive_time how long to track an idle local port before forgetting it
107107 * @param server_addr SOCKS5 server address. MUST BE ON LOCALHOST.
108+ * @param auth_info List of authentication info for BSocksClient. The pointer must remain
109+ * valid while this object exists, the data is not copied.
110+ * @param num_auth_info Number of the above.
108111 * @param reactor reactor we live in
109112 * @param user value passed to handler
110113 * @param handler_received handler for incoming UDP packets
114+ * @return 1 on success, 0 on failure
115+ */
116+ int SocksUdpClient_Init (SocksUdpClient * o , int udp_mtu , int max_connections ,
117+ int send_buf_size , btime_t keepalive_time , BAddr server_addr ,
118+ const struct BSocksClient_auth_info * auth_info , size_t num_auth_info ,
119+ BReactor * reactor , void * user , SocksUdpClient_handler_received handler_received );
120+
121+ /**
122+ * Frees the SOCKS5-UDP client object.
123+ *
124+ * @param o the object
111125 */
112- void SocksUdpClient_Init (SocksUdpClient * o , int udp_mtu , int max_connections , btime_t keepalive_time ,
113- BAddr server_addr , const struct BSocksClient_auth_info * auth_info , size_t num_auth_info ,
114- BReactor * reactor , void * user , SocksUdpClient_handler_received handler_received );
115126void SocksUdpClient_Free (SocksUdpClient * o );
116127
117128/**
118129 * Submit a packet to be sent through the proxy.
119130 *
120131 * This will reuse an existing connection for packets from local_addr, or create one if
121- * there is none. If the number of live connections exceeds max_connections, or if the number of
122- * buffered packets from this port exceeds a limit, packets will be dropped silently.
132+ * there is none. If the number of live connections exceeds max_connections, or if the
133+ * number of buffered packets from this port exceeds a limit, packets will be dropped
134+ * silently.
123135 *
124- * As a resource optimization, if a connection has only been used to send one DNS query, then
125- * the connection will be closed and freed once the reply is received.
136+ * As a resource optimization, if a connection has only been used to send one DNS query,
137+ * then the connection will be closed and freed once the reply is received.
126138 *
127139 * @param o the object
128- * @param local_addr the UDP packet's source address, and the expected destination for replies
140+ * @param local_addr the UDP packet's source address, and the expected destination for
141+ * replies
129142 * @param remote_addr the destination of the packet after it exits the proxy
130- * @param data the packet contents. Caller retains ownership.
143+ * @param data the packet contents. Caller retains ownership.
144+ * @param data_len number of bytes in the data
131145 */
132- void SocksUdpClient_SubmitPacket (SocksUdpClient * o , BAddr local_addr , BAddr remote_addr , const uint8_t * data , int data_len );
146+ void SocksUdpClient_SubmitPacket (SocksUdpClient * o ,
147+ BAddr local_addr , BAddr remote_addr , const uint8_t * data , int data_len );
133148
134149#endif
0 commit comments