From 155f0c6f37313c8389904bcf180a98c4fe8db237 Mon Sep 17 00:00:00 2001 From: Erik Ekman Date: Sat, 24 Jan 2009 22:19:11 +0000 Subject: [PATCH] Merged branch with mingw port. Compiles now, tun work to do --- src/Makefile | 5 +- src/common.c | 60 +++-- src/common.h | 14 +- src/dns.c | 18 +- src/fw_query.h | 5 + src/iodine.c | 37 +++- src/iodined.c | 47 ++-- src/login.c | 7 +- src/osflags | 3 + src/plibc.h | 577 +++++++++++++++++++++++++++++++++++++++++++++++++ src/tun.c | 26 +++ src/user.c | 11 +- src/windows.h | 84 +++++++ 13 files changed, 842 insertions(+), 52 deletions(-) create mode 100644 src/plibc.h diff --git a/src/Makefile b/src/Makefile index 8197c4d..73c42df 100644 --- a/src/Makefile +++ b/src/Makefile @@ -8,7 +8,8 @@ SERVER = ../bin/iodined OS = `uname | tr "a-z" "A-Z"` ARCH = `uname -m` -LDFLAGS = -lz `sh osflags link` +LIBPATH = -L. +LDFLAGS = -lz `sh osflags link` $(LIBPATH) CFLAGS = -c -g -Wall -D$(OS) -pedantic `sh osflags cflags` all: stateos $(CLIENT) $(SERVER) @@ -32,5 +33,5 @@ $(SERVER): $(COMMONOBJS) $(SERVEROBJS) clean: @echo "Cleaning src/" - @rm -f $(CLIENT) $(SERVER) *~ *.o *.core + @rm -f $(CLIENT){,.exe} $(SERVER){,.exe} *~ *.o *.core diff --git a/src/common.c b/src/common.c index 22b6d38..28cf769 100644 --- a/src/common.c +++ b/src/common.c @@ -14,14 +14,7 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include -#include -#include -#ifdef DARWIN -#include -#endif #include -#include #include #include #include @@ -33,12 +26,25 @@ #include #include #include + +#ifdef WINDOWS32 +#include +#else +#include +#ifdef DARWIN +#include +#endif #include +#include +#include +#include +#endif #include "common.h" +#include "plibc.h" /* daemon(3) exists only in 4.4BSD or later, and in GNU libc */ -#if !(defined(BSD) && (BSD >= 199306)) && !defined(__GLIBC__) +#if !defined(WINDOWS32) && !(defined(BSD) && (BSD >= 199306)) && !defined(__GLIBC__) static int daemon(int nochdir, int noclose) { int fd, i; @@ -86,18 +92,20 @@ int setgroups(int count, int *groups) void check_superuser(void (*usage_fn)(void)) { +#ifndef WINDOWS32 if (geteuid() != 0) { warnx("Run as root and you'll be happy.\n"); usage_fn(); /* NOTREACHED */ } +#endif } int open_dns(int localport, in_addr_t listen_ip) { struct sockaddr_in addr; - int flag; + int flag = 1; int fd; memset(&addr, 0, sizeof(addr)); @@ -106,17 +114,21 @@ open_dns(int localport, in_addr_t listen_ip) /* listen_ip already in network byte order from inet_addr, or 0 */ addr.sin_addr.s_addr = listen_ip; - if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) + if ((fd = SOCKET(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { + printf("got fd %d\n", fd); err(1, "socket"); + } flag = 1; #ifdef SO_REUSEPORT - setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &flag, sizeof(flag)); + SETSOCKOPT(fd, SOL_SOCKET, SO_REUSEPORT, (const void*) &flag, sizeof(flag)); #endif - setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag)); + SETSOCKOPT(fd, SOL_SOCKET, SO_REUSEADDR, (const void*) &flag, sizeof(flag)); +#ifndef WINDOWS32 /* To get destination address from each UDP datagram, see iodined.c:read_dns() */ - setsockopt(fd, IPPROTO_IP, DSTADDR_SOCKOPT, &flag, sizeof(flag)); + SETSOCKOPT(fd, IPPROTO_IP, DSTADDR_SOCKOPT, (const void*) &flag, sizeof(flag)); +#endif if(bind(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) err(1, "bind"); @@ -135,7 +147,7 @@ close_dns(int fd) void do_chroot(char *newroot) { -#if !defined(__BEOS__) || defined(__HAIKU__) +#if !(defined(WINDOWS32) || defined(__BEOS__) || defined(__HAIKU__)) if (chroot(newroot) != 0 || chdir("/") != 0) err(1, "%s", newroot); @@ -149,31 +161,39 @@ do_chroot(char *newroot) void do_detach() { +#ifndef WINDOWS32 printf("Detaching from terminal...\n"); daemon(0, 0); umask(0); alarm(0); +#else + printf("Windows version does not support detaching\n"); +#endif } void read_password(char *buf, size_t len) { + char pwd[80]; +#ifndef WINDOWS32 struct termios old; struct termios tp; - char pwd[80]; tcgetattr(0, &tp); old = tp; tp.c_lflag &= (~ECHO); tcsetattr(0, TCSANOW, &tp); +#endif printf("Enter password: "); fflush(stdout); scanf("%79s", pwd); printf("\n"); +#ifndef WINDOWS32 tcsetattr(0, TCSANOW, &old); +#endif strncpy(buf, pwd, len); buf[len-1] = '\0'; @@ -195,3 +215,13 @@ check_topdomain(char *str) } return 0; } + +#ifdef WINDOWS32 +int +inet_aton(const char *cp, struct in_addr *inp) +{ + inp->s_addr = inet_addr(cp); + return inp->s_addr != INADDR_ANY; +} +#endif + diff --git a/src/common.h b/src/common.h index 06d57db..44b4b59 100644 --- a/src/common.h +++ b/src/common.h @@ -17,10 +17,16 @@ #ifndef __COMMON_H__ #define __COMMON_H__ -#include -#include +#ifdef WINDOWS32 +#include "windows.h" +#include +#else #include +#include +#include #include +#endif + #ifndef MIN #define MIN(a,b) ((a)<(b)?(a):(b)) @@ -69,4 +75,8 @@ void read_password(char*, size_t); int check_topdomain(char *); +#ifdef WINDOWS32 +int inet_aton(const char *cp, struct in_addr *inp); +#endif + #endif diff --git a/src/dns.c b/src/dns.c index df06fd1..06d972b 100644 --- a/src/dns.c +++ b/src/dns.c @@ -14,13 +14,7 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include -#include -#ifdef DARWIN -#include -#endif #include -#include #include #include #include @@ -28,6 +22,18 @@ #include #include +#ifdef WINDOWS32 +#include "windows.h" +#else +#include +#ifdef DARWIN +#include +#endif +#include +#include +#endif + + #include "dns.h" #include "encoding.h" #include "read.h" diff --git a/src/fw_query.h b/src/fw_query.h index c47da41..5523c3e 100644 --- a/src/fw_query.h +++ b/src/fw_query.h @@ -18,7 +18,12 @@ #define __FW_QUERY_H__ #include +#ifdef WINDOWS32 +#include "windows.h" +#include +#else #include +#endif #define FW_QUERY_CACHE_SIZE 16 diff --git a/src/iodine.c b/src/iodine.c index 67a9bff..93d25ec 100644 --- a/src/iodine.c +++ b/src/iodine.c @@ -20,23 +20,30 @@ #include #include #include -#include -#include #include #include #include -#include #include -#include -#include -#include -#include #include + +#ifdef WINDOWS32 +#include "windows.h" +#include +#else #include #ifdef DARWIN #include #endif +#include +#include +#include +#include +#include +#include +#include +#endif +#include "plibc.h" #include "common.h" #include "encoding.h" #include "base32.h" @@ -165,13 +172,13 @@ read_dns(int fd, char *buf, int buflen) { struct sockaddr_in from; char data[64*1024]; - socklen_t addrlen; + int addrlen; struct query q; int rv; int r; addrlen = sizeof(struct sockaddr); - if ((r = recvfrom(fd, data, sizeof(data), 0, + if ((r = RECVFROM(fd, data, sizeof(data), 0, (struct sockaddr*)&from, &addrlen)) == -1) { warn("recvfrom"); return 0; @@ -308,7 +315,7 @@ tunnel(int tun_fd, int dns_fd) } FD_SET(dns_fd, &fds); - i = select(MAX(tun_fd, dns_fd) + 1, &fds, NULL, NULL, &tv); + i = SELECT(MAX(tun_fd, dns_fd) + 1, &fds, NULL, NULL, &tv); if (running == 0) break; @@ -873,7 +880,9 @@ int main(int argc, char **argv) { char *nameserv_addr; +#ifndef WINDOWS32 struct passwd *pw; +#endif char *username; int foreground; char *newroot; @@ -898,6 +907,10 @@ main(int argc, char **argv) b32 = get_base32_encoder(); dataenc = get_base32_encoder(); +#ifdef WINDOWS32 + plibc_init("Kryo", "iodine"); +#endif + #if !defined(BSD) && !defined(__GLIBC__) __progname = strrchr(argv[0], '/'); if (__progname == NULL) @@ -985,11 +998,13 @@ main(int argc, char **argv) } if (username != NULL) { +#ifndef WINDOWS32 if ((pw = getpwnam(username)) == NULL) { warnx("User %s does not exist!\n", username); usage(); /* NOTREACHED */ } +#endif } if (strlen(password) == 0) @@ -1015,6 +1030,7 @@ main(int argc, char **argv) do_chroot(newroot); if (username != NULL) { +#ifndef WINDOWS32 gid_t gids[1]; gids[0] = pw->pw_gid; if (setgroups(1, gids) < 0 || setgid(pw->pw_gid) < 0 || setuid(pw->pw_uid) < 0) { @@ -1022,6 +1038,7 @@ main(int argc, char **argv) usage(); /* NOTREACHED */ } +#endif } downstream_seqno = 0; diff --git a/src/iodined.c b/src/iodined.c index 3295d44..9a10ace 100644 --- a/src/iodined.c +++ b/src/iodined.c @@ -23,24 +23,32 @@ #include #include #include -#define _XPG4_2 -#include -#include #include -#include -#include #include -#include -#include -#include -#include -#include #include + +#ifdef WINDOWS32 +#include "windows.h" +#include +#else #include #ifdef DARWIN #include #endif +#define _XPG4_2 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#endif +#include "plibc.h" #include "common.h" #include "dns.h" #include "encoding.h" @@ -611,7 +619,7 @@ tunnel_bind(int bind_fd, int dns_fd) { char packet[64*1024]; struct sockaddr_in from; - socklen_t fromlen; + int fromlen; struct fw_query *query; short id; int r; @@ -772,13 +780,14 @@ static int read_dns(int fd, struct query *q) { struct sockaddr_in from; - socklen_t addrlen; + int addrlen; char packet[64*1024]; + int r; +#ifndef WINDOWS32 char address[96]; struct msghdr msg; struct iovec iov; struct cmsghdr *cmsg; - int r; addrlen = sizeof(struct sockaddr); iov.iov_base = packet; @@ -793,12 +802,17 @@ read_dns(int fd, struct query *q) msg.msg_flags = 0; r = recvmsg(fd, &msg, 0); +#else + addrlen = sizeof(struct sockaddr); + r = RECVFROM(fd, packet, sizeof(packet), 0, (struct sockaddr*)&from, &addrlen); +#endif /* !WINDOWS32 */ if (r > 0) { dns_decode(NULL, 0, q, QR_QUERY, packet, r); memcpy((struct sockaddr*)&q->from, (struct sockaddr*)&from, addrlen); q->fromlen = addrlen; +#ifndef WINDOWS32 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) { @@ -809,6 +823,7 @@ read_dns(int fd, struct query *q) break; } } +#endif return strlen(q->name); } else if (r < 0) { @@ -893,7 +908,9 @@ int main(int argc, char **argv) { in_addr_t listen_ip; +#ifndef WINDOWS32 struct passwd *pw; +#endif int foreground; char *username; char *newroot; @@ -1033,10 +1050,12 @@ main(int argc, char **argv) } if (username != NULL) { +#ifndef WINDOWS32 if ((pw = getpwnam(username)) == NULL) { warnx("User %s does not exist!\n", username); usage(); } +#endif } if (mtu <= 0) { @@ -1116,12 +1135,14 @@ main(int argc, char **argv) signal(SIGINT, sigint); if (username != NULL) { +#ifndef WINDOWS32 gid_t gids[1]; gids[0] = pw->pw_gid; if (setgroups(1, gids) < 0 || setgid(pw->pw_gid) < 0 || setuid(pw->pw_uid) < 0) { warnx("Could not switch to user %s!\n", username); usage(); } +#endif } tunnel(tun_fd, dnsd_fd, bind_fd); diff --git a/src/login.c b/src/login.c index 3aee250..cce0f31 100644 --- a/src/login.c +++ b/src/login.c @@ -15,9 +15,14 @@ */ #include -#include #include +#ifdef WINDOWS32 +#include "windows.h" +#else +#include +#endif + #include "md5.h" /* diff --git a/src/osflags b/src/osflags index 2e28ee8..d519695 100755 --- a/src/osflags +++ b/src/osflags @@ -13,6 +13,9 @@ link) Haiku) echo '-lnetwork'; ;; + windows32) + echo '-lplibc -lws2_32'; + ;; esac ;; cflags) diff --git a/src/plibc.h b/src/plibc.h new file mode 100644 index 0000000..6a52c19 --- /dev/null +++ b/src/plibc.h @@ -0,0 +1,577 @@ +/* + This file is part of PlibC. + (C) 2005, 2006 Nils Durner (and other contributing authors) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +/** + * @file include/plibc.h + * @brief PlibC header + * @attention This file is usually not installed under Unix, + * so ship it with your application + * @version $Revision: 1.32 $ + */ + +#ifndef _PLIBC_H_ +#define _PLIBC_H_ + +#ifndef SIGALRM + #define SIGALRM 14 +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef WINDOWS + +#if ENABLE_NLS + #include "langinfo.h" +#endif + +#include +#include +#include +#include +#include +#include +#include +#include + +#define __BYTE_ORDER BYTE_ORDER +#define __BIG_ENDIAN BIG_ENDIAN + +/* Conflicts with our definitions */ +#define __G_WIN32_H__ + +/* Convert LARGE_INTEGER to double */ +#define Li2Double(x) ((double)((x).HighPart) * 4.294967296E9 + \ + (double)((x).LowPart)) + +#define socklen_t int +#define ssize_t int +#ifndef HAVE_FTRUNCATE +#define ftruncate chsize +#endif +#define off_t int +#define int64_t long long +#define int32_t long + +struct stat64 +{ + _dev_t st_dev; + _ino_t st_ino; + _mode_t st_mode; + short st_nlink; + short st_uid; + short st_gid; + _dev_t st_rdev; + __int64 st_size; + __time64_t st_atime; + __time64_t st_mtime; + __time64_t st_ctime; +}; + +#ifndef pid_t + #define pid_t int +#endif + +#ifndef WEXITSTATUS + #define WEXITSTATUS(status) (((status) & 0xff00) >> 8) +#endif + +/* Thanks to the Cygwin project */ +#define ENOCSI 43 /* No CSI structure available */ +#define EL2HLT 44 /* Level 2 halted */ +#ifndef EDEADLK + #define EDEADLK 45 /* Deadlock condition */ +#endif +#ifndef ENOLCK + #define ENOLCK 46 /* No record locks available */ +#endif +#define EBADE 50 /* Invalid exchange */ +#define EBADR 51 /* Invalid request descriptor */ +#define EXFULL 52 /* Exchange full */ +#define ENOANO 53 /* No anode */ +#define EBADRQC 54 /* Invalid request code */ +#define EBADSLT 55 /* Invalid slot */ +#ifndef EDEADLOCK + #define EDEADLOCK EDEADLK /* File locking deadlock error */ +#endif +#define EBFONT 57 /* Bad font file fmt */ +#define ENOSTR 60 /* Device not a stream */ +#define ENODATA 61 /* No data (for no delay io) */ +#define ETIME 62 /* Timer expired */ +#define ENOSR 63 /* Out of streams resources */ +#define ENONET 64 /* Machine is not on the network */ +#define ENOPKG 65 /* Package not installed */ +#define EREMOTE 66 /* The object is remote */ +#define ENOLINK 67 /* The link has been severed */ +#define EADV 68 /* Advertise error */ +#define ESRMNT 69 /* Srmount error */ +#define ECOMM 70 /* Communication error on send */ +#define EPROTO 71 /* Protocol error */ +#define EMULTIHOP 74 /* Multihop attempted */ +#define ELBIN 75 /* Inode is remote (not really error) */ +#define EDOTDOT 76 /* Cross mount point (not really error) */ +#define EBADMSG 77 /* Trying to read unreadable message */ +#define ENOTUNIQ 80 /* Given log. name not unique */ +#define EBADFD 81 /* f.d. invalid for this operation */ +#define EREMCHG 82 /* Remote address changed */ +#define ELIBACC 83 /* Can't access a needed shared lib */ +#define ELIBBAD 84 /* Accessing a corrupted shared lib */ +#define ELIBSCN 85 /* .lib section in a.out corrupted */ +#define ELIBMAX 86 /* Attempting to link in too many libs */ +#define ELIBEXEC 87 /* Attempting to exec a shared library */ +#ifndef ENOSYS + #define ENOSYS 88 /* Function not implemented */ +#endif +#define ENMFILE 89 /* No more files */ +#ifndef ENOTEMPTY + #define ENOTEMPTY 90 /* Directory not empty */ +#endif +#ifndef ENAMETOOLONG + #define ENAMETOOLONG 91 /* File or path name too long */ +#endif +#define ELOOP 92 /* Too many symbolic links */ +#define EOPNOTSUPP 95 /* Operation not supported on transport endpoint */ +#define EPFNOSUPPORT 96 /* Protocol family not supported */ +#define ECONNRESET 104 /* Connection reset by peer */ +#define ENOBUFS 105 /* No buffer space available */ +#define EAFNOSUPPORT 106 /* Address family not supported by protocol family */ +#define EPROTOTYPE 107 /* Protocol wrong type for socket */ +#define ENOTSOCK 108 /* Socket operation on non-socket */ +#define ENOPROTOOPT 109 /* Protocol not available */ +#define ESHUTDOWN 110 /* Can't send after socket shutdown */ +#define ECONNREFUSED 111 /* Connection refused */ +#define EADDRINUSE 112 /* Address already in use */ +#define ECONNABORTED 113 /* Connection aborted */ +#define ENETUNREACH 114 /* Network is unreachable */ +#define ENETDOWN 115 /* Network interface is not configured */ +#ifndef ETIMEDOUT + #define ETIMEDOUT 116 /* Connection timed out */ +#endif +#define EHOSTDOWN 117 /* Host is down */ +#define EHOSTUNREACH 118 /* Host is unreachable */ +#define EINPROGRESS 119 /* Connection already in progress */ +#define EALREADY 120 /* Socket already connected */ +#define EDESTADDRREQ 121 /* Destination address required */ +#define EMSGSIZE 122 /* Message too long */ +#define EPROTONOSUPPORT 123 /* Unknown protocol */ +#define ESOCKTNOSUPPORT 124 /* Socket type not supported */ +#define EADDRNOTAVAIL 125 /* Address not available */ +#define ENETRESET 126 /* Connection aborted by network */ +#define EISCONN 127 /* Socket is already connected */ +#define ENOTCONN 128 /* Socket is not connected */ +#define ETOOMANYREFS 129 /* Too many references: cannot splice */ +#define EPROCLIM 130 /* Too many processes */ +#define EUSERS 131 /* Too many users */ +#define EDQUOT 132 /* Disk quota exceeded */ +#define ESTALE 133 /* Unknown error */ +#ifndef ENOTSUP + #define ENOTSUP 134 /* Not supported */ +#endif +#define ENOMEDIUM 135 /* No medium (in tape drive) */ +#define ENOSHARE 136 /* No such host or network path */ +#define ECASECLASH 137 /* Filename exists with different case */ +#define EWOULDBLOCK EAGAIN /* Operation would block */ +#define EOVERFLOW 139 /* Value too large for defined data type */ + +#undef HOST_NOT_FOUND +#define HOST_NOT_FOUND 1 +#undef TRY_AGAIN +#define TRY_AGAIN 2 +#undef NO_RECOVERY +#define NO_RECOVERY 3 +#undef NO_ADDRESS +#define NO_ADDRESS 4 + +#define PROT_READ 0x1 +#define PROT_WRITE 0x2 +#define MAP_SHARED 0x1 +#define MAP_PRIVATE 0x2 /* unsupported */ +#define MAP_FIXED 0x10 +#define MAP_FAILED ((void *)-1) + +struct statfs +{ + long f_type; /* type of filesystem (see below) */ + long f_bsize; /* optimal transfer block size */ + long f_blocks; /* total data blocks in file system */ + long f_bfree; /* free blocks in fs */ + long f_bavail; /* free blocks avail to non-superuser */ + long f_files; /* total file nodes in file system */ + long f_ffree; /* free file nodes in fs */ + long f_fsid; /* file system id */ + long f_namelen; /* maximum length of filenames */ + long f_spare[6]; /* spare for later */ +}; + +/* Taken from the Wine project + /wine/include/winternl.h */ +enum SYSTEM_INFORMATION_CLASS +{ + SystemBasicInformation = 0, + Unknown1, + SystemPerformanceInformation = 2, + SystemTimeOfDayInformation = 3, /* was SystemTimeInformation */ + Unknown4, + SystemProcessInformation = 5, + Unknown6, + Unknown7, + SystemProcessorPerformanceInformation = 8, + Unknown9, + Unknown10, + SystemDriverInformation, + Unknown12, + Unknown13, + Unknown14, + Unknown15, + SystemHandleList, + Unknown17, + Unknown18, + Unknown19, + Unknown20, + SystemCacheInformation, + Unknown22, + SystemInterruptInformation = 23, + SystemExceptionInformation = 33, + SystemRegistryQuotaInformation = 37, + SystemLookasideInformation = 45 +}; + +typedef struct +{ + LARGE_INTEGER IdleTime; + LARGE_INTEGER KernelTime; + LARGE_INTEGER UserTime; + LARGE_INTEGER Reserved1[2]; + ULONG Reserved2; +} SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION; + +#define sleep(secs) (Sleep(secs * 1000)) + +/*********************** statfs *****************************/ +/* fake block size */ +#define FAKED_BLOCK_SIZE 512 + +/* linux-compatible values for fs type */ +#define MSDOS_SUPER_MAGIC 0x4d44 +#define NTFS_SUPER_MAGIC 0x5346544E + +/*********************** End of statfs ***********************/ + +#define SHUT_RDWR SD_BOTH + +/* Operations for flock() */ +#define LOCK_SH 1 /* shared lock */ +#define LOCK_EX 2 /* exclusive lock */ +#define LOCK_NB 4 /* or'd with one of the above to prevent + blocking */ +#define LOCK_UN 8 /* remove lock */ + +/* Not supported under MinGW */ +#define S_IRGRP 0 +#define S_IWGRP 0 +#define S_IROTH 0 +#define S_IXGRP 0 +#define S_IWOTH 0 +#define S_IXOTH 0 +#define S_ISUID 0 +#define S_ISGID 0 +#define S_ISVTX 0 +#define S_IRWXG 0 +#define S_IRWXO 0 + +#define SetErrnoFromWinError(e) _SetErrnoFromWinError(e, __FILE__, __LINE__) + +/** + * @brief index() - same as strchr() + */ +#define index(s, c) strchr(s, c) + +BOOL _plibc_CreateShortcut(const char *pszSrc, const char *pszDest); +BOOL _plibc_DereferenceShortcut(char *pszShortcut); +char *plibc_ChooseDir(char *pszTitle, unsigned long ulFlags); +char *plibc_ChooseFile(char *pszTitle, unsigned long ulFlags); +long QueryRegistry(HKEY hMainKey, char *pszKey, char *pszSubKey, + char *pszBuffer, long *pdLength); + +BOOL __win_IsHandleMarkedAsBlocking(SOCKET hHandle); +void __win_SetHandleBlockingMode(SOCKET s, BOOL bBlocking); +void __win_DiscardHandleBlockingMode(SOCKET s); +int _win_isSocketValid(int s); +int plibc_conv_to_win_path(const char *pszUnix, char *pszWindows); + +typedef void (*TPanicProc) (int, char *); +void plibc_set_panic_proc(TPanicProc proc); + +int flock(int fd, int operation); +int fsync(int fildes); +int inet_pton(int af, const char *src, void *dst); +int inet_pton4(const char *src, u_char *dst, int pton); +#if USE_IPV6 +int inet_pton6(const char *src, u_char *dst); +#endif +int truncate(const char *fname, int distance); +int statfs(const char *path, struct statfs *buf); +const char *hstrerror(int err); +void gettimeofday(struct timeval *tp, void *tzp); +int mkstemp(char *tmplate); +char *strptime (const char *buf, const char *format, struct tm *tm); +char *ctime(const time_t *clock); +char *ctime_r(const time_t *clock, char *buf); +int plibc_init(char *pszOrg, char *pszApp); +void plibc_shutdown(); +int plibc_conv_to_win_path_ex(const char *pszUnix, char *pszWindows, int derefLinks); +void _SetErrnoFromWinError(long lWinError, char *pszCaller, int iLine); +void SetErrnoFromWinsockError(long lWinError); +void SetHErrnoFromWinError(long lWinError); +void SetErrnoFromHRESULT(HRESULT hRes); +FILE *_win_fopen(const char *filename, const char *mode); +DIR *_win_opendir(const char *dirname); +int _win_open(const char *filename, int oflag, ...); +#ifdef ENABLE_NLS +char *_win_bindtextdomain(const char *domainname, const char *dirname); +#endif +int _win_chdir(const char *path); +int _win_close(int fd); +int _win_creat(const char *path, mode_t mode); +int _win_fstat(int handle, struct stat *buffer); +int _win_pipe(int *phandles); +int _win_rmdir(const char *path); +int _win_access( const char *path, int mode ); +int _win_chmod(const char *filename, int pmode); +char *realpath(const char *file_name, char *resolved_name); +long _win_random(void); +int _win_remove(const char *path); +int _win_rename(const char *oldname, const char *newname); +int _win_stat(const char *path, struct stat *buffer); +int _win_stat64(const char *path, struct stat64 *buffer); +int _win_unlink(const char *filename); +int _win_write(int fildes, const void *buf, size_t nbyte); +int _win_read(int fildes, void *buf, size_t nbyte); +size_t _win_fwrite(const void *buffer, size_t size, size_t count, FILE *stream); +size_t _win_fread( void *buffer, size_t size, size_t count, FILE *stream ); +int _win_symlink(const char *path1, const char *path2); +void *_win_mmap(void *start, size_t len, int access, int flags, int fd, + unsigned long long offset); +int _win_munmap(void *start, size_t length); +int _win_lstat(const char *path, struct stat *buf); +int _win_lstat64(const char *path, struct stat64 *buf); +int _win_readlink(const char *path, char *buf, size_t bufsize); +int _win_accept(SOCKET s, struct sockaddr *addr, int *addrlen); +int _win_printf(const char *format,...); +int _win_fprintf(FILE *f,const char *format,...); +int _win_vprintf(const char *format, va_list ap); +int _win_vfprintf(FILE *stream, const char *format, va_list arg_ptr); +int _win_vsprintf(char *dest,const char *format, va_list arg_ptr); +int _win_vsnprintf(char* str, size_t size, const char *format, va_list arg_ptr); +int _win_snprintf(char *str,size_t size,const char *format,...); +int _win_sprintf(char *dest,const char *format,...); +int _win_vsscanf(const char* str, const char* format, va_list arg_ptr); +int _win_sscanf(const char *str, const char *format, ...); +int _win_vfscanf(FILE *stream, const char *format, va_list arg_ptr); +int _win_vscanf(const char *format, va_list arg_ptr); +int _win_scanf(const char *format, ...); +int _win_fscanf(FILE *stream, const char *format, ...); +pid_t _win_waitpid(pid_t pid, int *stat_loc, int options); +int _win_bind(SOCKET s, const struct sockaddr *name, int namelen); +int _win_connect(SOCKET s,const struct sockaddr *name, int namelen); +int _win_getpeername(SOCKET s, struct sockaddr *name, + int *namelen); +int _win_getsockname(SOCKET s, struct sockaddr *name, + int *namelen); +int _win_getsockopt(SOCKET s, int level, int optname, char *optval, + int *optlen); +int _win_listen(SOCKET s, int backlog); +int _win_recv(SOCKET s, char *buf, int len, int flags); +int _win_recvfrom(SOCKET s, void *buf, int len, int flags, + struct sockaddr *from, int *fromlen); +int _win_select(int max_fd, fd_set * rfds, fd_set * wfds, fd_set * efds, + const struct timeval *tv); +int _win_send(SOCKET s, const char *buf, int len, int flags); +int _win_sendto(SOCKET s, const char *buf, int len, int flags, + const struct sockaddr *to, int tolen); +int _win_setsockopt(SOCKET s, int level, int optname, const void *optval, + int optlen); +int _win_shutdown(SOCKET s, int how); +SOCKET _win_socket(int af, int type, int protocol); +struct hostent *_win_gethostbyaddr(const char *addr, int len, int type); +struct hostent *_win_gethostbyname(const char *name); +char *_win_strerror(int errnum); +int IsWinNT(); + +#if !HAVE_STRNDUP +char *strndup (const char *s, size_t n); +#endif +#if !HAVE_STRNLEN +size_t strnlen (const char *str, size_t maxlen); +#endif + +#define strcasecmp(a, b) stricmp(a, b) +#define strncasecmp(a, b, c) strnicmp(a, b, c) + +#endif /* WINDOWS */ + +#ifndef WINDOWS + #define DIR_SEPARATOR '/' + #define DIR_SEPARATOR_STR "/" + #define NEWLINE "\n" + +#ifdef ENABLE_NLS + #define BINDTEXTDOMAIN(d, n) bindtextdomain(d, n) +#endif + #define CREAT(p, m) creat(p, m) + #undef FOPEN + #define FOPEN(f, m) fopen(f, m) + #define OPENDIR(d) opendir(d) + #define OPEN(f) open(f) + #define CHDIR(d) chdir(d) + #define CLOSE(f) close(f) + #define RMDIR(f) rmdir(f) + #define ACCESS(p, m) access(p, m) + #define CHMOD(f, p) chmod(f, p) + #define FSTAT(h, b) fstat(h, b) + #define PIPE(h) pipe(h) + #define REMOVE(p) remove(p) + #define RENAME(o, n) rename(o, n) + #define STAT(p, b) stat(p, b) + #define STAT64(p, b) stat64(p, b) + #define UNLINK(f) unlink(f) + #define WRITE(f, b, n) write(f, b, n) + #define READ(f, b, n) read(f, b, n) + #define GN_FREAD(b, s, c, f) fread(b, s, c, f) + #define GN_FWRITE(b, s, c, f) fwrite(b, s, c, f) + #define SYMLINK(a, b) symlink(a, b) + #define MMAP(s, l, p, f, d, o) mmap(s, l, p, f, d, o) + #define MUNMAP(s, l) munmap(s, l) + #define STRERROR(i) strerror(i) + #define RANDOM() random() + #define READLINK(p, b, s) readlink(p, b, s) + #define LSTAT(p, b) lstat(p, b) + #define LSTAT64(p, b) lstat64(p, b) + #define PRINTF printf + #define FPRINTF fprintf + #define VPRINTF(f, a) vprintf(f, a) + #define VFPRINTF(s, f, a) vfprintf(s, f, a) + #define VSPRINTF(d, f, a) vsprintf(d, f, a) + #define VSNPRINTF(str, size, fmt, a) vsnprintf(str, size, fmt, a) + #define _REAL_SNPRINTF snprintf + #define SPRINTF sprintf + #define VSSCANF(s, f, a) vsscanf(s, f, a) + #define SSCANF sscanf + #define VFSCANF(s, f, a) vfscanf(s, f, a) + #define VSCANF(f, a) vscanf(f, a) + #define SCANF scanf + #define FSCANF fscanf + #define WAITPID(p, s, o) waitpid(p, s, o) + #define ACCEPT(s, a, l) accept(s, a, l) + #define BIND(s, n, l) bind(s, n, l) + #define CONNECT(s, n, l) connect(s, n, l) + #define GETPEERNAME(s, n, l) getpeername(s, n, l) + #define GETSOCKNAME(s, n, l) getsockname(s, n, l) + #define GETSOCKOPT(s, l, o, v, p) getsockopt(s, l, o, v, p) + #define LISTEN(s, b) listen(s, b) + #define RECV(s, b, l, f) recv(s, b, l, f) + #define RECVFROM(s, b, l, f, r, o) recvfrom(s, b, l, f, r, o) + #define SELECT(n, r, w, e, t) select(n, r, w, e, t) + #define SEND(s, b, l, f) send(s, b, l, f) + #define SENDTO(s, b, l, f, o, n) sendto(s, b, l, f, o, n) + #define SETSOCKOPT(s, l, o, v, n) setsockopt(s, l, o, v, n) + #define SHUTDOWN(s, h) shutdown(s, h) + #define SOCKET(a, t, p) socket(a, t, p) + #define GETHOSTBYADDR(a, l, t) gethostbyname(a, l, t) + #define GETHOSTBYNAME(n) gethostbyname(n) +#else + #define DIR_SEPARATOR '\\' + #define DIR_SEPARATOR_STR "\\" + #define NEWLINE "\r\n" + +#ifdef ENABLE_NLS + #define BINDTEXTDOMAIN(d, n) _win_bindtextdomain(d, n) +#endif + #define CREAT(p, m) _win_creat(p, m) + #define FOPEN(f, m) _win_fopen(f, m) + #define OPENDIR(d) _win_opendir(d) + #define OPEN(f) _win_open(f) + #define CHDIR(d) _win_chdir(d) + #define CLOSE(f) _win_close(f) + #define FSTAT(h, b) _win_fstat(h, b) + #define RMDIR(f) _win_rmdir(f) + #define ACCESS(p, m) _win_access(p, m) + #define CHMOD(f, p) _win_chmod(f, p) + #define PIPE(h) _win_pipe(h) + #define RANDOM() _win_random() + #define REMOVE(p) _win_remove(p) + #define RENAME(o, n) _win_rename(o, n) + #define STAT(p, b) _win_stat(p, b) + #define STAT64(p, b) _win_stat64(p, b) + #define UNLINK(f) _win_unlink(f) + #define WRITE(f, b, n) _win_write(f, b, n) + #define READ(f, b, n) _win_read(f, b, n) + #define GN_FREAD(b, s, c, f) _win_fread(b, s, c, f) + #define GN_FWRITE(b, s, c, f) _win_fwrite(b, s, c, f) + #define SYMLINK(a, b) _win_symlink(a, b) + #define MMAP(s, l, p, f, d, o) _win_mmap(s, l, p, f, d, o) + #define MUNMAP(s, l) _win_munmap(s, l) + #define STRERROR(i) _win_strerror(i) + #define READLINK(p, b, s) _win_readlink(p, b, s) + #define LSTAT(p, b) _win_lstat(p, b) + #define LSTAT64(p, b) _win_lstat64(p, b) + #define PRINTF(f, ...) _win_printf(f , __VA_ARGS__) + #define FPRINTF(fil, fmt, ...) _win_fprintf(fil, fmt, __VA_ARGS__) + #define VPRINTF(f, a) _win_vprintf(f, a) + #define VFPRINTF(s, f, a) _win_vfprintf(s, f, a) + #define VSPRINTF(d, f, a) _win_vsprintf(d, f, a) + #define VSNPRINTF(str, size, fmt, a) _win_vsnprintf(str, size, fmt, a) + #define _REAL_SNPRINTF(str, size, fmt, ...) _win_snprintf(str, size, fmt, __VA_ARGS__) + #define SPRINTF(d, f, ...) _win_sprintf(d, f, __VA_ARGS__) + #define VSSCANF(s, f, a) _win_vsscanf(s, f, a) + #define SSCANF(s, f, ...) _win_sscanf(s, f, __VA_ARGS__) + #define VFSCANF(s, f, a) _win_vfscanf(s, f, a) + #define VSCANF(f, a) _win_vscanf(f, a) + #define SCANF(f, ...) _win_scanf(f, __VA_ARGS__) + #define FSCANF(s, f, ...) _win_fscanf(s, f, __VA_ARGS__) + #define WAITPID(p, s, o) _win_waitpid(p, s, o) + #define ACCEPT(s, a, l) _win_accept(s, a, l) + #define BIND(s, n, l) _win_bind(s, n, l) + #define CONNECT(s, n, l) _win_connect(s, n, l) + #define GETPEERNAME(s, n, l) _win_getpeername(s, n, l) + #define GETSOCKNAME(s, n, l) _win_getsockname(s, n, l) + #define GETSOCKOPT(s, l, o, v, p) _win_getsockopt(s, l, o, v, p) + #define LISTEN(s, b) _win_listen(s, b) + #define RECV(s, b, l, f) _win_recv(s, b, l, f) + #define RECVFROM(s, b, l, f, r, o) _win_recvfrom(s, b, l, f, r, o) + #define SELECT(n, r, w, e, t) _win_select(n, r, w, e, t) + #define SEND(s, b, l, f) _win_send(s, b, l, f) + #define SENDTO(s, b, l, f, o, n) _win_sendto(s, b, l, f, o, n) + #define SETSOCKOPT(s, l, o, v, n) _win_setsockopt(s, l, o, v, n) + #define SHUTDOWN(s, h) _win_shutdown(s, h) + #define SOCKET(a, t, p) _win_socket(a, t, p) + #define GETHOSTBYADDR(a, l, t) _win_gethostbyname(a, l, t) + #define GETHOSTBYNAME(n) _win_gethostbyname(n) +#endif + + +#ifdef __cplusplus +} +#endif + + +#endif /*_PLIBC_H_*/ + +/* end of plibc.h */ diff --git a/src/tun.c b/src/tun.c index ba2be65..f342cce 100644 --- a/src/tun.c +++ b/src/tun.c @@ -23,16 +23,25 @@ #include #include #include + +#ifdef WINDOWS32 +#include +#include "windows.h" +#else #include #include #include +#endif +#include "plibc.h" #include "tun.h" +#include "common.h" #define TUN_MAX_TRY 50 char if_name[50]; +#ifndef WINDOWS32 #ifdef LINUX #include @@ -134,6 +143,13 @@ open_tun(const char *tun_device) } #endif /* !LINUX */ +#else /* WINDOWS32 */ +int +open_tun(const char *tun_device) +{ + return 10; +} +#endif void close_tun(int tun_fd) @@ -183,6 +199,7 @@ read_tun(int tun_fd, char *buf, size_t len) int tun_setip(const char *ip, int netbits) { +#ifndef WINDOWS32 char cmdline[512]; int netmask; struct in_addr net; @@ -224,11 +241,16 @@ tun_setip(const char *ip, int netbits) } return 1; +#else /* WINDOWS32 */ + + return 0; +#endif } int tun_setmtu(const unsigned mtu) { +#ifndef WINDOWS32 char cmdline[512]; if (mtu > 200 && mtu < 1500) { @@ -244,5 +266,9 @@ tun_setmtu(const unsigned mtu) } return 1; +#else /* WINDOWS32 */ + + return 0; +#endif } diff --git a/src/user.c b/src/user.c index 01b4021..8c3bb40 100644 --- a/src/user.c +++ b/src/user.c @@ -21,13 +21,18 @@ #include #include #include -#include -#include #include -#include #include + +#ifdef WINDOWS32 +#include +#else #include #include +#include +#include +#include +#endif #include "common.h" #include "encoding.h" diff --git a/src/windows.h b/src/windows.h index e69de29..7588d15 100644 --- a/src/windows.h +++ b/src/windows.h @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2006-2009 Bjorn Andersson , Erik Ekman + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef __FIX_WINDOWS_H__ +#define __FIX_WINDOWS_H__ + +typedef unsigned int in_addr_t; + +/* Bad err.h fix */ +#define warn printf +#define warnx printf +#define err(a, b, ...) do{printf("%s ", strerror(errno)); \ + printf(b, ## __VA_ARGS__); exit(a);}while(0) +#define errx(a, b, ...) do{printf(b, ## __VA_ARGS__); exit(a);}while(0) + +#include +#include +#include + +#define T_A DNS_TYPE_A +#define T_NS DNS_TYPE_NS +#define T_NULL DNS_TYPE_NULL + +#define C_IN 1 + +#define SERVFAIL 2 +#define NXDOMAIN 3 +#define NOTIMP 4 +#define REFUSED 5 + +typedef struct { + unsigned id :16; /* query identification number */ + /* fields in third byte */ + unsigned rd :1; /* recursion desired */ + unsigned tc :1; /* truncated message */ + unsigned aa :1; /* authoritive answer */ + unsigned opcode :4; /* purpose of message */ + unsigned qr :1; /* response flag */ + /* fields in fourth byte */ + unsigned rcode :4; /* response code */ + unsigned cd: 1; /* checking disabled by resolver */ + unsigned ad: 1; /* authentic data from named */ + unsigned unused :1; /* unused bits (MBZ as of 4.9.3a3) */ + unsigned ra :1; /* recursion available */ + /* remaining bytes */ + unsigned qdcount :16; /* number of question entries */ + unsigned ancount :16; /* number of answer entries */ + unsigned nscount :16; /* number of authority entries */ + unsigned arcount :16; /* number of resource entries */ +} HEADER; + +struct ip + { + unsigned int ip_hl:4; /* header length */ + unsigned int ip_v:4; /* version */ + u_char ip_tos; /* type of service */ + u_short ip_len; /* total length */ + u_short ip_id; /* identification */ + u_short ip_off; /* fragment offset field */ +#define IP_RF 0x8000 /* reserved fragment flag */ +#define IP_DF 0x4000 /* dont fragment flag */ +#define IP_MF 0x2000 /* more fragments flag */ +#define IP_OFFMASK 0x1fff /* mask for fragmenting bits */ + u_char ip_ttl; /* time to live */ + u_char ip_p; /* protocol */ + u_short ip_sum; /* checksum */ + struct in_addr ip_src, ip_dst; /* source and dest address */ + }; + + +#endif