diff options
Diffstat (limited to 'pjlib-util')
22 files changed, 4792 insertions, 155 deletions
diff --git a/pjlib-util/build/Makefile b/pjlib-util/build/Makefile index 7973eadf..f0973106 100644 --- a/pjlib-util/build/Makefile +++ b/pjlib-util/build/Makefile @@ -27,7 +27,7 @@ export _LDFLAGS := $(subst /,$(HOST_PSEP),$(PJLIB_UTIL_LIB)) \ export PJLIB_UTIL_SRCDIR = ../src/pjlib-util export PJLIB_UTIL_OBJS += $(OS_OBJS) $(M_OBJS) $(CC_OBJS) $(HOST_OBJS) \ errno.o dns.o dns_dump.o getopt.o md5.o resolver.o \ - scanner.o stun.o string.o stun_client.o xml.o + scanner.o string.o stun_simple.o stun_simple_client.o xml.o export PJLIB_UTIL_CFLAGS += $(_CFLAGS) ############################################################################### diff --git a/pjlib-util/build/pjlib_util.dsp b/pjlib-util/build/pjlib_util.dsp index 3941a623..c24f6342 100644 --- a/pjlib-util/build/pjlib_util.dsp +++ b/pjlib-util/build/pjlib_util.dsp @@ -129,11 +129,27 @@ SOURCE="..\src\pjlib-util\string.c" # End Source File
# Begin Source File
-SOURCE="..\src\pjlib-util\stun.c"
+SOURCE="..\src\pjlib-util\stun_endpoint.c"
# End Source File
# Begin Source File
-SOURCE="..\src\pjlib-util\stun_client.c"
+SOURCE="..\src\pjlib-util\stun_msg.c"
+# End Source File
+# Begin Source File
+
+SOURCE="..\src\pjlib-util\stun_server.c"
+# End Source File
+# Begin Source File
+
+SOURCE="..\src\pjlib-util\stun_simple.c"
+# End Source File
+# Begin Source File
+
+SOURCE="..\src\pjlib-util\stun_simple_client.c"
+# End Source File
+# Begin Source File
+
+SOURCE="..\src\pjlib-util\stun_transaction.c"
# End Source File
# Begin Source File
@@ -193,7 +209,27 @@ SOURCE="..\include\pjlib-util\string.h" # End Source File
# Begin Source File
-SOURCE="..\include\pjlib-util\stun.h"
+SOURCE="..\include\pjlib-util\stun_doc.h"
+# End Source File
+# Begin Source File
+
+SOURCE="..\include\pjlib-util\stun_endpoint.h"
+# End Source File
+# Begin Source File
+
+SOURCE="..\include\pjlib-util\stun_msg.h"
+# End Source File
+# Begin Source File
+
+SOURCE="..\include\pjlib-util\stun_server.h"
+# End Source File
+# Begin Source File
+
+SOURCE="..\include\pjlib-util\stun_simple.h"
+# End Source File
+# Begin Source File
+
+SOURCE="..\include\pjlib-util\stun_transaction.h"
# End Source File
# Begin Source File
diff --git a/pjlib-util/build/pjlib_util.dsw b/pjlib-util/build/pjlib_util.dsw index b2bcb1a0..5831c8c3 100644 --- a/pjlib-util/build/pjlib_util.dsw +++ b/pjlib-util/build/pjlib_util.dsw @@ -19,10 +19,6 @@ Project: "pjlib_test"="..\..\pjlib\build\pjlib_test.dsp" - Package Owner=<4> Package=<5>
{{{
- begin source code control
- "$/pjproject/pjlib/build", UIAAAAAA
- ..\..\pjlib\build
- end source code control
}}}
Package=<4>
@@ -61,6 +57,24 @@ Package=<4> ###############################################################################
+Project: "pjstun_srv"=".\pjstun_srv.dsp" - Package Owner=<4>
+
+Package=<5>
+{{{
+}}}
+
+Package=<4>
+{{{
+ Begin Project Dependency
+ Project_Dep_Name pjlib
+ End Project Dependency
+ Begin Project Dependency
+ Project_Dep_Name pjlib_util
+ End Project Dependency
+}}}
+
+###############################################################################
+
Global:
Package=<5>
diff --git a/pjlib-util/build/pjlib_util.vcproj b/pjlib-util/build/pjlib_util.vcproj index a393fd44..0ba86c6f 100644 --- a/pjlib-util/build/pjlib_util.vcproj +++ b/pjlib-util/build/pjlib_util.vcproj @@ -399,48 +399,12 @@ </FileConfiguration>
</File>
<File
- RelativePath="..\src\pjlib-util\stun.c"
+ RelativePath="..\src\pjlib-util\stun_simple.c"
>
- <FileConfiguration
- Name="Release|Win32"
- >
- <Tool
- Name="VCCLCompilerTool"
- AdditionalIncludeDirectories=""
- PreprocessorDefinitions=""
- />
- </FileConfiguration>
- <FileConfiguration
- Name="Debug|Win32"
- >
- <Tool
- Name="VCCLCompilerTool"
- AdditionalIncludeDirectories=""
- PreprocessorDefinitions=""
- />
- </FileConfiguration>
</File>
<File
- RelativePath="..\src\pjlib-util\stun_client.c"
+ RelativePath="..\src\pjlib-util\stun_simple_client.c"
>
- <FileConfiguration
- Name="Release|Win32"
- >
- <Tool
- Name="VCCLCompilerTool"
- AdditionalIncludeDirectories=""
- PreprocessorDefinitions=""
- />
- </FileConfiguration>
- <FileConfiguration
- Name="Debug|Win32"
- >
- <Tool
- Name="VCCLCompilerTool"
- AdditionalIncludeDirectories=""
- PreprocessorDefinitions=""
- />
- </FileConfiguration>
</File>
<File
RelativePath="..\src\pjlib-util\symbols.c"
@@ -536,7 +500,7 @@ >
</File>
<File
- RelativePath="..\include\pjlib-util\stun.h"
+ RelativePath="..\include\pjlib-util\stun_simple.h"
>
</File>
<File
diff --git a/pjlib-util/build/pjstun_srv.dsp b/pjlib-util/build/pjstun_srv.dsp new file mode 100644 index 00000000..61774d57 --- /dev/null +++ b/pjlib-util/build/pjstun_srv.dsp @@ -0,0 +1,106 @@ +# Microsoft Developer Studio Project File - Name="pjstun_srv" - Package Owner=<4>
+# Microsoft Developer Studio Generated Build File, Format Version 6.00
+# ** DO NOT EDIT **
+
+# TARGTYPE "Win32 (x86) Console Application" 0x0103
+
+CFG=pjstun_srv - Win32 Debug
+!MESSAGE This is not a valid makefile. To build this project using NMAKE,
+!MESSAGE use the Export Makefile command and run
+!MESSAGE
+!MESSAGE NMAKE /f "pjstun_srv.mak".
+!MESSAGE
+!MESSAGE You can specify a configuration when running NMAKE
+!MESSAGE by defining the macro CFG on the command line. For example:
+!MESSAGE
+!MESSAGE NMAKE /f "pjstun_srv.mak" CFG="pjstun_srv - Win32 Debug"
+!MESSAGE
+!MESSAGE Possible choices for configuration are:
+!MESSAGE
+!MESSAGE "pjstun_srv - Win32 Release" (based on "Win32 (x86) Console Application")
+!MESSAGE "pjstun_srv - Win32 Debug" (based on "Win32 (x86) Console Application")
+!MESSAGE
+
+# Begin Project
+# PROP AllowPerConfigDependencies 0
+# PROP Scc_ProjName ""
+# PROP Scc_LocalPath ""
+CPP=cl.exe
+RSC=rc.exe
+
+!IF "$(CFG)" == "pjstun_srv - Win32 Release"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 0
+# PROP BASE Output_Dir "./output/pjstun-srv-i386-win32-vc6-release"
+# PROP BASE Intermediate_Dir "./output/pjstun-srv-i386-win32-vc6-release"
+# PROP BASE Target_Dir ""
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 0
+# PROP Output_Dir "./output/pjstun-srv-i386-win32-vc6-release"
+# PROP Intermediate_Dir "./output/pjstun-srv-i386-win32-vc6-release"
+# PROP Ignore_Export_Lib 0
+# PROP Target_Dir ""
+# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
+# ADD CPP /nologo /MD /W3 /GX /O2 /I "../include" /I "../../pjlib/include" /D "NDEBUG" /D "WIN32" /D "_CONSOLE" /D "_MBCS" /D PJ_WIN32=1 /D PJ_M_I386=1 /FR /YX /FD /c
+# ADD BASE RSC /l 0x409 /d "NDEBUG"
+# ADD RSC /l 0x409 /d "NDEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+LINK32=link.exe
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
+# ADD LINK32 netapi32.lib mswsock.lib ws2_32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 /out:"../bin/pjstun-srv-i386-win32-vc6-release.exe"
+
+!ELSEIF "$(CFG)" == "pjstun_srv - Win32 Debug"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 1
+# PROP BASE Output_Dir "./output/pjstun-srv-i386-win32-vc6-debug"
+# PROP BASE Intermediate_Dir "./output/pjstun-srv-i386-win32-vc6-debug"
+# PROP BASE Target_Dir ""
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 1
+# PROP Output_Dir "./output/pjstun-srv-i386-win32-vc6-debug"
+# PROP Intermediate_Dir "./output/pjstun-srv-i386-win32-vc6-debug"
+# PROP Ignore_Export_Lib 0
+# PROP Target_Dir ""
+# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
+# ADD CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /I "../include" /I "../../pjlib/include" /D "_DEBUG" /D "WIN32" /D "_CONSOLE" /D "_MBCS" /D PJ_WIN32=1 /D PJ_M_I386=1 /FR /YX /FD /GZ /c
+# ADD BASE RSC /l 0x409 /d "_DEBUG"
+# ADD RSC /l 0x409 /d "_DEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+LINK32=link.exe
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
+# ADD LINK32 netapi32.lib ws2_32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /out:"../bin/pjstun-srv-i386-win32-vc6-debug.exe" /pdbtype:sept
+
+!ENDIF
+
+# Begin Target
+
+# Name "pjstun_srv - Win32 Release"
+# Name "pjstun_srv - Win32 Debug"
+# Begin Group "Source Files"
+
+# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
+# Begin Source File
+
+SOURCE="..\src\pjstun-srv\server_main.c"
+# End Source File
+# End Group
+# Begin Group "Header Files"
+
+# PROP Default_Filter "h;hpp;hxx;hm;inl"
+# Begin Source File
+
+SOURCE="..\src\pjstun-srv\server.h"
+# End Source File
+# End Group
+# Begin Group "Resource Files"
+
+# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
+# End Group
+# End Target
+# End Project
diff --git a/pjlib-util/include/pjlib-util.h b/pjlib-util/include/pjlib-util.h index 38f4ea1a..0f7848a8 100644 --- a/pjlib-util/include/pjlib-util.h +++ b/pjlib-util/include/pjlib-util.h @@ -30,9 +30,16 @@ #include <pjlib-util/md5.h> #include <pjlib-util/resolver.h> #include <pjlib-util/scanner.h> -#include <pjlib-util/stun.h> #include <pjlib-util/xml.h> +/* New STUN */ +#include <pjlib-util/stun_endpoint.h> +#include <pjlib-util/stun_msg.h> +#include <pjlib-util/stun_server.h> +#include <pjlib-util/stun_transaction.h> + +/* Old STUN */ +#include <pjlib-util/stun_simple.h> /** * @addtogroup PJLIB_UTIL diff --git a/pjlib-util/include/pjlib-util/config.h b/pjlib-util/include/pjlib-util/config.h index 7abba3c1..3127a415 100644 --- a/pjlib-util/include/pjlib-util/config.h +++ b/pjlib-util/include/pjlib-util/config.h @@ -191,7 +191,19 @@ */ /** - * Maximum number of attributes in the STUN packet. + * Maximum number of attributes in the STUN packet (for the old STUN + * library). + * + * Default: 16 + */ +#ifndef PJSTUN_MAX_ATTR +# define PJSTUN_MAX_ATTR 16 +#endif + + +/** + * Maximum number of attributes in the STUN packet (for the new STUN + * library). * * Default: 16 */ @@ -199,7 +211,6 @@ # define PJ_STUN_MAX_ATTR 16 #endif - /** * @} */ diff --git a/pjlib-util/include/pjlib-util/errno.h b/pjlib-util/include/pjlib-util/errno.h index fd12d7e7..6d8c0b1f 100644 --- a/pjlib-util/include/pjlib-util/errno.h +++ b/pjlib-util/include/pjlib-util/errno.h @@ -50,12 +50,12 @@ #define PJLIB_UTIL_ESTUNINMSGTYPE (PJLIB_UTIL_ERRNO_START+2) /* 320002 */ /** * @hideinitializer - * Invalid STUN message length. + * Invalid STUN message length */ #define PJLIB_UTIL_ESTUNINMSGLEN (PJLIB_UTIL_ERRNO_START+3) /* 320003 */ /** * @hideinitializer - * STUN attribute length error. + * Invalid STUN attribute length */ #define PJLIB_UTIL_ESTUNINATTRLEN (PJLIB_UTIL_ERRNO_START+4) /* 320004 */ /** @@ -245,6 +245,57 @@ #define PJLIB_UTIL_EDNS_NOTZONE PJ_STATUS_FROM_DNS_RCODE(10)/* 320060 */ +/************************************************************ + * NEW STUN ERROR + ***********************************************************/ +/* Messaging errors */ +/** + * @hideinitializer + * Invalid STUN attribute + */ +#define PJLIB_UTIL_ESTUNINATTR (PJLIB_UTIL_ERRNO_START+110)/* 320110 */ +/** + * @hideinitializer + * Too many STUN attributes. + */ +#define PJLIB_UTIL_ESTUNTOOMANYATTR (PJLIB_UTIL_ERRNO_START+111)/* 320111 */ +/** + * @hideinitializer + * Unknown STUN attribute. + */ +#define PJLIB_UTIL_ESTUNUNKNOWNATTR (PJLIB_UTIL_ERRNO_START+112)/* 320112 */ +/** + * @hideinitializer + * Invalid socket address length. + */ +#define PJLIB_UTIL_ESTUNINADDRLEN (PJLIB_UTIL_ERRNO_START+113)/* 320113 */ +/** + * @hideinitializer + * IPv6 attribute not supported + */ +#define PJLIB_UTIL_ESTUNIPV6NOTSUPP (PJLIB_UTIL_ERRNO_START+113)/* 320113 */ +/** + * @hideinitializer + * Expecting STUN response message. + */ +#define PJLIB_UTIL_ESTUNNOTRESPONSE (PJLIB_UTIL_ERRNO_START+114)/* 320114 */ +/** + * @hideinitializer + * Transaction ID mismatch. + */ +#define PJLIB_UTIL_ESTUNINVALIDID (PJLIB_UTIL_ERRNO_START+115)/* 320115 */ +/** + * @hideinitializer + * Unable to find handler for the request. + */ +#define PJLIB_UTIL_ESTUNNOHANDLER (PJLIB_UTIL_ERRNO_START+116)/* 320116 */ + + +#define PJ_STATUS_FROM_STUN_CODE(code) -1 + + + + /** * @} */ diff --git a/pjlib-util/include/pjlib-util/stun_doc.h b/pjlib-util/include/pjlib-util/stun_doc.h new file mode 100644 index 00000000..612bf1a7 --- /dev/null +++ b/pjlib-util/include/pjlib-util/stun_doc.h @@ -0,0 +1,100 @@ +/* $Id$ */ +/* + * Copyright (C) 2003-2005 Benny Prijono <benny@prijono.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#ifndef __PJ_STUN_SERVER_H__ +#define __PJ_STUN_SERVER_H__ + +/* + * STUN documentation. There is no code here. + */ + +/** + * @defgroup PJLIB_UTIL_STUN STUN and TURN + * @ingroup PJLIB_UTIL + + This is the implementation of STUN/TURN in PJLIB-UTIL library. + + The STUN/TURN implementation in PJLIB-UTIL has the following objectives: + - standard based (of course) + - supports both client and server side STUN/TURN services + - independent (that is, only dependent to pjlib), and general purpose + enough to be used not only by pjsip applications but also by other + types of applications + - must be able to support ICE. + + The STUN/TURN implementation is based on the following standards: + - <A HREF="http://www.ietf.org/internet-drafts/draft-ietf-behave-rfc3489bis-05.txt"> + draft-ietf-behave-rfc3489bis-05.txt</A> + - <A HREF="http://www.ietf.org/internet-drafts/draft-ietf-behave-turn-02.txt"> + draft-ietf-behave-turn-02.txt</A> + + But as STUN standards are currently defined as work in progress at IETF, + the implementation will be updated as these standards are updated. + + + + @section stun_org_sec Organization + + The implementation consists of the following components. + + @subsection stun_msg_sec Messaging and Parsing + + The lowest layer of the STUN implementation is the @ref PJLIB_UTIL_STUN_MSG + component. This part is responsible for encoding and decoding STUN messages. + + This layer only implements message representation and parsing. In particular, + it does not provide any transport functionalities, therefore it can be used + by different types of applications. + + + + @subsection stun_endpt_sec Endpoint + + The @ref PJLIB_UTIL_STUN_ENDPOINT is used by the library to put together + common settings for all STUN objects. For example, the STUN endpoint has a + reference of timer heap to poll all STUN timers, reference to ioqueue to + poll network events for STUN servers, and some common settings used by + various STUN objects. + + + @subsection stun_clt_tsx_sec Client Transaction + + The @ref PJLIB_UTIL_STUN_TRANSACTION is used to manage outgoing STUN request, + for example to retransmit the request and to notify application about the + completion of the request. + + The @ref PJLIB_UTIL_STUN_TRANSACTION does not use any networking operations, + but instead application must supply the transaction with a callback to + be used by the transaction to send outgoing requests. This way the STUN + transaction is made more generic and can work with different types of + networking codes in application. + + + + @subsection stun_srv_sec Server Components + + The @ref PJLIB_UTIL_STUN_SERVER is used for: + - implementing STUN servers, and/or + - implementing server side STUN handling (for example for ICE). + + */ + + + +#endif /* __PJ_STUN_SERVER_H__ */ + diff --git a/pjlib-util/include/pjlib-util/stun_endpoint.h b/pjlib-util/include/pjlib-util/stun_endpoint.h new file mode 100644 index 00000000..59bcdee1 --- /dev/null +++ b/pjlib-util/include/pjlib-util/stun_endpoint.h @@ -0,0 +1,85 @@ +/* $Id$ */ +/* + * Copyright (C) 2003-2005 Benny Prijono <benny@prijono.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#ifndef __PJ_STUN_ENDPOINT_H__ +#define __PJ_STUN_ENDPOINT_H__ + +/** + * @file stun_endpoint.h + * @brief STUN endpoint. + */ + +#include <pjlib-util/stun_msg.h> + + +PJ_BEGIN_DECL + + +/* **************************************************************************/ +/** + * @defgroup PJLIB_UTIL_STUN_ENDPOINT STUN Endpoint + * @brief Management of incoming and outgoing STUN transactions. + * @ingroup PJLIB_UTIL_STUN + * @{ + */ + +/** + * Opaque declaration for STUN endpoint. STUN endpoint manages client and + * server STUN transactions, and it needs to be initialized before application + * can send or receive STUN messages. + */ +typedef struct pj_stun_endpoint +{ + pj_pool_factory *pf; + pj_ioqueue_t *ioqueue; + pj_timer_heap_t *timer_heap; + unsigned options; + + unsigned rto_msec; + + pj_pool_t *pool; + +} pj_stun_endpoint; + + + +/** + * Create a STUN endpoint instance. + */ +PJ_DECL(pj_status_t) pj_stun_endpt_create(pj_pool_factory *factory, + unsigned options, + pj_ioqueue_t *ioqueue, + pj_timer_heap_t *timer_heap, + pj_stun_endpoint **p_endpt); + +/** + * Destroy STUN endpoint instance. + */ +PJ_DECL(pj_status_t) pj_stun_endpt_destroy(pj_stun_endpoint *endpt); + + +/** + * @} + */ + + +PJ_END_DECL + + +#endif /* __PJ_STUN_ENDPOINT_H__ */ + diff --git a/pjlib-util/include/pjlib-util/stun_msg.h b/pjlib-util/include/pjlib-util/stun_msg.h new file mode 100644 index 00000000..e8a4f1af --- /dev/null +++ b/pjlib-util/include/pjlib-util/stun_msg.h @@ -0,0 +1,1338 @@ +/* $Id$ */ +/* + * Copyright (C) 2003-2005 Benny Prijono <benny@prijono.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#ifndef __PJ_STUN_MSG_H__ +#define __PJ_STUN_MSG_H__ + +/** + * @file stun_msg.h + * @brief STUN message components. + */ + +#include <pjlib-util/types.h> +#include <pj/sock.h> + + +PJ_BEGIN_DECL + + +/* **************************************************************************/ +/** + * @defgroup PJLIB_UTIL_STUN_MSG STUN Message Representation and Parsing + * @brief Low-level representation and parsing of STUN messages. + * @ingroup PJLIB_UTIL_STUN + * @{ + */ + + +/** + * The default initial STUN round-trip time estimation (the RTO value + * in RFC 3489-bis), in miliseconds. + * This value is used to control the STUN request + * retransmit time. The initial value of retransmission interval + * would be set to this value, and will be doubled after each + * retransmission. + */ +#ifndef PJ_STUN_RTO_VALUE +# define PJ_STUN_RTO_VALUE 100 +#endif + + +/** + * The STUN transaction timeout value, in miliseconds. + * After the last retransmission is sent and if no response is received + * after this time, the STUN transaction will be considered to have failed. + * + * The default value is 1600 miliseconds (as per RFC 3489-bis). + */ +#ifndef PJ_STUN_TIMEOUT_VALUE +# define PJ_STUN_TIMEOUT_VALUE 1600 +#endif + + +/** + * Maximum number of STUN retransmission count. + * + * Default: 7 (as per RFC 3489-bis) + */ +#ifndef PJ_STUN_MAX_RETRANSMIT_COUNT +# define PJ_STUN_MAX_RETRANSMIT_COUNT 7 +#endif + + +/** + * Maximum size of STUN message. + */ +#ifndef PJ_STUN_MAX_PKT_LEN +# define PJ_STUN_MAX_PKT_LEN 512 +#endif + + +/** + * Default STUN port as defined by RFC 3489. + */ +#define PJ_STUN_PORT 3478 + +/** + * STUN magic cookie. + */ +#define PJ_STUN_MAGIC 0x2112A442 + + +/** + * STUN method constants. + */ +enum pj_stun_method_e +{ + /** + * STUN Binding method as defined by RFC 3489-bis. + */ + PJ_STUN_BINDING_METHOD = 1, + + /** + * STUN Shared Secret method as defined by RFC 3489-bis. + */ + PJ_STUN_SHARED_SECRET_METHOD = 2, + + /** + * STUN/TURN Allocate method as defined by draft-ietf-behave-turn + */ + PJ_STUN_ALLOCATE_METHOD = 3, + + /** + * STUN/TURN Send Indication as defined by draft-ietf-behave-turn + */ + PJ_STUN_SEND_INDICATION_METHOD = 4, + + /** + * STUN/TURN Data Indication as defined by draft-ietf-behave-turn + */ + PJ_STUN_DATA_INDICATION_METHOD = 5, + + /** + * STUN/TURN Set Active Destination as defined by draft-ietf-behave-turn + */ + PJ_STUN_SET_ACTIVE_DESTINATION_METHOD = 6, + + /** + * STUN/TURN Connect method as defined by draft-ietf-behave-turn + */ + PJ_STUN_CONNECT_METHOD = 7, + + /** + * STUN/TURN Connect Status indication method. + */ + PJ_STUN_CONNECT_STATUS_METHOD = 8 +}; + + +/** + * Retrieve the STUN method from the message-type field of the STUN + * message. + */ +#define PJ_STUN_GET_METHOD(msg_type) ((msg_type) & 0xFEEF) + + +/** + * STUN message classes constants. + */ +enum pj_stun_msg_class_e +{ + /** + * This specifies that the message type is a STUN request message. + */ + PJ_STUN_REQUEST_CLASS = 0, + + /** + * This specifies that the message type is a STUN indication message. + */ + PJ_STUN_INDICATION_CLASS = 1, + + /** + * This specifies that the message type is a STUN successful response. + */ + PJ_STUN_SUCCESS_CLASS = 2, + + /** + * This specifies that the message type is a STUN error response. + */ + PJ_STUN_ERROR_CLASS = 3 +}; + + +/** + * Determine if the message type is a request. + */ +#define PJ_STUN_IS_REQUEST(msg_type) (((msg_type) & 0x0110) == 0x0000) + + +/** + * Determine if the message type is a response. + */ +#define PJ_STUN_IS_RESPONSE(msg_type) (((msg_type) & 0x0110) == 0x0100) + + +/** + * The response bit in the message type. + */ +#define PJ_STUN_RESPONSE_BIT (0x0100) + +/** + * Determine if the message type is an error response. + */ +#define PJ_STUN_IS_ERROR_RESPONSE(msg_type) (((msg_type) & 0x0110) == 0x0110) + + +/** + * The error response bit in the message type. + */ +#define PJ_STUN_ERROR_RESPONSE_BIT (0x0110) + + +/** + * Determine if the message type is an indication message. + */ +#define PJ_STUN_IS_INDICATION(msg_type) (((msg_type) & 0x0110) == 0x0010) + + +/** + * The error response bit in the message type. + */ +#define PJ_STUN_INDICATION_BIT (0x0010) + + +/** + * This enumeration describes STUN message types. + */ +typedef enum pj_stun_msg_type +{ + /** + * STUN BINDING request. + */ + PJ_STUN_BINDING_REQUEST = 0x0001, + + /** + * Successful response to STUN BINDING-REQUEST. + */ + PJ_STUN_BINDING_RESPONSE = 0x0101, + + /** + * Error response to STUN BINDING-REQUEST. + */ + PJ_STUN_BINDING_ERROR_RESPONSE = 0x0111, + + /** + * STUN SHARED-SECRET reqeust. + */ + PJ_STUN_SHARED_SECRET_REQUEST = 0x0002, + + /** + * Successful response to STUN SHARED-SECRET reqeust. + */ + PJ_STUN_SHARED_SECRET_RESPONSE = 0x0102, + + /** + * Error response to STUN SHARED-SECRET reqeust. + */ + PJ_STUN_SHARED_SECRET_ERROR_RESPONSE = 0x0112, + + /** + * STUN/TURN Allocate Request + */ + PJ_STUN_ALLOCATE_REQUEST = 0x0003, + + /** + * Successful response to STUN/TURN Allocate Request + */ + PJ_STUN_ALLOCATE_RESPONSE = 0x0103, + + /** + * Failure response to STUN/TURN Allocate Request + */ + PJ_STUN_ALLOCATE_ERROR_RESPONSE = 0x0113, + + /** + * STUN/TURN Send Indication + */ + PJ_STUN_SEND_INDICATION = 0x0004, + + /** + * STUN/TURN Data Indication + */ + PJ_STUN_DATA_INDICATION = 0x0115, + + /** + * STUN/TURN Set Active Destination Request + */ + PJ_STUN_SET_ACTIVE_DESTINATION_REQUEST = 0x0006, + + /** + * STUN/TURN Set Active Destination Response + */ + PJ_STUN_SET_ACTIVE_DESTINATION_RESPONSE = 0x0106, + + /** + * STUN/TURN Set Active Destination Error Response + */ + PJ_STUN_SET_ACTIVE_DESTINATION_ERROR_RESPONSE = 0x0116, + + /** + * STUN/TURN Connect Request + */ + PJ_STUN_CONNECT_REQUEST = 0x0007, + + /** + * STUN/TURN Connect Response + */ + PJ_STUN_CONNECT_RESPONSE = 0x0107, + + /** + * STUN/TURN Connect Error Response + */ + PJ_STUN_CONNECT_ERROR_RESPONSE = 0x0117, + + /** + * STUN/TURN Connect Status Indication + */ + PJ_STUN_CONNECT_STATUS_INDICATION = 0x0118 + + +} pj_stun_msg_type; + + + +/** + * This enumeration describes STUN attribute types. + */ +typedef enum pj_stun_attr_type +{ + PJ_STUN_ATTR_MAPPED_ADDR = 0x0001,/**< MAPPED-ADDRESS. */ + PJ_STUN_ATTR_RESPONSE_ADDR = 0x0002,/**< RESPONSE-ADDRESS (deprcatd)*/ + PJ_STUN_ATTR_CHANGE_REQUEST = 0x0003,/**< CHANGE-REQUEST (deprecated)*/ + PJ_STUN_ATTR_SOURCE_ADDR = 0x0004,/**< SOURCE-ADDRESS (deprecated)*/ + PJ_STUN_ATTR_CHANGED_ADDR = 0x0005,/**< CHANGED-ADDRESS (deprecatd)*/ + PJ_STUN_ATTR_USERNAME = 0x0006,/**< USERNAME attribute. */ + PJ_STUN_ATTR_PASSWORD = 0x0007,/**< PASSWORD attribute. */ + PJ_STUN_ATTR_MESSAGE_INTEGRITY = 0x0008,/**< MESSAGE-INTEGRITY. */ + PJ_STUN_ATTR_ERROR_CODE = 0x0009,/**< ERROR-CODE. */ + PJ_STUN_ATTR_UNKNOWN_ATTRIBUTES = 0x000A,/**< UNKNOWN-ATTRIBUTES. */ + PJ_STUN_ATTR_REFLECTED_FROM = 0x000B,/**< REFLECTED-FROM (deprecatd)*/ + PJ_STUN_ATTR_LIFETIME = 0x000D,/**< LIFETIME attribute. */ + PJ_STUN_ATTR_BANDWIDTH = 0x0010,/**< BANDWIDTH attribute */ + PJ_STUN_ATTR_REMOTE_ADDRESS = 0x0012,/**< REMOTE-ADDRESS attribute */ + PJ_STUN_ATTR_DATA = 0x0013,/**< DATA attribute. */ + PJ_STUN_ATTR_REALM = 0x0014,/**< REALM attribute. */ + PJ_STUN_ATTR_NONCE = 0x0015,/**< NONCE attribute. */ + PJ_STUN_ATTR_RELAY_ADDRESS = 0x0016,/**< RELAY-ADDRESS attribute. */ + PJ_STUN_ATTR_REQUESTED_ADDR_TYPE= 0x0017,/**< REQUESTED-ADDRESS-TYPE */ + PJ_STUN_ATTR_REQUESTED_PORT_PROPS=0x0018,/**< REQUESTED-PORT-PROPS */ + PJ_STUN_ATTR_REQUESTED_TRANSPORT= 0x0019,/**< REQUESTED-TRANSPORT */ + PJ_STUN_ATTR_XOR_MAPPED_ADDRESS = 0x0020,/**< XOR-MAPPED-ADDRESS */ + PJ_STUN_ATTR_TIMER_VAL = 0x0021,/**< TIMER-VAL attribute. */ + PJ_STUN_ATTR_REQUESTED_IP = 0x0022,/**< REQUESTED-IP attribute */ + PJ_STUN_ATTR_XOR_REFLECTED_FROM = 0x0023,/**< XOR-REFLECTED-FROM */ + PJ_STUN_ATTR_PRIORITY = 0x0024,/**< PRIORITY */ + PJ_STUN_ATTR_USE_CANDIDATE = 0x0025,/**< USE-CANDIDATE */ + PJ_STUN_ATTR_XOR_INTERNAL_ADDR = 0x0026,/**< XOR-INTERNAL-ADDRESS */ + + PJ_STUN_ATTR_END_MANDATORY_ATTR, + + PJ_STUN_ATTR_START_EXTENDED_ATTR= 0x8021, + + PJ_STUN_ATTR_FINGERPRINT = 0x8021,/**< FINGERPRINT attribute. */ + PJ_STUN_ATTR_SERVER = 0x8022,/**< SERVER attribute. */ + PJ_STUN_ATTR_ALTERNATE_SERVER = 0x8023,/**< ALTERNATE-SERVER. */ + PJ_STUN_ATTR_REFRESH_INTERVAL = 0x8024,/**< REFRESH-INTERVAL. */ + + PJ_STUN_ATTR_END_EXTENDED_ATTR + +} pj_stun_attr_type; + + +/** + * STUN error codes, which goes into STUN ERROR-CODE attribute. + */ +typedef enum pj_stun_status +{ + PJ_STUN_STATUS_TRY_ALTERNATE = 300, /**< Try Alternate */ + PJ_STUN_STATUS_BAD_REQUEST = 400, /**< Bad Request */ + PJ_STUN_STATUS_UNAUTHORIZED = 401, /**< Unauthorized */ + PJ_STUN_STATUS_UNKNOWN_ATTRIBUTE = 420, /**< Unknown Attribute */ + PJ_STUN_STATUS_STALE_CREDENTIALS = 430, /**< Stale Credentials */ + PJ_STUN_STATUS_INTEGRITY_CHECK_FAILURE = 431, /**< Integrity Chk Fail */ + PJ_STUN_STATUS_MISSING_USERNAME = 432, /**< Missing Username */ + PJ_STUN_STATUS_USE_TLS = 433, /**< Use TLS */ + PJ_STUN_STATUS_MISSING_REALM = 434, /**< Missing Realm */ + PJ_STUN_STATUS_MISSING_NONCE = 435, /**< Missing Nonce */ + PJ_STUN_STATUS_UNKNOWN_USERNAME = 436, /**< Unknown Username */ + PJ_STUN_STATUS_NO_BINDING = 437, /**< No Binding. */ + PJ_STUN_STATUS_STALE_NONCE = 438, /**< Stale Nonce */ + PJ_STUN_STATUS_TRANSITIONING = 439, /**< Transitioning. */ + PJ_STUN_STATUS_WRONG_USERNAME = 441, /**< Wrong Username. */ + PJ_STUN_STATUS_UNSUPP_TRANSPORT_PROTO = 442, /**< Unsupported Transport Protocol */ + PJ_STUN_STATUS_INVALID_IP_ADDR = 443, /**< Invalid IP Address */ + PJ_STUN_STATUS_INVALID_PORT = 444, /**< Invalid Port */ + PJ_STUN_STATUS_OPER_TCP_ONLY = 445, /**< Operation for TCP Only */ + PJ_STUN_STATUS_CONNECTION_FAILURE = 446, /**< Connection Failure */ + PJ_STUN_STATUS_CONNECTION_TIMEOUT = 447, /**< Connection Timeout */ + PJ_STUN_STATUS_SERVER_ERROR = 500, /**< Server Error */ + PJ_STUN_STATUS_GLOBAL_FAILURE = 600 /**< Global Failure */ +} pj_stun_status; + + +/** + * This structure describes STUN message header. A STUN message has the + * following format: + * + * \verbatim + + 0 1 2 3 + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + |0 0| STUN Message Type | Message Length | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Magic Cookie | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + Transaction ID + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + + \endverbatim + */ +typedef struct pj_stun_msg_hdr +{ + /** + * STUN message type, which the first two bits must be zeroes. + */ + pj_uint16_t type; + + /** + * The message length is the size, in bytes, of the message not + * including the 20 byte STUN header. + */ + pj_uint16_t length; + + /** + * The magic cookie is a fixed value, 0x2112A442 (PJ_STUN_MAGIC constant). + * In the previous version of this specification [15] this field was part + * of the transaction ID. + */ + pj_uint32_t magic; + + /** + * The transaction ID is a 96 bit identifier. STUN transactions are + * identified by their unique 96-bit transaction ID. For request/ + * response transactions, the transaction ID is chosen by the STUN + * client and MUST be unique for each new STUN transaction generated by + * that STUN client. The transaction ID MUST be uniformly and randomly + * distributed between 0 and 2**96 - 1. + */ + pj_uint8_t tsx_id[12]; + +} pj_stun_msg_hdr; + + +/** + * This structre describes STUN attribute header. Each attribute is + * TLV encoded, with a 16 bit type, 16 bit length, and variable value. + * Each STUN attribute ends on a 32 bit boundary: + * + * \verbatim + + 0 1 2 3 + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Type | Length | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Value .... | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + + \endverbatim + */ +typedef struct pj_stun_attr_hdr +{ + /** + * STUN attribute type. + */ + pj_uint16_t type; + + /** + * The Length refers to the length of the actual useful content of the + * Value portion of the attribute, measured in bytes. The value + * in the Length field refers to the length of the Value part of the + * attribute prior to padding - i.e., the useful content. + */ + pj_uint16_t length; + +} pj_stun_attr_hdr; + + +/** + * This structure describes STUN generic IP address attribute, used for + * example to represent STUN MAPPED-ADDRESS attribute. + * + * The generic IP address attribute indicates the transport address. + * It consists of an eight bit address family, and a sixteen bit port, + * followed by a fixed length value representing the IP address. If the + * address family is IPv4, the address is 32 bits, in network byte + * order. If the address family is IPv6, the address is 128 bits in + * network byte order. + * + * The format of the generic IP address attribute is: + * + * \verbatim + + 0 1 2 3 + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + |x x x x x x x x| Family | Port | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Address (variable) + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + + \endverbatim + */ +typedef struct pj_stun_generic_ip_addr_attr +{ + /** + * Standard STUN attribute header. + */ + pj_stun_attr_hdr hdr; + + /** + * The socket address (as a union) + */ + union { + pj_sockaddr addr; /**< Generic socket address. */ + pj_sockaddr_in ipv4; /**< IPv4 socket address. */ + pj_sockaddr_in6 ipv6; /**< IPv6 socket address. */ + } addr; + +} pj_stun_generic_ip_addr_attr; + + +/** + * This structure represents a generic STUN attributes with no payload, + * and it is used for example by ICE USE-CANDIDATE attribute. + */ +typedef struct pj_stun_empty_attr +{ + /** + * Standard STUN attribute header. + */ + pj_stun_attr_hdr hdr; + +} pj_stun_empty_attr; + + +/** + * This structure represents generic STUN string attributes, such as STUN + * USERNAME, PASSWORD, SERVER, REALM, and NONCE attributes. Note that for REALM and + * NONCE attributes, the text MUST be quoted with. + */ +typedef struct pj_stun_generic_string_attr +{ + /** + * Standard STUN attribute header. + */ + pj_stun_attr_hdr hdr; + + /** + * The string value. + */ + pj_str_t value; + +} pj_stun_generic_string_attr; + + +/** + * This structure represents a generic STUN attributes with 32bit (unsigned) + * integer value, such as STUN FINGERPRINT and REFRESH-INTERVAL attributes. + */ +typedef struct pj_stun_generic_uint_attr +{ + /** + * Standard STUN attribute header. + */ + pj_stun_attr_hdr hdr; + + /** + * The 32bit value. + */ + pj_uint32_t value; + +} pj_stun_generic_uint_attr; + + +/** + * This structure represents generic STUN attributes to hold a raw binary + * data. + */ +typedef struct pj_stun_binary_attr +{ + /** + * Standard STUN attribute header. + */ + pj_stun_attr_hdr hdr; + + /** + * Length of the data. + */ + unsigned length; + + /** + * The raw data. + */ + char *data; + +} pj_stun_binary_attr; + + +/** + * This structure describes STUN MESSAGE-INTEGRITY attribute. + * The MESSAGE-INTEGRITY attribute contains an HMAC-SHA1 [10] of the + * STUN message. The MESSAGE-INTEGRITY attribute can be present in any + * STUN message type. Since it uses the SHA1 hash, the HMAC will be 20 + * bytes. + */ +typedef struct pj_stun_msg_integrity_attr +{ + /** + * Standard STUN attribute header. + */ + pj_stun_attr_hdr hdr; + + /** + * The 20 bytes hmac value. + */ + pj_uint8_t hmac[20]; + +} pj_stun_msg_integrity_attr; + + +/** + * This structure describes STUN FINGERPRINT attribute. The FINGERPRINT + * attribute can be present in all STUN messages. It is computed as + * the CRC-32 of the STUN message up to (but excluding) the FINGERPRINT + * attribute itself, xor-d with the 32 bit value 0x5354554e + */ +typedef struct pj_stun_generic_uint_attr pj_stun_fingerprint_attr; + + +/** + * This structure represents STUN ERROR-CODE attribute. The ERROR-CODE + * attribute is present in the Binding Error Response and Shared Secret + * Error Response. It is a numeric value in the range of 100 to 699 + * plus a textual reason phrase encoded in UTF-8 + * + * \verbatim + + 0 1 2 3 + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | 0 |Class| Number | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Reason Phrase (variable) .. + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + + \endverbatim + */ +typedef struct pj_stun_error_code_attr +{ + /** + * Standard STUN attribute header. + */ + pj_stun_attr_hdr hdr; + + /** + * The value must be zero. + */ + pj_uint16_t zero; + + /** + * Error class (1-6). + */ + pj_uint8_t err_class; + + /** + * Error number is the error number modulo 100. + */ + pj_uint8_t number; + + /** + * The reason phrase. + */ + pj_str_t reason; + +} pj_stun_error_code_attr; + + +/** + * This describes STUN REALM attribute. + * The REALM attribute is present in requests and responses. It + * contains text which meets the grammar for "realm" as described in RFC + * 3261 [11], and will thus contain a quoted string (including the + * quotes). + */ +typedef struct pj_stun_generic_string_attr pj_stun_realm_attr; + + +/** + * This describes STUN NONCE attribute. + * The NONCE attribute is present in requests and in error responses. + * It contains a sequence of qdtext or quoted-pair, which are defined in + * RFC 3261 [11]. See RFC 2617 [7] for guidance on selection of nonce + * values in a server. + */ +typedef struct pj_stun_generic_string_attr pj_stun_nonce_attr; + + +/** + * This describes STUN UNKNOWN-ATTRIBUTES attribute. + * The UNKNOWN-ATTRIBUTES attribute is present only in an error response + * when the response code in the ERROR-CODE attribute is 420. + * The attribute contains a list of 16 bit values, each of which + * represents an attribute type that was not understood by the server. + * If the number of unknown attributes is an odd number, one of the + * attributes MUST be repeated in the list, so that the total length of + * the list is a multiple of 4 bytes. + */ +typedef struct pj_stun_unknown_attr +{ + /** + * Standard STUN attribute header. + */ + pj_stun_attr_hdr hdr; + + /** + * Number of unknown attributes in the array. + */ + unsigned attr_count; + + /** + * Array of unknown attribute IDs. + */ + pj_uint16_t attrs[PJ_STUN_MAX_ATTR]; + +} pj_stun_unknown_attr; + + +/** + * This structure describes STUN MAPPED-ADDRESS attribute. + * The MAPPED-ADDRESS attribute indicates the mapped transport address. + */ +typedef struct pj_stun_generic_ip_addr_attr pj_stun_mapped_addr_attr; + + +/** + * This describes STUN XOR-MAPPED-ADDRESS attribute (which has the same + * format as STUN MAPPED-ADDRESS attribute). + * The XOR-MAPPED-ADDRESS attribute is present in responses. It + * provides the same information that would present in the MAPPED- + * ADDRESS attribute but because the NAT's public IP address is + * obfuscated through the XOR function, STUN messages are able to pass + * through NATs which would otherwise interfere with STUN. + */ +typedef struct pj_stun_generic_ip_addr_attr pj_stun_xor_mapped_addr_attr; + + +/** + * This describes STUN SERVER attribute. + * The server attribute contains a textual description of the software + * being used by the server, including manufacturer and version number. + * The attribute has no impact on operation of the protocol, and serves + * only as a tool for diagnostic and debugging purposes. The value of + * SERVER is variable length. + */ +typedef struct pj_stun_generic_string_attr pj_stun_server_attr; + + +/** + * This describes STUN ALTERNATE-SERVER attribute. + * The alternate server represents an alternate transport address for a + * different STUN server to try. It is encoded in the same way as + * MAPPED-ADDRESS. + */ +typedef struct pj_stun_generic_ip_addr_attr pj_stun_alt_server_attr; + + +/** + * This describes STUN REFRESH-INTERVAL attribute. + * The REFRESH-INTERVAL indicates the number of milliseconds that the + * server suggests the client should use between refreshes of the NAT + * bindings between the client and server. + */ +typedef struct pj_stun_generic_uint_attr pj_stun_refresh_interval_attr; + + +/** + * This structure describes STUN RESPONSE-ADDRESS attribute. + * The RESPONSE-ADDRESS attribute indicates where the response to a + * Binding Request should be sent. Its syntax is identical to MAPPED- + * ADDRESS. + * + * Note that the usage of this attribute has been deprecated by the + * RFC 3489-bis standard. + */ +typedef struct pj_stun_generic_ip_addr_attr pj_stun_response_addr_attr; + + +/** + * This structure describes STUN CHANGED-ADDRESS attribute. + * The CHANGED-ADDRESS attribute indicates the IP address and port where + * responses would have been sent from if the "change IP" and "change + * port" flags had been set in the CHANGE-REQUEST attribute of the + * Binding Request. The attribute is always present in a Binding + * Response, independent of the value of the flags. Its syntax is + * identical to MAPPED-ADDRESS. + * + * Note that the usage of this attribute has been deprecated by the + * RFC 3489-bis standard. + */ +typedef struct pj_stun_generic_ip_addr_attr pj_stun_changed_addr_attr; + + +/** + * This structure describes STUN CHANGE-REQUEST attribute. + * The CHANGE-REQUEST attribute is used by the client to request that + * the server use a different address and/or port when sending the + * response. + * + * Bit 29 of the value is the "change IP" flag. If true, it requests + * the server to send the Binding Response with a different IP address + * than the one the Binding Request was received on. + * + * Bit 30 of the value is the "change port" flag. If true, it requests + * the server to send the Binding Response with a different port than + * the one the Binding Request was received on. + * + * Note that the usage of this attribute has been deprecated by the + * RFC 3489-bis standard. + */ +typedef struct pj_stun_generic_uint_attr pj_stun_change_request_attr; + +/** + * This structure describes STUN SOURCE-ADDRESS attribute. + * The SOURCE-ADDRESS attribute is present in Binding Responses. It + * indicates the source IP address and port that the server is sending + * the response from. Its syntax is identical to that of MAPPED- + * ADDRESS. + * + * Note that the usage of this attribute has been deprecated by the + * RFC 3489-bis standard. + */ +typedef struct pj_stun_generic_ip_addr_attr pj_stun_src_addr_attr; + + +/** + * This describes the STUN REFLECTED-FROM attribute. + * The REFLECTED-FROM attribute is present only in Binding Responses, + * when the Binding Request contained a RESPONSE-ADDRESS attribute. The + * attribute contains the identity (in terms of IP address) of the + * source where the request came from. Its purpose is to provide + * traceability, so that a STUN server cannot be used as a reflector for + * denial-of-service attacks. + */ +typedef struct pj_stun_generic_ip_addr_attr pj_stun_reflected_from_attr; + + +/** + * This describes STUN USERNAME attribute. + * The USERNAME attribute is used for message integrity. It identifies + * the shared secret used in the message integrity check. Consequently, + * the USERNAME MUST be included in any request that contains the + * MESSAGE-INTEGRITY attribute. + */ +typedef struct pj_stun_generic_string_attr pj_stun_username_attr; + + +/** + * This describes STUN PASSWORD attribute. + * If the message type is Shared Secret Response it MUST include the + * PASSWORD attribute. + */ +typedef struct pj_stun_generic_string_attr pj_stun_password_attr; + + +/** + * This describes STUN LIFETIME attribute. + * The lifetime attribute represents the duration for which the server + * will maintain an allocation in the absence of data traffic either + * from or to the client. It is a 32 bit value representing the number + * of seconds remaining until expiration. + */ +typedef struct pj_stun_generic_uint_attr pj_stun_lifetime_attr; + + +/** + * This describes STUN BANDWIDTH attribute. + * The bandwidth attribute represents the peak bandwidth, measured in + * kbits per second, that the client expects to use on the binding. The + * value represents the sum in the receive and send directions. + */ +typedef struct pj_stun_generic_uint_attr pj_stun_bandwidth_attr; + + +/** + * This describes the STUN REMOTE-ADDRESS attribute. + * The REMOTE-ADDRESS specifies the address and port of the peer as seen + * from the STUN relay server. + */ +typedef struct pj_stun_generic_ip_addr_attr pj_stun_remote_addr_attr; + + +/** + * This describes the STUN DATA attribute. + * The DATA attribute is present in Send Indications and Data + * Indications. It contains raw payload data that is to be sent (in the + * case of a Send Request) or was received (in the case of a Data + * Indication).. + */ +typedef struct pj_stun_binary_attr pj_stun_data_attr; + + +/** + * This describes the STUN RELAY-ADDRESS attribute. + * The RELAY-ADDRESS is present in Allocate responses. It specifies the + * address and port that the server allocated to the client. + */ +typedef struct pj_stun_generic_ip_addr_attr pj_stun_relay_addr_attr; + + +/** + * This describes the REQUESTED-ADDRESS-TYPE attribute. + * The REQUESTED-ADDRESS-TYPE attribute is used by clients to request + * the allocation of a specific address type from a server. The + * following is the format of the REQUESTED-ADDRESS-TYPE attribute. + + \verbatim + + 0 1 2 3 + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Family | Reserved | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + + \endverbatim + */ +typedef struct pj_stun_generic_uint_attr pj_stun_requested_addr_type; + +/** + * This describes the STUN REQUESTED-PORT-PROPS attribute. + * This attribute allows the client to request certain properties for + * the port that is allocated by the server. The attribute can be used + * with any transport protocol that has the notion of a 16 bit port + * space (including TCP and UDP). The attribute is 32 bits long. Its + * format is: + + \verbatim + + 0 1 2 3 + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Reserved = 0 |B| A | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + + \endverbatim + */ +typedef struct pj_stun_generic_uint_attr pj_stun_requested_port_props_attr; + + +/** + * This describes the STUN REQUESTED-TRANSPORT attribute. + * This attribute is used by the client to request a specific transport + * protocol for the allocated transport address. It is a 32 bit + * unsigned integer. Its values are: 0x0000 for UDP and 0x0000 for TCP. + */ +typedef struct pj_stun_generic_uint_attr pj_stun_requested_transport_attr; + + +/** + * This describes the STUN REQUESTED-IP attribute. + * The REQUESTED-IP attribute is used by the client to request that a + * specific IP address be allocated to it. + */ +typedef struct pj_stun_generic_ip_addr_attr pj_stun_requested_ip_attr; + +/** + * This describes the XOR-REFLECTED-FROM attribute, as described by + * draft-macdonald-behave-nat-behavior-discovery-00. + * The XOR-REFLECTED-FROM attribute is used in place of the REFLECTED- + * FROM attribute. It provides the same information, but because the + * NAT's public address is obfuscated through the XOR function, It can + * pass through a NAT that would otherwise attempt to translate it to + * the private network address. XOR-REFLECTED-FROM has identical syntax + * to XOR-MAPPED-ADDRESS. + */ +typedef struct pj_stun_generic_ip_addr_attr pj_stun_xor_reflected_from_attr; + +/** + * This describes the PRIORITY attribute from draft-ietf-mmusic-ice-13. + * The PRIORITY attribute indicates the priority that is to be + * associated with a peer reflexive candidate, should one be discovered + * by this check. It is a 32 bit unsigned integer, and has an attribute + * type of 0x0024. + */ +typedef struct pj_stun_generic_uint_attr pj_stun_priority_attr; + +/** + * This describes the USE-CANDIDATE attribute from draft-ietf-mmusic-ice-13. + * The USE-CANDIDATE attribute indicates that the candidate pair + * resulting from this check should be used for transmission of media. + * The attribute has no content (the Length field of the attribute is + * zero); it serves as a flag. + */ +typedef struct pj_stun_empty_attr pj_stun_use_candidate_attr; + +/** + * This structure describes STUN XOR-INTERNAL-ADDRESS attribute from + * draft-wing-behave-nat-control-stun-usage-00. + * This attribute MUST be present in a Binding Response and may be used + * in other responses as well. This attribute is necessary to allow a + * STUN client to 'walk backwards' and communicate directly with all of + * the STUN-aware NATs along the path. + */ +typedef pj_stun_generic_ip_addr_attr pj_stun_xor_internal_addr_attr; + +/** + * This describes the STUN TIMER-VAL attribute. + * The TIMER-VAL attribute is used only in conjunction with the Set + * Active Destination response. It conveys from the server, to the + * client, the value of the timer used in the server state machine. + */ +typedef struct pj_stun_generic_uint_attr pj_stun_timer_val_attr; + + +/** + * This structure describes a parsed STUN message. All integral fields + * in this structure (including IP addresses) will be in the host + * byte order. + */ +typedef struct pj_stun_msg +{ + /** + * STUN message header. + */ + pj_stun_msg_hdr hdr; + + /** + * Number of attributes in the STUN message. + */ + unsigned attr_count; + + /** + * Array of STUN attributes. + */ + pj_stun_attr_hdr *attr[PJ_STUN_MAX_ATTR]; + +} pj_stun_msg; + + +/** + * Get STUN message method name. + * + * @param msg_type The STUN message type (in host byte order) + * + * @return The STUN message method name string. + */ +PJ_DECL(const char*) pj_stun_get_method_name(unsigned msg_type); + + +/** + * Get STUN message class name. + * + * @param msg_type The STUN message type (in host byte order) + * + * @return The STUN message class name string. + */ +PJ_DECL(const char*) pj_stun_get_class_name(unsigned msg_type); + + +/** + * Get STUN attribute name. + * + * @return attr_type The STUN attribute type (in host byte order). + * + * @return The STUN attribute type name string. + */ +PJ_DECL(const char*) pj_stun_get_attr_name(unsigned attr_type); + + +/** + * Get STUN standard reason phrase for the specified error code. + * + * @param err_code The STUN error code. + * + * @return The STUN error reason phrase. + */ +PJ_DECL(pj_str_t) pj_stun_get_err_reason(int err_code); + + +/** + * Create a blank STUN message. + * + * @param pool Pool to create the STUN message. + * @param msg_type The 14bit message type. + * @param magic Magic value to be put to the mesage; for requests, + * the value should be PJ_STUN_MAGIC. + * @param tsx_id Optional transaction ID, or NULL to let the + * function generates a random transaction ID. + * @param p_msg Pointer to receive the message. + * + * @return PJ_SUCCESS on success. + */ +PJ_DECL(pj_status_t) pj_stun_msg_create(pj_pool_t *pool, + unsigned msg_type, + pj_uint32_t magic, + const pj_uint8_t tsx_id[12], + pj_stun_msg **p_msg); + + +/** + * Add STUN attribute to STUN message. + * + * @param msg The STUN message. + * @param attr The STUN attribute to be added to the message. + * + * @return PJ_SUCCESS on success, or PJ_ETOOMANY if there are + * already too many attributes in the message. + */ +PJ_DECL(pj_status_t) pj_stun_msg_add_attr(pj_stun_msg *msg, + pj_stun_attr_hdr *attr); + + +/** + * Check that the PDU is potentially a valid STUN message. This function + * is useful when application needs to multiplex STUN packets with other + * application traffic. When this function returns PJ_SUCCESS, there is a + * big chance that the packet is a STUN packet. + * + * Note that we cannot be sure that the PDU is a really valid STUN message + * until we actually parse the PDU. + * + * @param pdu The packet buffer. + * @param pdu_len The length of the packet buffer. + * @param options Options. + * + * @return PJ_SUCCESS if the PDU is a potentially valid STUN + * message. + */ +PJ_DECL(pj_status_t) pj_stun_msg_check(const void *pdu, unsigned pdu_len, + unsigned options); + + +/** + * Parse incoming packet into STUN message. + * + * @param pool Pool to allocate the message. + * @param pdu The incoming packet to be parsed. + * @param pdu_len The length of the incoming packet. + * @param options Parsing flags. + * @param p_msg Pointer to receive the parsed message. + * @param p_parsed_len Optional pointer to receive how many bytes have + * been parsed for the STUN message. This is useful + * when the packet is received over stream oriented + * transport. + * @param p_err_code Optional pointer to receive STUN error code when + * parsing failed. + * @param uattr_cnt Optional pointer to specify the number of elements + * in uattr array. On return, this will be filled with + * the actual number of attributes set in the uattr. + * @param uattr Optional array to receive unknown attribute types. + * + * @return PJ_SUCCESS on success or the appropriate error code. + */ +PJ_DECL(pj_status_t) pj_stun_msg_decode(pj_pool_t *pool, + const pj_uint8_t *pdu, + unsigned pdu_len, + unsigned options, + pj_stun_msg **p_msg, + unsigned *p_parsed_len, + unsigned *p_err_code, + unsigned *uattr_cnt, + pj_uint16_t uattr[]); + +/** + * Print the message structure to a buffer. + * + * @param msg The message to be printed to a contiguous buffer. + * @param pkt_buf The buffer. + * @param buf_size Size of the buffer. + * @param options Options. + * @param p_msg_len Upon return, it will be filed with the size of + * the packet in bytes, or negative value on error. + * + * @return PJ_SUCCESS on success. + */ +PJ_DECL(pj_status_t) pj_stun_msg_encode(const pj_stun_msg *msg, + pj_uint8_t *pkt_buf, + unsigned buf_size, + unsigned options, + unsigned *p_msg_len); + + +/** + * Find STUN attribute in the STUN message, starting from the specified + * index. + * + * @param msg The STUN message. + * @param attr_type The attribute type to be found. + * @param start_index The start index of the attribute in the message. + * Specify zero to start searching from the first + * attribute. + * + * @return The attribute instance, or NULL if it cannot be + * found. + */ +PJ_DECL(pj_stun_attr_hdr*) pj_stun_msg_find_attr(const pj_stun_msg *msg, + int attr_type, + unsigned start_index); + + +/** + * Create a generic STUN IP address attribute for IPv4 address. Note that + * the port and ip_addr parameters are in host byte order. + * + * @param pool The pool to allocate memory from. + * @param attr_type Attribute type. + * @param xor_ed If non-zero, the port and address will be XOR-ed + * with magic, to make the XOR-MAPPED-ADDRESS attribute. + * @param addr_len Length of \a addr parameter. + * @param addr A pj_sockaddr_in or pj_sockaddr_in6 structure. + * @param p_attr Pointer to receive the attribute. + * + * @return PJ_SUCCESS on success or the appropriate error code. + */ +PJ_DECL(pj_status_t) +pj_stun_generic_ip_addr_attr_create(pj_pool_t *pool, + int attr_type, + pj_bool_t xor_ed, + unsigned addr_len, + const pj_sockaddr_t *addr, + pj_stun_generic_ip_addr_attr **p_attr); + + +/** + * Create a STUN generic string attribute. + * + * @param pool The pool to allocate memory from. + * @param value The string value to be assigned to the attribute. + * @param p_attr Pointer to receive the attribute. + * + * @return PJ_SUCCESS on success or the appropriate error code. + */ +PJ_DECL(pj_status_t) +pj_stun_generic_string_attr_create(pj_pool_t *pool, + int attr_type, + const pj_str_t *value, + pj_stun_generic_string_attr **p_attr); + + +/** + * Create a STUN generic 32bit value attribute. + * + * @param pool The pool to allocate memory from. + * @param attr_type Attribute type, from #pj_stun_attr_type. + * @param value The 32bit value to be assigned to the attribute. + * @param p_attr Pointer to receive the attribute. + * + * @return PJ_SUCCESS on success or the appropriate error code. + */ +PJ_DECL(pj_status_t) +pj_stun_generic_uint_attr_create(pj_pool_t *pool, + int attr_type, + pj_uint32_t value, + pj_stun_generic_uint_attr **p_attr); + + +/** + * Create a STUN MESSAGE-INTEGRITY attribute. + * + * @param pool The pool to allocate memory from. + * @param p_attr Pointer to receive the attribute. + * + * @return PJ_SUCCESS on success or the appropriate error code. + */ +PJ_DECL(pj_status_t) +pj_stun_msg_integrity_attr_create(pj_pool_t *pool, + pj_stun_msg_integrity_attr **p_attr); + + +/** + * Create a STUN ERROR-CODE attribute. + * + * @param pool The pool to allocate memory from. + * @param err_code STUN error code. + * @param err_reason Optional STUN error reason. If NULL is given, the + * standard error reason will be given. + * @param p_attr Pointer to receive the attribute. + * + * @return PJ_SUCCESS on success or the appropriate error code. + */ +PJ_DECL(pj_status_t) +pj_stun_error_code_attr_create(pj_pool_t *pool, + int err_code, + const pj_str_t *err_reason, + pj_stun_error_code_attr **p_attr); + + +/** + * Create an empty instance of STUN UNKNOWN-ATTRIBUTES attribute. + * + * @param pool The pool to allocate memory from. + * @param attr_cnt Number of attributes in the array (can be zero). + * @param attr Optional array of attributes. + * @param p_attr Pointer to receive the attribute. + * + * @return PJ_SUCCESS on success or the appropriate error code. + */ +PJ_DECL(pj_status_t) +pj_stun_unknown_attr_create(pj_pool_t *pool, + unsigned attr_cnt, + pj_uint16_t attr[], + pj_stun_unknown_attr **p_attr); + + +/** + * Create a blank binary attribute. + * + * @param pool The pool to allocate memory from. + * @param attr_type The attribute type. + * @param p_attr Pointer to receive the attribute. + * + * @return PJ_SUCCESS on success or the appropriate error code. + */ +PJ_DECL(pj_status_t) +pj_stun_binary_attr_create(pj_pool_t *pool, + int attr_type, + pj_stun_binary_attr **p_attr); + + +/** + * @} + */ + + +PJ_END_DECL + + +#endif /* __PJ_STUN_MSG_H__ */ + diff --git a/pjlib-util/include/pjlib-util/stun_server.h b/pjlib-util/include/pjlib-util/stun_server.h new file mode 100644 index 00000000..15ecf308 --- /dev/null +++ b/pjlib-util/include/pjlib-util/stun_server.h @@ -0,0 +1,109 @@ +/* $Id$ */ +/* + * Copyright (C) 2003-2005 Benny Prijono <benny@prijono.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#ifndef __PJ_STUN_SERVER_H__ +#define __PJ_STUN_SERVER_H__ + +/** + * @file stun_server.h + * @brief STUN server side services. + */ + +#include <pjlib-util/stun_msg.h> +#include <pjlib-util/stun_endpoint.h> + + +PJ_BEGIN_DECL + + +/* **************************************************************************/ +/** + * @defgroup PJLIB_UTIL_STUN_SERVER STUN Server Side Services + * @brief STUN server side services + * @ingroup PJLIB_UTIL_STUN + * @{ + */ + +typedef struct pj_stun_service pj_stun_service; + + +/** + * STUN service handler. + */ +typedef struct pj_stun_service_handler +{ + /** + * The STUN message type. + */ + int msg_type; + + /** + * Callback to be called to handle this STUN message type. + * + * @param svc The service. + * @param msg The STUN message. + */ + pj_status_t (*handle_msg)(pj_stun_service *svc, + void *handle_data, + const pj_stun_msg *msg); + +} pj_stun_service_handler; + + +/** + * Create STUN service. + */ +PJ_DECL(pj_status_t) pj_stun_service_create(pj_pool_t *pool, + const char *name, + unsigned options, + unsigned handler_cnt, + pj_stun_service_handler cb[], + void *user_data, + pj_stun_service **p_svc); + +/** + * Destroy STUN service + */ +PJ_DECL(pj_status_t) pj_stun_service_destroy(pj_stun_service *svc); + + +/** + * Get user data associated with the STUN service. + */ +PJ_DECL(void*) pj_stun_service_get_user_data(pj_stun_service *svc); + + +/** + * Instruct the STUN service to handle incoming STUN message. + */ +PJ_DECL(pj_status_t) pj_stun_service_handle_msg(pj_stun_service *svc, + void *handle_data, + const pj_stun_msg *msg); + + + +/** + * @} + */ + + +PJ_END_DECL + + +#endif /* __PJ_STUN_SERVER_H__ */ + diff --git a/pjlib-util/include/pjlib-util/stun.h b/pjlib-util/include/pjlib-util/stun_simple.h index e317877c..5a4f2c78 100644 --- a/pjlib-util/include/pjlib-util/stun.h +++ b/pjlib-util/include/pjlib-util/stun_simple.h @@ -1,4 +1,4 @@ -/* $Id */ +/* $Id$ */ /* * Copyright (C) 2003-2005 Benny Prijono <benny@prijono.org> * @@ -16,8 +16,8 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#ifndef __PJ_STUN_H__ -#define __PJ_STUN_H__ +#ifndef __PJSTUN_H__ +#define __PJSTUN_H__ /** * @file stun.h @@ -27,127 +27,133 @@ #include <pjlib-util/types.h> #include <pj/sock.h> -/** - * @defgroup PJLIB_UTIL_STUN_CLIENT Mini/Tiny STUN Client - * @ingroup PJLIB_UTIL - * @{ - */ PJ_BEGIN_DECL -/** +/* * This enumeration describes STUN message types. */ -typedef enum pj_stun_msg_type +typedef enum pjstun_msg_type { - PJ_STUN_BINDING_REQUEST = 0x0001, - PJ_STUN_BINDING_RESPONSE = 0x0101, - PJ_STUN_BINDING_ERROR_RESPONSE = 0x0111, - PJ_STUN_SHARED_SECRET_REQUEST = 0x0002, - PJ_STUN_SHARED_SECRET_RESPONSE = 0x0102, - PJ_STUN_SHARED_SECRET_ERROR_RESPONSE = 0x0112 -} pj_stun_msg_type; + PJSTUN_BINDING_REQUEST = 0x0001, + PJSTUN_BINDING_RESPONSE = 0x0101, + PJSTUN_BINDING_ERROR_RESPONSE = 0x0111, + PJSTUN_SHARED_SECRET_REQUEST = 0x0002, + PJSTUN_SHARED_SECRET_RESPONSE = 0x0102, + PJSTUN_SHARED_SECRET_ERROR_RESPONSE = 0x0112 +} pjstun_msg_type; -/** +/* * This enumeration describes STUN attribute types. */ -typedef enum pj_stun_attr_type +typedef enum pjstun_attr_type { - PJ_STUN_ATTR_MAPPED_ADDR = 1, - PJ_STUN_ATTR_RESPONSE_ADDR, - PJ_STUN_ATTR_CHANGE_REQUEST, - PJ_STUN_ATTR_SOURCE_ADDR, - PJ_STUN_ATTR_CHANGED_ADDR, - PJ_STUN_ATTR_USERNAME, - PJ_STUN_ATTR_PASSWORD, - PJ_STUN_ATTR_MESSAGE_INTEGRITY, - PJ_STUN_ATTR_ERROR_CODE, - PJ_STUN_ATTR_UNKNOWN_ATTRIBUTES, - PJ_STUN_ATTR_REFLECTED_FORM -} pj_stun_attr_type; - - -/** + PJSTUN_ATTR_MAPPED_ADDR = 1, + PJSTUN_ATTR_RESPONSE_ADDR, + PJSTUN_ATTR_CHANGE_REQUEST, + PJSTUN_ATTR_SOURCE_ADDR, + PJSTUN_ATTR_CHANGED_ADDR, + PJSTUN_ATTR_USERNAME, + PJSTUN_ATTR_PASSWORD, + PJSTUN_ATTR_MESSAGE_INTEGRITY, + PJSTUN_ATTR_ERROR_CODE, + PJSTUN_ATTR_UNKNOWN_ATTRIBUTES, + PJSTUN_ATTR_REFLECTED_FORM +} pjstun_attr_type; + + +/* * This structre describes STUN message header. */ -typedef struct pj_stun_msg_hdr +typedef struct pjstun_msg_hdr { pj_uint16_t type; pj_uint16_t length; pj_uint32_t tsx[4]; -} pj_stun_msg_hdr; +} pjstun_msg_hdr; -/** +/* * This structre describes STUN attribute header. */ -typedef struct pj_stun_attr_hdr +typedef struct pjstun_attr_hdr { pj_uint16_t type; pj_uint16_t length; -} pj_stun_attr_hdr; +} pjstun_attr_hdr; -/** +/* * This structre describes STUN MAPPED-ADDR attribute. */ -typedef struct pj_stun_mapped_addr_attr +typedef struct pjstun_mapped_addr_attr { - pj_stun_attr_hdr hdr; + pjstun_attr_hdr hdr; pj_uint8_t ignored; pj_uint8_t family; pj_uint16_t port; pj_uint32_t addr; -} pj_stun_mapped_addr_attr; +} pjstun_mapped_addr_attr; -typedef pj_stun_mapped_addr_attr pj_stun_response_addr_attr; -typedef pj_stun_mapped_addr_attr pj_stun_changed_addr_attr; -typedef pj_stun_mapped_addr_attr pj_stun_src_addr_attr; -typedef pj_stun_mapped_addr_attr pj_stun_reflected_form_attr; +typedef pjstun_mapped_addr_attr pjstun_response_addr_attr; +typedef pjstun_mapped_addr_attr pjstun_changed_addr_attr; +typedef pjstun_mapped_addr_attr pjstun_src_addr_attr; +typedef pjstun_mapped_addr_attr pjstun_reflected_form_attr; -typedef struct pj_stun_change_request_attr +typedef struct pjstun_change_request_attr { - pj_stun_attr_hdr hdr; + pjstun_attr_hdr hdr; pj_uint32_t value; -} pj_stun_change_request_attr; +} pjstun_change_request_attr; -typedef struct pj_stun_username_attr +typedef struct pjstun_username_attr { - pj_stun_attr_hdr hdr; + pjstun_attr_hdr hdr; pj_uint32_t value[1]; -} pj_stun_username_attr; +} pjstun_username_attr; -typedef pj_stun_username_attr pj_stun_password_attr; +typedef pjstun_username_attr pjstun_password_attr; -typedef struct pj_stun_error_code_attr +typedef struct pjstun_error_code_attr { - pj_stun_attr_hdr hdr; + pjstun_attr_hdr hdr; pj_uint16_t ignored; pj_uint8_t err_class; pj_uint8_t number; char reason[4]; -} pj_stun_error_code_attr; +} pjstun_error_code_attr; -typedef struct pj_stun_msg +typedef struct pjstun_msg { - pj_stun_msg_hdr *hdr; + pjstun_msg_hdr *hdr; int attr_count; - pj_stun_attr_hdr *attr[PJ_STUN_MAX_ATTR]; -} pj_stun_msg; + pjstun_attr_hdr *attr[PJSTUN_MAX_ATTR]; +} pjstun_msg; /* STUN message API (stun.c). */ -PJ_DECL(pj_status_t) pj_stun_create_bind_req( pj_pool_t *pool, +PJ_DECL(pj_status_t) pjstun_create_bind_req( pj_pool_t *pool, void **msg, pj_size_t *len, pj_uint32_t id_hi, pj_uint32_t id_lo); -PJ_DECL(pj_status_t) pj_stun_parse_msg( void *buf, pj_size_t len, - pj_stun_msg *msg); -PJ_DECL(void*) pj_stun_msg_find_attr( pj_stun_msg *msg, pj_stun_attr_type t); +PJ_DECL(pj_status_t) pjstun_parse_msg( void *buf, pj_size_t len, + pjstun_msg *msg); +PJ_DECL(void*) pjstun_msg_find_attr( pjstun_msg *msg, pjstun_attr_type t); /** + * @defgroup PJLIB_UTIL_STUN_CLIENT Simple STUN Helper + * @ingroup PJLIB_UTIL_STUN + * @brief A simple and small footprint STUN resolution helper + * @{ + * + * This is the older implementation of STUN client, with only one function + * provided (pjstun_get_mapped_addr()) to retrieve the public IP address + * of multiple sockets. + */ + +/** * This is the main function to request the mapped address of local sockets * to multiple STUN servers. This function is able to find the mapped * addresses of multiple sockets simultaneously, and for each socket, two @@ -185,7 +191,7 @@ PJ_DECL(void*) pj_stun_msg_find_attr( pj_stun_msg *msg, pj_stun_attr_type t); * - etc. * */ -PJ_DECL(pj_status_t) pj_stun_get_mapped_addr( pj_pool_factory *pf, +PJ_DECL(pj_status_t) pjstun_get_mapped_addr( pj_pool_factory *pf, int sock_cnt, pj_sock_t sock[], const pj_str_t *srv1, int port1, const pj_str_t *srv2, int port2, @@ -197,5 +203,5 @@ PJ_END_DECL * @} */ -#endif /* __PJ_STUN_H__ */ +#endif /* __PJSTUN_H__ */ diff --git a/pjlib-util/include/pjlib-util/stun_transaction.h b/pjlib-util/include/pjlib-util/stun_transaction.h new file mode 100644 index 00000000..2cacef36 --- /dev/null +++ b/pjlib-util/include/pjlib-util/stun_transaction.h @@ -0,0 +1,205 @@ +/* $Id$ */ +/* + * Copyright (C) 2003-2005 Benny Prijono <benny@prijono.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#ifndef __PJ_STUN_TRANSACTION_H__ +#define __PJ_STUN_TRANSACTION_H__ + +/** + * @file stun_transaction.h + * @brief STUN transaction + */ + +#include <pjlib-util/stun_msg.h> +#include <pjlib-util/stun_endpoint.h> + + +PJ_BEGIN_DECL + + +/* **************************************************************************/ +/** + * @defgroup PJLIB_UTIL_STUN_TRANSACTION STUN Client Transaction + * @brief STUN client transaction + * @ingroup PJLIB_UTIL_STUN + * @{ + * + The @ref PJLIB_UTIL_STUN_TRANSACTION is used to manage outgoing STUN request, + for example to retransmit the request and to notify application about the + completion of the request. + + The @ref PJLIB_UTIL_STUN_TRANSACTION does not use any networking operations, + but instead application must supply the transaction with a callback to + be used by the transaction to send outgoing requests. This way the STUN + transaction is made more generic and can work with different types of + networking codes in application. + + + */ + +/** + * Opaque declaration of STUN client transaction. + */ +typedef struct pj_stun_client_tsx pj_stun_client_tsx; + +/** + * STUN client transaction callback. + */ +typedef struct pj_stun_tsx_cb +{ + /** + * This callback is called when the STUN transaction completed. + * + * @param tsx The STUN transaction. + * @param status Status of the transaction. Status PJ_SUCCESS + * means that the request has received a successful + * response. + * @param response The STUN response, which value may be NULL if + * \a status is not PJ_SUCCESS. + */ + void (*on_complete)(pj_stun_client_tsx *tsx, + pj_status_t status, + pj_stun_msg *response); + + /** + * This callback is called by the STUN transaction when it wants to send + * outgoing message. + * + * @param tsx The STUN transaction instance. + * @param stun_pkt The STUN packet to be sent. + * @param pkt_size Size of the STUN packet. + * + * @return If return value of the callback is not PJ_SUCCESS, + * the transaction will fail. + */ + pj_status_t (*on_send_msg)(pj_stun_client_tsx *tsx, + const void *stun_pkt, + pj_size_t pkt_size); + +} pj_stun_tsx_cb; + + + +/** + * Create an instance of STUN client transaction. The STUN client + * transaction is used to transmit outgoing STUN request and to + * ensure the reliability of the request by periodically retransmitting + * the request, if necessary. + * + * @param endpt The STUN endpoint, which will be used to retrieve + * various settings for the transaction. + * @param cb Callback structure, to be used by the transaction + * to send message and to notify the application about + * the completion of the transaction. + * @param p_tsx Pointer to receive the transaction instance. + * + * @return PJ_SUCCESS on success, or the appropriate error code. + */ +PJ_DECL(pj_status_t) pj_stun_client_tsx_create( pj_stun_endpoint *endpt, + const pj_stun_tsx_cb *cb, + pj_stun_client_tsx **p_tsx); + +/** + * Destroy a STUN client transaction. + * + * @param tsx The STUN transaction. + * + * @return PJ_SUCCESS on success, or the appropriate error code. + */ +PJ_DECL(pj_status_t) pj_stun_client_tsx_destroy(pj_stun_client_tsx *tsx); + + +/** + * Associate an arbitrary data with the STUN transaction. This data + * can be then retrieved later from the transaction, by using + * pj_stun_client_tsx_get_data() function. + * + * @param tsx The STUN client transaction. + * @param data Application data to be associated with the + * STUN transaction. + * + * @return PJ_SUCCESS on success. + */ +PJ_DECL(pj_status_t) pj_stun_client_tsx_set_data(pj_stun_client_tsx *tsx, + void *data); + + +/** + * Get the user data that was previously associated with the STUN + * transaction. + * + * @param tsx The STUN client transaction. + * + * @return The user data. + */ +PJ_DECL(void*) pj_stun_client_tsx_get_data(pj_stun_client_tsx *tsx); + + +/** + * Start the STUN client transaction by sending STUN request using + * this transaction. If reliable transport such as TCP or TLS is used, + * the retransmit flag should be set to PJ_FALSE because reliablity + * will be assured by the transport layer. + * + * @param tsx The STUN client transaction. + * @param retransmit Should this message be retransmitted by the + * STUN transaction. + * @param msg The STUN message. + * + * @return PJ_SUCCESS on success or the appropriate error code. + */ +PJ_DECL(pj_status_t) pj_stun_client_tsx_send_msg(pj_stun_client_tsx *tsx, + pj_bool_t retransmit, + const pj_stun_msg *msg); + + +/** + * Notify the STUN transaction about the arrival of STUN response. + * If the STUN response contains a final error (300 and greater), the + * transaction will be terminated and callback will be called. If the + * STUN response contains response code 100-299, retransmission + * will cease, but application must still call this function again + * with a final response later to allow the transaction to complete. + * + * @param tsx The STUN client transaction instance. + * @param packet The incoming packet. + * @param pkt_size Size of the incoming packet. + * @param parsed_len Optional pointer to receive the number of bytes + * that have been parsed from the incoming packet + * for the STUN message. This is useful if the + * STUN transaction is running over stream oriented + * socket such as TCP or TLS. + * + * @return PJ_SUCCESS on success or the appropriate error code. + */ +PJ_DECL(pj_status_t) pj_stun_client_tsx_on_rx_msg(pj_stun_client_tsx *tsx, + const void *packet, + pj_size_t pkt_size, + unsigned *parsed_len); + + + +/** + * @} + */ + + +PJ_END_DECL + + +#endif /* __PJ_STUN_TRANSACTION_H__ */ + diff --git a/pjlib-util/src/pjlib-util/stun_endpoint.c b/pjlib-util/src/pjlib-util/stun_endpoint.c new file mode 100644 index 00000000..6a1de943 --- /dev/null +++ b/pjlib-util/src/pjlib-util/stun_endpoint.c @@ -0,0 +1,68 @@ +/* $Id$ */ +/* + * Copyright (C) 2003-2005 Benny Prijono <benny@prijono.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#include <pjlib-util/stun_endpoint.h> +#include <pjlib-util/errno.h> +#include <pj/assert.h> +#include <pj/pool.h> + + +/* + * Create a STUN endpoint instance. + */ +PJ_DEF(pj_status_t) pj_stun_endpt_create( pj_pool_factory *factory, + unsigned options, + pj_ioqueue_t *ioqueue, + pj_timer_heap_t *timer_heap, + pj_stun_endpoint **p_endpt) +{ + pj_pool_t *pool; + pj_stun_endpoint *endpt; + + PJ_ASSERT_RETURN(factory && p_endpt, PJ_EINVAL); + + pool = pj_pool_create(factory, "stunendpt", 1000, 1000, NULL); + if (!pool) + return PJ_ENOMEM; + + endpt = PJ_POOL_ZALLOC_TYPE(pool, pj_stun_endpoint); + endpt->pool = pool; + endpt->pf = factory; + endpt->options = options; + endpt->ioqueue = ioqueue; + endpt->timer_heap = timer_heap; + endpt->rto_msec = PJ_STUN_RTO_VALUE; + + *p_endpt = endpt; + + return PJ_SUCCESS; +} + + +/* + * Destroy STUN endpoint instance. + */ +PJ_DEF(pj_status_t) pj_stun_endpt_destroy(pj_stun_endpoint *endpt) +{ + PJ_ASSERT_RETURN(endpt, PJ_EINVAL); + + pj_pool_release(endpt->pool); + + return PJ_SUCCESS; +} + diff --git a/pjlib-util/src/pjlib-util/stun_msg.c b/pjlib-util/src/pjlib-util/stun_msg.c new file mode 100644 index 00000000..9abb2a4c --- /dev/null +++ b/pjlib-util/src/pjlib-util/stun_msg.c @@ -0,0 +1,1562 @@ +/* $Id$ */ +/* + * Copyright (C) 2003-2005 Benny Prijono <benny@prijono.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#include <pjlib-util/stun_msg.h> +#include <pjlib-util/errno.h> +#include <pj/assert.h> +#include <pj/log.h> +#include <pj/os.h> +#include <pj/pool.h> +#include <pj/rand.h> +#include <pj/string.h> + +#define THIS_FILE "stun_msg.c" + + +static const char *stun_method_names[] = +{ + "Unknown", /* 0 */ + "Binding", /* 1 */ + "Shared Secret", /* 2 */ + "Allocate", /* 3 */ + "Send", /* 4 */ + "Data", /* 5 */ + "Set Active Destination", /* 6 */ + "Connect", /* 7 */ + "Connect Status" /* 8 */ +}; + +static struct +{ + int err_code; + const char *err_msg; +} stun_err_msg_map[] = +{ + { PJ_STUN_STATUS_TRY_ALTERNATE, "Try Alternate"}, + { PJ_STUN_STATUS_BAD_REQUEST, "Bad Request"}, + { PJ_STUN_STATUS_UNAUTHORIZED, "Unauthorized"}, + { PJ_STUN_STATUS_UNKNOWN_ATTRIBUTE, "Unknown Attribute"}, + { PJ_STUN_STATUS_STALE_CREDENTIALS, "Stale Credentials"}, + { PJ_STUN_STATUS_INTEGRITY_CHECK_FAILURE,"Integrity Check Failure"}, + { PJ_STUN_STATUS_MISSING_USERNAME, "Missing Username"}, + { PJ_STUN_STATUS_USE_TLS, "Use TLS"}, + { PJ_STUN_STATUS_MISSING_REALM, "Missing Realm"}, + { PJ_STUN_STATUS_MISSING_NONCE, "Missing Nonce"}, + { PJ_STUN_STATUS_UNKNOWN_USERNAME, "Unknown Username"}, + { PJ_STUN_STATUS_NO_BINDING, "No Binding"}, + { PJ_STUN_STATUS_STALE_NONCE, "Stale Nonce"}, + { PJ_STUN_STATUS_TRANSITIONING, "Transitioning"}, + { PJ_STUN_STATUS_WRONG_USERNAME, "Wrong Username"}, + { PJ_STUN_STATUS_UNSUPP_TRANSPORT_PROTO,"Unsupported Transport Protocol"}, + { PJ_STUN_STATUS_INVALID_IP_ADDR, "Invalid IP Address"}, + { PJ_STUN_STATUS_INVALID_PORT, "Invalid Port"}, + { PJ_STUN_STATUS_OPER_TCP_ONLY, "Operation for TCP Only"}, + { PJ_STUN_STATUS_CONNECTION_FAILURE, "Connection Failure"}, + { PJ_STUN_STATUS_CONNECTION_TIMEOUT, "Connection Timeout"}, + { PJ_STUN_STATUS_SERVER_ERROR, "Server Error"}, + { PJ_STUN_STATUS_GLOBAL_FAILURE, "Global Failure"} +}; + + + +struct attr_desc +{ + const char *name; + pj_status_t (*decode_attr)(pj_pool_t *pool, const pj_uint8_t *buf, + void **p_attr); + pj_status_t (*encode_attr)(const void *a, pj_uint8_t *buf, + unsigned len, unsigned *printed); + +}; + +static pj_status_t decode_generic_ip_addr_attr(pj_pool_t *pool, + const pj_uint8_t *buf, + void **p_attr); +static pj_status_t encode_generic_ip_addr_attr(const void *a, pj_uint8_t *buf, + unsigned len, + unsigned *printed); +static pj_status_t decode_generic_string_attr(pj_pool_t *pool, + const pj_uint8_t *buf, + void **p_attr); +static pj_status_t encode_generic_string_attr(const void *a, pj_uint8_t *buf, + unsigned len, unsigned *printed); +static pj_status_t decode_msg_integrity_attr(pj_pool_t *pool, + const pj_uint8_t *buf, + void **p_attr); +static pj_status_t encode_msg_integrity_attr(const void *a, pj_uint8_t *buf, + unsigned len, unsigned *printed); +static pj_status_t decode_error_code_attr(pj_pool_t *pool, + const pj_uint8_t *buf, + void **p_attr); +static pj_status_t encode_error_code_attr(const void *a, pj_uint8_t *buf, + unsigned len, unsigned *printed); +static pj_status_t decode_unknown_attr(pj_pool_t *pool, + const pj_uint8_t *buf, + void **p_attr); +static pj_status_t encode_unknown_attr(const void *a, pj_uint8_t *buf, + unsigned len, unsigned *printed); +static pj_status_t decode_generic_uint_attr(pj_pool_t *pool, + const pj_uint8_t *buf, + void **p_attr); +static pj_status_t encode_generic_uint_attr(const void *a, pj_uint8_t *buf, + unsigned len, unsigned *printed); +static pj_status_t decode_binary_attr(pj_pool_t *pool, + const pj_uint8_t *buf, + void **p_attr); +static pj_status_t encode_binary_attr(const void *a, pj_uint8_t *buf, + unsigned len, unsigned *printed); +static pj_status_t decode_empty_attr(pj_pool_t *pool, + const pj_uint8_t *buf, + void **p_attr); +static pj_status_t encode_empty_attr(const void *a, pj_uint8_t *buf, + unsigned len, unsigned *printed); + + +struct attr_desc mandatory_attr_desc[] = +{ + { + /* type zero */ + NULL, + NULL, + NULL + }, + { + /* PJ_STUN_ATTR_MAPPED_ADDR, */ + "MAPPED-ADDRESS", + &decode_generic_ip_addr_attr, + &encode_generic_ip_addr_attr + }, + { + /* PJ_STUN_ATTR_RESPONSE_ADDR, */ + "RESPONSE-ADDRESS", + &decode_generic_ip_addr_attr, + &encode_generic_ip_addr_attr + }, + { + /* PJ_STUN_ATTR_CHANGE_REQUEST, */ + "CHANGE-REQUEST", + &decode_generic_uint_attr, + &encode_generic_uint_attr + }, + { + /* PJ_STUN_ATTR_SOURCE_ADDR, */ + "SOURCE-ADDRESS", + &decode_generic_ip_addr_attr, + &encode_generic_ip_addr_attr + }, + { + /* PJ_STUN_ATTR_CHANGED_ADDR, */ + "CHANGED-ADDRESS", + &decode_generic_ip_addr_attr, + &encode_generic_ip_addr_attr + }, + { + /* PJ_STUN_ATTR_USERNAME, */ + "USERNAME", + &decode_generic_string_attr, + &encode_generic_string_attr + }, + { + /* PJ_STUN_ATTR_PASSWORD, */ + "PASSWORD", + &decode_generic_string_attr, + &encode_generic_string_attr + }, + { + /* PJ_STUN_ATTR_MESSAGE_INTEGRITY, */ + "MESSAGE-INTEGRITY", + &decode_msg_integrity_attr, + &encode_msg_integrity_attr + }, + { + /* PJ_STUN_ATTR_ERROR_CODE, */ + "ERROR-CODE", + &decode_error_code_attr, + &encode_error_code_attr + }, + { + /* PJ_STUN_ATTR_UNKNOWN_ATTRIBUTES, */ + "UNKNOWN-ATTRIBUTES", + &decode_unknown_attr, + &encode_unknown_attr + }, + { + /* PJ_STUN_ATTR_REFLECTED_FROM, */ + "REFLECTED-FROM", + &decode_generic_ip_addr_attr, + &encode_generic_ip_addr_attr + }, + { + /* ID 0x000C is not assigned */ + NULL, + NULL, + NULL + }, + { + /* PJ_STUN_ATTR_LIFETIME, */ + "LIFETIME", + &decode_generic_uint_attr, + &encode_generic_uint_attr + }, + { + /* ID 0x000E is not assigned */ + NULL, + NULL, + NULL + }, + { + /* ID 0x000F is not assigned */ + NULL, + NULL, + NULL + }, + { + /* PJ_STUN_ATTR_BANDWIDTH, */ + "BANDWIDTH", + &decode_generic_uint_attr, + &encode_generic_uint_attr + }, + { + /* ID 0x0011 is not assigned */ + NULL, + NULL, + NULL + }, + { + /* PJ_STUN_ATTR_REMOTE_ADDRESS, */ + "REMOTE-ADDRESS", + &decode_generic_ip_addr_attr, + &encode_generic_ip_addr_attr + }, + { + /* PJ_STUN_ATTR_DATA, */ + "DATA", + &decode_binary_attr, + &encode_binary_attr + }, + { + /* PJ_STUN_ATTR_REALM, */ + "REALM", + &decode_generic_string_attr, + &encode_generic_string_attr + }, + { + /* PJ_STUN_ATTR_NONCE, */ + "NONCE", + &decode_generic_string_attr, + &encode_generic_string_attr + }, + { + /* PJ_STUN_ATTR_RELAY_ADDRESS, */ + "RELAY-ADDRESS", + &decode_generic_ip_addr_attr, + &encode_generic_ip_addr_attr + }, + { + /* PJ_STUN_ATTR_REQUESTED_ADDR_TYPE, */ + "REQUESTED-ADDRESS-TYPE", + &decode_generic_uint_attr, + &encode_generic_uint_attr + }, + { + /* PJ_STUN_ATTR_REQUESTED_PORT_PROPS, */ + "REQUESTED-PORT-PROPS", + &decode_generic_uint_attr, + &encode_generic_uint_attr + }, + { + /* PJ_STUN_ATTR_REQUESTED_TRANSPORT, */ + "REQUESTED-TRANSPORT", + &decode_generic_uint_attr, + &encode_generic_uint_attr + }, + { + /* ID 0x001A is not assigned */ + NULL, + NULL, + NULL + }, + { + /* ID 0x001B is not assigned */ + NULL, + NULL, + NULL + }, + { + /* ID 0x001C is not assigned */ + NULL, + NULL, + NULL + }, + { + /* ID 0x001D is not assigned */ + NULL, + NULL, + NULL + }, + { + /* ID 0x001E is not assigned */ + NULL, + NULL, + NULL + }, + { + /* ID 0x001F is not assigned */ + NULL, + NULL, + NULL + }, + { + /* PJ_STUN_ATTR_XOR_MAPPED_ADDRESS, */ + "XOR-MAPPED-ADDRESS", + &decode_generic_ip_addr_attr, + &encode_generic_ip_addr_attr + }, + { + /* PJ_STUN_ATTR_TIMER_VAL, */ + "TIMER-VAL", + &decode_generic_uint_attr, + &encode_generic_uint_attr + }, + { + /* PJ_STUN_ATTR_REQUESTED_IP, */ + "REQUESTED-IP", + &decode_generic_ip_addr_attr, + &encode_generic_ip_addr_attr + }, + { + /* PJ_STUN_ATTR_XOR_REFLECTED_FROM, */ + "XOR-REFLECTED-FROM", + &decode_generic_ip_addr_attr, + &encode_generic_ip_addr_attr + }, + { + /* PJ_STUN_ATTR_PRIORITY, */ + "PRIORITY", + &decode_generic_uint_attr, + &encode_generic_uint_attr + }, + { + /* PJ_STUN_ATTR_USE_CANDIDATE, */ + "USE-CANDIDATE", + &decode_empty_attr, + &encode_empty_attr + }, + { + /* PJ_STUN_ATTR_XOR_INTERNAL_ADDR, */ + "XOR-INTERNAL-ADDRESS", + &decode_generic_ip_addr_attr, + &encode_generic_ip_addr_attr + }, + + /* Sentinel */ + { + /* PJ_STUN_ATTR_END_MANDATORY_ATTR */ + NULL, + NULL, + NULL + } +}; + +static struct attr_desc extended_attr_desc[] = +{ + { + /* PJ_STUN_ATTR_FINGERPRINT, */ + "FINGERPRINT", + &decode_generic_uint_attr, + &encode_generic_uint_attr + }, + { + /* PJ_STUN_ATTR_SERVER, */ + "SERVER", + &decode_generic_string_attr, + &encode_generic_string_attr + }, + { + /* PJ_STUN_ATTR_ALTERNATE_SERVER, */ + "ALTERNATE-SERVER", + &decode_generic_ip_addr_attr, + &encode_generic_ip_addr_attr + }, + { + /* PJ_STUN_ATTR_REFRESH_INTERVAL, */ + "REFRESH-INTERVAL", + &decode_generic_uint_attr, + &encode_generic_uint_attr + }, +}; + + + +/* + * Get STUN message type name. + */ +PJ_DEF(const char*) pj_stun_get_method_name(unsigned msg_type) +{ + unsigned method = PJ_STUN_GET_METHOD(msg_type); + + if (method >= PJ_ARRAY_SIZE(stun_method_names)) + return "???"; + + return stun_method_names[method]; +} + + +/* + * Get STUN message class name. + */ +PJ_DEF(const char*) pj_stun_get_class_name(unsigned msg_type) +{ + if (PJ_STUN_IS_REQUEST(msg_type)) + return "request"; + else if (PJ_STUN_IS_RESPONSE(msg_type)) + return "success response"; + else if (PJ_STUN_IS_ERROR_RESPONSE(msg_type)) + return "error response"; + else if (PJ_STUN_IS_INDICATION(msg_type)) + return "indication"; + else + return "???"; +} + + +static const struct attr_desc *find_attr_desc(unsigned attr_type) +{ + struct attr_desc *desc; + + /* Check that attr_desc array is valid */ + pj_assert(PJ_ARRAY_SIZE(mandatory_attr_desc)== + PJ_STUN_ATTR_END_MANDATORY_ATTR+1); + pj_assert(mandatory_attr_desc[PJ_STUN_ATTR_END_MANDATORY_ATTR].decode_attr + == NULL); + pj_assert(mandatory_attr_desc[PJ_STUN_ATTR_USE_CANDIDATE].decode_attr + == &decode_empty_attr); + pj_assert(PJ_ARRAY_SIZE(extended_attr_desc) == + PJ_STUN_ATTR_END_EXTENDED_ATTR-PJ_STUN_ATTR_START_EXTENDED_ATTR); + + if (attr_type < PJ_STUN_ATTR_START_EXTENDED_ATTR) + desc = &mandatory_attr_desc[attr_type]; + else if (attr_type >= PJ_STUN_ATTR_START_EXTENDED_ATTR && + attr_type < PJ_STUN_ATTR_END_EXTENDED_ATTR) + desc = &extended_attr_desc[attr_type-PJ_STUN_ATTR_START_EXTENDED_ATTR]; + else + return NULL; + + return desc->decode_attr == NULL ? NULL : desc; +} + + +/* + * Get STUN attribute name. + */ +PJ_DEF(const char*) pj_stun_get_attr_name(unsigned attr_type) +{ + const struct attr_desc *attr_desc; + + attr_desc = find_attr_desc(attr_type); + if (!attr_desc || attr_desc->name==NULL) + return "???"; + + return attr_desc->name; +} + + +/** + * Get STUN standard reason phrase for the specified error code. + */ +PJ_DEF(pj_str_t) pj_stun_get_err_reason(int err_code) +{ + unsigned i; + + for (i=0; i<PJ_ARRAY_SIZE(stun_err_msg_map); ++i) { + if (stun_err_msg_map[i].err_code == err_code) + return pj_str((char*)stun_err_msg_map[i].err_msg); + } + return pj_str(NULL); +} + + + +#define INIT_ATTR(a,t,l) (a)->hdr.type=(pj_uint16_t)(t), \ + (a)->hdr.length=(pj_uint16_t)(l) +#define ATTR_HDR_LEN 4 + +#define getval16(p, pos) (pj_uint16_t)(((p)[(pos)] << 8) | \ + ((p)[(pos) + 1] << 0)) + + +////////////////////////////////////////////////////////////////////////////// +/* + * STUN generic IP address container + */ +#define STUN_GENERIC_IP_ADDR_LEN 8 + +/* + * Create a generic STUN IP address attribute for IPv4 address. + */ +PJ_DEF(pj_status_t) +pj_stun_generic_ip_addr_attr_create(pj_pool_t *pool, + int attr_type, + pj_bool_t xor_ed, + unsigned addr_len, + const pj_sockaddr_t *addr, + pj_stun_generic_ip_addr_attr **p_attr) +{ + pj_stun_generic_ip_addr_attr *attr; + + PJ_ASSERT_RETURN(pool && addr_len && addr && p_attr, PJ_EINVAL); + PJ_ASSERT_RETURN(addr_len == sizeof(pj_sockaddr_in) || + addr_len == sizeof(pj_sockaddr_in6), PJ_EINVAL); + + attr = PJ_POOL_ZALLOC_TYPE(pool, pj_stun_generic_ip_addr_attr); + INIT_ATTR(attr, attr_type, STUN_GENERIC_IP_ADDR_LEN); + + if (!xor_ed) { + pj_memcpy(&attr->addr, addr, addr_len); + } else if (addr_len == sizeof(pj_sockaddr_in)) { + const pj_sockaddr_in *addr4 = (const pj_sockaddr_in*) addr; + + pj_sockaddr_in_init(&attr->addr.ipv4, NULL, 0); + attr->addr.ipv4.sin_port = (pj_uint16_t)(addr4->sin_port ^ 0x2112); + attr->addr.ipv4.sin_addr.s_addr = (addr4->sin_addr.s_addr ^ + pj_htonl(0x2112A442)); + } else if (addr_len == sizeof(pj_sockaddr_in6)) { + return PJLIB_UTIL_ESTUNIPV6NOTSUPP; + } else { + return PJLIB_UTIL_ESTUNINADDRLEN; + } + + *p_attr = attr; + + return PJ_SUCCESS; +} + + +static pj_status_t decode_generic_ip_addr_attr(pj_pool_t *pool, + const pj_uint8_t *buf, + void **p_attr) +{ + pj_stun_generic_ip_addr_attr *attr; + pj_uint32_t val; + + /* Create the attribute */ + attr = PJ_POOL_ZALLOC_TYPE(pool, pj_stun_generic_ip_addr_attr); + pj_memcpy(attr, buf, ATTR_HDR_LEN); + + /* Convert to host byte order */ + attr->hdr.type = pj_ntohs(attr->hdr.type); + attr->hdr.length = pj_ntohs(attr->hdr.length); + + /* Check that the attribute length is valid */ + if (attr->hdr.length != STUN_GENERIC_IP_ADDR_LEN) + return PJLIB_UTIL_ESTUNINATTRLEN; + + /* Check address family */ + val = *(pj_uint8_t*)(buf + ATTR_HDR_LEN + 1); + + /* Check address family is valid (only supports ipv4 for now) */ + if (val != 1) + return PJLIB_UTIL_ESTUNIPV6NOTSUPP; + + /* Get port and address */ + pj_sockaddr_in_init(&attr->addr.ipv4, NULL, 0); + attr->addr.ipv4.sin_port = getval16(buf, ATTR_HDR_LEN + 2); + pj_memcpy(&attr->addr.ipv4.sin_addr, buf+ATTR_HDR_LEN+4, 4); + + /* Done */ + *p_attr = attr; + + return PJ_SUCCESS; +} + + +static pj_status_t encode_generic_ip_addr_attr(const void *a, pj_uint8_t *buf, + unsigned len, unsigned *printed) +{ + enum { + ATTR_LEN = ATTR_HDR_LEN + STUN_GENERIC_IP_ADDR_LEN + }; + pj_uint8_t *start_buf = buf; + const pj_stun_generic_ip_addr_attr *ca = + (const pj_stun_generic_ip_addr_attr *)a; + pj_stun_generic_ip_addr_attr *attr; + + if (len < ATTR_LEN) + return PJ_ETOOSMALL; + + /* Copy and convert headers to network byte order */ + pj_memcpy(buf, a, ATTR_HDR_LEN); + attr = (pj_stun_generic_ip_addr_attr*) buf; + attr->hdr.type = pj_htons(attr->hdr.type); + attr->hdr.length = pj_htons((pj_uint16_t)STUN_GENERIC_IP_ADDR_LEN); + buf += ATTR_HDR_LEN; + + /* Ignored */ + *buf++ = '\0'; + + /* Family (IPv4 only for now) */ + PJ_ASSERT_RETURN(ca->addr.addr.sa_family == PJ_AF_INET, PJ_EINVAL); + *buf++ = 1; + + /* Port */ + pj_memcpy(buf, &ca->addr.ipv4.sin_port, 2); + buf += 2; + + /* Address */ + pj_memcpy(buf, &ca->addr.ipv4.sin_addr, 4); + buf += 4; + + pj_assert(buf - start_buf == ATTR_LEN); + + /* Done */ + *printed = buf - start_buf; + + return PJ_SUCCESS; +} + + +////////////////////////////////////////////////////////////////////////////// +/* + * STUN generic string attribute + */ + +/* + * Create a STUN generic string attribute. + */ +PJ_DEF(pj_status_t) +pj_stun_generic_string_attr_create(pj_pool_t *pool, + int attr_type, + const pj_str_t *value, + pj_stun_generic_string_attr **p_attr) +{ + pj_stun_generic_string_attr *attr; + + PJ_ASSERT_RETURN(pool && value && p_attr, PJ_EINVAL); + + attr = PJ_POOL_ZALLOC_TYPE(pool, pj_stun_generic_string_attr); + INIT_ATTR(attr, attr_type, value->slen); + pj_strdup(pool, &attr->value, value); + + *p_attr = attr; + + return PJ_SUCCESS; +} + + +static pj_status_t decode_generic_string_attr(pj_pool_t *pool, + const pj_uint8_t *buf, + void **p_attr) +{ + pj_stun_generic_string_attr *attr; + pj_str_t value; + + /* Create the attribute */ + attr = PJ_POOL_ZALLOC_TYPE(pool, pj_stun_generic_string_attr); + + /* Copy the header */ + pj_memcpy(attr, buf, ATTR_HDR_LEN); + + /* Convert to host byte order */ + attr->hdr.type = pj_ntohs(attr->hdr.type); + attr->hdr.length = pj_ntohs(attr->hdr.length); + + /* Get pointer to the string in the message */ + value.ptr = ((char*)buf + ATTR_HDR_LEN); + value.slen = attr->hdr.length; + + /* Copy the string to the attribute */ + pj_strdup(pool, &attr->value, &value); + + /* Done */ + *p_attr = attr; + + return PJ_SUCCESS; + +} + + +static pj_status_t encode_generic_string_attr(const void *a, pj_uint8_t *buf, + unsigned len, unsigned *printed) +{ + const pj_stun_generic_string_attr *ca = + (const pj_stun_generic_string_attr*)a; + pj_stun_attr_hdr *attr; + + /* Calculated total attr_len (add padding if necessary) */ + *printed = (ca->value.slen + ATTR_HDR_LEN + 3) & (~3); + if (len < *printed) { + *printed = 0; + return PJ_ETOOSMALL; + } + + /* Copy header */ + pj_memcpy(buf, a, ATTR_HDR_LEN); + attr = (pj_stun_attr_hdr*)buf; + + /* Set the correct length */ + attr->length = (pj_uint16_t) ca->value.slen; + + /* Convert to network byte order */ + attr->type = pj_htons(attr->type); + attr->length = pj_htons(attr->length); + + /* Copy the string */ + pj_memcpy(buf+ATTR_HDR_LEN, ca->value.ptr, ca->value.slen); + + /* Done */ + return PJ_SUCCESS; +} + + +////////////////////////////////////////////////////////////////////////////// +/* + * STUN empty attribute (used by USE-CANDIDATE). + */ + +/* + * Create a STUN empty attribute. + */ +PJ_DEF(pj_status_t) +pj_stun_empty_attr_create(pj_pool_t *pool, + int attr_type, + pj_stun_empty_attr **p_attr) +{ + pj_stun_empty_attr *attr; + + PJ_ASSERT_RETURN(pool && p_attr, PJ_EINVAL); + + attr = PJ_POOL_ZALLOC_TYPE(pool, pj_stun_empty_attr); + INIT_ATTR(attr, attr_type, sizeof(pj_stun_empty_attr)); + + *p_attr = attr; + + return PJ_SUCCESS; +} + + +static pj_status_t decode_empty_attr(pj_pool_t *pool, + const pj_uint8_t *buf, + void **p_attr) +{ + pj_stun_empty_attr *attr; + + /* Check that the struct address is valid */ + pj_assert(sizeof(pj_stun_empty_attr) == ATTR_HDR_LEN); + + /* Create the attribute */ + attr = PJ_POOL_ZALLOC_TYPE(pool, pj_stun_empty_attr); + pj_memcpy(attr, buf, ATTR_HDR_LEN); + + /* Convert to host byte order */ + attr->hdr.type = pj_ntohs(attr->hdr.type); + attr->hdr.length = pj_ntohs(attr->hdr.length); + + /* Check that the attribute length is valid */ + if (attr->hdr.length != ATTR_HDR_LEN) + return PJLIB_UTIL_ESTUNINATTRLEN; + + /* Done */ + *p_attr = attr; + + return PJ_SUCCESS; +} + + +static pj_status_t encode_empty_attr(const void *a, pj_uint8_t *buf, + unsigned len, unsigned *printed) +{ + pj_stun_empty_attr *attr; + + if (len < ATTR_HDR_LEN) + return PJ_ETOOSMALL; + + /* Copy and convert attribute to network byte order */ + pj_memcpy(buf, a, ATTR_HDR_LEN); + attr = (pj_stun_empty_attr*) buf; + attr->hdr.type = pj_htons(attr->hdr.type); + pj_assert(attr->hdr.length == ATTR_HDR_LEN); + attr->hdr.length = pj_htons(ATTR_HDR_LEN); + + /* Done */ + *printed = ATTR_HDR_LEN; + + return PJ_SUCCESS; +} + + +////////////////////////////////////////////////////////////////////////////// +/* + * STUN generic 32bit integer attribute. + */ +#define STUN_UINT_LEN 4 + +/* + * Create a STUN generic 32bit value attribute. + */ +PJ_DEF(pj_status_t) +pj_stun_generic_uint_attr_create(pj_pool_t *pool, + int attr_type, + pj_uint32_t value, + pj_stun_generic_uint_attr **p_attr) +{ + pj_stun_generic_uint_attr *attr; + + PJ_ASSERT_RETURN(pool && p_attr, PJ_EINVAL); + + attr = PJ_POOL_ZALLOC_TYPE(pool, pj_stun_generic_uint_attr); + INIT_ATTR(attr, attr_type, STUN_UINT_LEN); + attr->value = value; + + *p_attr = attr; + + return PJ_SUCCESS; +} + + +static pj_status_t decode_generic_uint_attr(pj_pool_t *pool, + const pj_uint8_t *buf, + void **p_attr) +{ + enum + { + ATTR_LEN = STUN_UINT_LEN + ATTR_HDR_LEN + }; + pj_stun_generic_uint_attr *attr; + + /* Check that the struct address is valid */ + pj_assert(sizeof(pj_stun_generic_uint_attr) == ATTR_LEN); + + /* Create the attribute */ + attr = PJ_POOL_ZALLOC_TYPE(pool, pj_stun_generic_uint_attr); + pj_memcpy(attr, buf, ATTR_LEN); + + /* Convert to host byte order */ + attr->hdr.type = pj_ntohs(attr->hdr.type); + attr->hdr.length = pj_ntohs(attr->hdr.length); + attr->value = pj_ntohl(attr->value); + + /* Check that the attribute length is valid */ + if (attr->hdr.length != STUN_UINT_LEN) + return PJLIB_UTIL_ESTUNINATTRLEN; + + /* Done */ + *p_attr = attr; + + return PJ_SUCCESS; +} + + +static pj_status_t encode_generic_uint_attr(const void *a, pj_uint8_t *buf, + unsigned len, unsigned *printed) +{ + enum + { + ATTR_LEN = STUN_UINT_LEN + ATTR_HDR_LEN + }; + pj_stun_generic_uint_attr *attr; + + if (len < ATTR_LEN) + return PJ_ETOOSMALL; + + /* Copy and convert attribute to network byte order */ + pj_memcpy(buf, a, ATTR_LEN); + attr = (pj_stun_generic_uint_attr*) buf; + attr->hdr.type = pj_htons(attr->hdr.type); + pj_assert(attr->hdr.length == STUN_UINT_LEN); + attr->hdr.length = pj_htons(STUN_UINT_LEN); + attr->value = pj_htonl(attr->value); + + /* Done */ + *printed = ATTR_LEN; + + return PJ_SUCCESS; +} + +////////////////////////////////////////////////////////////////////////////// +/* + * STUN MESSAGE-INTEGRITY attribute. + */ + +#define STUN_MSG_INTEGRITY_LEN 20 + +/* + * Create a STUN MESSAGE-INTEGRITY attribute. + */ +PJ_DEF(pj_status_t) +pj_stun_msg_integrity_attr_create(pj_pool_t *pool, + pj_stun_msg_integrity_attr **p_attr) +{ + pj_stun_msg_integrity_attr *attr; + + PJ_ASSERT_RETURN(pool && p_attr, PJ_EINVAL); + + attr = PJ_POOL_ZALLOC_TYPE(pool, pj_stun_msg_integrity_attr); + INIT_ATTR(attr, PJ_STUN_ATTR_MESSAGE_INTEGRITY, STUN_MSG_INTEGRITY_LEN); + + *p_attr = attr; + + return PJ_SUCCESS; +} + + +static pj_status_t decode_msg_integrity_attr(pj_pool_t *pool, + const pj_uint8_t *buf, + void **p_attr) +{ + enum + { + ATTR_LEN = STUN_MSG_INTEGRITY_LEN + ATTR_HDR_LEN + }; + pj_stun_msg_integrity_attr *attr; + + /* Check that struct size is valid */ + pj_assert(sizeof(pj_stun_msg_integrity_attr)==STUN_MSG_INTEGRITY_LEN); + + /* Create attribute */ + attr = PJ_POOL_ZALLOC_TYPE(pool, pj_stun_msg_integrity_attr); + pj_memcpy(attr, buf, sizeof(pj_stun_msg_integrity_attr)); + attr->hdr.type = pj_ntohs(attr->hdr.type); + attr->hdr.length = pj_ntohs(attr->hdr.length); + + /* Check that the attribute length is valid */ + if (attr->hdr.length != STUN_MSG_INTEGRITY_LEN) + return PJLIB_UTIL_ESTUNINATTRLEN; + + /* Done */ + *p_attr = attr; + return PJ_SUCCESS; +} + + +static pj_status_t encode_msg_integrity_attr(const void *a, pj_uint8_t *buf, + unsigned len, unsigned *printed) +{ + enum + { + ATTR_LEN = STUN_MSG_INTEGRITY_LEN + ATTR_HDR_LEN + }; + pj_stun_msg_integrity_attr *attr; + + if (len < ATTR_LEN) + return PJ_ETOOSMALL; + + /* Copy and convert attribute to network byte order */ + pj_memcpy(buf, a, ATTR_LEN); + attr = (pj_stun_msg_integrity_attr*) buf; + attr->hdr.type = pj_htons(attr->hdr.type); + pj_assert(attr->hdr.length == STUN_MSG_INTEGRITY_LEN); + attr->hdr.length = pj_htons(STUN_MSG_INTEGRITY_LEN); + + /* Done */ + *printed = ATTR_LEN; + + return PJ_SUCCESS; +} + +////////////////////////////////////////////////////////////////////////////// +/* + * STUN ERROR-CODE + */ + +/* + * Create a STUN ERROR-CODE attribute. + */ +PJ_DEF(pj_status_t) +pj_stun_error_code_attr_create(pj_pool_t *pool, + int err_code, + const pj_str_t *err_reason, + pj_stun_error_code_attr **p_attr) +{ + pj_stun_error_code_attr *attr; + char err_buf[80]; + pj_str_t str; + + PJ_ASSERT_RETURN(pool && err_code && p_attr, PJ_EINVAL); + + if (err_reason == NULL) { + str = pj_stun_get_err_reason(err_code); + if (str.slen == 0) { + str.slen = pj_ansi_snprintf(err_buf, sizeof(err_buf), + "Unknown error %d", err_code); + str.ptr = err_buf; + } + err_reason = &str; + } + + attr = PJ_POOL_ZALLOC_TYPE(pool, pj_stun_error_code_attr); + INIT_ATTR(attr, PJ_STUN_ATTR_ERROR_CODE, 4+err_reason->slen); + attr->err_class = (pj_uint8_t)(err_code / 100); + attr->number = (pj_uint8_t) (err_code % 100); + pj_strdup(pool, &attr->reason, err_reason); + + *p_attr = attr; + + return PJ_SUCCESS; +} + + +static pj_status_t decode_error_code_attr(pj_pool_t *pool, + const pj_uint8_t *buf, + void **p_attr) +{ + pj_stun_error_code_attr *attr; + pj_str_t value; + + /* Create the attribute */ + attr = PJ_POOL_ZALLOC_TYPE(pool, pj_stun_error_code_attr); + + /* Copy the header */ + pj_memcpy(attr, buf, ATTR_HDR_LEN + 4); + + /* Convert to host byte order */ + attr->hdr.type = pj_ntohs(attr->hdr.type); + attr->hdr.length = pj_ntohs(attr->hdr.length); + + /* Get pointer to the string in the message */ + value.ptr = ((char*)buf + ATTR_HDR_LEN + 4); + value.slen = attr->hdr.length - 4; + + /* Copy the string to the attribute */ + pj_strdup(pool, &attr->reason, &value); + + /* Done */ + *p_attr = attr; + + return PJ_SUCCESS; +} + + +static pj_status_t encode_error_code_attr(const void *a, pj_uint8_t *buf, + unsigned len, unsigned *printed) +{ + const pj_stun_error_code_attr *ca = + (const pj_stun_error_code_attr*)a; + pj_stun_error_code_attr *attr; + + if (len < ATTR_HDR_LEN + 4 + (unsigned)ca->reason.slen) + return PJ_ETOOSMALL; + + /* Copy and convert attribute to network byte order */ + pj_memcpy(buf, ca, ATTR_HDR_LEN + 4); + + /* Update length */ + attr = (pj_stun_error_code_attr*) buf; + attr->hdr.length = (pj_uint16_t)(4 + ca->reason.slen); + + /* Convert fiends to network byte order */ + attr->hdr.type = pj_htons(attr->hdr.type); + attr->hdr.length = pj_htons(attr->hdr.length); + + /* Copy error string */ + pj_memcpy(buf + ATTR_HDR_LEN + 4, ca->reason.ptr, ca->reason.slen); + + /* Done */ + *printed = (ATTR_HDR_LEN + 4 + ca->reason.slen + 3) & (~3); + + return PJ_SUCCESS; +} + +////////////////////////////////////////////////////////////////////////////// +/* + * STUN UNKNOWN-ATTRIBUTES attribute + */ + +/* + * Create an empty instance of STUN UNKNOWN-ATTRIBUTES attribute. + * + * @param pool The pool to allocate memory from. + * @param p_attr Pointer to receive the attribute. + * + * @return PJ_SUCCESS on success or the appropriate error code. + */ +PJ_DEF(pj_status_t) +pj_stun_unknown_attr_create(pj_pool_t *pool, + unsigned attr_cnt, + pj_uint16_t attr_array[], + pj_stun_unknown_attr **p_attr) +{ + pj_stun_unknown_attr *attr; + unsigned i; + + PJ_ASSERT_RETURN(pool && attr_cnt < PJ_STUN_MAX_ATTR && p_attr, PJ_EINVAL); + + attr = PJ_POOL_ZALLOC_TYPE(pool, pj_stun_unknown_attr); + INIT_ATTR(attr, PJ_STUN_ATTR_UNKNOWN_ATTRIBUTES, attr_cnt * 2); + + attr->attr_count = attr_cnt; + for (i=0; i<attr_cnt; ++i) { + attr->attrs[i] = attr_array[i]; + } + + /* If the number of unknown attributes is an odd number, one of the + * attributes MUST be repeated in the list. + */ + if ((attr_cnt & 0x01)) { + attr->attrs[attr_cnt] = attr_array[attr_cnt-1]; + } + + *p_attr = NULL; + + return PJ_SUCCESS; +} + + +static pj_status_t decode_unknown_attr(pj_pool_t *pool, + const pj_uint8_t *buf, + void **p_attr) +{ + pj_stun_unknown_attr *attr; + const pj_uint16_t *punk_attr; + unsigned i; + + attr = PJ_POOL_ZALLOC_TYPE(pool, pj_stun_unknown_attr); + pj_memcpy(attr, buf, ATTR_HDR_LEN); + + attr->hdr.type = pj_ntohs(attr->hdr.type); + attr->hdr.length = pj_ntohs(attr->hdr.length); + + attr->attr_count = (attr->hdr.length >> 1); + if (attr->attr_count > PJ_STUN_MAX_ATTR) + return PJ_ETOOMANY; + + punk_attr = (const pj_uint16_t*)(buf + ATTR_HDR_LEN); + for (i=0; i<attr->attr_count; ++i) { + attr->attrs[i] = pj_ntohs(punk_attr[i]); + } + + /* Done */ + *p_attr = attr; + + return PJ_SUCCESS; +} + + +static pj_status_t encode_unknown_attr(const void *a, pj_uint8_t *buf, + unsigned len, unsigned *printed) +{ + const pj_stun_unknown_attr *ca = (const pj_stun_unknown_attr*) a; + pj_stun_unknown_attr *attr; + pj_uint16_t *dst_unk_attr; + unsigned i; + + /* Check that buffer is enough */ + if (len < ATTR_HDR_LEN + (ca->attr_count << 1)) + return PJ_ETOOSMALL; + + /* Copy to message */ + pj_memcpy(buf, ca, ATTR_HDR_LEN); + + /* Set the correct length */ + attr = (pj_stun_unknown_attr *) buf; + attr->hdr.length = (pj_uint16_t)(ca->attr_count << 1); + + /* Convert to network byte order */ + attr->hdr.type = pj_htons(attr->hdr.type); + attr->hdr.length = pj_htons(attr->hdr.length); + + /* Copy individual attribute */ + dst_unk_attr = (pj_uint16_t*)(buf + ATTR_HDR_LEN); + for (i=0; i < ca->attr_count; ++i, ++dst_unk_attr) { + *dst_unk_attr = pj_htons(attr->attrs[i]); + } + + /* Done */ + *printed = (ATTR_HDR_LEN + (ca->attr_count << 1) + 3) & (~3); + + return PJ_SUCCESS; +} + + +////////////////////////////////////////////////////////////////////////////// +/* + * STUN generic binary attribute + */ + +/* + * Create a blank binary attribute. + */ +PJ_DEF(pj_status_t) +pj_stun_binary_attr_create(pj_pool_t *pool, + int attr_type, + pj_stun_binary_attr **p_attr) +{ + pj_stun_binary_attr *attr; + + PJ_ASSERT_RETURN(pool && attr_type && p_attr, PJ_EINVAL); + + attr = PJ_POOL_ZALLOC_TYPE(pool, pj_stun_binary_attr); + INIT_ATTR(attr, attr_type, sizeof(pj_stun_binary_attr)); + + *p_attr = attr; + + return PJ_SUCCESS; +} + + +static pj_status_t decode_binary_attr(pj_pool_t *pool, + const pj_uint8_t *buf, + void **p_attr) +{ + pj_stun_binary_attr *attr; + + /* Create the attribute */ + attr = PJ_POOL_ZALLOC_TYPE(pool, pj_stun_binary_attr); + + /* Copy the header */ + pj_memcpy(attr, buf, ATTR_HDR_LEN); + + /* Convert to host byte order */ + attr->hdr.type = pj_ntohs(attr->hdr.type); + attr->hdr.length = pj_ntohs(attr->hdr.length); + + /* Copy the data to the attribute */ + attr->length = attr->hdr.length; + attr->data = pj_pool_alloc(pool, attr->length); + pj_memcpy(attr->data, buf+ATTR_HDR_LEN, attr->length); + + /* Done */ + *p_attr = attr; + + return PJ_SUCCESS; + +} + + +static pj_status_t encode_binary_attr(const void *a, pj_uint8_t *buf, + unsigned len, unsigned *printed) +{ + const pj_stun_binary_attr *ca = (const pj_stun_binary_attr*)a; + pj_stun_attr_hdr *attr; + + /* Calculated total attr_len (add padding if necessary) */ + *printed = (ca->length + ATTR_HDR_LEN + 3) & (~3); + if (len < *printed) + return PJ_ETOOSMALL; + + /* Copy header */ + pj_memcpy(buf, a, ATTR_HDR_LEN); + + /* Set the correct length */ + attr = (pj_stun_attr_hdr*)buf; + attr->length = (pj_uint16_t) ca->length; + + /* Convert to network byte order */ + attr->type = pj_htons(attr->type); + attr->length = pj_htons(attr->length); + + /* Copy the data */ + pj_memcpy(buf+ATTR_HDR_LEN, ca->data, ca->length); + + /* Done */ + return PJ_SUCCESS; +} + + +////////////////////////////////////////////////////////////////////////////// + +/* + * Create a blank STUN message. + */ +PJ_DEF(pj_status_t) pj_stun_msg_create( pj_pool_t *pool, + unsigned msg_type, + pj_uint32_t magic, + const pj_uint8_t tsx_id[12], + pj_stun_msg **p_msg) +{ + pj_stun_msg *msg; + + PJ_ASSERT_RETURN(pool && msg_type && p_msg, PJ_EINVAL); + + msg = PJ_POOL_ZALLOC_TYPE(pool, pj_stun_msg); + msg->hdr.type = (pj_uint16_t) msg_type; + msg->hdr.magic = magic; + + if (tsx_id) { + pj_memcpy(&msg->hdr.tsx_id, tsx_id, sizeof(msg->hdr.tsx_id)); + } else { + struct transaction_id + { + pj_uint32_t proc_id; + pj_uint32_t random; + pj_uint32_t counter; + } id; + static pj_uint32_t pj_stun_tsx_id_counter; + + id.proc_id = pj_getpid(); + id.random = pj_rand(); + id.counter = pj_stun_tsx_id_counter++; + + pj_memcpy(&msg->hdr.tsx_id, &id, sizeof(msg->hdr.tsx_id)); + } + + *p_msg = msg; + + return PJ_SUCCESS; +} + + +/* + * Add STUN attribute to STUN message. + */ +PJ_DEF(pj_status_t) pj_stun_msg_add_attr(pj_stun_msg *msg, + pj_stun_attr_hdr *attr) +{ + PJ_ASSERT_RETURN(msg && attr, PJ_EINVAL); + PJ_ASSERT_RETURN(msg->attr_count < PJ_STUN_MAX_ATTR, PJ_ETOOMANY); + + msg->attr[msg->attr_count++] = attr; + return PJ_SUCCESS; +} + + +/* + * Check that the PDU is potentially a valid STUN message. + */ +PJ_DEF(pj_status_t) pj_stun_msg_check(const void *pdu, unsigned pdu_len, + unsigned options) +{ + pj_stun_msg_hdr *hdr; + + PJ_ASSERT_RETURN(pdu && pdu_len > sizeof(pj_stun_msg_hdr), + PJLIB_UTIL_ESTUNINMSGLEN); + + PJ_UNUSED_ARG(options); + + hdr = (pj_stun_msg_hdr*) pdu; + + /* First byte of STUN message is always 0x00 or 0x01. */ + if ((*(const char*)pdu) != 0x00 && (*(const char*)pdu) != 0x01) + return PJLIB_UTIL_ESTUNINMSGTYPE; + + /* If magic is set, then there is great possibility that this is + * a STUN message. + */ + if (pj_ntohl(hdr->magic) == PJ_STUN_MAGIC) + return PJ_SUCCESS; + + /* Check the PDU length */ + if (pj_ntohs(hdr->length) > pdu_len) + return PJLIB_UTIL_ESTUNINMSGLEN; + + /* Could be a STUN message */ + return PJ_SUCCESS; +} + + +/* + * Parse incoming packet into STUN message. + */ +PJ_DEF(pj_status_t) pj_stun_msg_decode(pj_pool_t *pool, + const pj_uint8_t *pdu, + unsigned pdu_len, + unsigned options, + pj_stun_msg **p_msg, + unsigned *p_parsed_len, + unsigned *p_err_code, + unsigned *p_uattr_cnt, + pj_uint16_t uattr[]) +{ + + pj_stun_msg *msg; + unsigned uattr_cnt; + const pj_uint8_t *start_pdu = pdu; + pj_status_t status; + + PJ_UNUSED_ARG(options); + + PJ_ASSERT_RETURN(pool && pdu && pdu_len && p_msg, PJ_EINVAL); + PJ_ASSERT_RETURN(sizeof(pj_stun_msg_hdr) == 20, PJ_EBUG); + + /* Application should have checked that this is a valid STUN msg */ + PJ_ASSERT_RETURN((status=pj_stun_msg_check(pdu, pdu_len, options)) + == PJ_SUCCESS, status); + + /* Create the message, copy the header, and convert to host byte order */ + msg = PJ_POOL_ZALLOC_TYPE(pool, pj_stun_msg); + pj_memcpy(&msg->hdr, pdu, sizeof(pj_stun_msg_hdr)); + msg->hdr.type = pj_ntohs(msg->hdr.type); + msg->hdr.length = pj_ntohs(msg->hdr.length); + msg->hdr.magic = pj_ntohl(msg->hdr.magic); + + pdu += sizeof(pj_stun_msg_hdr); + pdu_len -= sizeof(pj_stun_msg_hdr); + + if (p_err_code) + *p_err_code = 0; + + /* Parse attributes */ + uattr_cnt = 0; + while (pdu_len > 0) { + unsigned attr_type, attr_val_len; + const struct attr_desc *adesc; + + /* Get attribute type and length. If length is not aligned + * to 4 bytes boundary, add padding. + */ + attr_type = pj_ntohs(*(pj_uint16_t*)pdu); + attr_val_len = pj_ntohs(*(pj_uint16_t*)(pdu+2)); + attr_val_len = (attr_val_len + 3) & (~3); + + /* Check length */ + if (pdu_len < attr_val_len) + return PJLIB_UTIL_ESTUNINATTRLEN; + + /* Get the attribute descriptor */ + adesc = find_attr_desc(attr_type); + + if (adesc == NULL) { + /* Unrecognized attribute */ + + PJ_LOG(4,(THIS_FILE, "Unrecognized attribute type %d", + attr_type)); + + /* Put to unrecognized attribute array */ + if (p_uattr_cnt && uattr && uattr_cnt < *p_uattr_cnt) { + uattr[uattr_cnt++] = (pj_uint16_t)attr_type; + } + + /* Is this a fatal condition? */ + if (attr_type <= 0x7FFF) { + /* This is a mandatory attribute, we must return error + * if we don't understand the attribute. + */ + if (p_err_code && *p_err_code == 0) + *p_err_code = PJ_STUN_STATUS_UNKNOWN_ATTRIBUTE; + + return PJLIB_UTIL_ESTUNUNKNOWNATTR; + } + + } else { + void *attr; + + /* Parse the attribute */ + status = (adesc->decode_attr)(pool, pdu, &attr); + + if (status != PJ_SUCCESS) { + PJ_LOG(4,(THIS_FILE, + "Error parsing STUN attribute type %d: status=%d", + attr_type, status)); + return status; + } + + /* Make sure we have rooms for the new attribute */ + if (msg->attr_count >= PJ_STUN_MAX_ATTR) + return PJLIB_UTIL_ESTUNTOOMANYATTR; + + /* Add the attribute */ + msg->attr[msg->attr_count++] = (pj_stun_attr_hdr*)attr; + } + + pdu += (attr_val_len + 4); + pdu_len -= (attr_val_len + 4); + } + + *p_msg = msg; + + if (p_uattr_cnt) + *p_uattr_cnt = uattr_cnt; + + if (p_parsed_len) + *p_parsed_len = (pdu - start_pdu); + + return PJ_SUCCESS; +} + + +/* + * Print the message structure to a buffer. + */ +PJ_DEF(pj_status_t) pj_stun_msg_encode(const pj_stun_msg *msg, + pj_uint8_t *buf, unsigned buf_size, + unsigned options, + unsigned *p_msg_len) +{ + pj_stun_msg_hdr *hdr; + pj_uint8_t *start = buf; + unsigned i; + + PJ_ASSERT_RETURN(msg && buf && buf_size, PJ_EINVAL); + + PJ_UNUSED_ARG(options); + + /* Copy the message header part and convert the header fields to + * network byte order + */ + if (buf_size < sizeof(pj_stun_msg_hdr)) + return PJ_ETOOSMALL; + pj_memcpy(buf, &msg->hdr, sizeof(pj_stun_msg_hdr)); + hdr = (pj_stun_msg_hdr*) buf; + hdr->magic = pj_htonl(hdr->magic); + hdr->type = pj_htons(hdr->type); + /* We'll fill in the length later */ + + buf += sizeof(pj_stun_msg_hdr); + buf_size -= sizeof(pj_stun_msg_hdr); + + /* Print each attribute */ + for (i=0; i<msg->attr_count; ++i) { + const struct attr_desc *adesc; + const pj_stun_attr_hdr *attr_hdr; + unsigned printed; + pj_status_t status; + + attr_hdr = msg->attr[i]; + + adesc = find_attr_desc(attr_hdr->type); + PJ_ASSERT_RETURN(adesc != NULL, PJ_EBUG); + + status = adesc->encode_attr(attr_hdr, buf, buf_size, &printed); + if (status != PJ_SUCCESS) + return status; + + buf += printed; + buf_size -= printed; + } + + /* Update the message length in the header. + * Note that length is not including the 20 bytes header. + */ + hdr->length = (pj_uint16_t)((buf - start) - 20); + hdr->length = pj_htons(hdr->length); + + /* Done */ + if (p_msg_len) + *p_msg_len = (buf - start); + + return PJ_SUCCESS; +} + + +/* + * Find STUN attribute in the STUN message, starting from the specified + * index. + */ +PJ_DEF(pj_stun_attr_hdr*) pj_stun_msg_find_attr( const pj_stun_msg *msg, + int attr_type, + unsigned index) +{ + PJ_ASSERT_RETURN(msg, NULL); + + for (; index < msg->attr_count; ++index) { + if (msg->attr[index]->type == attr_type) + return (pj_stun_attr_hdr*) &msg->attr[index]; + } + + return NULL; +} + diff --git a/pjlib-util/src/pjlib-util/stun_server.c b/pjlib-util/src/pjlib-util/stun_server.c new file mode 100644 index 00000000..d4d7fa8f --- /dev/null +++ b/pjlib-util/src/pjlib-util/stun_server.c @@ -0,0 +1,129 @@ +/* $Id$ */ +/* + * Copyright (C) 2003-2005 Benny Prijono <benny@prijono.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#include <pjlib-util/stun_server.h> +#include <pjlib-util/errno.h> +#include <pj/assert.h> +#include <pj/pool.h> +#include <pj/string.h> + + +struct pj_stun_service +{ + pj_str_t name; + unsigned options; + void *user_data; + unsigned cb_cnt; + pj_stun_service_handler *cb; +}; + + +/* + * Create STUN service. + */ +PJ_DEF(pj_status_t) pj_stun_service_create( pj_pool_t *pool, + const char *name, + unsigned options, + unsigned handler_cnt, + pj_stun_service_handler cb[], + void *user_data, + pj_stun_service **p_svc) +{ + pj_stun_service *svc; + + PJ_ASSERT_RETURN(pool && handler_cnt && cb && p_svc, PJ_EINVAL); + + svc = PJ_POOL_ZALLOC_TYPE(pool, pj_stun_service); + svc->options = options; + svc->user_data = user_data; + + if (!name) name = "pj_stun_service"; + + pj_strdup2_with_null(pool, &svc->name, name); + + svc->cb_cnt = handler_cnt; + svc->cb = pj_pool_calloc(pool, handler_cnt, + sizeof(pj_stun_service_handler)); + pj_memcpy(svc->cb, cb, handler_cnt * sizeof(pj_stun_service_handler)); + + *p_svc = svc; + + return PJ_SUCCESS; +} + + +/* + * Destroy STUN service + */ +PJ_DEF(pj_status_t) pj_stun_service_destroy(pj_stun_service *svc) +{ + PJ_ASSERT_RETURN(svc, PJ_EINVAL); + return PJ_SUCCESS; +} + + +/* + * Get user data associated with the STUN service. + */ +PJ_DEF(void*) pj_stun_service_get_user_data(pj_stun_service *svc) +{ + PJ_ASSERT_RETURN(svc, NULL); + return svc->user_data; +} + + +/* + * Find handler. + */ +static pj_stun_service_handler *find_handler(pj_stun_service *svc, + int msg_type) +{ + unsigned i; + + for (i=0; i<svc->cb_cnt; ++i) { + if (svc->cb[i].msg_type == msg_type) + return &svc->cb[i]; + } + + return NULL; +} + + +/* + * Instruct the STUN service to handle incoming STUN message. + */ +PJ_DEF(pj_status_t) pj_stun_service_handle_msg( pj_stun_service *svc, + void *handle_data, + const pj_stun_msg *msg) +{ + pj_stun_service_handler *handler; + + PJ_ASSERT_RETURN(svc && msg, PJ_EINVAL); + + handler = find_handler(svc, msg->hdr.type); + if (handler == NULL) + return PJLIB_UTIL_ESTUNNOHANDLER; + + return (*handler->handle_msg)(svc, handle_data, msg); +} + + +////////////////////////////////////////////////////////////////////////////// + + + diff --git a/pjlib-util/src/pjlib-util/stun.c b/pjlib-util/src/pjlib-util/stun_simple.c index c06479ac..6ab6dbbe 100644 --- a/pjlib-util/src/pjlib-util/stun.c +++ b/pjlib-util/src/pjlib-util/stun_simple.c @@ -1,6 +1,6 @@ /* $Id$ */ /* - * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org> + * Copyright (C) 2003-2006 Benny Prijono <benny@prijono.org> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -16,7 +16,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include <pjlib-util/stun.h> +#include <pjlib-util/stun_simple.h> #include <pjlib-util/errno.h> #include <pj/pool.h> #include <pj/log.h> @@ -25,47 +25,47 @@ #define THIS_FILE "stun.c" -PJ_DEF(pj_status_t) pj_stun_create_bind_req( pj_pool_t *pool, +PJ_DEF(pj_status_t) pjstun_create_bind_req( pj_pool_t *pool, void **msg, pj_size_t *len, pj_uint32_t id_hi, pj_uint32_t id_lo) { - pj_stun_msg_hdr *hdr; + pjstun_msg_hdr *hdr; PJ_CHECK_STACK(); - hdr = pj_pool_calloc(pool, 1, sizeof(pj_stun_msg_hdr)); + hdr = pj_pool_calloc(pool, 1, sizeof(pjstun_msg_hdr)); if (!hdr) return PJ_ENOMEM; - hdr->type = pj_htons(PJ_STUN_BINDING_REQUEST); + hdr->type = pj_htons(PJSTUN_BINDING_REQUEST); hdr->tsx[2] = pj_htonl(id_hi); hdr->tsx[3] = pj_htonl(id_lo); *msg = hdr; - *len = sizeof(pj_stun_msg_hdr); + *len = sizeof(pjstun_msg_hdr); return PJ_SUCCESS; } -PJ_DEF(pj_status_t) pj_stun_parse_msg( void *buf, pj_size_t len, - pj_stun_msg *msg) +PJ_DEF(pj_status_t) pjstun_parse_msg( void *buf, pj_size_t len, + pjstun_msg *msg) { pj_uint16_t msg_type, msg_len; char *p_attr; PJ_CHECK_STACK(); - msg->hdr = (pj_stun_msg_hdr*)buf; + msg->hdr = (pjstun_msg_hdr*)buf; msg_type = pj_ntohs(msg->hdr->type); switch (msg_type) { - case PJ_STUN_BINDING_REQUEST: - case PJ_STUN_BINDING_RESPONSE: - case PJ_STUN_BINDING_ERROR_RESPONSE: - case PJ_STUN_SHARED_SECRET_REQUEST: - case PJ_STUN_SHARED_SECRET_RESPONSE: - case PJ_STUN_SHARED_SECRET_ERROR_RESPONSE: + case PJSTUN_BINDING_REQUEST: + case PJSTUN_BINDING_RESPONSE: + case PJSTUN_BINDING_ERROR_RESPONSE: + case PJSTUN_SHARED_SECRET_REQUEST: + case PJSTUN_SHARED_SECRET_RESPONSE: + case PJSTUN_SHARED_SECRET_ERROR_RESPONSE: break; default: PJ_LOG(4,(THIS_FILE, "Error: unknown msg type %d", msg_type)); @@ -73,21 +73,21 @@ PJ_DEF(pj_status_t) pj_stun_parse_msg( void *buf, pj_size_t len, } msg_len = pj_ntohs(msg->hdr->length); - if (msg_len != len - sizeof(pj_stun_msg_hdr)) { + if (msg_len != len - sizeof(pjstun_msg_hdr)) { PJ_LOG(4,(THIS_FILE, "Error: invalid msg_len %d (expecting %d)", - msg_len, len - sizeof(pj_stun_msg_hdr))); + msg_len, len - sizeof(pjstun_msg_hdr))); return PJLIB_UTIL_ESTUNINMSGLEN; } msg->attr_count = 0; - p_attr = (char*)buf + sizeof(pj_stun_msg_hdr); + p_attr = (char*)buf + sizeof(pjstun_msg_hdr); while (msg_len > 0) { - pj_stun_attr_hdr **attr = &msg->attr[msg->attr_count]; + pjstun_attr_hdr **attr = &msg->attr[msg->attr_count]; pj_uint32_t len; - *attr = (pj_stun_attr_hdr*)p_attr; - len = pj_ntohs((pj_uint16_t) ((*attr)->length)) + sizeof(pj_stun_attr_hdr); + *attr = (pjstun_attr_hdr*)p_attr; + len = pj_ntohs((pj_uint16_t) ((*attr)->length)) + sizeof(pjstun_attr_hdr); if (msg_len < len) { PJ_LOG(4,(THIS_FILE, "Error: length mismatch in attr %d", @@ -95,7 +95,7 @@ PJ_DEF(pj_status_t) pj_stun_parse_msg( void *buf, pj_size_t len, return PJLIB_UTIL_ESTUNINATTRLEN; } - if (pj_ntohs((*attr)->type) > PJ_STUN_ATTR_REFLECTED_FORM) { + if (pj_ntohs((*attr)->type) > PJSTUN_ATTR_REFLECTED_FORM) { PJ_LOG(5,(THIS_FILE, "Warning: unknown attr type %x in attr %d. " "Attribute was ignored.", pj_ntohs((*attr)->type), msg->attr_count)); @@ -109,14 +109,14 @@ PJ_DEF(pj_status_t) pj_stun_parse_msg( void *buf, pj_size_t len, return PJ_SUCCESS; } -PJ_DEF(void*) pj_stun_msg_find_attr( pj_stun_msg *msg, pj_stun_attr_type t) +PJ_DEF(void*) pjstun_msg_find_attr( pjstun_msg *msg, pjstun_attr_type t) { int i; PJ_CHECK_STACK(); for (i=0; i<msg->attr_count; ++i) { - pj_stun_attr_hdr *attr = msg->attr[i]; + pjstun_attr_hdr *attr = msg->attr[i]; if (pj_ntohs(attr->type) == t) return attr; } diff --git a/pjlib-util/src/pjlib-util/stun_client.c b/pjlib-util/src/pjlib-util/stun_simple_client.c index 6005694c..7d6e472b 100644 --- a/pjlib-util/src/pjlib-util/stun_client.c +++ b/pjlib-util/src/pjlib-util/stun_simple_client.c @@ -1,6 +1,6 @@ /* $Id$ */ /* - * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org> + * Copyright (C) 2003-2006 Benny Prijono <benny@prijono.org> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -16,7 +16,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include <pjlib-util/stun.h> +#include <pjlib-util/stun_simple.h> #include <pjlib-util/errno.h> #include <pj/os.h> #include <pj/pool.h> @@ -32,7 +32,7 @@ static int stun_timer[] = {1600, 1600, 1600 }; #define LOG_ADDR(addr) pj_inet_ntoa(addr.sin_addr), pj_ntohs(addr.sin_port) -PJ_DECL(pj_status_t) pj_stun_get_mapped_addr( pj_pool_factory *pf, +PJ_DECL(pj_status_t) pjstun_get_mapped_addr( pj_pool_factory *pf, int sock_cnt, pj_sock_t sock[], const pj_str_t *srv1, int port1, const pj_str_t *srv2, int port2, @@ -69,7 +69,7 @@ PJ_DECL(pj_status_t) pj_stun_get_mapped_addr( pj_pool_factory *pf, /* Create the outgoing BIND REQUEST message template */ - status = pj_stun_create_bind_req( pool, &out_msg, &out_msg_len, + status = pjstun_create_bind_req( pool, &out_msg, &out_msg_len, pj_rand(), pj_rand()); if (status != PJ_SUCCESS) goto on_error; @@ -97,7 +97,7 @@ PJ_DECL(pj_status_t) pj_stun_get_mapped_addr( pj_pool_factory *pf, /* Send messages to servers that has not given us response. */ for (i=0; i<sock_cnt && status==PJ_SUCCESS; ++i) { for (j=0; j<2 && status==PJ_SUCCESS; ++j) { - pj_stun_msg_hdr *msg_hdr = out_msg; + pjstun_msg_hdr *msg_hdr = out_msg; pj_ssize_t sent_len; if (rec[i].srv[j].mapped_port != 0) @@ -150,10 +150,10 @@ PJ_DECL(pj_status_t) pj_stun_get_mapped_addr( pj_pool_factory *pf, for (i=0; i<sock_cnt; ++i) { int sock_idx, srv_idx; pj_ssize_t len; - pj_stun_msg msg; + pjstun_msg msg; pj_sockaddr_in addr; int addrlen = sizeof(addr); - pj_stun_mapped_addr_attr *attr; + pjstun_mapped_addr_attr *attr; char recv_buf[128]; if (!PJ_FD_ISSET(sock[i], &r)) @@ -170,7 +170,7 @@ PJ_DECL(pj_status_t) pj_stun_get_mapped_addr( pj_pool_factory *pf, if (status != PJ_SUCCESS) continue; - status = pj_stun_parse_msg(recv_buf, len, &msg); + status = pjstun_parse_msg(recv_buf, len, &msg); if (status != PJ_SUCCESS) { continue; } @@ -184,17 +184,17 @@ PJ_DECL(pj_status_t) pj_stun_get_mapped_addr( pj_pool_factory *pf, continue; } - if (pj_ntohs(msg.hdr->type) != PJ_STUN_BINDING_RESPONSE) { + if (pj_ntohs(msg.hdr->type) != PJSTUN_BINDING_RESPONSE) { status = PJLIB_UTIL_ESTUNNOBINDRES; continue; } - if (pj_stun_msg_find_attr(&msg, PJ_STUN_ATTR_ERROR_CODE) != NULL) { + if (pjstun_msg_find_attr(&msg, PJSTUN_ATTR_ERROR_CODE) != NULL) { status = PJLIB_UTIL_ESTUNRECVERRATTR; continue; } - attr = (void*)pj_stun_msg_find_attr(&msg, PJ_STUN_ATTR_MAPPED_ADDR); + attr = (void*)pjstun_msg_find_attr(&msg, PJSTUN_ATTR_MAPPED_ADDR); if (!attr) { status = PJLIB_UTIL_ESTUNNOMAP; continue; diff --git a/pjlib-util/src/pjlib-util/stun_transaction.c b/pjlib-util/src/pjlib-util/stun_transaction.c new file mode 100644 index 00000000..6720b8d7 --- /dev/null +++ b/pjlib-util/src/pjlib-util/stun_transaction.c @@ -0,0 +1,333 @@ +/* $Id$ */ +/* + * Copyright (C) 2003-2005 Benny Prijono <benny@prijono.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#include <pjlib-util/stun_transaction.h> +#include <pjlib-util/errno.h> +#include <pj/assert.h> +#include <pj/log.h> +#include <pj/pool.h> +#include <pj/string.h> +#include <pj/timer.h> + + +#define TIMER_ACTIVE 1 + + +struct pj_stun_client_tsx +{ + char obj_name[PJ_MAX_OBJ_NAME]; + pj_pool_t *pool; + pj_stun_endpoint *endpt; + pj_stun_tsx_cb cb; + void *user_data; + + pj_uint32_t tsx_id[4]; + + pj_bool_t require_retransmit; + pj_timer_entry timer; + unsigned transmit_count; + pj_time_val retransmit_time; + + pj_uint8_t last_pkt[PJ_STUN_MAX_PKT_LEN]; + unsigned last_pkt_size; +}; + + +static void retransmit_timer_callback(pj_timer_heap_t *timer_heap, + pj_timer_entry *timer); + +static void stun_perror(pj_stun_client_tsx *tsx, const char *title, + pj_status_t status) +{ + char errmsg[PJ_ERR_MSG_SIZE]; + + pj_strerror(status, errmsg, sizeof(errmsg)); + PJ_LOG(4,(tsx->obj_name, "%s: %s", title, errmsg)); +} + + +/* + * Create a STUN client transaction. + */ +PJ_DEF(pj_status_t) pj_stun_client_tsx_create(pj_stun_endpoint *endpt, + const pj_stun_tsx_cb *cb, + pj_stun_client_tsx **p_tsx) +{ + pj_pool_t *pool; + pj_stun_client_tsx *tsx; + + PJ_ASSERT_RETURN(endpt && cb && p_tsx, PJ_EINVAL); + PJ_ASSERT_RETURN(cb->on_send_msg, PJ_EINVAL); + + pool = pj_pool_create(endpt->pf, "tsx", 1000, 1000, NULL); + tsx = PJ_POOL_ZALLOC_TYPE(pool, pj_stun_client_tsx); + tsx->pool = pool; + tsx->endpt = endpt; + pj_memcpy(&tsx->cb, cb, sizeof(*cb)); + + tsx->timer.cb = &retransmit_timer_callback; + tsx->timer.user_data = tsx; + + pj_ansi_snprintf(tsx->obj_name, sizeof(tsx->obj_name), "stuntsx%p", pool); + + *p_tsx = tsx; + + PJ_LOG(4,(tsx->obj_name, "STUN client transaction created")); + return PJ_SUCCESS; +} + + +/* + * . + */ +PJ_DEF(pj_status_t) pj_stun_client_tsx_destroy(pj_stun_client_tsx *tsx) +{ + PJ_ASSERT_RETURN(tsx, PJ_EINVAL); + + if (tsx->timer.id != 0) { + pj_timer_heap_cancel(tsx->endpt->timer_heap, &tsx->timer); + tsx->timer.id = 0; + } + pj_pool_release(tsx->pool); + return PJ_SUCCESS; +} + + +/* + * Set user data. + */ +PJ_DEF(pj_status_t) pj_stun_client_tsx_set_data(pj_stun_client_tsx *tsx, + void *data) +{ + PJ_ASSERT_RETURN(tsx, PJ_EINVAL); + tsx->user_data = data; + return PJ_SUCCESS; +} + + +/* + * Get the user data + */ +PJ_DEF(void*) pj_stun_client_tsx_get_data(pj_stun_client_tsx *tsx) +{ + PJ_ASSERT_RETURN(tsx, NULL); + return tsx->user_data; +} + + +/* + * Transmit message. + */ +static pj_status_t tsx_transmit_msg(pj_stun_client_tsx *tsx) +{ + pj_status_t status; + + PJ_ASSERT_RETURN(tsx->timer.id == 0, PJ_EBUSY); + + if (tsx->require_retransmit) { + /* Calculate retransmit/timeout delay */ + if (tsx->transmit_count == 0) { + tsx->retransmit_time.sec = 0; + tsx->retransmit_time.msec = tsx->endpt->rto_msec; + + } else if (tsx->transmit_count < PJ_STUN_MAX_RETRANSMIT_COUNT) { + unsigned msec; + + msec = PJ_TIME_VAL_MSEC(tsx->retransmit_time); + msec = (msec >> 1) + 100; + tsx->retransmit_time.sec = msec / 1000; + tsx->retransmit_time.msec = msec % 100; + + } else { + tsx->retransmit_time.sec = PJ_STUN_TIMEOUT_VALUE / 1000; + tsx->retransmit_time.msec = PJ_STUN_TIMEOUT_VALUE % 1000; + } + + /* Schedule timer first because when send_msg() failed we can + * cancel it (as opposed to when schedule_timer() failed we cannot + * cancel transmission). + */ + status = pj_timer_heap_schedule(tsx->endpt->timer_heap, &tsx->timer, + &tsx->retransmit_time); + if (status != PJ_SUCCESS) { + tsx->timer.id = 0; + return status; + } + } + + + /* Send message */ + status = tsx->cb.on_send_msg(tsx, tsx->last_pkt, tsx->last_pkt_size); + if (status != PJ_SUCCESS) { + if (tsx->timer.id != 0) { + pj_timer_heap_cancel(tsx->endpt->timer_heap, &tsx->timer); + tsx->timer.id = 0; + } + stun_perror(tsx, "STUN error sending message", status); + return status; + } + + tsx->transmit_count++; + + PJ_LOG(4,(tsx->obj_name, "STUN sending message (transmit count=%d)", + tsx->transmit_count)); + return status; +} + + +/* + * Send outgoing message and start STUN transaction. + */ +PJ_DEF(pj_status_t) pj_stun_client_tsx_send_msg(pj_stun_client_tsx *tsx, + pj_bool_t retransmit, + const pj_stun_msg *msg) +{ + pj_status_t status; + + PJ_ASSERT_RETURN(tsx && msg, PJ_EINVAL); + PJ_ASSERT_RETURN(tsx->timer.id != 0, PJ_EBUSY); + + /* Encode message */ + status = pj_stun_msg_encode(msg, tsx->last_pkt, sizeof(tsx->last_pkt), + 0, &tsx->last_pkt_size); + if (status != PJ_SUCCESS) { + stun_perror(tsx, "STUN msg_encode() failed", status); + return status; + } + + /* Update STUN transaction ID */ + tsx->tsx_id[0] = msg->hdr.magic; + pj_memcpy(&tsx->tsx_id[1], msg->hdr.tsx_id, 12); + + /* Update STUN retransmit flag */ + tsx->require_retransmit = retransmit; + + /* Send the message */ + return tsx_transmit_msg(tsx); +} + + +/* Retransmit timer callback */ +static void retransmit_timer_callback(pj_timer_heap_t *timer_heap, + pj_timer_entry *timer) +{ + pj_stun_client_tsx *tsx = (pj_stun_client_tsx *) timer->user_data; + pj_status_t status; + + PJ_UNUSED_ARG(timer_heap); + + if (tsx->transmit_count >= PJ_STUN_MAX_RETRANSMIT_COUNT) { + /* Retransmission count exceeded. Transaction has failed */ + tsx->timer.id = 0; + PJ_LOG(4,(tsx->obj_name, "STUN timeout waiting for response")); + if (tsx->cb.on_complete) { + tsx->cb.on_complete(tsx, PJLIB_UTIL_ESTUNNOTRESPOND, NULL); + } + return; + } + + tsx->timer.id = 0; + status = tsx_transmit_msg(tsx); + if (status != PJ_SUCCESS) { + tsx->timer.id = 0; + if (tsx->cb.on_complete) { + tsx->cb.on_complete(tsx, status, NULL); + } + } +} + + +/* + * Notify the STUN transaction about the arrival of STUN response. + */ +PJ_DEF(pj_status_t) pj_stun_client_tsx_on_rx_msg(pj_stun_client_tsx *tsx, + const void *packet, + pj_size_t pkt_size, + unsigned *parsed_len) +{ + pj_stun_msg *msg; + pj_stun_error_code_attr *err_attr; + pj_status_t status; + + PJ_ASSERT_RETURN(tsx && packet && pkt_size, PJ_EINVAL); + + /* Try to parse the message */ + status = pj_stun_msg_decode(tsx->pool, (const pj_uint8_t*)packet, + pkt_size, 0, &msg, parsed_len, + NULL, NULL, NULL); + if (status != PJ_SUCCESS) { + stun_perror(tsx, "STUN msg_decode() error", status); + return status; + } + + /* Must be STUN response message */ + if (!PJ_STUN_IS_RESPONSE(msg->hdr.type) && + !PJ_STUN_IS_ERROR_RESPONSE(msg->hdr.type)) + { + PJ_LOG(4,(tsx->obj_name, + "STUN rx_msg() error: not response message")); + return PJLIB_UTIL_ESTUNNOTRESPONSE; + } + + /* Compare response's transaction ID */ + if (msg->hdr.magic != tsx->tsx_id[0] || + pj_memcmp(msg->hdr.tsx_id, &tsx->tsx_id[1], 12) != 0) + { + return PJLIB_UTIL_ESTUNINVALIDID; + } + + /* We have a response with matching transaction ID. + * We can cancel retransmit timer now. + */ + if (tsx->timer.id) { + pj_timer_heap_cancel(tsx->endpt->timer_heap, &tsx->timer); + tsx->timer.id = 0; + } + + /* Find STUN error code attribute */ + err_attr = (pj_stun_error_code_attr*) + pj_stun_msg_find_attr(msg, PJ_STUN_ATTR_ERROR_CODE, 0); + + if (err_attr && err_attr->err_class <= 2) { + /* draft-ietf-behave-rfc3489bis-05.txt Section 8.3.2: + * Any response between 100 and 299 MUST result in the cessation + * of request retransmissions, but otherwise is discarded. + */ + PJ_LOG(4,(tsx->obj_name, + "STUN rx_msg() error: received provisional %d code (%.*s)", + err_attr->err_class * 100 + err_attr->number, + (int)err_attr->reason.slen, + err_attr->reason.ptr)); + return PJ_SUCCESS; + } + + if (err_attr == NULL) { + status = PJ_SUCCESS; + } else { + status = PJ_STATUS_FROM_STUN_CODE(err_attr->err_class * 100 + + err_attr->number); + } + + /* Call callback */ + if (tsx->cb.on_complete) { + tsx->cb.on_complete(tsx, status, msg); + } + + return PJ_SUCCESS; +} + diff --git a/pjlib-util/src/pjstun-srv/server.h b/pjlib-util/src/pjstun-srv/server.h new file mode 100644 index 00000000..9de5adce --- /dev/null +++ b/pjlib-util/src/pjstun-srv/server.h @@ -0,0 +1,61 @@ +/* $Id$ */ +/* + * Copyright (C) 2003-2005 Benny Prijono <benny@prijono.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#ifndef __PJSTUN_SERVER_H__ +#define __PJSTUN_SERVER_H__ + + +#define MAX_SERVICE 16 +#define MAX_PKT_LEN 512 + +struct service +{ + unsigned index; + pj_uint16_t port; + pj_bool_t is_stream; + pj_sock_t sock; + pj_ioqueue_key_t *key; + pj_ioqueue_op_key_t recv_opkey, + send_opkey; + + int src_addr_len; + pj_sockaddr_in src_addr; + pj_ssize_t rx_pkt_len; + pj_uint8_t rx_pkt[MAX_PKT_LEN]; + pj_uint8_t tx_pkt[MAX_PKT_LEN]; +}; + +struct stun_server_tag +{ + pj_caching_pool cp; + pj_pool_t *pool; + pj_ioqueue_t *ioqueue; + unsigned service_cnt; + struct service services[MAX_SERVICE]; + + pj_bool_t thread_quit_flag; + unsigned thread_cnt; + pj_thread_t *threads[16]; + +}; + +extern struct stun_server_tag server; + + +#endif /* __PJSTUN_SERVER_H__ */ + diff --git a/pjlib-util/src/pjstun-srv/server_main.c b/pjlib-util/src/pjstun-srv/server_main.c new file mode 100644 index 00000000..56a1bf85 --- /dev/null +++ b/pjlib-util/src/pjstun-srv/server_main.c @@ -0,0 +1,452 @@ +/* $Id$ */ +/* + * Copyright (C) 2003-2005 Benny Prijono <benny@prijono.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#include <pjlib-util.h> +#include <pjlib.h> +#include "server.h" + +#include <stdio.h> +#include <conio.h> + + +#define THIS_FILE "server_main.c" +#define MAX_THREADS 8 + +struct stun_server_tag server; + + +pj_status_t server_perror(const char *sender, const char *title, + pj_status_t status) +{ + char errmsg[PJ_ERR_MSG_SIZE]; + pj_strerror(status, errmsg, sizeof(errmsg)); + + PJ_LOG(3,(sender, "%s: %s", title, errmsg)); + + return status; +} + + +static pj_status_t create_response(pj_pool_t *pool, + const pj_stun_msg *req_msg, + unsigned err_code, + unsigned uattr_cnt, + pj_uint16_t uattr_types[], + pj_stun_msg **p_response) +{ + pj_uint32_t msg_type = req_msg->hdr.type; + pj_stun_msg *response; + pj_stun_error_code_attr *err_attr; + pj_status_t status; + + /* Create response or error response */ + if (err_code) + msg_type |= PJ_STUN_ERROR_RESPONSE_BIT; + else + msg_type |= PJ_STUN_RESPONSE_BIT; + + status = pj_stun_msg_create(pool, msg_type, req_msg->hdr.magic, + req_msg->hdr.tsx_id, &response); + if (status != PJ_SUCCESS) { + return status; + } + + /* Add error code attribute */ + if (err_code) { + status = pj_stun_error_code_attr_create(pool, err_code, NULL, + &err_attr); + if (status != PJ_SUCCESS) { + return status; + } + + pj_stun_msg_add_attr(response, &err_attr->hdr); + } + + /* Add unknown_attribute attributes if err_code is 420 */ + if (err_code == PJ_STUN_STATUS_UNKNOWN_ATTRIBUTE) { + pj_stun_unknown_attr *uattr; + + status = pj_stun_unknown_attr_create(pool, uattr_cnt, uattr_types, + &uattr); + if (status != PJ_SUCCESS) + return status; + + pj_stun_msg_add_attr(response, &uattr->hdr); + } + + *p_response = response; + return PJ_SUCCESS; +} + + +static pj_status_t send_msg(struct service *svc, const pj_stun_msg *msg) +{ + unsigned tx_pkt_len; + pj_ssize_t length; + pj_status_t status; + + /* Encode packet */ + tx_pkt_len = sizeof(svc->tx_pkt); + status = pj_stun_msg_encode(msg, svc->tx_pkt, tx_pkt_len, 0, + &tx_pkt_len); + if (status != PJ_SUCCESS) + return status; + + length = tx_pkt_len; + + /* Send packet */ + if (svc->is_stream) { + status = pj_ioqueue_send(svc->key, &svc->send_opkey, svc->tx_pkt, + &length, 0); + } else { + status = pj_ioqueue_sendto(svc->key, &svc->send_opkey, svc->tx_pkt, + &length, 0, &svc->src_addr, + svc->src_addr_len); + } + + PJ_LOG(4,(THIS_FILE, "Sending STUN %s %s", + pj_stun_get_method_name(msg->hdr.type), + pj_stun_get_class_name(msg->hdr.type))); + + return (status == PJ_SUCCESS || status == PJ_EPENDING) ? + PJ_SUCCESS : status; +} + + +static pj_status_t err_respond(struct service *svc, + pj_pool_t *pool, + const pj_stun_msg *req_msg, + unsigned err_code, + unsigned uattr_cnt, + pj_uint16_t uattr_types[]) +{ + pj_stun_msg *response; + pj_status_t status; + + /* Create the error response */ + status = create_response(pool, req_msg, err_code, + uattr_cnt, uattr_types, &response); + if (status != PJ_SUCCESS) { + server_perror(THIS_FILE, "Error creating response", status); + return status; + } + + /* Send response */ + status = send_msg(svc, response); + if (status != PJ_SUCCESS) { + server_perror(THIS_FILE, "Error sending response", status); + return status; + } + + return PJ_SUCCESS; +} + + +static void handle_binding_request(struct service *svc, pj_pool_t *pool, + const pj_stun_msg *rx_msg) +{ + pj_stun_msg *response; + pj_stun_generic_ip_addr_attr *m_attr; + pj_status_t status; + + status = create_response(pool, rx_msg, 0, 0, NULL, &response); + if (status != PJ_SUCCESS) { + server_perror(THIS_FILE, "Error creating response", status); + return; + } + + /* Create MAPPED-ADDRESS attribute */ + status = pj_stun_generic_ip_addr_attr_create(pool, + PJ_STUN_ATTR_MAPPED_ADDR, + PJ_FALSE, + svc->src_addr_len, + &svc->src_addr, &m_attr); + if (status != PJ_SUCCESS) { + server_perror(THIS_FILE, "Error creating response", status); + return; + } + pj_stun_msg_add_attr(response, &m_attr->hdr); + + /* On the presence of magic, create XOR-MAPPED-ADDRESS attribute */ + if (rx_msg->hdr.magic == PJ_STUN_MAGIC) { + status = + pj_stun_generic_ip_addr_attr_create(pool, + PJ_STUN_ATTR_XOR_MAPPED_ADDRESS, + PJ_TRUE, + svc->src_addr_len, + &svc->src_addr, &m_attr); + if (status != PJ_SUCCESS) { + server_perror(THIS_FILE, "Error creating response", status); + return; + } + } + + /* Send */ + status = send_msg(svc, response); + if (status != PJ_SUCCESS) + server_perror(THIS_FILE, "Error sending response", status); +} + + +static void handle_unknown_request(struct service *svc, pj_pool_t *pool, + pj_stun_msg *rx_msg) +{ + err_respond(svc, pool, rx_msg, PJ_STUN_STATUS_BAD_REQUEST, 0, NULL); +} + + +static void on_read_complete(pj_ioqueue_key_t *key, + pj_ioqueue_op_key_t *op_key, + pj_ssize_t bytes_read) +{ + struct service *svc = (struct service *) pj_ioqueue_get_user_data(key); + pj_pool_t *pool = NULL; + pj_stun_msg *rx_msg; + unsigned err_code; + unsigned uattr_cnt; + pj_uint16_t uattr_types[16]; + pj_status_t status; + + if (bytes_read <= 0) + goto next_read; + + pool = pj_pool_create(&server.cp.factory, "service", 4000, 4000, NULL); + + err_code = 0; + uattr_cnt = PJ_ARRAY_SIZE(uattr_types); + rx_msg = NULL; + status = pj_stun_msg_decode(pool, svc->rx_pkt, bytes_read, 0, &rx_msg, + NULL, &err_code, &uattr_cnt, uattr_types); + if (status != PJ_SUCCESS) { + server_perror(THIS_FILE, "STUN msg_decode() error", status); + if (err_code != 0 && rx_msg && PJ_STUN_IS_REQUEST(rx_msg->hdr.type)) { + err_respond(svc, pool, rx_msg, err_code, + uattr_cnt, uattr_types); + } + goto next_read; + } + + PJ_LOG(4,(THIS_FILE, "RX STUN %s %s message", + pj_stun_get_method_name(rx_msg->hdr.type), + pj_stun_get_class_name(rx_msg->hdr.type))); + + if (PJ_STUN_IS_REQUEST(rx_msg->hdr.type)) { + switch (rx_msg->hdr.type) { + case PJ_STUN_BINDING_REQUEST: + handle_binding_request(svc, pool, rx_msg); + break; + default: + handle_unknown_request(svc, pool, rx_msg); + } + + } + +next_read: + if (pool != NULL) + pj_pool_release(pool); + + if (bytes_read < 0) { + server_perror(THIS_FILE, "on_read_complete()", -bytes_read); + } + + svc->rx_pkt_len = sizeof(svc->rx_pkt); + svc->src_addr_len = sizeof(svc->src_addr); + + status = pj_ioqueue_recvfrom(svc->key, &svc->recv_opkey, + svc->rx_pkt, &svc->rx_pkt_len, + PJ_IOQUEUE_ALWAYS_ASYNC, + &svc->src_addr, &svc->src_addr_len); + if (status != PJ_EPENDING) + server_perror(THIS_FILE, "error starting async read", status); +} + + +static pj_status_t init_service(struct service *svc) +{ + pj_status_t status; + pj_ioqueue_callback service_callback; + pj_sockaddr_in addr; + + status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &svc->sock); + if (status != PJ_SUCCESS) + return status; + + status = pj_sockaddr_in_init(&addr, NULL, svc->port); + if (status != PJ_SUCCESS) + goto on_error; + + status = pj_sock_bind(svc->sock, &addr, sizeof(addr)); + if (status != PJ_SUCCESS) + goto on_error; + + pj_bzero(&service_callback, sizeof(service_callback)); + service_callback.on_read_complete = &on_read_complete; + + status = pj_ioqueue_register_sock(server.pool, server.ioqueue, svc->sock, + svc, &service_callback, &svc->key); + if (status != PJ_SUCCESS) + goto on_error; + + + pj_ioqueue_op_key_init(&svc->recv_opkey, sizeof(svc->recv_opkey)); + pj_ioqueue_op_key_init(&svc->send_opkey, sizeof(svc->send_opkey)); + + on_read_complete(svc->key, &svc->recv_opkey, 0); + + PJ_LOG(4,(THIS_FILE, "Service started on port %d", svc->port)); + return PJ_SUCCESS; + +on_error: + if (svc->key != NULL) { + pj_ioqueue_unregister(svc->key); + svc->key = NULL; + svc->sock = PJ_INVALID_SOCKET; + } else if (svc->sock != 0 && svc->sock != PJ_INVALID_SOCKET) { + pj_sock_close(svc->sock); + svc->sock = PJ_INVALID_SOCKET; + } + + return status; +} + + +static int worker_thread(void *p) +{ + PJ_UNUSED_ARG(p); + + while (!server.thread_quit_flag) { + pj_time_val timeout = { 0, 50 }; + pj_ioqueue_poll(server.ioqueue, &timeout); + } + + return 0; +} + + +pj_status_t server_init(void) +{ + pj_status_t status; + + status = pj_init(); + if (status != PJ_SUCCESS) + return server_perror(THIS_FILE, "pj_init() error", status); + + status = pjlib_util_init(); + if (status != PJ_SUCCESS) + return server_perror(THIS_FILE, "pjlib_util_init() error", status); + + pj_caching_pool_init(&server.cp, + &pj_pool_factory_default_policy, 0); + + + server.pool = pj_pool_create(&server.cp.factory, "server", 4000, 4000, + NULL); + + status = pj_ioqueue_create(server.pool, PJ_IOQUEUE_MAX_HANDLES, + &server.ioqueue); + if (status != PJ_SUCCESS) + return server_perror(THIS_FILE, "pj_ioqueue_create()", status); + + server.service_cnt = 1; + server.services[0].index = 0; + server.services[0].port = PJ_STUN_PORT; + + status = init_service(&server.services[0]); + if (status != PJ_SUCCESS) + return server_perror(THIS_FILE, "init_service() error", status); + + return PJ_SUCCESS; +} + + +pj_status_t server_main(void) +{ +#if 1 + for (;;) { + pj_time_val timeout = { 0, 50 }; + pj_ioqueue_poll(server.ioqueue, &timeout); + + if (kbhit() && _getch()==27) + break; + } +#else + pj_status_t status; + char line[10]; + + status = pj_thread_create(server.pool, "stun_server", &worker_thread, NULL, + 0, 0, &server.threads[0]); + if (status != PJ_SUCCESS) + return server_perror(THIS_FILE, "create_thread() error", status); + + puts("Press ENTER to quit"); + fgets(line, sizeof(line), stdin); + +#endif + + return PJ_SUCCESS; +} + + +pj_status_t server_destroy(void) +{ + unsigned i; + + for (i=0; i<PJ_ARRAY_SIZE(server.services); ++i) { + struct service *svc = &server.services[i]; + + if (svc->key != NULL) { + pj_ioqueue_unregister(svc->key); + svc->key = NULL; + svc->sock = PJ_INVALID_SOCKET; + } else if (svc->sock != 0 && svc->sock != PJ_INVALID_SOCKET) { + pj_sock_close(svc->sock); + svc->sock = PJ_INVALID_SOCKET; + } + } + + server.thread_quit_flag = PJ_TRUE; + for (i=0; i<PJ_ARRAY_SIZE(server.threads); ++i) { + if (server.threads[i]) { + pj_thread_join(server.threads[i]); + pj_thread_destroy(server.threads[i]); + server.threads[i] = NULL; + } + } + + pj_ioqueue_destroy(server.ioqueue); + pj_pool_release(server.pool); + pj_caching_pool_destroy(&server.cp); + + pj_shutdown(); + + return PJ_SUCCESS; +} + + +int main() +{ + if (server_init()) { + server_destroy(); + return 1; + } + + server_main(); + server_destroy(); + + return 0; +} |