From 196cd7a443bc09ac1485cce0e82e472a46a78152 Mon Sep 17 00:00:00 2001 From: fsb4000 Date: Fri, 28 Nov 2014 14:34:01 +0600 Subject: [PATCH] MSVC compatibility for STUN module implementation. --- MSVC/libcommon/libcommon.vcxproj | 2 + MSVC/libcommon/libcommon.vcxproj.filters | 6 ++ src/ministun.h | 76 +++++++++++++++++++++-------- src/stun.cpp | 10 +++- 4 files changed, 71 insertions(+), 23 deletions(-) diff --git a/MSVC/libcommon/libcommon.vcxproj b/MSVC/libcommon/libcommon.vcxproj index fda7fda..8abc579 100644 --- a/MSVC/libcommon/libcommon.vcxproj +++ b/MSVC/libcommon/libcommon.vcxproj @@ -21,6 +21,7 @@ + @@ -74,6 +75,7 @@ + diff --git a/MSVC/libcommon/libcommon.vcxproj.filters b/MSVC/libcommon/libcommon.vcxproj.filters index 20c4f7a..9d1b8d5 100644 --- a/MSVC/libcommon/libcommon.vcxproj.filters +++ b/MSVC/libcommon/libcommon.vcxproj.filters @@ -107,6 +107,9 @@ Source Files + + Source Files + @@ -223,5 +226,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/src/ministun.h b/src/ministun.h index 8b19df8..4dbd186 100644 --- a/src/ministun.h +++ b/src/ministun.h @@ -20,30 +20,64 @@ #define STUN_COUNT 3 #define STUN_TIMEOUT 3 -typedef struct { unsigned int id[4]; } __attribute__((packed)) stun_trans_id; +#ifndef _MSC_VER + typedef struct { unsigned int id[4]; } __attribute__((packed)) stun_trans_id; -struct stun_header { - unsigned short msgtype; - unsigned short msglen; - stun_trans_id id; - unsigned char ies[0]; -} __attribute__((packed)); + struct stun_header { + unsigned short msgtype; + unsigned short msglen; + stun_trans_id id; + unsigned char ies[0]; + } __attribute__((packed)); -struct stun_attr { - unsigned short attr; - unsigned short len; - unsigned char value[0]; -} __attribute__((packed)); + struct stun_attr { + unsigned short attr; + unsigned short len; + unsigned char value[0]; + } __attribute__((packed)); -/* - * The format normally used for addresses carried by STUN messages. - */ -struct stun_addr { - unsigned char unused; - unsigned char family; - unsigned short port; - unsigned int addr; -} __attribute__((packed)); + /* + * The format normally used for addresses carried by STUN messages. + */ + + struct stun_addr { + unsigned char unused; + unsigned char family; + unsigned short port; + unsigned int addr; + } __attribute__((packed)); +#else + typedef struct { unsigned int id[4]; } stun_trans_id; + +#pragma pack(push, 1) + struct stun_header { + unsigned short msgtype; + unsigned short msglen; + stun_trans_id id; + unsigned char ies[0]; + }; +#pragma pack(pop) + +#pragma pack(push, 1) + struct stun_attr { + unsigned short attr; + unsigned short len; + unsigned char value[0]; + }; +#pragma pack(pop) + + /* + * The format normally used for addresses carried by STUN messages. + */ +#pragma pack(push, 1) + struct stun_addr { + unsigned char unused; + unsigned char family; + unsigned short port; + unsigned int addr; + }; +#pragma pack(pop) +#endif #define STUN_IGNORE (0) #define STUN_ACCEPT (1) diff --git a/src/stun.cpp b/src/stun.cpp index ee45833..f724eaa 100644 --- a/src/stun.cpp +++ b/src/stun.cpp @@ -41,11 +41,14 @@ #include #include #endif +#ifndef _MSC_VER #include +#else +#include +#endif #include #include - #include "ministun.h" /*---------------------------------------------------------------------*/ @@ -506,8 +509,11 @@ static int StunRequest(const char *host, uint16_t port, struct sockaddr_in *mapp int rc = -3; if(bind(sock, (struct sockaddr*)&client, sizeof(client)) >= 0) rc = StunRequest2(sock, &server, mapped); - +#ifndef _MSC_VER close(sock); +#else + _close(sock); +#endif return rc; } // StunRequest -- 1.7.1