├── TODO ├── helpers ├── strtok_r.c ├── gettimeofday.c ├── winhelpers.h └── xgetopt.c ├── acl.h ├── Makefile ├── Makefile.Win32 ├── list.h ├── socket.h ├── message.h ├── common.h ├── udptunnel.c ├── README ├── message.c ├── client.h ├── acl.c ├── list.c ├── client.c ├── udpclient.c ├── udpserver.c ├── socket.c └── COPYING /TODO: -------------------------------------------------------------------------------- 1 | ============================================================================= 2 | 3 | UDPTUNNEL TODO 4 | 5 | Author: Daniel Meekins 6 | Contact: dmeekins - gmail 7 | 8 | Copyright (C) 2009 Daniel Meekins 9 | 10 | ============================================================================= 11 | 12 | - Allow to run using IPv6 and IPv4 at the same time (e.g. IPv4 on the outter 13 | connections and IPv6 for the tunnel, or visa versa). 14 | - Make sure can use ipv4-mapped, like ffff:1.2.3.4 15 | -------------------------------------------------------------------------------- /helpers/strtok_r.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* 4 | * strtok_r code directly from glibc.git /string/strtok_r.c since windows 5 | * doesn't have it. 6 | */ 7 | char *strtok_r(char *s, const char *delim, char **save_ptr) 8 | { 9 | char *token; 10 | 11 | if(s == NULL) 12 | s = *save_ptr; 13 | 14 | /* Scan leading delimiters. */ 15 | s += strspn(s, delim); 16 | if(*s == '\0') 17 | { 18 | *save_ptr = s; 19 | return NULL; 20 | } 21 | 22 | /* Find the end of the token. */ 23 | token = s; 24 | s = strpbrk(token, delim); 25 | 26 | if(s == NULL) 27 | { 28 | /* This token finishes the string. */ 29 | *save_ptr = strchr(token, '\0'); 30 | } 31 | else 32 | { 33 | /* Terminate the token and make *SAVE_PTR point past it. */ 34 | *s = '\0'; 35 | *save_ptr = s + 1; 36 | } 37 | 38 | return token; 39 | } 40 | -------------------------------------------------------------------------------- /helpers/gettimeofday.c: -------------------------------------------------------------------------------- 1 | /* 2 | * From http://www.openasthra.com/c-tidbits/gettimeofday-function-for-windows/ 3 | */ 4 | #include "winhelpers.h" 5 | 6 | int gettimeofday(struct timeval *tv, struct timezone *tz) 7 | { 8 | FILETIME ft; 9 | unsigned __int64 tmpres = 0; 10 | static int tzflag = 0; 11 | 12 | if (NULL != tv) 13 | { 14 | GetSystemTimeAsFileTime(&ft); 15 | 16 | tmpres |= ft.dwHighDateTime; 17 | tmpres <<= 32; 18 | tmpres |= ft.dwLowDateTime; 19 | 20 | tmpres /= 10; /*convert into microseconds*/ 21 | 22 | /*converting file time to unix epoch*/ 23 | tmpres -= DELTA_EPOCH_IN_MICROSECS; 24 | tv->tv_sec = (long)(tmpres / 1000000UL); 25 | tv->tv_usec = (long)(tmpres % 1000000UL); 26 | } 27 | 28 | if (NULL != tz) 29 | { 30 | if (!tzflag) 31 | { 32 | _tzset(); 33 | tzflag++; 34 | } 35 | 36 | tz->tz_minuteswest = _timezone / 60; 37 | tz->tz_dsttime = _daylight; 38 | } 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /acl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Project: udptunnel 3 | * File: acl.h 4 | * 5 | * Copyright (C) 2011 Daniel Meekins 6 | * Contact: dmeekins - gmail 7 | * 8 | * Extended from Andreas Rottmann's (a.rottmann@gmx.at) work, which was 9 | * previously the "destination" module. 10 | * 11 | * This program is free software: you can redistribute it and/or modify 12 | * it under the terms of the GNU General Public License as published by 13 | * the Free Software Foundation, either version 3 of the License, or 14 | * (at your option) any later version. 15 | * 16 | * This program is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | * GNU General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU General Public License 22 | * along with this program. If not, see . 23 | */ 24 | 25 | #ifndef ACL_H 26 | #define ACL_H 27 | 28 | #include "common.h" 29 | #include "socket.h" 30 | 31 | #define ACL_ACTION_NOMATCH 0 32 | #define ACL_ACTION_DENY 1 33 | #define ACL_ACTION_ALLOW 2 34 | 35 | #define ACL_DEFAULT ((char *)"a=allow") 36 | 37 | typedef struct acl { 38 | socket_t *src; 39 | socket_t *dst; 40 | int action; 41 | } acl_t; 42 | 43 | acl_t *acl_create(char *acl_entry, int ipver); 44 | void acl_free(acl_t *acl); 45 | int acl_action(acl_t *acl, char *src, uint16_t sport, char *dst, uint16_t dport); 46 | void acl_print(acl_t *acl); 47 | 48 | #define p_acl_free ((void (*)(void *))&acl_free) 49 | 50 | #endif /* ACL_H */ 51 | -------------------------------------------------------------------------------- /helpers/winhelpers.h: -------------------------------------------------------------------------------- 1 | #ifndef WINHELPERS_H 2 | #define WINHELPERS_H 3 | 4 | /********************************************************************* 5 | * strtok_r 6 | *********************************************************************/ 7 | 8 | char *strtok_r(char *s, const char *delim, char **save_ptr); 9 | 10 | 11 | /********************************************************************* 12 | * xgetopt 13 | *********************************************************************/ 14 | 15 | extern int optind, opterr; 16 | extern char *optarg; 17 | 18 | int getopt(int argc, char *argv[], char *optstring); 19 | 20 | 21 | /********************************************************************* 22 | * gettimeofday 23 | *********************************************************************/ 24 | 25 | #include 26 | #include 27 | 28 | #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) 29 | #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 30 | #else 31 | #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL 32 | #endif 33 | 34 | struct timezone 35 | { 36 | int tz_minuteswest; /* minutes W of Greenwich */ 37 | int tz_dsttime; /* type of dst correction */ 38 | }; 39 | 40 | int gettimeofday(struct timeval *tv, struct timezone *tz); 41 | 42 | #define timeradd(a, b, result) \ 43 | do { \ 44 | (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \ 45 | (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \ 46 | if ((result)->tv_usec >= 1000000) \ 47 | { \ 48 | ++(result)->tv_sec; \ 49 | (result)->tv_usec -= 1000000; \ 50 | } \ 51 | } while (0) 52 | 53 | #endif /*WINHELPERS_H*/ 54 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Project: udptunnel 3 | # File: Makefile 4 | # 5 | # Copyright (C) 2009 Daniel Meekins 6 | # Contact: dmeekins - gmail 7 | # 8 | # This program is free software: you can redistribute it and/or modify 9 | # it under the terms of the GNU General Public License as published by 10 | # the Free Software Foundation, either version 3 of the License, or 11 | # (at your option) any later version. 12 | # 13 | # This program is distributed in the hope that it will be useful, 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | # GNU General Public License for more details. 17 | # 18 | # You should have received a copy of the GNU General Public License 19 | # along with this program. If not, see . 20 | 21 | # Uncomment appropriate one for the system this is compiling for 22 | #OS=LINUX 23 | #OS=SOLARIS 24 | OS=CYGWIN 25 | 26 | # Uncomment to build 32-bit binary (if on 64-bit system) 27 | #M32=-m32 28 | 29 | # Uncomment to build with debugging symbols 30 | #DEBUG=-g 31 | 32 | CC=gcc 33 | CFLAGS=-Wall -Wshadow -Wpointer-arith -Wwrite-strings ${M32} ${DEBUG} -D ${OS} 34 | 35 | ifeq (${OS}, SOLARIS) 36 | LDFLAGS=-lnsl -lsocket -lresolv 37 | endif 38 | 39 | all: udptunnel 40 | 41 | # 42 | # Main program 43 | # 44 | OBJS=socket.o message.o client.o list.o acl.o udpserver.o udpclient.o 45 | udptunnel: udptunnel.c ${OBJS} 46 | ${CC} ${CFLAGS} -o udptunnel udptunnel.c ${OBJS} ${LDFLAGS} 47 | strip udptunnel 48 | 49 | # 50 | # Supporting code 51 | # 52 | list.o: list.c list.h common.h 53 | socket.o: socket.c socket.h common.h 54 | client.o: client.c client.h common.h 55 | message.o: message.c message.h common.h 56 | acl.o: acl.c acl.h common.h 57 | udpclient.o: udpclient.c list.h socket.h client.h message.h common.h 58 | udpserver.o: udpserver.c list.h socket.h client.h message.h acl.h common.h 59 | 60 | # 61 | # Clean compiled and temporary files 62 | # 63 | clean: 64 | ifeq (${OS}, CYGWIN) 65 | rm -f udptunnel.exe 66 | else 67 | rm -f udptunnel 68 | endif 69 | rm -f *~ *.o helpers/*~ 70 | -------------------------------------------------------------------------------- /Makefile.Win32: -------------------------------------------------------------------------------- 1 | # 2 | # Project: udptunnel 3 | # File: Makefile.Win32 4 | # 5 | # Copyright (C) 2009 Daniel Meekins 6 | # Contact: dmeekins - gmail 7 | # 8 | # This program is free software: you can redistribute it and/or modify 9 | # it under the terms of the GNU General Public License as published by 10 | # the Free Software Foundation, either version 3 of the License, or 11 | # (at your option) any later version. 12 | # 13 | # This program is distributed in the hope that it will be useful, 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | # GNU General Public License for more details. 17 | # 18 | # You should have received a copy of the GNU General Public License 19 | # along with this program. If not, see . 20 | 21 | CC=cl.exe 22 | CFLAGS=/nologo /DWIN32 /c 23 | LDFLAGS= 24 | LIBS="C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Lib\WS2_32.Lib" 25 | 26 | all: udptunnel.exe 27 | 28 | OBJS=socket.obj message.obj client.obj list.obj acl.obj udpserver.obj \ 29 | udpclient.obj udptunnel.obj xgetopt.obj gettimeofday.obj \ 30 | strtok_r.obj 31 | udptunnel.exe: $(OBJS) 32 | link.exe /NOLOGO /OUT:udptunnel.exe $(LDFLAGS) $(OBJS) $(LIBS) 33 | 34 | list.obj: list.c list.h common.h 35 | $(CC) $(CFLAGS) list.c 36 | socket.obj: socket.c socket.h common.h 37 | $(CC) $(CFLAGS) socket.c 38 | client.obj: client.c client.h common.h 39 | $(CC) $(CFLAGS) client.c 40 | message.obj: message.c message.h common.h 41 | $(CC) $(CFLAGS) message.c 42 | acl.obj: acl.c acl.h common.h 43 | $(CC) $(CFLAGS) acl.c 44 | udpclient.obj: udpclient.c list.h socket.h client.h message.h common.h 45 | $(CC) $(CFLAGS) udpclient.c 46 | udpserver.obj: udpserver.c list.h socket.h client.h message.h common.h 47 | $(CC) $(CFLAGS) udpserver.c 48 | udptunnel.obj: udptunnel.c common.h 49 | $(CC) $(CFLAGS) udptunnel.c 50 | xgetopt.obj: helpers/xgetopt.c 51 | $(CC) $(CFLAGS) helpers/xgetopt.c 52 | gettimeofday.obj: helpers/gettimeofday.c 53 | $(CC) $(CFLAGS) helpers/gettimeofday.c 54 | strtok_r.obj: helpers/strtok_r.c 55 | $(CC) $(CFLAGS) helpers/strtok_r.c 56 | 57 | clean: 58 | del *.obj udptunnel.exe 59 | -------------------------------------------------------------------------------- /list.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Project: udptunnel 3 | * File: list.h 4 | * 5 | * Copyright (C) 2009 Daniel Meekins 6 | * Contact: dmeekins - gmail 7 | * 8 | * This program is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | * 13 | * This program is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | 22 | #ifndef LIST_H 23 | #define LIST_H 24 | 25 | #include "common.h" 26 | 27 | #define LIST_INIT_SIZE 10 /* Start off an array with 10 elements */ 28 | 29 | typedef struct list { 30 | void **obj_arr; /* Array of pointers to each object */ 31 | size_t obj_sz; /* Number of bytes each individual objects takes up */ 32 | int num_objs; /* Number of object pointers in the array */ 33 | int length; /* Actual length of the pointer array */ 34 | int sort; /* 0 - don't sort, 1 - keep sorted */ 35 | /* Function pointers to use for specific type of data types */ 36 | int (*obj_cmp)(const void *, const void *, size_t); 37 | void* (*obj_copy)(void *, const void *, size_t); 38 | void (*obj_free)(void *); 39 | } list_t; 40 | 41 | #define LIST_LEN(l) ((l)->num_objs) 42 | 43 | list_t *list_create(int obj_sz, 44 | int (*obj_cmp)(const void *, const void *, size_t), 45 | void* (*obj_copy)(void *, const void *, size_t), 46 | void (*obj_free)(void *), int sort); 47 | void *list_add(list_t *list, void *obj, int copy); 48 | void *list_get(list_t *list, void *obj); 49 | void *list_get_at(list_t *list, int i); 50 | int list_get_index(list_t *list, void *obj); 51 | list_t *list_copy(list_t *src); 52 | void list_action(list_t *list, void (*action)(void *)); 53 | void list_delete(list_t *list, void *obj); 54 | void list_delete_at(list_t *list, int i); 55 | void list_free(list_t *list); 56 | 57 | static _inline_ int int_cmp(int *i, int *j, size_t sz) 58 | { 59 | return *i - *j; 60 | } 61 | 62 | #define p_int_cmp ((int (*)(const void *, const void *, size_t))&int_cmp) 63 | 64 | #endif /* LIST_H */ 65 | -------------------------------------------------------------------------------- /socket.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Project: udptunnel 3 | * File: socket.h 4 | * 5 | * Copyright (C) 2009 Daniel Meekins 6 | * Contact: dmeekins - gmail 7 | * 8 | * This program is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | * 13 | * This program is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | 22 | #ifndef SOCKET_H 23 | #define SOCKET_H 24 | 25 | #ifndef WIN32 26 | #include 27 | #include 28 | #include 29 | #else 30 | #include 31 | #include 32 | #endif /*WIN32*/ 33 | 34 | #include 35 | #include "common.h" 36 | 37 | #define BACKLOG 10 38 | #define ADDRSTRLEN (INET6_ADDRSTRLEN + 9) 39 | 40 | #define SOCK_TYPE_TCP 1 41 | #define SOCK_TYPE_UDP 2 42 | #define SOCK_IPV4 3 43 | #define SOCK_IPV6 4 44 | 45 | #define SIN(sa) ((struct sockaddr_in *)sa) 46 | #define SIN6(sa) ((struct sockaddr_in6 *)sa) 47 | #define PADDR(a) ((struct sockaddr *)a) 48 | 49 | typedef struct socket { 50 | int fd; /* Socket file descriptor to send/recv on */ 51 | int type; /* SOCK_STREAM or SOCK_DGRAM */ 52 | struct sockaddr_storage addr; /* IP and port */ 53 | socklen_t addr_len; /* Length of sockaddr type */ 54 | } socket_t; 55 | 56 | #define SOCK_FD(s) ((s)->fd) 57 | #define SOCK_LEN(s) ((s)->addr_len) 58 | #define SOCK_PADDR(s) ((struct sockaddr *)&(s)->addr) 59 | #define SOCK_TYPE(s) ((s)->type) 60 | 61 | socket_t *sock_create(char *host, char *port, int ipver, int sock_type, 62 | int is_serv, int conn); 63 | socket_t *sock_copy(socket_t *sock); 64 | int sock_connect(socket_t *sock, int is_serv); 65 | socket_t *sock_accept(socket_t *serv); 66 | void sock_close(socket_t *s); 67 | void sock_free(socket_t *s); 68 | int sock_addr_equal(socket_t *s1, socket_t *s2); 69 | int sock_ipaddr_cmp(socket_t *s1, socket_t *s2); 70 | int sock_port_cmp(socket_t *s1, socket_t *s2); 71 | int sock_isaddrany(socket_t *s); 72 | char *sock_get_str(socket_t *s, char *buf, int len); 73 | char *sock_get_addrstr(socket_t *s, char *buf, int len); 74 | uint16_t sock_get_port(socket_t *s); 75 | int sock_recv(socket_t *sock, socket_t *from, char *data, int len); 76 | int sock_send(socket_t *to, char *data, int len); 77 | int isipaddr(char *ip, int ipver); 78 | 79 | static int _inline_ sock_get_ipver(socket_t *s) 80 | { 81 | return (s->addr.ss_family == AF_INET) ? SOCK_IPV4 : 82 | ((s->addr.ss_family == AF_INET6) ? SOCK_IPV6 : 0); 83 | } 84 | 85 | #endif /* SOCKET_H */ 86 | -------------------------------------------------------------------------------- /message.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Project: udptunnel 3 | * File: message.h 4 | * 5 | * Copyright (C) 2009 Daniel Meekins 6 | * Contact: dmeekins - gmail 7 | * 8 | * This program is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | * 13 | * This program is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | 22 | #ifndef MESSAGE_H 23 | #define MESSAGE_H 24 | 25 | #ifndef WIN32 26 | #include 27 | #include 28 | #endif /*WIN32*/ 29 | 30 | #include "common.h" 31 | #include "socket.h" 32 | 33 | #define MSG_MAX_LEN 1024 /* max bytes to send in body of message (16 bits) */ 34 | #define KEEP_ALIVE_SECS 60 35 | #define KEEP_ALIVE_TIMEOUT_SECS (7*60+1) /* has 7 tries to send a keep alive */ 36 | 37 | /* Message types: max 8 bits */ 38 | #define MSG_TYPE_GOODBYE 0x01 39 | #define MSG_TYPE_HELLO 0x02 40 | #define MSG_TYPE_HELLOACK 0x03 41 | #define MSG_TYPE_KEEPALIVE 0x04 42 | #define MSG_TYPE_DATA0 0x05 43 | #define MSG_TYPE_DATA1 0x06 44 | #define MSG_TYPE_ACK0 0x07 45 | #define MSG_TYPE_ACK1 0x08 46 | 47 | #ifndef WIN32 48 | struct msg_hdr 49 | { 50 | uint16_t client_id; 51 | uint8_t type; 52 | uint16_t length; 53 | } __attribute__ ((__packed__)); 54 | #else 55 | #pragma pack(push, 1) 56 | struct msg_hdr 57 | { 58 | uint16_t client_id; 59 | uint8_t type; 60 | uint16_t length; 61 | }; 62 | #pragma pack(pop) 63 | #endif /*WIN32*/ 64 | 65 | typedef struct msg_hdr msg_hdr_t; 66 | 67 | typedef struct data_buf 68 | { 69 | char buf[MSG_MAX_LEN]; 70 | int len; 71 | } data_buf_t; 72 | 73 | int msg_send_msg(socket_t *to, uint16_t client_id, uint8_t type, 74 | char *data, int data_len); 75 | int msg_send_hello(socket_t *to, char *host, char *port, uint16_t req_id); 76 | int msg_recv_msg(socket_t *sock, socket_t *from, 77 | char *data, int data_len, 78 | uint16_t *client_id, uint8_t *type, uint16_t *length); 79 | 80 | /* Inline functions for working with the message header struct */ 81 | static _inline_ void msg_init_header(msg_hdr_t *hdr, uint16_t client_id, 82 | uint8_t type, uint16_t len) 83 | { 84 | hdr->client_id = htons(client_id); 85 | hdr->type = type; 86 | hdr->length = htons(len); 87 | } 88 | 89 | static _inline_ uint16_t msg_get_client_id(msg_hdr_t *h) 90 | { 91 | return ntohs(h->client_id); 92 | } 93 | 94 | static _inline_ uint8_t msg_get_type(msg_hdr_t *h) 95 | { 96 | return h->type; 97 | } 98 | 99 | static _inline_ uint16_t msg_get_length(msg_hdr_t *h) 100 | { 101 | return ntohs(h->length); 102 | } 103 | 104 | #endif /* MESSAGE_H */ 105 | -------------------------------------------------------------------------------- /common.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Project: udptunnel 3 | * File: common.h 4 | * 5 | * Copyright (C) 2009 Daniel Meekins 6 | * Contact: dmeekins - gmail 7 | * 8 | * This program is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | * 13 | * This program is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | 22 | #ifndef COMMON_H 23 | #define COMMON_H 24 | 25 | #include 26 | #include 27 | 28 | #define NO_DEBUG 0 29 | #define DEBUG_LEVEL1 1 30 | #define DEBUG_LEVEL2 2 31 | #define DEBUG_LEVEL3 3 32 | 33 | #ifdef WIN32 34 | typedef unsigned char uint8_t; 35 | typedef unsigned short uint16_t; 36 | typedef unsigned long uint32_t; 37 | #endif 38 | 39 | /* cl.exe has a different 'inline' keyword for some dumb reason */ 40 | #ifdef WIN32 41 | #define _inline_ __inline 42 | #else 43 | #define _inline_ inline 44 | #endif 45 | 46 | #define PERROR_GOTO(cond,err,label){ \ 47 | if(cond) \ 48 | { \ 49 | if(debug_level >= DEBUG_LEVEL1) \ 50 | perror(err) ; \ 51 | goto label; \ 52 | }} 53 | 54 | #define ERROR_GOTO(cond,str,label){ \ 55 | if(cond) \ 56 | { \ 57 | if(debug_level >= DEBUG_LEVEL2) \ 58 | fprintf(stderr, "Error: %s\n", str); \ 59 | goto label; \ 60 | }} 61 | 62 | #define MAX(a,b) ((a) > (b) ? (a) : (b)) 63 | #define MIN(a,b) ((a) < (b) ? (a) : (b)) 64 | 65 | #ifdef SOLARIS 66 | /* Copied from sys/time.h on linux system since solaris system that I tried to 67 | * compile on didn't have timeradd macro. */ 68 | #define timeradd(a, b, result) \ 69 | do { \ 70 | (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \ 71 | (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \ 72 | if ((result)->tv_usec >= 1000000) \ 73 | { \ 74 | ++(result)->tv_sec; \ 75 | (result)->tv_usec -= 1000000; \ 76 | } \ 77 | } while (0) 78 | #endif /* SOLARIS */ 79 | 80 | static _inline_ int isnum(char *s) 81 | { 82 | for(; *s != '\0'; s++) 83 | { 84 | if(!isdigit((int)*s)) 85 | return 0; 86 | } 87 | 88 | return 1; 89 | } 90 | 91 | #endif /* COMMON_H */ 92 | -------------------------------------------------------------------------------- /udptunnel.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Project: udptunnel 3 | * File: udptunnel.c 4 | * 5 | * Copyright (C) 2009 Daniel Meekins 6 | * Contact: dmeekins - gmail 7 | * 8 | * This program is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | * 13 | * This program is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | 22 | #include 23 | #include 24 | 25 | #ifndef WIN32 26 | #include 27 | #else 28 | #include "helpers/winhelpers.h" 29 | #endif 30 | 31 | #include "common.h" 32 | #include "socket.h" 33 | 34 | int debug_level = NO_DEBUG; 35 | int ipver = SOCK_IPV4; 36 | 37 | int udpclient(int argc, char *argv[]); 38 | int udpserver(int argc, char *argv[]); 39 | void usage(char *progname); 40 | 41 | int main(int argc, char *argv[]) 42 | { 43 | int ret; 44 | int isserv = 0; 45 | 46 | #ifdef WIN32 47 | WSADATA wsa_data; 48 | ret = WSAStartup(MAKEWORD(2,0), &wsa_data); 49 | ERROR_GOTO(ret != 0, "WSAStartup() failed", error); 50 | #endif 51 | 52 | while((ret = getopt(argc, argv, "hscv6")) != EOF) 53 | { 54 | switch(ret) 55 | { 56 | case '6': 57 | ipver = SOCK_IPV6; 58 | break; 59 | 60 | case 's': 61 | isserv = 1; 62 | break; 63 | 64 | case 'c': 65 | isserv = 0; 66 | break; 67 | 68 | case 'v': 69 | if(debug_level < 3) 70 | debug_level++; 71 | break; 72 | 73 | case 'h': 74 | /* fall through */ 75 | default: 76 | goto error; 77 | } 78 | } 79 | 80 | ret = 0; 81 | 82 | if(isserv) 83 | { 84 | if(argc - optind < 1) 85 | goto error; 86 | ret = udpserver(argc - optind, argv + optind); 87 | } 88 | else 89 | { 90 | if(argc - optind != 5 && argc - optind != 6) 91 | goto error; 92 | ret = udpclient(argc - optind, argv + optind); 93 | } 94 | 95 | #ifdef WIN32 96 | WSACleanup(); 97 | #endif 98 | 99 | return ret; 100 | 101 | error: 102 | usage(argv[0]); 103 | exit(1); 104 | } 105 | 106 | void usage(char *progname) 107 | { 108 | printf("usage: %s [-v] [-6] <-s|-c> \n", progname); 109 | printf(" -c client mode (default)\n" 110 | " : [local host] \n" 111 | " \n" 112 | " -s server mode\n" 113 | " : [host] port [acl ...]\n" 114 | " acl: [s=,][d=,][dp=,][a=allow|deny]\n" 115 | " -6 use IPv6\n" 116 | " -v show some debugging output (use up to 3 for increaing levels)\n" 117 | " -h show this junks and exit\n"); 118 | } 119 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | ============================================================================= 2 | 3 | UDPTUNNEL README 4 | 5 | Author: Daniel Meekins 6 | Contact: dmeekins - gmail 7 | 8 | Copyright (C) 2009 Daniel Meekins 9 | 10 | ============================================================================= 11 | 12 | This project tunnels TCP data through a UDP tunnel. The executable can act as 13 | the server or client. The server acts as a proxy for the client, listening 14 | on a specified UDP port and creating a connection to a TCP server that the 15 | client specifies. The client listens on a TCP port, acting as the server that 16 | some TCP client connects to. The client recevies any TCP data on that port 17 | and sends the data to the udpserver, which sends it to the TCP connection it 18 | made with the desired TCP server. 19 | 20 | 21 | ----------------------------------------------------------------------------- 22 | 1.) Building 23 | 24 | On *nix systems in Makefile, make sure that the correct value is set for the 25 | "OS" variable. Then just run 'make'. 26 | 27 | On Windows, if using GCC in Cygwin, make sure the "OS" variable is set to 28 | CYGWIN in Makefile and run 'make'. 29 | 30 | If using the VC++ compiler (cl.exe), make sure to be in the "Visual Studio 31 | Command Prompt", or at least have all the environment variables set correctly, 32 | then run 'nmake.exe /f Makefile.Win32'. Also make sure the location of 33 | WS2_32.Lib is specified correctly fo the LIBS variable (if building for x64, 34 | set path to the 64-bit version of WS2_32.Lib). 35 | 36 | 37 | ----------------------------------------------------------------------------- 38 | 2.) Running 39 | 40 | usage: ./udptunnel [-v] [-6] <-s|-c> 41 | -c client mode (default) 42 | : [local host] 43 | 44 | -s server mode 45 | : [host] port [acl ...] 46 | acl: [s=,][d=,][dp=,][a=allow|deny] 47 | -6 use IPv6 48 | -v show some debugging output (use up to 3 for increaing levels) 49 | -h show this junks and exit 50 | 51 | 52 | To run the server: 53 | 54 | udptunnel -s [-6] [host] port [acl ...] 55 | 56 | where the port is a UDP port to listen for messages from the udpclient and host 57 | is the address to listen on. Use the -6 option to listen on IPv6 addresses. 58 | 59 | After the port, the rest of the arguments are the access control list, with 60 | comma-separated parameters for each entry. Access can be control based on the 61 | source IP, destination IP, source port, and destination port. A catch-all 62 | entry is added at the end of every list that allows everything. To deny anything 63 | that doesn't match your entries, put "a=deny" as the last entry. In a Window's 64 | shell, each acl entry needs double quotes (") around them. 65 | 66 | Server Examples: 67 | udptunnel -s 4444 68 | udptunnel -s -6 2001::10:3 4444 69 | udptunnel -s 4444 d=10.0.0.1,dport=80,a=deny dport=80,a=allow a=deny 70 | 71 | 72 | To run the client: 73 | 74 | udptunnel -c [-6] [local host] 75 | 76 | 77 | local host/port - Host and port for the TCP server to listen on. If the host 78 | isn't supplied, it will listen on all available addresses. 79 | proxy host/port - Host and port that udpserver is listening on. 80 | remote host/port - Host and port to forward the received TCP data to. The host 81 | is relative to the proxy machine (e.g. specifiying 127.0.0.1 82 | is the proxy machine itself). 83 | Use the -6 option to listen and connect using IPv6 addresses. 84 | 85 | Example for tunneling ssh data through the tunnel between two computers with IP 86 | addresses 192.168.1.2 (client) and 192.168.1.1 (server): 87 | 88 | server# ./udptunnel -s 192.168.1.1 4444 89 | client# ./udptunnel -c 127.0.0.1 3333 192.168.1.1 4444 127.0.0.1 22 90 | client# ssh -p 3333 user@127.0.0.1 91 | 92 | 93 | This code has been tested and works on Linux, Solaris 10 x86, and Cygwin (but 94 | requires the IPv6 extension - http://win6.jp/Cygwin/index.html). Please send 95 | any bugs or issues to the contact listed above. 96 | -------------------------------------------------------------------------------- /message.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Project: udptunnel 3 | * File: message.c 4 | * 5 | * Copyright (C) 2009 Daniel Meekins 6 | * Contact: dmeekins - gmail 7 | * 8 | * This program is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | * 13 | * This program is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | 22 | #include 23 | #include 24 | 25 | #ifndef WIN32 26 | #include 27 | #include 28 | #endif /*WIN32*/ 29 | 30 | #include "common.h" 31 | #include "message.h" 32 | #include "socket.h" 33 | 34 | /* 35 | * Sends a message to the UDP tunnel with the specified client ID, type, and 36 | * data. The data can be NULL and data_len 0 if the type of message won't have 37 | * a body, based on the protocol. 38 | * Returns 0 for success, -1 on error, or -2 to close the connection. 39 | */ 40 | int msg_send_msg(socket_t *to, uint16_t client_id, uint8_t type, 41 | char *data, int data_len) 42 | { 43 | char buf[MSG_MAX_LEN + sizeof(msg_hdr_t)]; 44 | int len; /* length for entire packet */ 45 | 46 | if(data_len > MSG_MAX_LEN) 47 | return -1; 48 | 49 | switch(type) 50 | { 51 | case MSG_TYPE_HELLO: 52 | case MSG_TYPE_HELLOACK: 53 | case MSG_TYPE_DATA0: 54 | case MSG_TYPE_DATA1: 55 | memcpy(buf+sizeof(msg_hdr_t), data, data_len); 56 | break; 57 | 58 | case MSG_TYPE_GOODBYE: 59 | case MSG_TYPE_KEEPALIVE: 60 | case MSG_TYPE_ACK0: 61 | case MSG_TYPE_ACK1: 62 | data_len = 0; 63 | break; 64 | 65 | default: 66 | return -1; 67 | } 68 | 69 | len = data_len + sizeof(msg_hdr_t); 70 | msg_init_header((msg_hdr_t *)buf, client_id, type, data_len); 71 | 72 | len = sock_send(to, buf, len); 73 | if(len < 0) 74 | return -1; 75 | else if(len == 0) 76 | return -2; 77 | else 78 | return 0; 79 | } 80 | 81 | /* 82 | * Sends a HELLO type message to the UDP tunnel with the specified host and 83 | * port in the body. 84 | * Returns 0 for success, -1 on error, or -2 to disconnect. 85 | */ 86 | int msg_send_hello(socket_t *to, char *host, char *port, uint16_t req_id) 87 | { 88 | char *data; 89 | int str_len; 90 | int len; 91 | 92 | str_len = strlen(host) + strlen(port) + 2; 93 | len = str_len + sizeof(req_id); 94 | 95 | data = malloc(len); 96 | if(!data) 97 | return -1; 98 | 99 | *((uint16_t *)data) = htons(req_id); 100 | 101 | #ifdef WIN32 102 | _snprintf(data + sizeof(req_id), str_len, "%s %s", host, port); 103 | #else 104 | snprintf(data + sizeof(req_id), str_len, "%s %s", host, port); 105 | #endif 106 | 107 | len = msg_send_msg(to, 0, MSG_TYPE_HELLO, data, len-1); 108 | free(data); 109 | 110 | if(len < 0) 111 | return -1; 112 | else if(len == 0) 113 | return -2; 114 | else 115 | return 0; 116 | } 117 | 118 | /* 119 | * Receives a message that is ready to be read from the UDP socket. Writes the 120 | * body of the message into data, and sets the client ID, type, and length 121 | * of the message. 122 | * Returns 0 for success, -1 on error, or -2 to disconnect. 123 | */ 124 | int msg_recv_msg(socket_t *sock, socket_t *from, char *data, int data_len, 125 | uint16_t *client_id, uint8_t *type, uint16_t *length) 126 | { 127 | char buf[MSG_MAX_LEN + sizeof(msg_hdr_t)]; 128 | msg_hdr_t *hdr_ptr; 129 | char *msg_ptr; 130 | int ret; 131 | 132 | hdr_ptr = (msg_hdr_t *)buf; 133 | msg_ptr = buf + sizeof(msg_hdr_t); 134 | 135 | ret = sock_recv(sock, from, buf, sizeof(buf)); 136 | if(ret < 0) 137 | return -1; 138 | else if(ret == 0) 139 | return -2; 140 | 141 | *client_id = msg_get_client_id(hdr_ptr); 142 | *type = msg_get_type(hdr_ptr); 143 | *length = msg_get_length(hdr_ptr); 144 | 145 | if(ret-sizeof(msg_hdr_t) != *length) 146 | return -1; 147 | 148 | *length = MIN(data_len, *length); 149 | memcpy(data, msg_ptr, *length); 150 | 151 | return 0; 152 | } 153 | -------------------------------------------------------------------------------- /client.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Project: udptunnel 3 | * File: client.h 4 | * 5 | * Copyright (C) 2009 Daniel Meekins 6 | * Contact: dmeekins - gmail 7 | * 8 | * This program is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | * 13 | * This program is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | 22 | #ifndef CLIENT_H 23 | #define CLIENT_H 24 | 25 | #ifndef WIN32 26 | #include 27 | #include 28 | #endif /*WIN32*/ 29 | 30 | #include "common.h" 31 | #include "socket.h" 32 | #include "message.h" 33 | #include "list.h" 34 | 35 | #define CLIENT_TIMEOUT 1 /* in seconds */ 36 | #define CLIENT_MAX_RESEND 10 37 | 38 | #define CLIENT_WAIT_HELLO 1 39 | #define CLIENT_WAIT_DATA0 2 40 | #define CLIENT_WAIT_DATA1 3 41 | #define CLIENT_WAIT_ACK0 4 42 | #define CLIENT_WAIT_ACK1 5 43 | 44 | typedef struct client 45 | { 46 | uint16_t id; /* Must be first in struct */ 47 | socket_t *tcp_sock; /* Socket for connection to TCP server */ 48 | socket_t *udp_sock; /* Socket to hold address from UDP client */ 49 | int connected; 50 | int to_disconnect; 51 | struct timeval keepalive; 52 | 53 | /* For data going from UDP tunnel to TCP connection */ 54 | char udp2tcp[MSG_MAX_LEN]; 55 | int udp2tcp_len; 56 | int udp2tcp_state; 57 | 58 | /* For data going from TCP connection to UDP tunnel */ 59 | list_t *tcp2udp_q; 60 | int tcp2udp_state; 61 | struct timeval tcp2udp_timeout; 62 | int resend_count; 63 | } client_t; 64 | 65 | #define CLIENT_ID(c) ((c)->id) 66 | #define CLIENT_UDP_SOCK(c) ((c)->udp_sock) 67 | #define CLIENT_TCP_SOCK(c) ((c)->tcp_sock) 68 | 69 | client_t *client_create(uint16_t id, socket_t *tcp_sock, socket_t *udp_sock, 70 | int connected); 71 | client_t *client_copy(client_t *dst, client_t *src, size_t len); 72 | int client_cmp(client_t *c1, client_t *c2, size_t len); 73 | int client_connect_tcp(client_t *c); 74 | void client_disconnect_tcp(client_t *c); 75 | void client_disconnect_udp(client_t *c); 76 | void client_free(client_t *c); 77 | int client_recv_udp_msg(client_t *client, char *data, int data_len, 78 | uint16_t *id, uint8_t *msg_type, uint16_t *len); 79 | int client_got_udp_data(client_t *client, char *data, int data_len, 80 | uint8_t msg_type); 81 | int client_send_tcp_data(client_t *client); 82 | int client_recv_tcp_data(client_t *client); 83 | int client_send_udp_data(client_t *client); 84 | int client_got_ack(client_t *client, uint8_t ack_type); 85 | int client_send_hello(client_t *client, char *host, char *port, 86 | uint16_t req_id); 87 | int client_send_helloack(client_t *client, uint16_t req_id); 88 | int client_got_helloack(client_t *client); 89 | int client_send_goodbye(client_t *client); 90 | int client_check_and_resend(client_t *client, struct timeval curr_tv); 91 | int client_check_and_send_keepalive(client_t *client, struct timeval curr_tv); 92 | void client_reset_keepalive(client_t *client); 93 | int client_timed_out(client_t *client, struct timeval curr_tv); 94 | 95 | /* Function pointers to use when making a list_t of clients */ 96 | #define p_client_copy ((void* (*)(void *, const void *, size_t))&client_copy) 97 | #define p_client_cmp ((int (*)(const void *, const void *, size_t))&client_cmp) 98 | #define p_client_free ((void (*)(void *))&client_free) 99 | 100 | /* Inline functions as wrappers for handling the file descriptors in the 101 | * client's sockets */ 102 | 103 | static _inline_ void client_add_tcp_fd_to_set(client_t *c, fd_set *set) 104 | { 105 | if(SOCK_FD(c->tcp_sock) >= 0) 106 | FD_SET(SOCK_FD(c->tcp_sock), set); 107 | } 108 | 109 | static _inline_ void client_add_udp_fd_to_set(client_t *c, fd_set *set) 110 | { 111 | if(SOCK_FD(c->udp_sock) >= 0) 112 | FD_SET(SOCK_FD(c->udp_sock), set); 113 | } 114 | 115 | static _inline_ int client_tcp_fd_isset(client_t *c, fd_set *set) 116 | { 117 | return SOCK_FD(c->tcp_sock) >= 0 ? FD_ISSET(SOCK_FD(c->tcp_sock), set) : 0; 118 | } 119 | 120 | static _inline_ int client_udp_fd_isset(client_t *c, fd_set *set) 121 | { 122 | return SOCK_FD(c->udp_sock) >= 0 ? FD_ISSET(SOCK_FD(c->udp_sock), set) : 0; 123 | } 124 | 125 | static _inline_ void client_remove_tcp_fd_from_set(client_t *c, fd_set *set) 126 | { 127 | if(SOCK_FD(c->tcp_sock) >= 0) 128 | FD_CLR(SOCK_FD(c->tcp_sock), set); 129 | } 130 | 131 | static _inline_ void client_remove_udp_fd_from_set(client_t *c, fd_set *set) 132 | { 133 | if(SOCK_FD(c->udp_sock) >= 0) 134 | FD_CLR(SOCK_FD(c->udp_sock), set); 135 | } 136 | 137 | static _inline_ void client_mark_to_disconnect(client_t *c) 138 | { 139 | c->to_disconnect = 1; 140 | } 141 | 142 | static _inline_ int client_ready_to_disconnect(client_t *c) 143 | { 144 | return (c->to_disconnect == 1 && LIST_LEN(c->tcp2udp_q) == 0); 145 | } 146 | 147 | #endif /* CLIENT_H */ 148 | -------------------------------------------------------------------------------- /acl.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Project: udptunnel 3 | * File: acl.c 4 | * 5 | * Copyright (C) 2011 Daniel Meekins 6 | * Contact: dmeekins - gmail 7 | * 8 | * Extended from Andreas Rottmann's (a.rottmann@gmx.at) work, which was 9 | * previously the "destination" module. 10 | * 11 | * This program is free software: you can redistribute it and/or modify 12 | * it under the terms of the GNU General Public License as published by 13 | * the Free Software Foundation, either version 3 of the License, or 14 | * (at your option) any later version. 15 | * 16 | * This program is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | * GNU General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU General Public License 22 | * along with this program. If not, see . 23 | */ 24 | 25 | #include 26 | #include 27 | #include "socket.h" 28 | #include "acl.h" 29 | 30 | #ifdef WIN32 31 | #include "helpers/winhelpers.h" 32 | #endif /*WIN32*/ 33 | 34 | extern int debug_level; 35 | 36 | /* 37 | * Creates an ACL entry given the line of the following format: 38 | * s=src,d=dst,dp=dport,a=allow|deny 39 | * Can only be one of either IPv4 or IPv6. Currently src port is ignored. 40 | */ 41 | acl_t *acl_create(char *acl_entry, int ipver) 42 | { 43 | char *line = NULL; 44 | char *saveptr = NULL; 45 | char *tok; 46 | char *param, *arg; 47 | char *pdst = NULL; 48 | char *psrc = NULL; 49 | char *pdp = NULL; 50 | char *psp = NULL; 51 | int i; 52 | acl_t *acl = NULL; 53 | 54 | line = malloc(strlen(acl_entry) + 1); 55 | ERROR_GOTO(line == NULL, "Allocating memory error", error); 56 | 57 | acl = calloc(1, sizeof(*acl)); 58 | ERROR_GOTO(acl == NULL, "Allocating memory error", error); 59 | 60 | acl->action = ACL_ACTION_ALLOW; 61 | strncpy(line, acl_entry, strlen(acl_entry) + 1); 62 | 63 | /* go through line, getting each param=arg separated by a ',' */ 64 | for(tok = strtok_r(line, ",", &saveptr); 65 | tok != NULL; 66 | tok = strtok_r(NULL, ",", &saveptr)) 67 | { 68 | param = tok; 69 | 70 | /* find the arg */ 71 | for(i = 0; i < strlen(tok); i++) 72 | { 73 | if(tok[i] == '=') 74 | break; 75 | } 76 | 77 | ERROR_GOTO(i == strlen(tok), "Invalid acl entry", error); 78 | tok[i] = '\0'; 79 | arg = &tok[i + 1]; 80 | 81 | if(strcmp(param, "s") == 0) 82 | { 83 | ERROR_GOTO(psrc != NULL, "Source IP already specified", error); 84 | psrc = arg; 85 | } 86 | else if(strcmp(param, "d") == 0) 87 | { 88 | ERROR_GOTO(pdst != NULL, "Destination IP already specified", error); 89 | pdst = arg; 90 | } 91 | else if(strcmp(param, "sp") == 0) 92 | { 93 | ERROR_GOTO(psp != 0, "Source port already specified", error); 94 | ERROR_GOTO(!isnum(arg), "Invalid port format", error); 95 | psp = arg; 96 | } 97 | else if(strcmp(param, "dp") == 0) 98 | { 99 | ERROR_GOTO(pdp != 0, "Destination port already specified", error); 100 | ERROR_GOTO(!isnum(arg), "Invalid port format", error); 101 | pdp = arg; 102 | } 103 | else if(strcmp(param, "a") == 0) 104 | { 105 | for(i = 0; i < strlen(arg); i++) 106 | arg[i] = tolower((int)arg[i]); 107 | 108 | if(strcmp(arg, "allow") == 0) 109 | acl->action = ACL_ACTION_ALLOW; 110 | else if(strcmp(arg, "deny") == 0) 111 | acl->action = ACL_ACTION_DENY; 112 | else 113 | ERROR_GOTO(1, "Invalid ACL action", error); 114 | } 115 | else 116 | { 117 | ERROR_GOTO(1, "Invalid ACL parameter", error); 118 | } 119 | } 120 | 121 | acl->src = sock_create(psrc, psp, ipver, SOCK_TYPE_TCP, 1, 0); 122 | ERROR_GOTO(acl->src == NULL, "Couldn't create acl->src", error); 123 | 124 | acl->dst = sock_create(pdst, pdp, ipver, SOCK_TYPE_TCP, 1, 0); 125 | ERROR_GOTO(acl->dst == NULL, "Couldn't create acl->dst", error); 126 | 127 | free(line); 128 | 129 | return acl; 130 | 131 | error: 132 | if(line) 133 | free(line); 134 | if(acl) 135 | acl_free(acl); 136 | 137 | return NULL; 138 | } 139 | 140 | /* 141 | * Frees an acl struct 142 | */ 143 | void acl_free(acl_t *acl) 144 | { 145 | if(acl->src) 146 | sock_free(acl->src); 147 | if(acl->dst) 148 | sock_free(acl->dst); 149 | free(acl); 150 | } 151 | 152 | /* 153 | * Returns the action if the arguments match the given ACL. 154 | */ 155 | int acl_action(acl_t *acl, char *src, uint16_t sport, char *dst, uint16_t dport) 156 | { 157 | socket_t *s = NULL; 158 | socket_t *d = NULL; 159 | uint16_t p; 160 | int ret; 161 | 162 | ret = ACL_ACTION_NOMATCH; 163 | 164 | s = sock_create(src, NULL, sock_get_ipver(acl->src), 165 | SOCK_TYPE(acl->src), 0, 0); 166 | if(s == NULL) 167 | goto done; 168 | 169 | d = sock_create(dst, NULL, sock_get_ipver(acl->dst), 170 | SOCK_TYPE(acl->dst), 0, 0); 171 | if(d == NULL) 172 | goto done; 173 | 174 | p = sock_get_port(acl->src); 175 | if(p != 0 && p != sport) 176 | goto done; 177 | 178 | p = sock_get_port(acl->dst); 179 | if(p != 0 && p != dport) 180 | goto done; 181 | 182 | if(!sock_isaddrany(acl->src) && sock_ipaddr_cmp(acl->src, s) != 0) 183 | goto done; 184 | 185 | if(!sock_isaddrany(acl->dst) && sock_ipaddr_cmp(acl->dst, d) != 0) 186 | goto done; 187 | 188 | ret = acl->action; 189 | 190 | done: 191 | if(s) 192 | sock_free(s); 193 | if(d) 194 | sock_free(d); 195 | 196 | return ret; 197 | } 198 | 199 | /* 200 | * For debugging, prints the ACL in the same format as acl_create expects 201 | */ 202 | void acl_print(acl_t *acl) 203 | { 204 | char addr_str[ADDRSTRLEN]; 205 | int len; 206 | uint16_t port; 207 | 208 | len = sizeof(addr_str); 209 | 210 | if(!sock_isaddrany(acl->src)) 211 | { 212 | sock_get_addrstr(acl->src, addr_str, len); 213 | printf("s=%s,", addr_str); 214 | } 215 | 216 | if((port = sock_get_port(acl->src)) != 0) 217 | printf("sp=%hu,", port); 218 | 219 | if(!sock_isaddrany(acl->dst)) 220 | { 221 | sock_get_addrstr(acl->dst, addr_str, len); 222 | printf("d=%s,", addr_str); 223 | } 224 | 225 | if((port = sock_get_port(acl->dst)) != 0) 226 | printf("dp=%hu,", port); 227 | 228 | switch(acl->action) 229 | { 230 | case ACL_ACTION_ALLOW: 231 | printf("a=allow\n"); 232 | break; 233 | 234 | case ACL_ACTION_DENY: 235 | printf("a=deny\n"); 236 | break; 237 | 238 | default: 239 | printf("a=??\n"); 240 | break; 241 | } 242 | } 243 | -------------------------------------------------------------------------------- /helpers/xgetopt.c: -------------------------------------------------------------------------------- 1 | // from http://yellosoft.svn.beanstalkapp.com/apps/IOS7Crypt/ 2 | // Note: made some changes so it compiles with cl.exe 3 | 4 | // XGetopt.cpp Version 1.2 5 | // 6 | // Author: Hans Dietrich 7 | // hdietrich2@hotmail.com 8 | // 9 | // Description: 10 | // XGetopt.cpp implements getopt(), a function to parse command lines. 11 | // 12 | // History 13 | // Version 1.2 - 2003 May 17 14 | // - Added Unicode support 15 | // 16 | // Version 1.1 - 2002 March 10 17 | // - Added example to XGetopt.cpp module header 18 | // 19 | // This software is released into the public domain. 20 | // You are free to use it in any way you like. 21 | // 22 | // This software is provided "as is" with no expressed 23 | // or implied warranty. I accept no liability for any 24 | // damage or loss of business that this software may cause. 25 | // 26 | /////////////////////////////////////////////////////////////////////////////// 27 | 28 | #include 29 | #include 30 | #include "winhelpers.h" 31 | 32 | /////////////////////////////////////////////////////////////////////////////// 33 | // 34 | // X G e t o p t . c p p 35 | // 36 | // 37 | // NAME 38 | // getopt -- parse command line options 39 | // 40 | // SYNOPSIS 41 | // int getopt(int argc, char *argv[], char *optstring) 42 | // 43 | // extern char *optarg; 44 | // extern int optind; 45 | // 46 | // DESCRIPTION 47 | // The getopt() function parses the command line arguments. Its 48 | // arguments argc and argv are the argument count and array as 49 | // passed into the application on program invocation. In the case 50 | // of Visual C++ programs, argc and argv are available via the 51 | // variables __argc and __argv (double underscores), respectively. 52 | // getopt returns the next option letter in argv that matches a 53 | // letter in optstring. (Note: Unicode programs should use 54 | // __targv instead of __argv. Also, all character and string 55 | // literals should be enclosed in _T( ) ). 56 | // 57 | // optstring is a string of recognized option letters; if a letter 58 | // is followed by a colon, the option is expected to have an argument 59 | // that may or may not be separated from it by white space. optarg 60 | // is set to point to the start of the option argument on return from 61 | // getopt. 62 | // 63 | // Option letters may be combined, e.g., "-ab" is equivalent to 64 | // "-a -b". Option letters are case sensitive. 65 | // 66 | // getopt places in the external variable optind the argv index 67 | // of the next argument to be processed. optind is initialized 68 | // to 0 before the first call to getopt. 69 | // 70 | // When all options have been processed (i.e., up to the first 71 | // non-option argument), getopt returns EOF, optarg will point 72 | // to the argument, and optind will be set to the argv index of 73 | // the argument. If there are no non-option arguments, optarg 74 | // will be set to NULL. 75 | // 76 | // The special option "--" may be used to delimit the end of the 77 | // options; EOF will be returned, and "--" (and everything after it) 78 | // will be skipped. 79 | // 80 | // RETURN VALUE 81 | // For option letters contained in the string optstring, getopt 82 | // will return the option letter. getopt returns a question mark (?) 83 | // when it encounters an option letter not included in optstring. 84 | // EOF is returned when processing is finished. 85 | // 86 | // BUGS 87 | // 1) Long options are not supported. 88 | // 2) The GNU double-colon extension is not supported. 89 | // 3) The environment variable POSIXLY_CORRECT is not supported. 90 | // 4) The + syntax is not supported. 91 | // 5) The automatic permutation of arguments is not supported. 92 | // 6) This implementation of getopt() returns EOF if an error is 93 | // encountered, instead of -1 as the latest standard requires. 94 | // 95 | // EXAMPLE 96 | // BOOL CMyApp::ProcessCommandLine(int argc, char *argv[]) { 97 | // int c; 98 | // 99 | // while ((c = getopt(argc, argv, _T("aBn:"))) != EOF) { 100 | // switch (c) { 101 | // case _T('a'): 102 | // TRACE(_T("option a\n")); 103 | // // 104 | // // set some flag here 105 | // // 106 | // break; 107 | // 108 | // case _T('B'): 109 | // TRACE( _T("option B\n")); 110 | // // 111 | // // set some other flag here 112 | // // 113 | // break; 114 | // 115 | // case _T('n'): 116 | // TRACE(_T("option n: value=%d\n"), atoi(optarg)); 117 | // // 118 | // // do something with value here 119 | // // 120 | // break; 121 | // 122 | // case _T('?'): 123 | // TRACE(_T("ERROR: illegal option %s\n"), argv[optind-1]); 124 | // return FALSE; 125 | // break; 126 | // 127 | // default: 128 | // TRACE(_T("WARNING: no handler for option %c\n"), c); 129 | // return FALSE; 130 | // break; 131 | // } 132 | // } 133 | // // 134 | // // check for non-option args here 135 | // // 136 | // return TRUE; 137 | // } 138 | // 139 | /////////////////////////////////////////////////////////////////////////////// 140 | 141 | char *optarg = NULL; // global argument pointer 142 | int optind = 0; // global argv index 143 | 144 | int getopt(int argc, char *argv[], char *optstring) { 145 | static char *next = NULL; 146 | char c; 147 | char *cp = NULL; 148 | 149 | if (optind == 0) 150 | next = NULL; 151 | 152 | optarg = NULL; 153 | 154 | if (next == NULL || *next == '\0') { 155 | if (optind == 0) 156 | optind++; 157 | 158 | if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0') { 159 | optarg = NULL; 160 | if (optind < argc) 161 | optarg = argv[optind]; 162 | return EOF; 163 | } 164 | 165 | if (strncmp(argv[optind], "--", 2) == 0) { 166 | optind++; 167 | optarg = NULL; 168 | if (optind < argc) 169 | optarg = argv[optind]; 170 | return EOF; 171 | } 172 | 173 | next = argv[optind]; 174 | next++; // skip past - 175 | optind++; 176 | } 177 | 178 | c = *next++; 179 | cp = strchr(optstring, c); 180 | 181 | if (cp == NULL || c == ':') 182 | return '?'; 183 | 184 | cp++; 185 | if (*cp == ':') { 186 | if (*next != '\0') { 187 | optarg = next; 188 | next = NULL; 189 | } 190 | else if (optind < argc) { 191 | optarg = argv[optind]; 192 | optind++; 193 | } 194 | else { 195 | return '?'; 196 | } 197 | } 198 | 199 | return c; 200 | } 201 | -------------------------------------------------------------------------------- /list.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Project: udptunnel 3 | * File: list.c 4 | * 5 | * Copyright (C) 2009 Daniel Meekins 6 | * Contact: dmeekins - gmail 7 | * 8 | * This program is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | * 13 | * This program is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | 22 | #include 23 | #include 24 | #include "list.h" 25 | 26 | /* 27 | * Allocates and initializes a new list type to hold objects of obj_sz bytes, 28 | * and uses the cmp, copy, and free functions. If any of the function pointers 29 | * are NULL, they will be set to memcmp, memcpy, or free. 30 | */ 31 | list_t *list_create(int obj_sz, 32 | int (*obj_cmp)(const void *, const void *, size_t), 33 | void* (*obj_copy)(void *, const void *, size_t), 34 | void (*obj_free)(void *), int sort) 35 | { 36 | list_t *new; 37 | 38 | new = malloc(sizeof(*new)); 39 | if(!new) 40 | return NULL; 41 | 42 | new->obj_arr = malloc(LIST_INIT_SIZE * sizeof(void *)); 43 | if(!new->obj_arr) 44 | { 45 | free(new); 46 | return NULL; 47 | } 48 | 49 | new->obj_sz = obj_sz; 50 | new->num_objs = 0; 51 | new->length = LIST_INIT_SIZE; 52 | new->sort = sort; 53 | new->obj_cmp = obj_cmp ? obj_cmp : &memcmp; 54 | new->obj_copy = obj_copy ? obj_copy : &memcpy; 55 | new->obj_free = obj_free ? obj_free : &free; 56 | 57 | return new; 58 | } 59 | 60 | /* 61 | * Inserts a new object into the list in sorted order (if set). If specified, 62 | * makes a deep copy of the object and returns a pointer to that new object if 63 | * obj wasn't already in the list, or a pointer to the object already in the 64 | * list. 65 | */ 66 | void *list_add(list_t *list, void *obj, int copy) 67 | { 68 | void *o; 69 | void **new_arr; 70 | void *temp; 71 | int i; 72 | 73 | /* Check if obj is already in the list, and return it if so */ 74 | o = list_get(list, obj); 75 | if(o) 76 | return o; 77 | 78 | if(copy == 1) 79 | { 80 | o = malloc(list->obj_sz); 81 | if(o == NULL) 82 | return NULL; 83 | 84 | list->obj_copy(o, obj, list->obj_sz); 85 | } 86 | else 87 | { 88 | o = obj; 89 | } 90 | 91 | /* Resize the object array if needed, doubling the size */ 92 | if(list->num_objs == list->length) 93 | { 94 | new_arr = realloc(list->obj_arr, list->length * 2 * sizeof(void *)); 95 | if(!new_arr) 96 | { 97 | list->obj_free(o); 98 | return NULL; 99 | } 100 | 101 | list->obj_arr = new_arr; 102 | list->length *= 2; 103 | } 104 | 105 | /* Insert new object at end of array */ 106 | list->obj_arr[list->num_objs++] = o; 107 | 108 | if(list->sort == 1) 109 | { 110 | /* Move object up in array until list is sorted again */ 111 | for(i = list->num_objs-1; i > 0; i--) 112 | { 113 | if(list->obj_cmp(list->obj_arr[i-1], list->obj_arr[i], 114 | list->obj_sz) > 0) 115 | { 116 | temp = list->obj_arr[i-1]; 117 | list->obj_arr[i-1] = list->obj_arr[i]; 118 | list->obj_arr[i] = temp; 119 | } 120 | else 121 | break; /* since was sorted before, just need to go this far */ 122 | } 123 | } 124 | 125 | return o; 126 | } 127 | 128 | /* 129 | * Returns a pointer to the object that matches the one passed, or NULL if 130 | * one wasn't found. Does a simple linear search for now. 131 | */ 132 | void *list_get(list_t *list, void *obj) 133 | { 134 | int i; 135 | 136 | i = list_get_index(list, obj); 137 | if(i == -1) 138 | return NULL; 139 | 140 | return list->obj_arr[i]; 141 | } 142 | 143 | /* 144 | * Returns a pointer to the object at position i in the list or NULL if i is 145 | * out of bounds. 146 | */ 147 | void *list_get_at(list_t *list, int i) 148 | { 149 | if(i >= list->num_objs || i < 0) 150 | return NULL; 151 | 152 | return list->obj_arr[i]; 153 | } 154 | 155 | /* 156 | * Gets the index of object that's equal to the passed object. 157 | */ 158 | int list_get_index(list_t *list, void *obj) 159 | { 160 | int i; 161 | 162 | for(i = 0; i < list->num_objs; i++) 163 | { 164 | if(list->obj_cmp(list->obj_arr[i], obj, list->obj_sz) == 0) 165 | return i; 166 | } 167 | 168 | return -1; 169 | } 170 | 171 | /* 172 | * Does a deep copy of src into a newly created list, and returns a pointer to 173 | * the new list. 174 | */ 175 | list_t *list_copy(list_t *src) 176 | { 177 | list_t *dst; 178 | int i; 179 | 180 | dst = malloc(sizeof(*dst)); 181 | if(!dst) 182 | return NULL; 183 | 184 | memcpy(dst, src, sizeof(*src)); 185 | 186 | /* Create the pointer array */ 187 | dst->obj_arr = malloc(sizeof(void *) * src->length); 188 | if(!dst->obj_arr) 189 | { 190 | free(dst); 191 | return NULL; 192 | } 193 | 194 | /* Make copies of all the objects in the src array */ 195 | for(i = 0; i < src->num_objs; i++) 196 | { 197 | dst->obj_arr[i] = malloc(dst->obj_sz); 198 | if(!dst->obj_arr[i]) 199 | { 200 | dst->num_objs = i; /* so only will free objs up to this point */ 201 | list_free(dst); 202 | return NULL; 203 | } 204 | 205 | dst->obj_copy(dst->obj_arr[i], src->obj_arr[i], dst->obj_sz); 206 | } 207 | 208 | return dst; 209 | } 210 | 211 | /* 212 | * Calls a function 'action', passing each object in the list to it, one at 213 | * a time. 214 | */ 215 | void list_action(list_t *list, void (*action)(void *)) 216 | { 217 | int i; 218 | 219 | for(i = 0; i < list->num_objs; i++) 220 | action(list->obj_arr[i]); 221 | } 222 | 223 | /* 224 | * Removes the object from the list that compares equally to obj. 225 | */ 226 | void list_delete(list_t *list, void *obj) 227 | { 228 | list_delete_at(list, list_get_index(list, obj)); 229 | } 230 | 231 | /* 232 | * Removes and frees an object from the list at the specified index 233 | */ 234 | void list_delete_at(list_t *list, int i) 235 | { 236 | if(i >= list->num_objs || i < 0) 237 | return; 238 | 239 | list->obj_free(list->obj_arr[i]); 240 | 241 | /* Shift the rest of the object pointers one to the left */ 242 | for(; i < list->num_objs - 1; i++) 243 | list->obj_arr[i] = list->obj_arr[i+1]; 244 | 245 | list->obj_arr[i] = NULL; 246 | list->num_objs--; 247 | } 248 | 249 | /* 250 | * Frees each element in the list and then the list and then the list struct 251 | * itself. 252 | */ 253 | void list_free(list_t *list) 254 | { 255 | int i; 256 | 257 | for(i = 0; i < list->num_objs; i++) 258 | list->obj_free(list->obj_arr[i]); 259 | 260 | free(list->obj_arr); 261 | free(list); 262 | } 263 | -------------------------------------------------------------------------------- /client.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Project: udptunnel 3 | * File: client.c 4 | * 5 | * Copyright (C) 2009 Daniel Meekins 6 | * Contact: dmeekins - gmail 7 | * 8 | * This program is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | * 13 | * This program is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | 22 | #include 23 | #include 24 | 25 | #ifndef WIN32 26 | #include 27 | #endif /*WIN32*/ 28 | 29 | #include "common.h" 30 | #include "client.h" 31 | #include "socket.h" 32 | 33 | extern int debug_level; 34 | 35 | /* 36 | * Allocates and initializes a new client object. 37 | * id - ID number for the client to have 38 | * tcp_sock/udp_sock - sockets attributed to the client. this function copies 39 | * the structure, so the calling function can free the sockets passed to 40 | * here. 41 | * connected - whether the TCP socket is connected or not. 42 | * Returns a pointer to the new structure. Call client_free() when done with 43 | * it. 44 | */ 45 | client_t *client_create(uint16_t id, socket_t *tcp_sock, socket_t *udp_sock, 46 | int connected) 47 | { 48 | client_t *c = NULL; 49 | 50 | c = calloc(1, sizeof(client_t)); 51 | if(!c) 52 | goto error; 53 | 54 | c->id = id; 55 | c->tcp_sock = sock_copy(tcp_sock); 56 | c->udp_sock = sock_copy(udp_sock); 57 | c->tcp2udp_q = list_create(sizeof(data_buf_t), NULL, NULL, NULL, 0); 58 | c->udp2tcp_state = CLIENT_WAIT_HELLO; 59 | c->tcp2udp_state = CLIENT_WAIT_DATA0; 60 | c->connected = connected; 61 | 62 | timerclear(&c->keepalive); 63 | timerclear(&c->tcp2udp_timeout); 64 | c->resend_count = 0; 65 | 66 | if(!c->tcp_sock || !c->udp_sock || !c->tcp2udp_q) 67 | goto error; 68 | 69 | return c; 70 | 71 | error: 72 | if(c) 73 | { 74 | if(c->tcp_sock) 75 | sock_free(c->tcp_sock); 76 | if(c->udp_sock) 77 | sock_free(c->udp_sock); 78 | if(c->tcp2udp_q) 79 | list_free(c->tcp2udp_q); 80 | free(c); 81 | } 82 | 83 | return NULL; 84 | } 85 | 86 | /* 87 | * Performs a deep copy of the client structure. 88 | */ 89 | client_t *client_copy(client_t *dst, client_t *src, size_t len) 90 | { 91 | if(!dst || !src) 92 | return NULL; 93 | 94 | memcpy(dst, src, sizeof(*src)); 95 | 96 | dst->tcp_sock = NULL; 97 | dst->udp_sock = NULL; 98 | dst->tcp2udp_q = NULL; 99 | 100 | dst->tcp_sock = sock_copy(src->tcp_sock); 101 | if(!dst->tcp_sock) 102 | goto error; 103 | 104 | dst->udp_sock = sock_copy(src->udp_sock); 105 | if(!dst->udp_sock) 106 | goto error; 107 | 108 | dst->tcp2udp_q = list_copy(src->tcp2udp_q); 109 | if(!dst->tcp2udp_q) 110 | goto error; 111 | 112 | return dst; 113 | 114 | error: 115 | if(dst->tcp_sock) 116 | sock_free(dst->tcp_sock); 117 | if(dst->udp_sock) 118 | sock_free(dst->udp_sock); 119 | if(dst->tcp2udp_q) 120 | list_free(dst->tcp2udp_q); 121 | 122 | return NULL; 123 | } 124 | 125 | /* 126 | * Compares the ID of the two clients. 127 | */ 128 | int client_cmp(client_t *c1, client_t *c2, size_t len) 129 | { 130 | return c1->id - c2->id; 131 | } 132 | 133 | /* 134 | * Connects the TCP socket of the client (wrapper for sock_connect()). Returns 135 | * 0 on success or -1 on error. 136 | */ 137 | int client_connect_tcp(client_t *c) 138 | { 139 | if(!c->connected) 140 | { 141 | if(sock_connect(c->tcp_sock, 0) == 0) 142 | { 143 | c->connected = 1; 144 | return 0; 145 | } 146 | } 147 | 148 | return -1; 149 | } 150 | 151 | /* 152 | * Closes the TCP socket for the client (wrapper for sock_close()). 153 | */ 154 | void client_disconnect_tcp(client_t *c) 155 | { 156 | if(c->connected) 157 | { 158 | sock_close(c->tcp_sock); 159 | c->connected = 0; 160 | } 161 | } 162 | 163 | /* 164 | * Closes the UDP socket for the client (wrapper for sock_close()). 165 | */ 166 | void client_disconnect_udp(client_t *c) 167 | { 168 | sock_close(c->udp_sock); 169 | } 170 | 171 | /* 172 | * Releases the memory used by the client. 173 | */ 174 | void client_free(client_t *c) 175 | { 176 | if(c) 177 | { 178 | if(debug_level >= DEBUG_LEVEL2) 179 | { 180 | printf("Freeing client id %d\n", c->id); 181 | printf(" q cnt left: %d\n", LIST_LEN(c->tcp2udp_q)); 182 | } 183 | 184 | sock_free(c->tcp_sock); 185 | sock_free(c->udp_sock); 186 | list_free(c->tcp2udp_q); 187 | free(c); 188 | } 189 | } 190 | 191 | /* 192 | * Receives a message from the UDP tunnel for the client. Only used in 193 | * udpclient program because each client has their own UDP socket. Returns 0 194 | * for success or -1 on error. The data is written to memory pointed to by 195 | * data, and the id, msg_type, and len are set from the message header. 196 | */ 197 | int client_recv_udp_msg(client_t *client, char *data, int data_len, 198 | uint16_t *id, uint8_t *msg_type, uint16_t *len) 199 | { 200 | int ret; 201 | socket_t from; 202 | 203 | ret = msg_recv_msg(client->udp_sock, &from, data, data_len, 204 | id, msg_type, len); 205 | if(ret < 0) 206 | return ret; 207 | 208 | if(!sock_addr_equal(client->udp_sock, &from)) 209 | return -1; 210 | 211 | return 0; 212 | } 213 | 214 | /* 215 | * Copy data to the internal buffer for sending to tcp connection and send ACK 216 | * back to tunnel. Returns 0 on success, 1 if this was "resending" data, -1 217 | * on error, or -2 if need to disconnect. 218 | */ 219 | int client_got_udp_data(client_t *client, char *data, int data_len, 220 | uint8_t msg_type) 221 | { 222 | int ret; 223 | int is_resend = 0; 224 | 225 | if(data_len > MSG_MAX_LEN) 226 | return -1; 227 | 228 | /* Check if got new data, which is when got the data type (DATA0 or DATA1) 229 | that it was waiting for, and write that new data to the buffer. */ 230 | if((msg_type == MSG_TYPE_DATA0 && 231 | client->udp2tcp_state == CLIENT_WAIT_DATA0) 232 | || (msg_type == MSG_TYPE_DATA1 && 233 | client->udp2tcp_state == CLIENT_WAIT_DATA1)) 234 | { 235 | memcpy(client->udp2tcp, data, data_len); 236 | client->udp2tcp_len = data_len; 237 | } 238 | else 239 | is_resend = 1; /* Otherwise, the other host resent the data */ 240 | 241 | msg_type = (msg_type == MSG_TYPE_DATA0) ? MSG_TYPE_ACK0 : MSG_TYPE_ACK1; 242 | 243 | /* Send the ACK for the data */ 244 | ret = msg_send_msg(client->udp_sock, client->id, msg_type, NULL, 0); 245 | if(ret < 0) 246 | return ret; 247 | 248 | if(is_resend) 249 | return 1; 250 | 251 | /* Set the state to wait for the next type of data */ 252 | client->udp2tcp_state = client->udp2tcp_state == CLIENT_WAIT_DATA0 ? 253 | CLIENT_WAIT_DATA1 : CLIENT_WAIT_DATA0; 254 | 255 | return 0; 256 | } 257 | 258 | /* 259 | * Send data received from UDP tunnel to TCP connection. Need to call 260 | * client_got_udp_data() first. Returns -1 on general error, -2 if need to 261 | * disconnect, and 0 on success. 262 | */ 263 | int client_send_tcp_data(client_t *client) 264 | { 265 | int ret; 266 | 267 | ret = sock_send(client->tcp_sock, client->udp2tcp, client->udp2tcp_len); 268 | 269 | if(ret < 0) 270 | return -1; 271 | else if(ret == 0) 272 | return -2; 273 | else 274 | return 0; 275 | } 276 | 277 | /* 278 | * Reads data that is ready on the TCP socket and stores it in the internal 279 | * buffer. The routine client_send_udp_data() send that data to the tunnel. 280 | */ 281 | int client_recv_tcp_data(client_t *client) 282 | { 283 | int ret; 284 | data_buf_t buf; 285 | 286 | ret = sock_recv(client->tcp_sock, NULL, buf.buf, sizeof(buf.buf)); 287 | if(ret < 0) 288 | return -1; 289 | if(ret == 0) 290 | return -2; 291 | 292 | buf.len = ret; 293 | list_add(client->tcp2udp_q, &buf, 1); 294 | 295 | return 0; 296 | } 297 | 298 | /* 299 | * Sends the data in the tcp2udp buffer to the UDP tunnel. Returns 0 for 300 | * success, -1 on error, and -2 if needs to disconnect. 301 | */ 302 | int client_send_udp_data(client_t *client) 303 | { 304 | data_buf_t *buf; 305 | uint8_t msg_type; 306 | int ret; 307 | 308 | if(LIST_LEN(client->tcp2udp_q) == 0) 309 | return 0; 310 | 311 | if(client->resend_count >= CLIENT_MAX_RESEND) 312 | return -2; 313 | 314 | /* Don't send the tcp data yet if waiting for an ack or the hello */ 315 | if(client->tcp2udp_state == CLIENT_WAIT_ACK0 || 316 | client->tcp2udp_state == CLIENT_WAIT_ACK1 || 317 | client->udp2tcp_state == CLIENT_WAIT_HELLO) 318 | return 1; 319 | 320 | /* Set the message type it is sending. If the client is in the WAIT_ACK 321 | state, then it will send the same type of data again (since this would 322 | have been called b/c of a timeout. */ 323 | switch(client->tcp2udp_state) 324 | { 325 | case CLIENT_WAIT_DATA0: 326 | case CLIENT_WAIT_ACK0: 327 | msg_type = MSG_TYPE_DATA0; 328 | break; 329 | 330 | case CLIENT_WAIT_DATA1: 331 | case CLIENT_WAIT_ACK1: 332 | msg_type = MSG_TYPE_DATA1; 333 | break; 334 | 335 | default: 336 | return -1; 337 | } 338 | 339 | buf = list_get_at(client->tcp2udp_q, 0); 340 | ret = msg_send_msg(client->udp_sock, client->id, msg_type, 341 | buf->buf, buf->len); 342 | if(ret < 0) 343 | return ret; 344 | 345 | /* Set the state to wait for an ACK and set the timeout to some time in 346 | the future */ 347 | client->tcp2udp_state = (msg_type == MSG_TYPE_DATA0) ? 348 | CLIENT_WAIT_ACK0 : CLIENT_WAIT_ACK1; 349 | gettimeofday(&client->tcp2udp_timeout, NULL); 350 | client->tcp2udp_timeout.tv_sec += (client->resend_count+1)*CLIENT_TIMEOUT; 351 | 352 | return 0; 353 | } 354 | 355 | /* 356 | * Notifies the client that it got an ACK to change the internal state to 357 | * wait for data and remove buffer packet at head of the queue. Returns 0 if 358 | * ok or -1 if something weird happened. 359 | */ 360 | int client_got_ack(client_t *client, uint8_t ack_type) 361 | { 362 | if(ack_type == MSG_TYPE_ACK0 && client->tcp2udp_state == CLIENT_WAIT_ACK0) 363 | { 364 | list_delete_at(client->tcp2udp_q, 0); 365 | client->tcp2udp_state = CLIENT_WAIT_DATA1; 366 | client->resend_count = 0; 367 | return 0; 368 | } 369 | 370 | if(ack_type == MSG_TYPE_ACK1 && client->tcp2udp_state == CLIENT_WAIT_ACK1) 371 | { 372 | list_delete_at(client->tcp2udp_q, 0); 373 | client->tcp2udp_state = CLIENT_WAIT_DATA0; 374 | client->resend_count = 0; 375 | return 0; 376 | } 377 | 378 | return -1; 379 | } 380 | 381 | /* 382 | * Sends a HELLO type message to the udpserver (proxy) to tell it to make a 383 | * TCP connection to the specified host:port. 384 | */ 385 | int client_send_hello(client_t *client, char *host, char *port, 386 | uint16_t req_id) 387 | { 388 | return msg_send_hello(client->udp_sock, host, port, req_id); 389 | } 390 | 391 | /* 392 | * Sends a Hello ACK to the UDP tunnel. 393 | */ 394 | int client_send_helloack(client_t *client, uint16_t req_id) 395 | { 396 | req_id = htons(req_id); 397 | 398 | return msg_send_msg(client->udp_sock, client->id, MSG_TYPE_HELLOACK, 399 | (char *)&req_id, sizeof(req_id)); 400 | } 401 | 402 | /* 403 | * Notify the client that it got a Hello ACK. 404 | */ 405 | int client_got_helloack(client_t *client) 406 | { 407 | if(client->udp2tcp_state == CLIENT_WAIT_HELLO) 408 | client->udp2tcp_state = CLIENT_WAIT_DATA0; 409 | 410 | return 0; 411 | } 412 | 413 | /* 414 | * Sends a goodbye message to the UDP server. 415 | */ 416 | int client_send_goodbye(client_t *client) 417 | { 418 | return msg_send_msg(client->udp_sock, client->id, MSG_TYPE_GOODBYE, 419 | NULL, 0); 420 | } 421 | 422 | /* 423 | * Checks the timeout state of the client and resend the data if the timeout 424 | * is up. 425 | */ 426 | int client_check_and_resend(client_t *client, struct timeval curr_tv) 427 | { 428 | if((client->tcp2udp_state == CLIENT_WAIT_ACK0 || 429 | client->tcp2udp_state == CLIENT_WAIT_ACK1) 430 | && timercmp(&curr_tv, &client->tcp2udp_timeout, >)) 431 | { 432 | client->resend_count++; 433 | if(debug_level >= DEBUG_LEVEL2) 434 | printf("client(%d): resending data, count %d\n", 435 | CLIENT_ID(client), client->resend_count); 436 | 437 | return client_send_udp_data(client); 438 | } 439 | 440 | return 0; 441 | } 442 | 443 | /* 444 | * Sends a keepalive message to the UDP server. 445 | */ 446 | int client_check_and_send_keepalive(client_t *client, struct timeval curr_tv) 447 | { 448 | if(client_timed_out(client, curr_tv)) 449 | { 450 | curr_tv.tv_sec += KEEP_ALIVE_SECS; 451 | memcpy(&client->keepalive, &curr_tv, sizeof(struct timeval)); 452 | 453 | return msg_send_msg(client->udp_sock, client->id, MSG_TYPE_KEEPALIVE, 454 | NULL, 0); 455 | } 456 | 457 | return 0; 458 | } 459 | 460 | /* 461 | * Sets the client's keepalive timeout to be the current time plus the timeout 462 | * period. 463 | */ 464 | void client_reset_keepalive(client_t *client) 465 | { 466 | struct timeval curr; 467 | 468 | gettimeofday(&curr, NULL); 469 | curr.tv_sec += KEEP_ALIVE_TIMEOUT_SECS; 470 | memcpy(&client->keepalive, &curr, sizeof(struct timeval)); 471 | } 472 | 473 | /* 474 | * Returns 1 if the client timed out (didn't get any data or keep alive 475 | * messages in the period), or 0 if it hasn't yet. 476 | */ 477 | int client_timed_out(client_t *client, struct timeval curr_tv) 478 | { 479 | if(timercmp(&curr_tv, &client->keepalive, >)) 480 | return 1; 481 | else 482 | return 0; 483 | } 484 | -------------------------------------------------------------------------------- /udpclient.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Project: udptunnel 3 | * File: udpclient.c 4 | * 5 | * Copyright (C) 2009 Daniel Meekins 6 | * Contact: dmeekins - gmail 7 | * 8 | * This program is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | * 13 | * This program is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #ifndef WIN32 29 | #include 30 | #include 31 | #include 32 | #else 33 | #include "helpers/winhelpers.h" 34 | #endif 35 | 36 | #include "common.h" 37 | #include "message.h" 38 | #include "socket.h" 39 | #include "client.h" 40 | #include "list.h" 41 | 42 | extern int debug_level; 43 | extern int ipver; 44 | static int running = 1; 45 | static uint16_t next_req_id; 46 | 47 | /* internal functions */ 48 | static int handle_message(client_t *c, uint16_t id, uint8_t msg_type, 49 | char *data, int data_len); 50 | static void disconnect_and_remove_client(uint16_t id, list_t *clients, 51 | fd_set *fds, int full_disconnect); 52 | static void signal_handler(int sig); 53 | 54 | int udpclient(int argc, char *argv[]) 55 | { 56 | char *lhost, *lport, *phost, *pport, *rhost, *rport; 57 | list_t *clients = NULL; 58 | list_t *conn_clients; 59 | client_t *client; 60 | client_t *client2; 61 | socket_t *tcp_serv = NULL; 62 | socket_t *tcp_sock = NULL; 63 | socket_t *udp_sock = NULL; 64 | char data[MSG_MAX_LEN]; 65 | char addrstr[ADDRSTRLEN]; 66 | 67 | struct timeval curr_time; 68 | struct timeval check_time; 69 | struct timeval check_interval; 70 | struct timeval timeout; 71 | fd_set client_fds; 72 | fd_set read_fds; 73 | uint16_t tmp_id; 74 | uint8_t tmp_type; 75 | uint16_t tmp_len; 76 | uint16_t tmp_req_id; 77 | int num_fds; 78 | 79 | int ret; 80 | int i; 81 | 82 | signal(SIGINT, &signal_handler); 83 | 84 | i = 0; 85 | lhost = (argc - i == 5) ? NULL : argv[i++]; 86 | lport = argv[i++]; 87 | phost = argv[i++]; 88 | pport = argv[i++]; 89 | rhost = argv[i++]; 90 | rport = argv[i++]; 91 | 92 | /* Check validity of ports (can't check ip's b/c might be host names) */ 93 | ERROR_GOTO(!isnum(lport), "Invalid local port.", done); 94 | ERROR_GOTO(!isnum(pport), "Invalid proxy port.", done); 95 | ERROR_GOTO(!isnum(rport), "Invalid remote port.", done); 96 | 97 | srand(time(NULL)); 98 | next_req_id = rand() % 0xffff; 99 | 100 | /* Create an empty list for the clients */ 101 | clients = list_create(sizeof(client_t), p_client_cmp, p_client_copy, 102 | p_client_free, 1); 103 | ERROR_GOTO(clients == NULL, "Error creating clients list.", done); 104 | 105 | /* Create and empty list for the connecting clients */ 106 | conn_clients = list_create(sizeof(client_t), p_client_cmp, p_client_copy, 107 | p_client_free, 1); 108 | ERROR_GOTO(conn_clients == NULL, "Error creating clients list.", done); 109 | 110 | /* Create a TCP server socket to listen for incoming connections */ 111 | tcp_serv = sock_create(lhost, lport, ipver, SOCK_TYPE_TCP, 1, 1); 112 | ERROR_GOTO(tcp_serv == NULL, "Error creating TCP socket.", done); 113 | if(debug_level >= DEBUG_LEVEL1) 114 | { 115 | printf("Listening on TCP %s\n", 116 | sock_get_str(tcp_serv, addrstr, sizeof(addrstr))); 117 | } 118 | 119 | FD_ZERO(&client_fds); 120 | 121 | /* Initialize all the timers */ 122 | timerclear(&timeout); 123 | check_interval.tv_sec = 0; 124 | check_interval.tv_usec = 500000; 125 | gettimeofday(&check_time, NULL); 126 | 127 | while(running) 128 | { 129 | if(!timerisset(&timeout)) 130 | timeout.tv_usec = 50000; 131 | 132 | read_fds = client_fds; 133 | FD_SET(SOCK_FD(tcp_serv), &read_fds); 134 | 135 | ret = select(FD_SETSIZE, &read_fds, NULL, NULL, &timeout); 136 | PERROR_GOTO(ret < 0, "select", done); 137 | num_fds = ret; 138 | 139 | gettimeofday(&curr_time, NULL); 140 | 141 | /* Go through all the clients and check if didn't get an ACK for sent 142 | data during the timeout period */ 143 | if(timercmp(&curr_time, &check_time, >)) 144 | { 145 | for(i = 0; i < LIST_LEN(clients); i++) 146 | { 147 | client = list_get_at(clients, i); 148 | 149 | ret = client_check_and_resend(client, curr_time); 150 | if(ret == -2) 151 | { 152 | disconnect_and_remove_client(CLIENT_ID(client), clients, 153 | &client_fds, 1); 154 | i--; 155 | continue; 156 | } 157 | 158 | ret = client_check_and_send_keepalive(client, curr_time); 159 | if(ret == -2) 160 | { 161 | disconnect_and_remove_client(CLIENT_ID(client), clients, 162 | &client_fds, 1); 163 | i--; 164 | } 165 | } 166 | 167 | timeradd(&curr_time, &check_interval, &check_time); 168 | } 169 | 170 | if(num_fds == 0) 171 | continue; 172 | 173 | /* Check if pending TCP connection to accept and create a new client 174 | and UDP connection if one is ready */ 175 | if(FD_ISSET(SOCK_FD(tcp_serv), &read_fds)) 176 | { 177 | tcp_sock = sock_accept(tcp_serv); 178 | if(tcp_sock == NULL) 179 | continue; 180 | udp_sock = sock_create(phost, pport, ipver, SOCK_TYPE_UDP, 0, 1); 181 | if(udp_sock == NULL) 182 | { 183 | sock_close(tcp_sock); 184 | sock_free(tcp_sock); 185 | continue; 186 | } 187 | 188 | client = client_create(next_req_id++, tcp_sock, udp_sock, 1); 189 | if(!client || !tcp_sock || !udp_sock) 190 | { 191 | if(tcp_sock) 192 | sock_close(tcp_sock); 193 | if(udp_sock) 194 | sock_close(udp_sock); 195 | } 196 | else 197 | { 198 | client2 = list_add(conn_clients, client, 1); 199 | client_free(client); 200 | client = NULL; 201 | 202 | client_send_hello(client2, rhost, rport, CLIENT_ID(client2)); 203 | client_add_tcp_fd_to_set(client2, &client_fds); 204 | client_add_udp_fd_to_set(client2, &client_fds); 205 | } 206 | 207 | sock_free(tcp_sock); 208 | sock_free(udp_sock); 209 | tcp_sock = NULL; 210 | udp_sock = NULL; 211 | 212 | num_fds--; 213 | } 214 | 215 | /* Check for pending handshakes from UDP connection */ 216 | for(i = 0; i < LIST_LEN(conn_clients) && num_fds > 0; i++) 217 | { 218 | client = list_get_at(conn_clients, i); 219 | 220 | if(client_udp_fd_isset(client, &read_fds)) 221 | { 222 | num_fds--; 223 | tmp_req_id = CLIENT_ID(client); 224 | 225 | ret = client_recv_udp_msg(client, data, sizeof(data), 226 | &tmp_id, &tmp_type, &tmp_len); 227 | if(ret == 0) 228 | ret = handle_message(client, tmp_id, tmp_type, 229 | data, tmp_len); 230 | 231 | if(ret < 0) 232 | { 233 | disconnect_and_remove_client(tmp_req_id, conn_clients, 234 | &client_fds, 1); 235 | i--; 236 | } 237 | else 238 | { 239 | client = list_add(clients, client, 1); 240 | list_delete_at(conn_clients, i); 241 | client_remove_udp_fd_from_set(client, &read_fds); 242 | i--; 243 | } 244 | } 245 | } 246 | 247 | /* Check if data is ready from any of the clients */ 248 | for(i = 0; i < LIST_LEN(clients); i++) 249 | { 250 | client = list_get_at(clients, i); 251 | 252 | /* Check for UDP data */ 253 | if(num_fds > 0 && client_udp_fd_isset(client, &read_fds)) 254 | { 255 | num_fds--; 256 | 257 | ret = client_recv_udp_msg(client, data, sizeof(data), 258 | &tmp_id, &tmp_type, &tmp_len); 259 | if(ret == 0) 260 | ret = handle_message(client, tmp_id, tmp_type, 261 | data, tmp_len); 262 | if(ret < 0) 263 | { 264 | disconnect_and_remove_client(CLIENT_ID(client), clients, 265 | &client_fds, 1); 266 | i--; 267 | continue; /* Don't go to check the TCP connection */ 268 | } 269 | } 270 | 271 | /* Check for TCP data */ 272 | if(num_fds > 0 && client_tcp_fd_isset(client, &read_fds)) 273 | { 274 | ret = client_recv_tcp_data(client); 275 | if(ret == -1) 276 | { 277 | disconnect_and_remove_client(CLIENT_ID(client), clients, 278 | &client_fds, 1); 279 | i--; 280 | continue; 281 | } 282 | else if(ret == -2) 283 | { 284 | client_mark_to_disconnect(client); 285 | disconnect_and_remove_client(CLIENT_ID(client), 286 | clients, &client_fds, 0); 287 | } 288 | 289 | num_fds--; 290 | } 291 | 292 | /* send any TCP data that was ready */ 293 | ret = client_send_udp_data(client); 294 | if(ret < 0) 295 | { 296 | disconnect_and_remove_client(CLIENT_ID(client), clients, 297 | &client_fds, 1); 298 | i--; 299 | } 300 | } 301 | 302 | /* Finally, send any udp data that's still in the queue */ 303 | for(i = 0; i < LIST_LEN(clients); i++) 304 | { 305 | client = list_get_at(clients, i); 306 | ret = client_send_udp_data(client); 307 | 308 | if(ret < 0 || client_ready_to_disconnect(client)) 309 | { 310 | disconnect_and_remove_client(CLIENT_ID(client), clients, 311 | &client_fds, 1); 312 | i--; 313 | } 314 | } 315 | } 316 | 317 | done: 318 | if(debug_level >= DEBUG_LEVEL1) 319 | printf("Cleaning up...\n"); 320 | if(tcp_serv) 321 | { 322 | sock_close(tcp_serv); 323 | sock_free(tcp_serv); 324 | } 325 | if(udp_sock) 326 | { 327 | sock_close(udp_sock); 328 | sock_free(udp_sock); 329 | } 330 | if(clients) 331 | list_free(clients); 332 | if(conn_clients) 333 | list_free(conn_clients); 334 | if(debug_level >= DEBUG_LEVEL1) 335 | printf("Goodbye.\n"); 336 | return 0; 337 | } 338 | 339 | /* 340 | * Closes the TCP and UDP connections for the client and remove its stuff from 341 | * the lists. 342 | */ 343 | void disconnect_and_remove_client(uint16_t id, list_t *clients, 344 | fd_set *fds, int full_disconnect) 345 | { 346 | client_t *c; 347 | 348 | c = list_get(clients, &id); 349 | if(!c) 350 | return; 351 | 352 | client_remove_tcp_fd_from_set(c, fds); 353 | client_disconnect_tcp(c); 354 | 355 | if(full_disconnect) 356 | { 357 | client_send_goodbye(c); 358 | 359 | if(debug_level >= DEBUG_LEVEL1) 360 | printf("Client %d disconnected.\n", CLIENT_ID(c)); 361 | 362 | client_remove_udp_fd_from_set(c, fds); 363 | client_disconnect_udp(c); 364 | list_delete(clients, &id); 365 | } 366 | } 367 | 368 | /* 369 | * Handles a message received from the UDP tunnel. Returns 0 if successful, -1 370 | * on some error it handled, or -2 if the client is to disconnect. 371 | */ 372 | int handle_message(client_t *c, uint16_t id, uint8_t msg_type, 373 | char *data, int data_len) 374 | { 375 | int ret = 0; 376 | char addrstr[ADDRSTRLEN]; 377 | 378 | switch(msg_type) 379 | { 380 | case MSG_TYPE_GOODBYE: 381 | ret = -2; 382 | break; 383 | 384 | case MSG_TYPE_HELLOACK: 385 | client_got_helloack(c); 386 | CLIENT_ID(c) = id; 387 | ret = client_send_helloack(c, ntohs(*((uint16_t *)data))); 388 | 389 | if(debug_level >= DEBUG_LEVEL1) 390 | { 391 | sock_get_str(c->tcp_sock, addrstr, sizeof(addrstr)); 392 | printf("New connection(%d): tcp://%s", CLIENT_ID(c), addrstr); 393 | sock_get_str(c->udp_sock, addrstr, sizeof(addrstr)); 394 | printf(" -> udp://%s\n", addrstr); 395 | } 396 | break; 397 | 398 | case MSG_TYPE_DATA0: 399 | case MSG_TYPE_DATA1: 400 | ret = client_got_udp_data(c, data, data_len, msg_type); 401 | if(ret == 0) 402 | ret = client_send_tcp_data(c); 403 | break; 404 | 405 | case MSG_TYPE_ACK0: 406 | case MSG_TYPE_ACK1: 407 | ret = client_got_ack(c, msg_type); 408 | break; 409 | 410 | default: 411 | ret = -1; 412 | break; 413 | } 414 | 415 | return ret; 416 | } 417 | 418 | void signal_handler(int sig) 419 | { 420 | switch(sig) 421 | { 422 | case SIGINT: 423 | running = 0; 424 | } 425 | } 426 | -------------------------------------------------------------------------------- /udpserver.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Project: udptunnel 3 | * File: udpserver.c 4 | * 5 | * Copyright (C) 2009 Daniel Meekins 6 | * Contact: dmeekins - gmail 7 | * 8 | * This program is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | * 13 | * This program is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | #ifndef WIN32 28 | #include 29 | #include 30 | #include 31 | #else 32 | #include "helpers/winhelpers.h" 33 | #endif 34 | 35 | #include "common.h" 36 | #include "list.h" 37 | #include "client.h" 38 | #include "message.h" 39 | #include "socket.h" 40 | #include "acl.h" 41 | 42 | extern int debug_level; 43 | extern int ipver; 44 | static int running = 1; 45 | static int next_client_id = 1; 46 | 47 | /* internal functions */ 48 | static int handle_message(uint16_t id, uint8_t msg_type, char *data, 49 | int data_len, socket_t *from, list_t *clients, 50 | fd_set *client_fds, 51 | list_t *acls); 52 | static void disconnect_and_remove_client(uint16_t id, list_t *clients, 53 | fd_set *fds, int full_disconnect); 54 | static void signal_handler(int sig); 55 | 56 | /* 57 | * UDP Tunnel server main(). Handles program arguments, initializes everything, 58 | * and runs the main loop. 59 | */ 60 | int udpserver(int argc, char *argv[]) 61 | { 62 | char host_str[ADDRSTRLEN]; 63 | char port_str[ADDRSTRLEN]; 64 | char addrstr[ADDRSTRLEN]; 65 | 66 | list_t *clients = NULL; 67 | list_t *acls = NULL; 68 | socket_t *udp_sock = NULL; 69 | socket_t *udp_from = NULL; 70 | char data[MSG_MAX_LEN]; 71 | 72 | client_t *client; 73 | uint16_t tmp_id; 74 | uint8_t tmp_type; 75 | uint16_t tmp_len; 76 | acl_t *tmp_acl; 77 | 78 | struct timeval curr_time; 79 | struct timeval timeout; 80 | struct timeval check_time; 81 | struct timeval check_interval; 82 | fd_set client_fds; 83 | fd_set read_fds; 84 | int num_fds; 85 | 86 | int i; 87 | int ret; 88 | 89 | signal(SIGINT, &signal_handler); 90 | 91 | if(argc == 1) /* only port specified */ 92 | { 93 | ERROR_GOTO(!isnum(argv[0]), "invalid port format", done); 94 | strncpy(port_str, argv[0], sizeof(port_str)); 95 | port_str[sizeof(port_str)-1] = 0; 96 | host_str[0] = 0; 97 | argv++; 98 | argc--; 99 | } 100 | else 101 | { 102 | /* first arg will be host IP if it's not the port */ 103 | if(!isnum(argv[0])) 104 | { 105 | ERROR_GOTO(!isipaddr(argv[0], ipver), 106 | "invalid IP address format", done); 107 | strncpy(host_str, argv[0], sizeof(host_str)); 108 | host_str[sizeof(host_str)-1] = 0; 109 | argv++; 110 | argc--; 111 | } 112 | else 113 | { 114 | host_str[0] = 0; 115 | } 116 | 117 | /* next arg will be the port */ 118 | ERROR_GOTO(!isnum(argv[0]), "invalid port format", done); 119 | strncpy(port_str, argv[0], sizeof(port_str)); 120 | port_str[sizeof(port_str)-1] = 0; 121 | argv++; 122 | argc--; 123 | } 124 | 125 | /* create empty unsorted acl list */ 126 | acls = list_create(sizeof(acl_t), NULL, NULL, p_acl_free, 0); 127 | ERROR_GOTO(acls == NULL, "creating acl list", done); 128 | 129 | for(i = 0; i < argc; i++) 130 | { 131 | tmp_acl = acl_create(argv[i], ipver); 132 | ERROR_GOTO(tmp_acl == NULL, "creating acl", done); 133 | list_add(acls, tmp_acl, 0); 134 | 135 | if(debug_level >= DEBUG_LEVEL2) 136 | { 137 | printf("adding acl entry: "); 138 | acl_print(tmp_acl); 139 | } 140 | } 141 | 142 | /* add ALLOW ALL entry at end of list */ 143 | tmp_acl = acl_create(ACL_DEFAULT, ipver); 144 | ERROR_GOTO(tmp_acl == NULL, "creating acl", done); 145 | list_add(acls, tmp_acl, 0); 146 | 147 | if(debug_level >= DEBUG_LEVEL2) 148 | { 149 | printf("adding acl entry: "); 150 | acl_print(tmp_acl); 151 | } 152 | 153 | /* Create an empty list for the clients */ 154 | clients = list_create(sizeof(client_t), p_client_cmp, p_client_copy, 155 | p_client_free, 1); 156 | if(!clients) 157 | goto done; 158 | 159 | /* Create the socket to receive UDP messages on the specified port */ 160 | udp_sock = sock_create((host_str[0] == 0 ? NULL : host_str), port_str, 161 | ipver, SOCK_TYPE_UDP, 1, 1); 162 | if(!udp_sock) 163 | goto done; 164 | if(debug_level >= DEBUG_LEVEL1) 165 | { 166 | printf("Listening on UDP %s\n", 167 | sock_get_str(udp_sock, addrstr, sizeof(addrstr))); 168 | } 169 | 170 | /* Create empty udp socket for getting source address of udp packets */ 171 | udp_from = sock_create(NULL, NULL, ipver, SOCK_TYPE_UDP, 0, 0); 172 | if(!udp_from) 173 | goto done; 174 | 175 | FD_ZERO(&client_fds); 176 | 177 | timerclear(&timeout); 178 | gettimeofday(&check_time, NULL); 179 | check_interval.tv_sec = 0; 180 | check_interval.tv_usec = 500000; 181 | 182 | while(running) 183 | { 184 | if(!timerisset(&timeout)) 185 | timeout.tv_usec = 50000; 186 | 187 | /* Reset the file desc. set */ 188 | read_fds = client_fds; 189 | FD_SET(SOCK_FD(udp_sock), &read_fds); 190 | 191 | ret = select(FD_SETSIZE, &read_fds, NULL, NULL, &timeout); 192 | PERROR_GOTO(ret < 0, "select", done); 193 | num_fds = ret; 194 | 195 | gettimeofday(&curr_time, NULL); 196 | 197 | /* Go through all the clients and check if didn't get an ACK for sent 198 | data during the timeout period */ 199 | if(timercmp(&curr_time, &check_time, >)) 200 | { 201 | for(i = 0; i < LIST_LEN(clients); i++) 202 | { 203 | client = list_get_at(clients, i); 204 | 205 | if(client_timed_out(client, curr_time)) 206 | { 207 | disconnect_and_remove_client(CLIENT_ID(client), clients, 208 | &client_fds, 1); 209 | i--; 210 | continue; 211 | } 212 | 213 | ret = client_check_and_resend(client, curr_time); 214 | if(ret == -2) 215 | { 216 | disconnect_and_remove_client(CLIENT_ID(client), clients, 217 | &client_fds, 1); 218 | i--; 219 | continue; 220 | } 221 | } 222 | 223 | /* Set time to chech this stuff next */ 224 | timeradd(&curr_time, &check_interval, &check_time); 225 | } 226 | 227 | if(num_fds == 0) 228 | continue; 229 | 230 | /* Get any data received on the UDP socket */ 231 | if(FD_ISSET(SOCK_FD(udp_sock), &read_fds)) 232 | { 233 | ret = msg_recv_msg(udp_sock, udp_from, data, sizeof(data), 234 | &tmp_id, &tmp_type, &tmp_len); 235 | 236 | if(ret == 0) 237 | ret = handle_message(tmp_id, tmp_type, data, tmp_len, 238 | udp_from, clients, &client_fds, acls); 239 | if(ret < 0) 240 | disconnect_and_remove_client(tmp_id, clients, &client_fds, 1); 241 | 242 | num_fds--; 243 | } 244 | 245 | /* Go through all the clients and get any TCP data that is ready */ 246 | for(i = 0; i < LIST_LEN(clients); i++) 247 | { 248 | client = list_get_at(clients, i); 249 | 250 | if(num_fds > 0 && client_tcp_fd_isset(client, &read_fds)) 251 | { 252 | ret = client_recv_tcp_data(client); 253 | if(ret == -1) 254 | { 255 | disconnect_and_remove_client(CLIENT_ID(client), 256 | clients, &client_fds, 1); 257 | i--; /* Since there will be one less element in list */ 258 | continue; 259 | } 260 | else if(ret == -2) 261 | { 262 | client_mark_to_disconnect(client); 263 | disconnect_and_remove_client(CLIENT_ID(client), 264 | clients, &client_fds, 0); 265 | } 266 | 267 | num_fds--; 268 | } 269 | 270 | /* send any TCP data that was ready */ 271 | ret = client_send_udp_data(client); 272 | if(ret < 0) 273 | { 274 | disconnect_and_remove_client(CLIENT_ID(client), 275 | clients, &client_fds, 1); 276 | i--; /* Since there will be one less element in list */ 277 | continue; 278 | } 279 | } 280 | 281 | /* Finally, send any udp data that's still in the queue */ 282 | for(i = 0; i < LIST_LEN(clients); i++) 283 | { 284 | client = list_get_at(clients, i); 285 | ret = client_send_udp_data(client); 286 | 287 | if(ret < 0 || client_ready_to_disconnect(client)) 288 | { 289 | disconnect_and_remove_client(CLIENT_ID(client), clients, 290 | &client_fds, 1); 291 | i--; 292 | continue; 293 | } 294 | } 295 | } 296 | 297 | done: 298 | if(debug_level >= DEBUG_LEVEL1) 299 | printf("Cleaning up...\n"); 300 | if(acls) 301 | list_free(acls); 302 | if(clients) 303 | list_free(clients); 304 | if(udp_sock) 305 | { 306 | sock_close(udp_sock); 307 | sock_free(udp_sock); 308 | } 309 | if(udp_from) 310 | sock_free(udp_from); 311 | if(debug_level >= DEBUG_LEVEL1) 312 | printf("Goodbye.\n"); 313 | 314 | return 0; 315 | } 316 | 317 | /* 318 | * Closes the client's TCP socket (not UDP, since it is shared) and remove from 319 | * the fd set. If full_disconnect is set, remove the list. 320 | */ 321 | void disconnect_and_remove_client(uint16_t id, list_t *clients, 322 | fd_set *fds, int full_disconnect) 323 | { 324 | client_t *c; 325 | 326 | if(id == 0) 327 | return; 328 | 329 | c = list_get(clients, &id); 330 | if(!c) 331 | return; 332 | 333 | /* ok to call multiple times since fd will be -1 after first disconnect */ 334 | client_remove_tcp_fd_from_set(c, fds); 335 | client_disconnect_tcp(c); 336 | 337 | if(full_disconnect) 338 | { 339 | client_send_goodbye(c); 340 | 341 | if(debug_level >= DEBUG_LEVEL1) 342 | printf("Client %d disconnected.\n", CLIENT_ID(c)); 343 | 344 | list_delete(clients, &id); 345 | } 346 | } 347 | 348 | /* 349 | * Handles the message received from the UDP tunnel. Returns 0 for success, -1 350 | * for some error that it handled, and -2 if the connection should be 351 | * disconnected. 352 | */ 353 | int handle_message(uint16_t id, uint8_t msg_type, char *data, int data_len, 354 | socket_t *from, list_t *clients, fd_set *client_fds, 355 | list_t *acls) 356 | { 357 | client_t *c = NULL; 358 | client_t *c2 = NULL; 359 | socket_t *tcp_sock = NULL; 360 | int ret = 0; 361 | 362 | if(id != 0) 363 | { 364 | c = list_get(clients, &id); 365 | if(!c) 366 | return -1; 367 | } 368 | 369 | if(id == 0 && msg_type != MSG_TYPE_HELLO) 370 | return -2; 371 | 372 | switch(msg_type) 373 | { 374 | case MSG_TYPE_GOODBYE: 375 | ret = -2; 376 | break; 377 | 378 | /* Data in the hello message will be like "hostname port", possibly 379 | without the null terminator. This will look for the space and 380 | parse out the hostname or ip address and port number */ 381 | case MSG_TYPE_HELLO: 382 | { 383 | int i; 384 | char port[6]; /* need this so port str can have null term. */ 385 | char src_addrstr[ADDRSTRLEN]; 386 | char dst_addrstr[ADDRSTRLEN]; 387 | uint16_t sport, dport; 388 | uint16_t req_id; 389 | 390 | if(id != 0) 391 | break; 392 | 393 | req_id = ntohs(*((uint16_t*)data)); 394 | data += sizeof(uint16_t); 395 | data_len -= sizeof(uint16_t); 396 | 397 | /* look for the space separating the host and port */ 398 | for(i = 0; i < data_len; i++) 399 | if(data[i] == ' ') 400 | break; 401 | if(i == data_len) 402 | break; 403 | 404 | /* null terminate the host and get the port number to the string */ 405 | data[i++] = 0; 406 | strncpy(port, data+i, data_len-i); 407 | port[data_len-i] = 0; 408 | 409 | /* Create an unconnected TCP socket for the remote host, the 410 | client itself, add it to the list of clients */ 411 | tcp_sock = sock_create(data, port, ipver, SOCK_TYPE_TCP, 0, 0); 412 | ERROR_GOTO(tcp_sock == NULL, "Error creating tcp socket", error); 413 | 414 | c = client_create(next_client_id++, tcp_sock, from, 0); 415 | sock_free(tcp_sock); 416 | ERROR_GOTO(c == NULL, "Error creating client", error); 417 | 418 | c2 = list_add(clients, c, 1); 419 | ERROR_GOTO(c2 == NULL, "Error adding client to list", error); 420 | 421 | sock_get_addrstr(CLIENT_UDP_SOCK(c2), src_addrstr, 422 | sizeof(src_addrstr)); 423 | sock_get_addrstr(CLIENT_TCP_SOCK(c2), dst_addrstr, 424 | sizeof(dst_addrstr)); 425 | sport = sock_get_port(CLIENT_UDP_SOCK(c2)); 426 | dport = sock_get_port(CLIENT_TCP_SOCK(c2)); 427 | 428 | for(i = 0; i < LIST_LEN(acls); i++) 429 | { 430 | ret = acl_action(list_get_at(acls, i), src_addrstr, sport, 431 | dst_addrstr, dport); 432 | 433 | if(ret == ACL_ACTION_ALLOW) 434 | { 435 | if(debug_level >= DEBUG_LEVEL2) 436 | printf("Connection %s:%hu -> %s:%hu allowed\n", 437 | src_addrstr, sport, dst_addrstr, dport); 438 | break; 439 | } 440 | else if(ret == ACL_ACTION_DENY) 441 | { 442 | if(debug_level >= DEBUG_LEVEL2) 443 | printf("Connection to %s:%hu -> %s:%hu denied\n", 444 | src_addrstr, sport, dst_addrstr, dport); 445 | 446 | msg_send_msg(from, next_client_id, MSG_TYPE_GOODBYE, 447 | NULL, 0); 448 | client_free(c); 449 | return -2; 450 | } 451 | } 452 | 453 | if(debug_level >= DEBUG_LEVEL1) 454 | { 455 | sock_get_str(CLIENT_UDP_SOCK(c2), src_addrstr, 456 | sizeof(src_addrstr)); 457 | sock_get_str(CLIENT_TCP_SOCK(c2), dst_addrstr, 458 | sizeof(dst_addrstr)); 459 | printf("New connection(%d): udp://%s -> tcp://%s\n", 460 | CLIENT_ID(c2), src_addrstr, dst_addrstr); 461 | } 462 | 463 | /* Send the Hello ACK message if created client successfully */ 464 | client_send_helloack(c2, req_id); 465 | client_reset_keepalive(c2); 466 | client_free(c); 467 | 468 | break; 469 | } 470 | 471 | /* Can connect to TCP connection once received the Hello ACK */ 472 | case MSG_TYPE_HELLOACK: 473 | if(client_connect_tcp(c) != 0) 474 | return -2; 475 | client_got_helloack(c); 476 | client_add_tcp_fd_to_set(c, client_fds); 477 | break; 478 | 479 | /* Resets the timeout of the client's keep alive time */ 480 | case MSG_TYPE_KEEPALIVE: 481 | client_reset_keepalive(c); 482 | break; 483 | 484 | /* Receives the data it got from the UDP tunnel and sends it to the 485 | TCP connection. */ 486 | case MSG_TYPE_DATA0: 487 | case MSG_TYPE_DATA1: 488 | ret = client_got_udp_data(c, data, data_len, msg_type); 489 | if(ret == 0) 490 | ret = client_send_tcp_data(c); 491 | break; 492 | 493 | /* Receives the ACK from the UDP tunnel to set the internal client 494 | state. */ 495 | case MSG_TYPE_ACK0: 496 | case MSG_TYPE_ACK1: 497 | client_got_ack(c, msg_type); 498 | break; 499 | 500 | default: 501 | ret = -1; 502 | } 503 | 504 | return ret; 505 | 506 | error: 507 | return -1; 508 | } 509 | 510 | void signal_handler(int sig) 511 | { 512 | switch(sig) 513 | { 514 | case SIGINT: 515 | running = 0; 516 | } 517 | } 518 | -------------------------------------------------------------------------------- /socket.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Project: udptunnel 3 | * File: socket.c 4 | * 5 | * Copyright (C) 2009 Daniel Meekins 6 | * Contact: dmeekins - gmail 7 | * 8 | * This program is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | * 13 | * This program is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | #ifndef WIN32 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #endif /* ~WIN32 */ 34 | 35 | #include "socket.h" 36 | #include "common.h" 37 | 38 | extern int debug_level; 39 | 40 | void print_hexdump(char *data, int len); 41 | 42 | /* 43 | * Allocates and returns a new socket structure. 44 | * host - string of host or address to listen on (can be NULL for servers) 45 | * port - string of port number or service (can be NULL for clients) 46 | * ipver - SOCK_IPV4 or SOCK_IPV6 47 | * sock_type - SOCK_TYPE_TCP or SOCK_TYPE_UDP 48 | * is_serv - 1 if is a server socket to bind and listen on port, 0 if client 49 | * conn - call socket(), bind(), and listen() if is_serv, or connect() 50 | * if not is_serv. Doesn't call these if conn is 0. 51 | */ 52 | socket_t *sock_create(char *host, char *port, int ipver, int sock_type, 53 | int is_serv, int conn) 54 | { 55 | socket_t *sock = NULL; 56 | struct addrinfo hints; 57 | struct addrinfo *info = NULL; 58 | struct sockaddr *paddr; 59 | int ret; 60 | 61 | sock = calloc(1, sizeof(*sock)); 62 | if(!sock) 63 | return NULL; 64 | 65 | paddr = SOCK_PADDR(sock); 66 | sock->fd = -1; 67 | 68 | switch(sock_type) 69 | { 70 | case SOCK_TYPE_TCP: 71 | sock->type = SOCK_STREAM; 72 | break; 73 | case SOCK_TYPE_UDP: 74 | sock->type = SOCK_DGRAM; 75 | break; 76 | default: 77 | goto error; 78 | } 79 | 80 | /* If both host and port are null, then don't create any socket or 81 | address, but still set the AF. */ 82 | if(host == NULL && port == NULL) 83 | { 84 | sock->addr.ss_family = (ipver == SOCK_IPV6) ? AF_INET6 : AF_INET; 85 | goto done; 86 | } 87 | 88 | /* Setup type of address to get */ 89 | memset(&hints, 0, sizeof(hints)); 90 | hints.ai_family = (ipver == SOCK_IPV6) ? AF_INET6 : AF_INET; 91 | hints.ai_socktype = sock->type; 92 | hints.ai_flags = is_serv ? AI_PASSIVE : 0; 93 | 94 | /* Get address from the machine */ 95 | ret = getaddrinfo(host, port, &hints, &info); 96 | PERROR_GOTO(ret != 0, "getaddrinfo", error); 97 | memcpy(paddr, info->ai_addr, info->ai_addrlen); 98 | sock->addr_len = info->ai_addrlen; 99 | 100 | if(conn) 101 | { 102 | if(sock_connect(sock, is_serv) != 0) 103 | goto error; 104 | } 105 | 106 | done: 107 | if(info) 108 | freeaddrinfo(info); 109 | 110 | return sock; 111 | 112 | error: 113 | if(sock) 114 | free(sock); 115 | if(info) 116 | freeaddrinfo(info); 117 | 118 | return NULL; 119 | } 120 | 121 | socket_t *sock_copy(socket_t *sock) 122 | { 123 | socket_t *new; 124 | 125 | new = malloc(sizeof(*sock)); 126 | if(!new) 127 | return NULL; 128 | 129 | memcpy(new, sock, sizeof(*sock)); 130 | 131 | return new; 132 | } 133 | 134 | /* 135 | * If the socket is a server, start listening. If it's a client, connect to 136 | * to destination specified in sock_create(). Returns -1 on error or -2 if 137 | * the sockect is already connected. 138 | */ 139 | int sock_connect(socket_t *sock, int is_serv) 140 | { 141 | struct sockaddr *paddr; 142 | int ret; 143 | 144 | if(sock->fd != -1) 145 | return -2; 146 | 147 | paddr = SOCK_PADDR(sock); 148 | 149 | /* Create socket file descriptor */ 150 | sock->fd = socket(paddr->sa_family, sock->type, 0); 151 | PERROR_GOTO(sock->fd < 0, "socket", error); 152 | 153 | if(is_serv) 154 | { 155 | /* Bind socket to address and port */ 156 | ret = bind(sock->fd, paddr, sock->addr_len); 157 | PERROR_GOTO(ret != 0, "bind", error); 158 | 159 | /* Start listening on the port if tcp */ 160 | if(sock->type == SOCK_STREAM) 161 | { 162 | ret = listen(sock->fd, BACKLOG); 163 | PERROR_GOTO(ret != 0, "listen", error); 164 | } 165 | } 166 | else 167 | { 168 | /* Connect to the server if tcp */ 169 | if(sock->type == SOCK_STREAM) 170 | { 171 | ret = connect(sock->fd, paddr, sock->addr_len); 172 | PERROR_GOTO(ret != 0, "connect", error); 173 | } 174 | } 175 | 176 | return 0; 177 | 178 | error: 179 | return -1; 180 | } 181 | 182 | /* 183 | * Accept a new connection and return a newly allocated socket representing 184 | * the remote connection. 185 | */ 186 | socket_t *sock_accept(socket_t *serv) 187 | { 188 | socket_t *client; 189 | 190 | client = calloc(1, sizeof(*client)); 191 | if(!client) 192 | goto error; 193 | 194 | client->type = serv->type; 195 | client->addr_len = sizeof(struct sockaddr_storage); 196 | client->fd = accept(serv->fd, SOCK_PADDR(client), &client->addr_len); 197 | PERROR_GOTO(SOCK_FD(client) < 0, "accept", error); 198 | 199 | return client; 200 | 201 | error: 202 | if(client) 203 | free(client); 204 | 205 | return NULL; 206 | } 207 | 208 | /* 209 | * Closes the file descriptor for the socket. 210 | */ 211 | void sock_close(socket_t *s) 212 | { 213 | if(s->fd != -1) 214 | { 215 | #ifdef WIN32 216 | closesocket(s->fd); 217 | #else 218 | close(s->fd); 219 | #endif 220 | s->fd = -1; 221 | } 222 | } 223 | 224 | /* 225 | * Frees the socket structure. 226 | */ 227 | void sock_free(socket_t *s) 228 | { 229 | free(s); 230 | } 231 | 232 | /* 233 | * Returns non zero if IP addresses and ports are same, or 0 if not. 234 | */ 235 | int sock_addr_equal(socket_t *s1, socket_t *s2) 236 | { 237 | if(s1->addr_len != s2->addr_len) 238 | return 0; 239 | 240 | return (memcmp(&s1->addr, &s2->addr, s1->addr_len) == 0); 241 | } 242 | 243 | /* 244 | * Compares only the IP address of two sockets 245 | */ 246 | int sock_ipaddr_cmp(socket_t *s1, socket_t *s2) 247 | { 248 | char *a1; 249 | char *a2; 250 | int len; 251 | 252 | if(s1->addr.ss_family != s2->addr.ss_family) 253 | return s1->addr.ss_family - s2->addr.ss_family; /* ? */ 254 | 255 | switch(s1->addr.ss_family) 256 | { 257 | case AF_INET: 258 | a1 = (char *)(&SIN(&s1->addr)->sin_addr); 259 | a2 = (char *)(&SIN(&s2->addr)->sin_addr); 260 | len = 4; /* 32 bits */ 261 | break; 262 | 263 | case AF_INET6: 264 | a1 = (char *)(&SIN6(&s1->addr)->sin6_addr); 265 | a2 = (char *)(&SIN6(&s2->addr)->sin6_addr); 266 | len = 16; /* 128 bits */ 267 | break; 268 | 269 | default: 270 | return 0; /* ? */ 271 | } 272 | 273 | return memcmp(a1, a2, len); 274 | } 275 | 276 | /* 277 | * Compares only the ports of two sockets 278 | */ 279 | int sock_port_cmp(socket_t *s1, socket_t *s2) 280 | { 281 | uint16_t p1; 282 | uint16_t p2; 283 | 284 | if(s1->addr.ss_family != s2->addr.ss_family) 285 | return s1->addr.ss_family - s2->addr.ss_family; /* ? */ 286 | 287 | switch(s1->addr.ss_family) 288 | { 289 | case AF_INET: 290 | p1 = ntohs(SIN(&s1->addr)->sin_port); 291 | p2 = ntohs(SIN(&s2->addr)->sin_port); 292 | 293 | case AF_INET6: 294 | p1 = ntohs(SIN6(&s1->addr)->sin6_port); 295 | p2 = ntohs(SIN6(&s2->addr)->sin6_port); 296 | 297 | default: 298 | return 0; /* ? */ 299 | } 300 | 301 | return p1 - p2; 302 | } 303 | 304 | /* 305 | * Returns 1 if the address in the socket is 0.0.0.0 or ::, and 0 if not. 306 | */ 307 | int sock_isaddrany(socket_t *s) 308 | { 309 | struct in6_addr zaddr = IN6ADDR_ANY_INIT; 310 | 311 | switch(s->addr.ss_family) 312 | { 313 | case AF_INET: 314 | return (SIN(&s->addr)->sin_addr.s_addr == INADDR_ANY) ? 1 : 0; 315 | 316 | case AF_INET6: 317 | if(memcmp(&SIN6(&s->addr)->sin6_addr, &zaddr, sizeof(zaddr)) == 0) 318 | return 1; 319 | else 320 | return 0; 321 | 322 | default: 323 | return 1; 324 | } 325 | } 326 | /* 327 | * Gets the string representation of the IP address and port from addr. Will 328 | * store result in buf, which len must be at least INET6_ADDRLEN + 6. Returns a 329 | * pointer to buf. String will be in the form of "ip_address:port". 330 | */ 331 | #ifdef WIN32 332 | char *sock_get_str(socket_t *s, char *buf, int len) 333 | { 334 | DWORD plen = len; 335 | 336 | if(WSAAddressToString(SOCK_PADDR(s), SOCK_LEN(s), NULL, buf, &plen) != 0) 337 | return NULL; 338 | 339 | return buf; 340 | } 341 | #else 342 | char *sock_get_str(socket_t *s, char *buf, int len) 343 | { 344 | void *src_addr; 345 | char addr_str[INET6_ADDRSTRLEN]; 346 | uint16_t port; 347 | 348 | switch(s->addr.ss_family) 349 | { 350 | case AF_INET: 351 | src_addr = (void *)&SIN(&s->addr)->sin_addr; 352 | port = ntohs(SIN(&s->addr)->sin_port); 353 | break; 354 | 355 | case AF_INET6: 356 | src_addr = (void *)&SIN6(&s->addr)->sin6_addr; 357 | port = ntohs(SIN6(&s->addr)->sin6_port); 358 | break; 359 | 360 | default: 361 | return NULL; 362 | } 363 | 364 | if(inet_ntop(s->addr.ss_family, src_addr, 365 | addr_str, sizeof(addr_str)) == NULL) 366 | return NULL; 367 | 368 | snprintf(buf, len, (s->addr.ss_family == AF_INET6) ? "[%s]:%hu" : "%s:%hu", 369 | addr_str, port); 370 | 371 | return buf; 372 | } 373 | #endif /*WIN32*/ 374 | 375 | /* 376 | * Gets the string representation of the IP address and puts it in buf. Will 377 | * return the pointer to buf or NULL if there was an error. 378 | */ 379 | #ifdef WIN32 380 | char *sock_get_addrstr(socket_t *s, char *buf, int len) 381 | { 382 | socket_t *copy = NULL; 383 | 384 | if((copy = sock_copy(s)) == NULL) 385 | return NULL; 386 | 387 | switch(copy->addr.ss_family) 388 | { 389 | case AF_INET: 390 | SIN(©->addr)->sin_port = 0; 391 | break; 392 | 393 | case AF_INET6: 394 | SIN6(©->addr)->sin6_port = 0; 395 | break; 396 | 397 | default: 398 | return NULL; 399 | } 400 | 401 | /* Calls to this will put the port in the string, so seting the port to 0 402 | * will just return the IP address. */ 403 | if(sock_get_str(copy, buf, len) == NULL) 404 | goto error; 405 | 406 | free(copy); 407 | return buf; 408 | 409 | error: 410 | if(copy) 411 | free(copy); 412 | 413 | return NULL; 414 | } 415 | #else /*~WIN32*/ 416 | char *sock_get_addrstr(socket_t *s, char *buf, int len) 417 | { 418 | void *src_addr; 419 | 420 | switch(s->addr.ss_family) 421 | { 422 | case AF_INET: 423 | src_addr = (void *)&SIN(&s->addr)->sin_addr; 424 | break; 425 | 426 | case AF_INET6: 427 | src_addr = (void *)&SIN6(&s->addr)->sin6_addr; 428 | break; 429 | 430 | default: 431 | return NULL; 432 | } 433 | 434 | if(inet_ntop(s->addr.ss_family, src_addr, buf, len) == NULL) 435 | return NULL; 436 | 437 | return buf; 438 | } 439 | #endif /*WIN32*/ 440 | 441 | /* 442 | * Returns the 16-bit port number in host byte order from the passed sockaddr. 443 | */ 444 | uint16_t sock_get_port(socket_t *s) 445 | { 446 | switch(s->addr.ss_family) 447 | { 448 | case AF_INET: 449 | return (uint16_t)ntohs(SIN(&s->addr)->sin_port); 450 | 451 | case AF_INET6: 452 | return (uint16_t)ntohs(SIN6(&s->addr)->sin6_port); 453 | } 454 | 455 | return 0; 456 | } 457 | 458 | /* 459 | * Receives data from the socket. Calles recv() or recvfrom() depending on the 460 | * type of socket. Ignores the 'from' argument if type is for TCP, or puts 461 | * remove address in from socket for UDP. Reads up to len bytes and puts it in 462 | * data. Returns number of bytes sent, or 0 if remote host disconnected, or -1 463 | * on error. 464 | */ 465 | int sock_recv(socket_t *sock, socket_t *from, char *data, int len) 466 | { 467 | int bytes_recv = 0; 468 | socket_t tmp; 469 | 470 | switch(sock->type) 471 | { 472 | case SOCK_STREAM: 473 | bytes_recv = recv(sock->fd, data, len, 0); 474 | break; 475 | 476 | case SOCK_DGRAM: 477 | if(!from) 478 | from = &tmp; /* In case caller wants to ignore from socket */ 479 | from->fd = sock->fd; 480 | from->addr_len = sock->addr_len; 481 | bytes_recv = recvfrom(from->fd, data, len, 0, 482 | SOCK_PADDR(from), &SOCK_LEN(from)); 483 | break; 484 | } 485 | 486 | PERROR_GOTO(bytes_recv < 0, "recv", error); 487 | ERROR_GOTO(bytes_recv == 0, "disconnect", disconnect); 488 | 489 | if(debug_level >= DEBUG_LEVEL3) 490 | { 491 | printf("sock_recv: type=%d, fd=%d, bytes=%d\n", 492 | sock->type, sock->fd, bytes_recv); 493 | print_hexdump(data, bytes_recv); 494 | } 495 | 496 | return bytes_recv; 497 | 498 | disconnect: 499 | return 0; 500 | 501 | error: 502 | return -1; 503 | } 504 | 505 | /* 506 | * Sends len bytes in data to the socket connection. Returns number of bytes 507 | * sent, or 0 on disconnect, or -1 on error. 508 | */ 509 | int sock_send(socket_t *to, char *data, int len) 510 | { 511 | int bytes_sent = 0; 512 | int ret; 513 | 514 | switch(to->type) 515 | { 516 | case SOCK_STREAM: 517 | while(bytes_sent < len) 518 | { 519 | ret = send(to->fd, data + bytes_sent, len - bytes_sent, 0); 520 | PERROR_GOTO(ret < 0, "send", error); 521 | ERROR_GOTO(ret == 0, "disconnected", disconnect); 522 | bytes_sent += ret; 523 | } 524 | break; 525 | 526 | case SOCK_DGRAM: 527 | bytes_sent = sendto(to->fd, data, len, 0, 528 | SOCK_PADDR(to), to->addr_len); 529 | PERROR_GOTO(bytes_sent < 0, "sendto", error); 530 | break; 531 | 532 | default: 533 | return 0; 534 | } 535 | 536 | if(debug_level >= DEBUG_LEVEL3) 537 | { 538 | printf("sock_send: type=%d, fd=%d, bytes=%d\n", 539 | to->type, to->fd, bytes_sent); 540 | print_hexdump(data, bytes_sent); 541 | } 542 | 543 | return bytes_sent; 544 | 545 | disconnect: 546 | return 0; 547 | 548 | error: 549 | return -1; 550 | } 551 | 552 | /* 553 | * Checks validity of an IP address string based on the version 554 | */ 555 | int isipaddr(char *ip, int ipver) 556 | { 557 | char addr[sizeof(struct in6_addr)]; 558 | int len; 559 | int af_type; 560 | 561 | af_type = (ipver == SOCK_IPV6) ? AF_INET6 : AF_INET; 562 | len = sizeof(addr); 563 | 564 | #ifdef WIN32 565 | if(WSAStringToAddress(ip, af_type, NULL, PADDR(addr), &len) == 0) 566 | return 1; 567 | #else /*~WIN32*/ 568 | if(inet_pton(af_type, ip, addr) == 1) 569 | return 1; 570 | #endif /*WIN32*/ 571 | 572 | return 0; 573 | } 574 | 575 | /* 576 | * Debugging function to print a hexdump of data with ascii, for example: 577 | * 00000000 74 68 69 73 20 69 73 20 61 20 74 65 73 74 20 6d this is a test m 578 | * 00000010 65 73 73 61 67 65 2e 20 62 6c 61 68 2e 00 essage. blah.. 579 | */ 580 | void print_hexdump(char *data, int len) 581 | { 582 | int line; 583 | int max_lines = (len / 16) + (len % 16 == 0 ? 0 : 1); 584 | int i; 585 | 586 | for(line = 0; line < max_lines; line++) 587 | { 588 | printf("%08x ", line * 16); 589 | 590 | /* print hex */ 591 | for(i = line * 16; i < (8 + (line * 16)); i++) 592 | { 593 | if(i < len) 594 | printf("%02x ", (uint8_t)data[i]); 595 | else 596 | printf(" "); 597 | } 598 | printf(" "); 599 | for(i = (line * 16) + 8; i < (16 + (line * 16)); i++) 600 | { 601 | if(i < len) 602 | printf("%02x ", (uint8_t)data[i]); 603 | else 604 | printf(" "); 605 | } 606 | 607 | printf(" "); 608 | 609 | /* print ascii */ 610 | for(i = line * 16; i < (8 + (line * 16)); i++) 611 | { 612 | if(i < len) 613 | { 614 | if(32 <= data[i] && data[i] <= 126) 615 | printf("%c", data[i]); 616 | else 617 | printf("."); 618 | } 619 | else 620 | printf(" "); 621 | } 622 | printf(" "); 623 | for(i = (line * 16) + 8; i < (16 + (line * 16)); i++) 624 | { 625 | if(i < len) 626 | { 627 | if(32 <= data[i] && data[i] <= 126) 628 | printf("%c", data[i]); 629 | else 630 | printf("."); 631 | } 632 | else 633 | printf(" "); 634 | } 635 | 636 | printf("\n"); 637 | } 638 | } 639 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------