diff options
-rw-r--r-- | Makefile | 12 | ||||
-rw-r--r-- | pdesc.c | 64 | ||||
-rw-r--r-- | pdesc.h | 11 | ||||
-rw-r--r-- | ptunnel.c | 145 | ||||
-rw-r--r-- | ptunnel.h | 9 |
5 files changed, 130 insertions, 111 deletions
@@ -2,15 +2,15 @@ # (c) 2004-2009 Daniel Stoedle, daniels@cs.uit.no # ptunnel.exe target added by Mike Miller, mike@mikeage.net -CC = gcc +CC = gcc CFLAGS = -Wall -g -fstrict-aliasing LDOPTS = -lpthread -lpcap -PT_OBJS = options.o ptunnel.o md5.o base64.o +PT_OBJS = options.o pdesc.o ptunnel.o md5.o base64.o -WIN32_CC = mingw32-gcc -WIN32_CFLAGS = -g -Wall -DWIN32 -I"c:\Program Files\WpdPack\Include" -WIN32_LDOPTS = -lwpcap -lwsock32 -L"c:\Program Files\WpdPack\Lib" -WIN32_PT_OBJS = options.obj ptunnel.obj md5.obj base64.obj +WIN32_CC = mingw32-gcc +WIN32_CFLAGS = -g -Wall -DWIN32 -I"c:\Program Files\WpdPack\Include" +WIN32_LDOPTS = -lwpcap -lwsock32 -L"c:\Program Files\WpdPack\Lib" +WIN32_PT_OBJS = options.obj pdesc.obj ptunnel.obj md5.obj base64.obj prefix = $(DESTDIR)/usr bindir = $(prefix)/sbin @@ -0,0 +1,64 @@ +#include "pdesc.h" +#include "options.h" +#include "ptunnel.h" + + +/* create_and_insert_proxy_desc: Creates a new proxy descriptor, linking it into + * the descriptor chain. If the sock argument is 0, the function will establish + * a TCP connection to the ip and port given by dst_ip, dst_port. + */ +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 *cur; + + pthread_mutex_lock(&chain_lock); + if (num_tunnels >= opts.max_tunnels) { + pt_log(kLog_info, "Discarding incoming connection - too many tunnels! Maximum count is %u (adjust with the -m switch).\n", opts.max_tunnels); + if (sock) + close(sock); + pthread_mutex_unlock(&chain_lock); + return 0; + } + num_tunnels++; + pthread_mutex_unlock(&chain_lock); + + pt_log(kLog_debug, "Adding proxy desc to run loop. Type is %s. Will create socket: %s\n", (type == kUser_flag ? "user" : "proxy"), (sock ? "No" : "Yes")); + cur = calloc(1, sizeof(proxy_desc_t)); + cur->id_no = id_no; + cur->dest_addr = *addr; + cur->dst_ip = dst_ip; + cur->dst_port = dst_port; + cur->icmp_id = icmp_id; + if (!sock) { + cur->sock = socket(AF_INET, SOCK_STREAM, 0); + memset(addr, 0, sizeof(struct sockaddr_in)); + addr->sin_port = htons((uint16_t)dst_port); + addr->sin_addr.s_addr = dst_ip; + addr->sin_family = AF_INET; + /* Let's just assume success, shall we? */ + if (connect(cur->sock, (struct sockaddr*)addr, sizeof(struct sockaddr_in)) < 0) { + pt_log(kLog_error, "Connect to %s:%d failed: %s\n", inet_ntoa(*(struct in_addr*)&addr->sin_addr.s_addr) , ntohs(addr->sin_port), strerror(errno)); + } + } + else + cur->sock = sock; + cur->state = init_state; + cur->type_flag = type; + if (cur->type_flag == kUser_flag) + cur->pkt_type = kICMP_echo_request; + else + cur->pkt_type = (opts.unprivileged ? kICMP_echo_request : kICMP_echo_reply); + cur->buf = malloc(icmp_receive_buf_len); + cur->last_activity = time_as_double(); + cur->authenticated = 0; + + pthread_mutex_lock(&chain_lock); + cur->next = chain; + chain = cur; + pthread_mutex_unlock(&chain_lock); + cur->xfer.bytes_in = 0.0; + cur->xfer.bytes_out = 0.0; + return cur; +} @@ -2,6 +2,9 @@ #define PDESC_H 1 #include <stdint.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> #include "pkt.h" #include "challenge.h" @@ -112,4 +115,12 @@ typedef struct proxy_desc_t { 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); +void remove_proxy_desc(proxy_desc_t *cur, proxy_desc_t *prev); +forward_desc_t* create_fwd_desc(uint16_t seq_no, uint32_t data_len, char *data); + #endif @@ -1,44 +1,47 @@ -/* ptunnel.c - ptunnel is licensed under the BSD license: - - Copyright (c) 2004-2011, Daniel Stoedle <daniels@cs.uit.no>, - Yellow Lemon Software. All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - - Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - - - 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. - - - Neither the name of the Yellow Lemon Software 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 COPYRIGHT OWNER OR CONTRIBUTORS 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. - - Contacting the author: - You can get in touch with me, Daniel Stødle (that's the Norwegian letter oe, - in case your text editor didn't realize), here: <daniels@cs.uit.no> - - The official ptunnel website is here: - <http://www.cs.uit.no/~daniels/PingTunnel/> - - Note that the source code is best viewed with tabs set to 4 spaces. -*/ +/* + * ptunnel.c + * ptunnel is licensed under the BSD license: + * + * Copyright (c) 2004-2011, Daniel Stoedle <daniels@cs.uit.no>, + * Yellow Lemon Software. All rights reserved. + * + * Copyright (c) 2017 Toni Uhlig <matzeton@googlemail.com> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - 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. + * + * - Neither the name of the Yellow Lemon Software 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 COPYRIGHT OWNER OR CONTRIBUTORS 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. + * + * Contacting the author: + * You can get in touch with me, Daniel Stødle (that's the Norwegian letter oe, + * in case your text editor didn't realize), here: <daniels@cs.uit.no> + * + * The official ptunnel website is here: + * <http://www.cs.uit.no/~daniels/PingTunnel/> + * + * Note that the source code is best viewed with tabs set to 4 spaces. + */ #include "ptunnel.h" #include "options.h" @@ -888,66 +891,6 @@ void handle_packet(char *buf, int bytes, int is_pcap, struct sockaddr_in *addr, } } - - -/* create_and_insert_proxy_desc: Creates a new proxy descriptor, linking it into - the descriptor chain. If the sock argument is 0, the function will establish - a TCP connection to the ip and port given by dst_ip, dst_port. -*/ -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 *cur; - - pthread_mutex_lock(&chain_lock); - if (num_tunnels >= opts.max_tunnels) { - pt_log(kLog_info, "Discarding incoming connection - too many tunnels! Maximum count is %u (adjust with the -m switch).\n", opts.max_tunnels); - if (sock) - close(sock); - pthread_mutex_unlock(&chain_lock); - return 0; - } - num_tunnels++; - pthread_mutex_unlock(&chain_lock); - - pt_log(kLog_debug, "Adding proxy desc to run loop. Type is %s. Will create socket: %s\n", (type == kUser_flag ? "user" : "proxy"), (sock ? "No" : "Yes")); - cur = calloc(1, sizeof(proxy_desc_t)); - cur->id_no = id_no; - cur->dest_addr = *addr; - cur->dst_ip = dst_ip; - cur->dst_port = dst_port; - cur->icmp_id = icmp_id; - if (!sock) { - cur->sock = socket(AF_INET, SOCK_STREAM, 0); - memset(addr, 0, sizeof(struct sockaddr_in)); - addr->sin_port = htons((uint16_t)dst_port); - addr->sin_addr.s_addr = dst_ip; - addr->sin_family = AF_INET; - // Let's just assume success, shall we? - if (connect(cur->sock, (struct sockaddr*)addr, sizeof(struct sockaddr_in)) < 0) { - pt_log(kLog_error, "Connect to %s:%d failed: %s\n", inet_ntoa(*(struct in_addr*)&addr->sin_addr.s_addr), ntohs(addr->sin_port), strerror(errno)); - } - } - else - cur->sock = sock; - cur->state = init_state; - cur->type_flag = type; - if (cur->type_flag == kUser_flag) - cur->pkt_type = kICMP_echo_request; - else - cur->pkt_type = (opts.unprivileged ? kICMP_echo_request : kICMP_echo_reply); - cur->buf = malloc(icmp_receive_buf_len); - cur->last_activity = time_as_double(); - cur->authenticated = 0; - - pthread_mutex_lock(&chain_lock); - cur->next = chain; - chain = cur; - pthread_mutex_unlock(&chain_lock); - cur->xfer.bytes_in = 0.0; - cur->xfer.bytes_out = 0.0; - return cur; -} - - /* remove_proxy_desc: Removes the given proxy desc, freeing its resources. Assumes that we hold the chain_lock. */ @@ -74,6 +74,11 @@ #include "pdesc.h" #include "challenge.h" +extern pthread_mutex_t chain_lock; +extern int num_tunnels; +extern const int icmp_receive_buf_len; +extern proxy_desc_t *chain; + /* pt_thread_info_t: A simple (very simple, in fact) structure that allows us to pass an arbitrary number of params to the threads we create. Currently, that's just one single parameter: The socket which the thread should listen @@ -129,9 +134,6 @@ typedef struct { void pcap_packet_handler(u_char *refcon, const struct pcap_pkthdr *hdr, const u_char* pkt); void handle_packet(char *buf, int bytes, int is_pcap, struct sockaddr_in *addr, int icmp_sock); - 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 pt_forwarder(void); void print_statistics(xfer_stats_t *xfer, int is_continuous); @@ -139,7 +141,6 @@ typedef struct { uint32_t send_packets(forward_desc_t *ring[], int *xfer_idx, int *await_send, int *sock); void handle_data(icmp_echo_packet_t *pkt, int total_len, forward_desc_t *ring[], int *await_send, int *insert_idx, uint16_t *next_expected_seq); void handle_ack(uint16_t seq_no, icmp_desc_t ring[], int *packets_awaiting_ack, int one_ack_only, int insert_idx, int *first_ack, uint16_t *remote_ack, int is_pcap); - forward_desc_t* create_fwd_desc(uint16_t seq_no, uint32_t data_len, char *data); void init_ip_packet(ip_packet_t *packet, uint16_t id, uint16_t frag_offset, uint16_t pkt_len, uint8_t ttl, uint32_t src_ip, uint32_t dst_ip, bool is_last_frag, bool dont_frag); uint16_t calc_ip_checksum(ip_packet_t *pkt); uint16_t calc_icmp_checksum(uint16_t *data, int bytes); |