diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2020-05-27 18:07:34 +0200 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2020-05-27 18:07:34 +0200 |
commit | ed456a32ffe6fea0cb52b5184a00a14cb0059f4a (patch) | |
tree | 8970d63b89090e8eb778d97043af9b74999828ae | |
parent | a0fca977bf366b64a5cd3e1c71fce14a93b8a44c (diff) |
splitted bigfat anon enum in pconfig.h into multiple smaller ones hoping to improve readability
* clang-format headers
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
-rw-r--r-- | src/pconfig.h | 124 | ||||
-rw-r--r-- | src/pdesc.h | 167 | ||||
-rw-r--r-- | src/pkt.c | 4 | ||||
-rw-r--r-- | src/utils.c | 134 | ||||
-rw-r--r-- | src/utils.h | 8 |
5 files changed, 224 insertions, 213 deletions
diff --git a/src/pconfig.h b/src/pconfig.h index 53fcbe7..140cad5 100644 --- a/src/pconfig.h +++ b/src/pconfig.h @@ -47,50 +47,68 @@ #define PCONFIG_H 1 enum { - /** Ping tunnel's operating mode (client) */ - kMode_forward = 0, - /** Ping tunnel's operating mode (server) */ - kMode_proxy, - /** Set this constant to the number of - * concurrent connections you wish to handle by default. - */ - kMax_tunnels = 10, - /** Different verbosity levels. */ - kNo_log = -1, - kLog_error = 0, - kLog_info, - kLog_event, - kLog_verbose, - kLog_debug, - kLog_sendrecv, - /** numbers */ - kIP_packet_max_size = 576, - /** In bytes, mind you */ - kIP_header_size = 20, - kIP_actual_size = (kIP_packet_max_size - kIP_header_size) - ((kIP_packet_max_size - kIP_header_size) % 8), - /** Also in bytes */ - kICMP_header_size = 8, - /** This constant control the maximum size of - * the payload-portion of the ICMP packets - * we send. Note that this does not include - * the IP or ICMP headers! - */ - kDefault_buf_size = 1024, - /** Type code for echo request and replies */ - kICMP_echo_request = 8, - kICMP_echo_reply = 0, - /** number of packets we can have in our send/receive ring */ - kPing_window_size = 64, - /** Tunnels are automatically closed after one minute of inactivity. Since - * we continously send acknowledgements between the two peers, this mechanism - * won't disconnect "valid" connections. - */ - kAutomatic_close_timeout = 60, // Seconds! - /** size of md5 digest in bytes */ - kMD5_digest_size = 16, - /** size of sha512 digest in bytes */ - kSHA512_digest_size = 64, - /** These constants are used to indicate the protocol state. The protocol + /** Set this constant to the number of + * concurrent connections you wish to handle by default. + */ + kMax_tunnels = 10, + /** numbers */ + kIP_packet_max_size = 576, + /** In bytes, mind you */ + kIP_header_size = 20, + kIP_actual_size = (kIP_packet_max_size - kIP_header_size) - ((kIP_packet_max_size - kIP_header_size) % 8), + /** Also in bytes */ + kICMP_header_size = 8, + /** This constant control the maximum size of + * the payload-portion of the ICMP packets + * we send. Note that this does not include + * the IP or ICMP headers! + */ + kDefault_buf_size = 1024, + /** Type code for echo request and replies */ + kICMP_echo_request = 8, + kICMP_echo_reply = 0, + /** number of packets we can have in our send/receive ring */ + kPing_window_size = 64, + /** Tunnels are automatically closed after one minute of inactivity. Since + * we continously send acknowledgements between the two peers, this mechanism + * won't disconnect "valid" connections. + */ + kAutomatic_close_timeout = 60, // Seconds! + /** size of md5 digest in bytes */ + kMD5_digest_size = 16, + /** size of sha512 digest in bytes */ + kSHA512_digest_size = 64, + kDNS_port = 53 +}; + +enum oper_mode { + /** Ping tunnel's operating mode (client) */ + kMode_forward = 0, + /** Ping tunnel's operating mode (server) */ + kMode_proxy +}; + +enum pkt_flag { + /** set when packet comes from a user */ + kUser_flag = 1 << 30, + /** set when packet comes from the proxy */ + kProxy_flag = 1 << 31, + kFlag_mask = kUser_flag | kProxy_flag +}; + +enum log_level { + /** Different verbosity levels. */ + kNo_log = -1, + kLog_error = 0, + kLog_info, + kLog_event, + kLog_verbose, + kLog_debug, + kLog_sendrecv +}; + +enum proxy_state { + /** These constants are used to indicate the protocol state. The protocol * works as follows: * - The identifier is used by both the proxy and the forwarder * to identify the session (and thus the relevant sockets). @@ -118,18 +136,12 @@ enum { * actually returning the ping. This is mostly relevant for users, and for * proxies running in unprivileged mode. */ - kProxy_start = 0, - kProto_ack, - kProto_data, - kProto_close, - kProto_authenticate, - kNum_proto_types, - /** set when packet comes from a user */ - kUser_flag = 1 << 30, - /** set when packet comes from the proxy */ - kProxy_flag = 1 << 31, - kFlag_mask = kUser_flag | kProxy_flag, - kDNS_port = 53 + kProxy_start = 0, + kProto_ack, + kProto_data, + kProto_close, + kProto_authenticate, + kNum_proto_types }; #endif diff --git a/src/pdesc.h b/src/pdesc.h index 7ff6898..f8b06e6 100644 --- a/src/pdesc.h +++ b/src/pdesc.h @@ -72,13 +72,13 @@ * proxy with client and vice versa in the list above. */ typedef struct forward_desc_t { - /** ping_tunnel_pkt_t seq_no */ - uint16_t seq_no; - /** length of data */ - uint16_t length; - /** amount of data not yet transferred */ - size_t remaining; - char data[0]; + /** ping_tunnel_pkt_t seq_no */ + uint16_t seq_no; + /** length of data */ + uint16_t length; + /** amount of data not yet transferred */ + size_t remaining; + char data[0]; } forward_desc_t; /** icmp_desc_t: This structure is used to track the ICMP packets sent by either @@ -88,98 +88,101 @@ typedef struct forward_desc_t { * ICMP packets. */ typedef struct icmp_desc_t { - /** total length of ICMP packet, including ICMP header and ptunnel data. */ - uint16_t pkt_len; - double last_resend; - uint16_t seq_no; - uint16_t icmp_id; - icmp_echo_packet_t *pkt; + /** total length of ICMP packet, including ICMP header and ptunnel data. */ + uint16_t pkt_len; + double last_resend; + uint16_t seq_no; + uint16_t icmp_id; + icmp_echo_packet_t * pkt; } icmp_desc_t; /** xfer_stats_t: Various transfer statistics, such as bytes sent and received, * number of ping packets sent/received, etc. */ typedef struct xfer_stats_t { - double bytes_in; - double bytes_out; - uint32_t icmp_in; - uint32_t icmp_out; - uint32_t icmp_resent; - uint32_t icmp_ack_out; + double bytes_in; + double bytes_out; + uint32_t icmp_in; + uint32_t icmp_out; + uint32_t icmp_resent; + uint32_t icmp_ack_out; } xfer_stats_t; /** proxy_desc_t: This massive structure describes a tunnel instance. */ typedef struct proxy_desc_t { - /** ICMP or UDP socket */ - int sock; - /** number of bytes in receive buffer */ - int bytes; - /** set to true once this instance should be removed */ - int should_remove; - /** data buffer, used to receive ping and pong packets */ - char *buf; - uint16_t id_no; - uint16_t my_seq; - uint16_t ping_seq; - uint16_t next_remote_seq; - uint16_t pkt_type; - uint16_t remote_ack_val; - uint16_t icmp_id; - /** first available slot in recv ring */ - int recv_idx; - /** current slot in recv ring being transferred */ - int recv_xfer_idx; - /** first available slot in send ring */ - int send_idx; - /** first packet in send ring not yet acked */ - int send_first_ack; - /** number of items in recv ring awaiting send */ - int recv_wait_send; - /** number of items in send ring awaiting ack */ - int send_wait_ack; - int next_resend_start; - int authenticated; - /** Contains the challenge, if used. */ - challenge_t *challenge; - /** Protocol state */ - uint32_t state; - /** Either kProxy_flag or kUser_flag */ - uint32_t type_flag; - /** IP and port to which data should be forwarded. */ - uint32_t dst_ip; - uint32_t dst_port; - /** Same as above */ - struct sockaddr_in dest_addr; - /** Time when last ack packet was sent. */ - double last_ack; - /** Time when a packet was last received. */ - double last_activity; - double last_data_activity; - uint16_t window_size; - double ack_interval; - double resend_interval; - icmp_desc_t *send_ring; - forward_desc_t **recv_ring; - xfer_stats_t xfer; - struct proxy_desc_t *next; + /** ICMP or UDP socket */ + int sock; + /** number of bytes in receive buffer */ + int bytes; + /** set to true once this instance should be removed */ + int should_remove; + /** data buffer, used to receive ping and pong packets */ + char * buf; + uint16_t id_no; + uint16_t my_seq; + uint16_t ping_seq; + uint16_t next_remote_seq; + uint16_t pkt_type; + uint16_t remote_ack_val; + uint16_t icmp_id; + /** first available slot in recv ring */ + int recv_idx; + /** current slot in recv ring being transferred */ + int recv_xfer_idx; + /** first available slot in send ring */ + int send_idx; + /** first packet in send ring not yet acked */ + int send_first_ack; + /** number of items in recv ring awaiting send */ + int recv_wait_send; + /** number of items in send ring awaiting ack */ + int send_wait_ack; + int next_resend_start; + int authenticated; + /** Contains the challenge, if used. */ + challenge_t * challenge; + /** Protocol state */ + uint32_t state; + /** Either kProxy_flag or kUser_flag */ + enum pkt_flag type_flag; + /** IP and port to which data should be forwarded. */ + uint32_t dst_ip; + uint32_t dst_port; + /** Same as above */ + struct sockaddr_in dest_addr; + /** Time when last ack packet was sent. */ + double last_ack; + /** Time when a packet was last received. */ + double last_activity; + double last_data_activity; + uint16_t window_size; + double ack_interval; + double resend_interval; + icmp_desc_t * send_ring; + forward_desc_t ** recv_ring; + xfer_stats_t xfer; + struct proxy_desc_t * next; } proxy_desc_t; +proxy_desc_t * create_and_insert_proxy_desc(uint16_t id_no, + uint16_t icmp_id, + int sock, + struct sockaddr_in * addr, + uint32_t dst_ip, + uint32_t dst_port, + uint32_t init_state, + uint32_t type); -proxy_desc_t* create_and_insert_proxy_desc(uint16_t id_no, uint16_t icmp_id, - int sock, struct sockaddr_in *addr, - uint32_t dst_ip, uint32_t dst_port, - uint32_t init_state, uint32_t type); +void remove_proxy_desc(proxy_desc_t * cur, proxy_desc_t * prev); -void remove_proxy_desc(proxy_desc_t *cur, proxy_desc_t *prev); +void remove_proxy_desc_rings(proxy_desc_t * cur); -void remove_proxy_desc_rings(proxy_desc_t *cur); +forward_desc_t * create_fwd_desc(uint16_t seq_no, uint32_t data_len, char * data); -forward_desc_t* create_fwd_desc(uint16_t seq_no, uint32_t data_len, char *data); +int queue_packet( + int sock_fd, proxy_desc_t * cur, char * buf, size_t bufsiz, uint32_t dest_ip, uint32_t dest_port, uint32_t state); -int queue_packet(int sock_fd, proxy_desc_t *cur, char *buf, size_t bufsiz, - uint32_t dest_ip, uint32_t dest_port, uint32_t state); - -uint32_t send_packets(forward_desc_t *ring[], int *xfer_idx, int *await_send, int *sock, uint16_t window_size); +uint32_t send_packets(forward_desc_t * ring[], int * xfer_idx, int * await_send, int * sock, uint16_t window_size); #endif @@ -219,7 +219,7 @@ void handle_packet(char * buf, unsigned bytes, int is_pcap, struct sockaddr_in * icmp_echo_packet_t * pkt; ping_tunnel_pkt_t * pt_pkt; proxy_desc_t * cur; - uint32_t type_flag, pkt_flag, proxy_flag; + enum pkt_flag type_flag, pkt_flag, proxy_flag; challenge_t * challenge; proxy_flag = kProxy_flag; @@ -261,7 +261,7 @@ void handle_packet(char * buf, unsigned bytes, int is_pcap, struct sockaddr_in * */ if (cur) { type_flag = cur->type_flag; - if (type_flag == (uint32_t)kProxy_flag) { + if (type_flag == kProxy_flag) { cur->icmp_id = pkt->identifier; cur->ping_seq = pkt->seq; } diff --git a/src/utils.c b/src/utils.c index 64f6f88..cba85fe 100644 --- a/src/utils.c +++ b/src/utils.c @@ -73,67 +73,63 @@ #include "utils.h" #include "options.h" -void pt_log(int level, const char *fmt, ...) { - va_list args; - const char *header[] = { "[err]: ", - "[inf]: ", - "[evt]: ", - "[vbs]: ", - "[dbg]: ", - "[xfr]: " }; +void pt_log(enum log_level level, const char * fmt, ...) +{ + va_list args; + const char * header[] = {"[err]: ", "[inf]: ", "[evt]: ", "[vbs]: ", "[dbg]: ", "[xfr]: "}; #ifndef WIN32 - int syslog_levels[] = {LOG_ERR, LOG_NOTICE, LOG_NOTICE, LOG_INFO, LOG_DEBUG, LOG_DEBUG}; + int syslog_levels[] = {LOG_ERR, LOG_NOTICE, LOG_NOTICE, LOG_INFO, LOG_DEBUG, LOG_DEBUG}; #endif /* !WIN32 */ - if (level <= opts.log_level) { - va_start(args, fmt); + if (level <= opts.log_level) { + va_start(args, fmt); #ifndef WIN32 - if (opts.use_syslog) { - char log[255]; - int header_len; - header_len = snprintf(log,sizeof(log),"%s",header[level]); - vsnprintf(log+header_len,sizeof(log)-header_len,fmt,args); - syslog(syslog_levels[level], "%s", log); - } - else + if (opts.use_syslog) { + char log[255]; + int header_len; + header_len = snprintf(log, sizeof(log), "%s", header[level]); + vsnprintf(log + header_len, sizeof(log) - header_len, fmt, args); + syslog(syslog_levels[level], "%s", log); + } else #endif /* !WIN32 */ - fprintf(opts.log_file, "%s", header[level]), vfprintf(opts.log_file, fmt, args); - va_end(args); + fprintf(opts.log_file, "%s", header[level]), vfprintf(opts.log_file, fmt, args); + va_end(args); #ifndef WIN32 - if (opts.log_file != stdout && !opts.use_syslog) + if (opts.log_file != stdout && !opts.use_syslog) #else - if (opts.log_file != stdout) + if (opts.log_file != stdout) #endif - fflush(opts.log_file); - } + fflush(opts.log_file); + } } -double time_as_double(void) { - double result; - struct timeval tt; +double time_as_double(void) +{ + double result; + struct timeval tt; - gettimeofday(&tt, 0); - result = (double)tt.tv_sec + ((double)tt.tv_usec / (double)10e5); - return result; + gettimeofday(&tt, 0); + result = (double)tt.tv_sec + ((double)tt.tv_usec / (double)10e5); + return result; } -int host_to_addr(const char *hostname, uint32_t *result) +int host_to_addr(const char * hostname, uint32_t * result) { - int ret; - struct addrinfo *addrs = NULL; - struct addrinfo hints; - struct sockaddr_in *addr; + int ret; + struct addrinfo * addrs = NULL; + struct addrinfo hints; + struct sockaddr_in * addr; - memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_INET; + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; - if ((ret = getaddrinfo(hostname, NULL, &hints, &addrs)) != 0) - return ret; - addr = (struct sockaddr_in *) addrs->ai_addr; - *result = *(uint32_t *) &addr->sin_addr; - freeaddrinfo(addrs); + if ((ret = getaddrinfo(hostname, NULL, &hints, &addrs)) != 0) + return ret; + addr = (struct sockaddr_in *)addrs->ai_addr; + *result = *(uint32_t *)&addr->sin_addr; + freeaddrinfo(addrs); - return 0; + return 0; } #if 0 @@ -156,36 +152,34 @@ void print_hexstr(unsigned char *buf, size_t siz) { } #endif -int pt_random(void) { +int pt_random(void) +{ #if defined(HAVE_ARC4RANDOM) || defined(__COVERITY__) - return arc4random(); + return arc4random(); #else #if defined(RNGDEV) && !defined(_WIN32) - static int rng_fd = -1; - ssize_t bytes_read; - int rnd_val; - if (rng_fd < 0) { - rng_fd = open(RNGDEV, O_RDONLY); - if (rng_fd < 0) { - pt_log(kLog_error, "FATAL: Could not open random device '%s': %s\n", - RNGDEV, strerror(errno)); - exit(EXIT_FAILURE); - } - } - bytes_read = read(rng_fd, &rnd_val, sizeof rnd_val); - if (bytes_read != sizeof rnd_val) { - if (bytes_read < 0) - pt_log(kLog_error, "FATAL: Read from random device failed: %s\n", - strerror(errno)); - else - pt_log(kLog_error, "FATAL: Read only %zd bytes (wanted %zd bytes)\n", - bytes_read, sizeof rnd_val); - exit(EXIT_FAILURE); - } - return rnd_val; + static int rng_fd = -1; + ssize_t bytes_read; + int rnd_val; + if (rng_fd < 0) { + rng_fd = open(RNGDEV, O_RDONLY); + if (rng_fd < 0) { + pt_log(kLog_error, "FATAL: Could not open random device '%s': %s\n", RNGDEV, strerror(errno)); + exit(EXIT_FAILURE); + } + } + bytes_read = read(rng_fd, &rnd_val, sizeof rnd_val); + if (bytes_read != sizeof rnd_val) { + if (bytes_read < 0) + pt_log(kLog_error, "FATAL: Read from random device failed: %s\n", strerror(errno)); + else + pt_log(kLog_error, "FATAL: Read only %zd bytes (wanted %zd bytes)\n", bytes_read, sizeof rnd_val); + exit(EXIT_FAILURE); + } + return rnd_val; #else - srand(time(0)); - return rand(); + srand(time(0)); + return rand(); #endif #endif } diff --git a/src/utils.h b/src/utils.h index 0796848..acb5f71 100644 --- a/src/utils.h +++ b/src/utils.h @@ -48,13 +48,15 @@ #include <stdint.h> -#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) +#include "pconfig.h" -void pt_log(int level, const char *fmt, ...); +#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) + +void pt_log(enum log_level level, const char * fmt, ...); double time_as_double(void); -int host_to_addr(const char *hostname, uint32_t *result); +int host_to_addr(const char * hostname, uint32_t * result); #if 0 void print_hexstr(unsigned char *buf, size_t siz); |