diff options
| author | tomas <tomas@0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e> | 2008-07-14 11:45:35 +0000 |
|---|---|---|
| committer | tomas <tomas@0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e> | 2008-07-14 11:45:35 +0000 |
| commit | 8284a34173b34c0b6e5ffa9158dfceb4d286ecbc (patch) | |
| tree | 76083e7886d9a67bef3c845678f7e1af60e9a790 | |
| parent | e792ee9cbe5191fcc81273b65bdb4ac9108435d5 (diff) | |
- port net/ na win32
- pridany inet_ntop () a inet_pton () pro win32 kvuli kompatibilite zdrojoveho kodu
git-svn-id: http://opensvn.csie.org/tuxanci_ng@128 0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e
| -rw-r--r-- | config.h | 2 | ||||
| -rw-r--r-- | src/base/main.c | 17 | ||||
| -rw-r--r-- | src/net/dns.c | 13 | ||||
| -rw-r--r-- | src/net/udp.c | 118 | ||||
| -rw-r--r-- | src/net/udp.h | 13 |
5 files changed, 142 insertions, 21 deletions
@@ -1 +1 @@ -#define TUXANCI_NG_VERSION "svn127" +#define TUXANCI_NG_VERSION "svn128" diff --git a/src/base/main.c b/src/base/main.c index ebc2018..60a6066 100644 --- a/src/base/main.c +++ b/src/base/main.c @@ -8,6 +8,11 @@ #include <assert.h> #include <sys/stat.h> +#ifdef __WIN32__ +# include <windows.h> +# include <wininet.h> +#endif + #include "main.h" #include "tux.h" @@ -122,6 +127,18 @@ int main(int argc, char *argv[]) exit(0); */ +#ifdef __WIN32__ + WORD wVersionRequested = MAKEWORD(1,1); // WinSock version + WSADATA data; // WinSock information structure + + /* Let's initialize WinSock */ + if( WSAStartup(wVersionRequested, &data) != 0 ) + { + fprintf(stderr, "WinSock initialization failed !\nProgram shutdown !\n"); + exit(-1); + } +#endif + #ifndef PUBLIC_SERVER startGame(); #endif diff --git a/src/net/dns.c b/src/net/dns.c index 0910a74..657bbea 100644 --- a/src/net/dns.c +++ b/src/net/dns.c @@ -1,8 +1,13 @@ -#include <sys/socket.h> -#include <netinet/in.h> -#include <arpa/inet.h> +#ifndef __WIN32__ +# include <sys/socket.h> +# include <netinet/in.h> +# include <arpa/inet.h> +# include <netdb.h> +#else +# include <windows.h> +# include <wininet.h> +#endif -#include <netdb.h> #include <string.h> #include "main.h" diff --git a/src/net/udp.c b/src/net/udp.c index 1438e4e..1ec1d2f 100644 --- a/src/net/udp.c +++ b/src/net/udp.c @@ -1,19 +1,24 @@ -#include <sys/socket.h> -#include <sys/types.h> -#include <netinet/in.h> -#include <arpa/inet.h> -#include <netdb.h> +#ifndef __WIN32__ +# include <sys/socket.h> +# include <sys/types.h> +# include <netinet/in.h> +# include <arpa/inet.h> +# include <netdb.h> +#else +# include <windows.h> +# include <wininet.h> +#endif + #include <unistd.h> #include <stdlib.h> -#include <string.h> #include <stdio.h> -#include <netdb.h> #include <string.h> #include <assert.h> #include <fcntl.h> + #define BUFSIZE 1000 #include "main.h" @@ -21,6 +26,58 @@ #include "udp.h" +#ifdef __WIN32__ +const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt) +{ + if ( af == AF_INET ) + { + struct sockaddr_in in; + memset(&in, 0, sizeof(in)); + in.sin_family = AF_INET; + memcpy(&in.sin_addr, src, sizeof(struct in_addr)); + getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in), dst, cnt, NULL, 0, NI_NUMERICHOST); + + return dst; + } + else if ( af == AF_INET6 ) + { + struct sockaddr_in6 in; + memset(&in, 0, sizeof(in)); + in.sin6_family = AF_INET6; + memcpy(&in.sin6_addr, src, sizeof(struct in_addr6)); + getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in6), dst, cnt, NULL, 0, NI_NUMERICHOST); + + return dst; + } + + return NULL; +} + +int inet_pton(int af, const char *src, void *dst) +{ + struct addrinfo hints, *res, *ressave; + + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_family = af; + + if ( getaddrinfo(src, NULL, &hints, &res) != 0 ) + { + return -1; + } + + ressave = res; + + while ( res ) + { + memcpy(dst, res->ai_addr, res->ai_addrlen); + res = res->ai_next; + } + + freeaddrinfo(ressave); + return 0; +} +#endif + sock_udp_t* newSockUdp(int proto) { sock_udp_t *new; @@ -65,6 +122,9 @@ sock_udp_t* bindUdpSocket(char *address, int port, int proto) { fprintf(stderr, "nemozem vytvorit socket !\n"); destroySockUdp(new); +#ifdef __WIN32__ + WSACleanup(); +#endif return NULL; } @@ -94,6 +154,9 @@ sock_udp_t* bindUdpSocket(char *address, int port, int proto) { fprintf(stderr, "nemozem nastavit sokcet %s %d !\n", address, port); destroySockUdp(new); +#ifdef __WIN32__ + WSACleanup(); +#endif return NULL; } @@ -127,6 +190,9 @@ sock_udp_t* connectUdpSocket(char *address, int port, int proto) { fprintf(stderr, "nemozem vytvorit socket !\n"); destroySockUdp(new); +#ifdef __WIN32__ + WSACleanup(); +#endif return NULL; } @@ -152,15 +218,25 @@ sock_udp_t* connectUdpSocket(char *address, int port, int proto) int setUdpSockNonBlock(sock_udp_t *p) { + /* Set to nonblocking socket mode */ +#ifndef __WIN32__ int oldFlag; oldFlag = fcntl (p->sock, F_GETFL, 0); - if( fcntl(p->sock, F_SETFL, oldFlag|O_NONBLOCK ) == -1 ) + if( fcntl(p->sock, F_SETFL, oldFlag | O_NONBLOCK) == -1 ) { return -1; } - +#else + unsigned long arg = 1; + // Operation is FIONBIO. Parameter is pointer on non-zero number. + if( ioctlsocket(p->sock, FIONBIO, &arg) == SOCKET_ERROR ) + { + WSACleanup(); + return -1; + } +#endif return 0; } @@ -177,8 +253,11 @@ int readUdpSocket(sock_udp_t *src, sock_udp_t *dst, void *address, int len) { addrlen = sizeof(src->sockAddr); - size = recvfrom(src->sock, address, len, 0, - (struct sockaddr *)&dst->sockAddr, (socklen_t *)&addrlen); +#ifndef __WIN32 + size = recvfrom(src->sock, address, len, 0, (struct sockaddr *)&dst->sockAddr, (socklen_t *)&addrlen); +#else + size = recvfrom(src->sock, address, len, 0, (struct sockaddr *)&dst->sockAddr, (int *)&addrlen); +#endif } #ifdef SUPPORT_IPv6 @@ -198,6 +277,9 @@ int readUdpSocket(sock_udp_t *src, sock_udp_t *dst, void *address, int len) getSockUdpIp(dst, str_ip, STR_IP_SIZE); fprintf(stderr, "nemozem zapisat zo socketu %d %s %d!\n", size, str_ip, getSockUdpPort(dst)); +#ifdef __WIN32__ + WSACleanup(); +#endif return -1; } @@ -217,8 +299,7 @@ int writeUdpSocket(sock_udp_t *src, sock_udp_t *dst, void *address, int len) { addrlen = sizeof(src->sockAddr); - size = sendto(src->sock, address, len, 0, - (struct sockaddr *)&dst->sockAddr, (socklen_t)addrlen); + size = sendto(src->sock, address, len, 0, (struct sockaddr *)&dst->sockAddr, addrlen); } #ifdef SUPPORT_IPv6 @@ -226,8 +307,7 @@ int writeUdpSocket(sock_udp_t *src, sock_udp_t *dst, void *address, int len) { addrlen = sizeof(src->sockAddr6); - size = sendto(src->sock, address, len, 0, - (struct sockaddr *)&dst->sockAddr6, (socklen_t)addrlen); + size = sendto(src->sock, address, len, 0, (struct sockaddr *)&dst->sockAddr6, addrlen); } #endif @@ -238,6 +318,9 @@ int writeUdpSocket(sock_udp_t *src, sock_udp_t *dst, void *address, int len) getSockUdpIp(dst, str_ip, STR_IP_SIZE); fprintf(stderr, "nemozem zapisat na socket %d %s %d!\n", size, str_ip, getSockUdpPort(dst)); +#ifdef __WIN32__ + WSACleanup(); +#endif return -1; } @@ -291,7 +374,12 @@ void closeUdpSocket(sock_udp_t *p) { assert( p != NULL ); +#ifndef __WIN32__ close(p->sock); +#else + closesocket(p->sock); + WSACleanup(); +#endif destroySockUdp(p); } diff --git a/src/net/udp.h b/src/net/udp.h index a9a38f6..ded5a6d 100644 --- a/src/net/udp.h +++ b/src/net/udp.h @@ -1,15 +1,26 @@ #ifndef MY_UDP -#include <netinet/in.h> +#ifndef __WIN32__ +# include <netinet/in.h> +#else +# include <windows.h> +# include <wininet.h> +#endif #define MY_UDP +#ifdef __WIN32__ #define SUPPORT_IPv6 +#endif typedef struct struct_sock_udp_t { +#ifndef __WIN32__ int sock; +#else + SOCKET sock; +#endif int proto; #ifdef SUPPORT_IPv6 |