From 7b80afbe61b63fcc3d539856e4da005b614d835a Mon Sep 17 00:00:00 2001 From: Benny Prijono Date: Thu, 13 Mar 2008 15:11:29 +0000 Subject: More ticket #485: added TURN client application git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@1867 74dad513-b988-da41-8d7b-12977e46ad98 --- pjnath/build/pjnath.dsw | 4 +- pjnath/build/pjnath.vcproj | 14 +- pjnath/build/pjstun_client.dsp | 102 ----- pjnath/build/pjstun_client.vcproj | 246 ---------- pjnath/build/pjturn_client.dsp | 102 +++++ pjnath/build/pjturn_client.vcproj | 246 ++++++++++ pjnath/include/pjnath.h | 1 + pjnath/include/pjnath/turn_session.h | 23 +- pjnath/include/pjnath/turn_udp.h | 7 + pjnath/src/pjnath/turn_session.c | 31 ++ pjnath/src/pjnath/turn_udp.c | 42 +- pjnath/src/pjstun-client/client_main.c | 805 --------------------------------- pjnath/src/pjturn-client/client_main.c | 546 ++++++++++++++++++++++ 13 files changed, 1008 insertions(+), 1161 deletions(-) delete mode 100644 pjnath/build/pjstun_client.dsp delete mode 100644 pjnath/build/pjstun_client.vcproj create mode 100644 pjnath/build/pjturn_client.dsp create mode 100644 pjnath/build/pjturn_client.vcproj delete mode 100644 pjnath/src/pjstun-client/client_main.c create mode 100644 pjnath/src/pjturn-client/client_main.c (limited to 'pjnath') diff --git a/pjnath/build/pjnath.dsw b/pjnath/build/pjnath.dsw index 8eaa0776..7e479164 100644 --- a/pjnath/build/pjnath.dsw +++ b/pjnath/build/pjnath.dsw @@ -60,7 +60,7 @@ Package=<4> ############################################################################### -Project: "pjstun_client"=.\pjstun_client.dsp - Package Owner=<4> +Project: "pjstun_srv_test"=.\pjstun_srv_test.dsp - Package Owner=<4> Package=<5> {{{ @@ -81,7 +81,7 @@ Package=<4> ############################################################################### -Project: "pjstun_srv_test"=.\pjstun_srv_test.dsp - Package Owner=<4> +Project: "pjturn_client"=.\pjturn_client.dsp - Package Owner=<4> Package=<5> {{{ diff --git a/pjnath/build/pjnath.vcproj b/pjnath/build/pjnath.vcproj index 47720b09..766f1846 100644 --- a/pjnath/build/pjnath.vcproj +++ b/pjnath/build/pjnath.vcproj @@ -319,6 +319,14 @@ /> + + + + + + -# Microsoft Developer Studio Generated Build File, Format Version 6.00 -# ** DO NOT EDIT ** - -# TARGTYPE "Win32 (x86) Console Application" 0x0103 - -CFG=pjstun_client - 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_client.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_client.mak" CFG="pjstun_client - Win32 Debug" -!MESSAGE -!MESSAGE Possible choices for configuration are: -!MESSAGE -!MESSAGE "pjstun_client - Win32 Release" (based on "Win32 (x86) Console Application") -!MESSAGE "pjstun_client - 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_client - Win32 Release" - -# PROP BASE Use_MFC 0 -# PROP BASE Use_Debug_Libraries 0 -# PROP BASE Output_Dir "./output/pjstun-client-i386-win32-vc6-release" -# PROP BASE Intermediate_Dir "./output/pjstun-client-i386-win32-vc6-release" -# PROP BASE Target_Dir "" -# PROP Use_MFC 0 -# PROP Use_Debug_Libraries 0 -# PROP Output_Dir "./output/pjstun-client-i386-win32-vc6-release" -# PROP Intermediate_Dir "./output/pjstun-client-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" /I "../../pjlib-util/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-client-i386-win32-vc6-release.exe" - -!ELSEIF "$(CFG)" == "pjstun_client - Win32 Debug" - -# PROP BASE Use_MFC 0 -# PROP BASE Use_Debug_Libraries 1 -# PROP BASE Output_Dir "./output/pjstun-client-i386-win32-vc6-debug" -# PROP BASE Intermediate_Dir "./output/pjstun-client-i386-win32-vc6-debug" -# PROP BASE Target_Dir "" -# PROP Use_MFC 0 -# PROP Use_Debug_Libraries 1 -# PROP Output_Dir "./output/pjstun-client-i386-win32-vc6-debug" -# PROP Intermediate_Dir "./output/pjstun-client-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" /I "../../pjlib-util/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-client-i386-win32-vc6-debug.exe" /pdbtype:sept - -!ENDIF - -# Begin Target - -# Name "pjstun_client - Win32 Release" -# Name "pjstun_client - 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-client\client_main.c" -# End Source File -# End Group -# Begin Group "Header Files" - -# PROP Default_Filter "h;hpp;hxx;hm;inl" -# 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/pjnath/build/pjstun_client.vcproj b/pjnath/build/pjstun_client.vcproj deleted file mode 100644 index 5d421bce..00000000 --- a/pjnath/build/pjstun_client.vcproj +++ /dev/null @@ -1,246 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/pjnath/build/pjturn_client.dsp b/pjnath/build/pjturn_client.dsp new file mode 100644 index 00000000..ff591c94 --- /dev/null +++ b/pjnath/build/pjturn_client.dsp @@ -0,0 +1,102 @@ +# Microsoft Developer Studio Project File - Name="pjturn_client" - Package Owner=<4> +# Microsoft Developer Studio Generated Build File, Format Version 6.00 +# ** DO NOT EDIT ** + +# TARGTYPE "Win32 (x86) Console Application" 0x0103 + +CFG=pjturn_client - 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 "pjturn_client.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 "pjturn_client.mak" CFG="pjturn_client - Win32 Debug" +!MESSAGE +!MESSAGE Possible choices for configuration are: +!MESSAGE +!MESSAGE "pjturn_client - Win32 Release" (based on "Win32 (x86) Console Application") +!MESSAGE "pjturn_client - 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)" == "pjturn_client - Win32 Release" + +# PROP BASE Use_MFC 0 +# PROP BASE Use_Debug_Libraries 0 +# PROP BASE Output_Dir "./output/pjturn-client-i386-win32-vc6-release" +# PROP BASE Intermediate_Dir "./output/pjturn-client-i386-win32-vc6-release" +# PROP BASE Target_Dir "" +# PROP Use_MFC 0 +# PROP Use_Debug_Libraries 0 +# PROP Output_Dir "./output/pjturn-client-i386-win32-vc6-release" +# PROP Intermediate_Dir "./output/pjturn-client-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" /I "../../pjlib-util/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/pjturn-client-i386-win32-vc6-release.exe" + +!ELSEIF "$(CFG)" == "pjturn_client - Win32 Debug" + +# PROP BASE Use_MFC 0 +# PROP BASE Use_Debug_Libraries 1 +# PROP BASE Output_Dir "./output/pjturn-client-i386-win32-vc6-debug" +# PROP BASE Intermediate_Dir "./output/pjturn-client-i386-win32-vc6-debug" +# PROP BASE Target_Dir "" +# PROP Use_MFC 0 +# PROP Use_Debug_Libraries 1 +# PROP Output_Dir "./output/pjturn-client-i386-win32-vc6-debug" +# PROP Intermediate_Dir "./output/pjturn-client-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" /I "../../pjlib-util/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/pjturn-client-i386-win32-vc6-debug.exe" /pdbtype:sept + +!ENDIF + +# Begin Target + +# Name "pjturn_client - Win32 Release" +# Name "pjturn_client - Win32 Debug" +# Begin Group "Source Files" + +# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat" +# Begin Source File + +SOURCE="..\src\pjturn-client\client_main.c" +# End Source File +# End Group +# Begin Group "Header Files" + +# PROP Default_Filter "h;hpp;hxx;hm;inl" +# 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/pjnath/build/pjturn_client.vcproj b/pjnath/build/pjturn_client.vcproj new file mode 100644 index 00000000..eb3cf8c6 --- /dev/null +++ b/pjnath/build/pjturn_client.vcproj @@ -0,0 +1,246 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pjnath/include/pjnath.h b/pjnath/include/pjnath.h index 6a259714..f8e4d73d 100644 --- a/pjnath/include/pjnath.h +++ b/pjnath/include/pjnath.h @@ -28,5 +28,6 @@ #include #include #include +#include #include diff --git a/pjnath/include/pjnath/turn_session.h b/pjnath/include/pjnath/turn_session.h index bb00e0de..b4c18592 100644 --- a/pjnath/include/pjnath/turn_session.h +++ b/pjnath/include/pjnath/turn_session.h @@ -191,16 +191,31 @@ typedef struct pj_turn_alloc_param */ typedef struct pj_turn_session_info { + /** + * Session state. + */ + pj_turn_state_t state; + + /** + * Type of connection to the TURN server. + */ + pj_turn_tp_type tp_type; + /** * The relay address */ pj_sockaddr relay_addr; /** - * The TURN server address for informational purpose. + * The selected TURN server address. */ pj_sockaddr server; + /** + * Current seconds before allocation expires. + */ + int lifetime; + } pj_turn_session_info; @@ -229,6 +244,12 @@ PJ_DECL(pj_status_t) pj_turn_session_create(pj_stun_config *cfg, PJ_DECL(pj_status_t) pj_turn_session_destroy(pj_turn_session *sess); +/** + * Get TURN session info. + */ +PJ_DECL(pj_status_t) pj_turn_session_get_info(pj_turn_session *sess, + pj_turn_session_info *info); + /** * Re-assign user data. */ diff --git a/pjnath/include/pjnath/turn_udp.h b/pjnath/include/pjnath/turn_udp.h index f0d7c3c4..2c701590 100644 --- a/pjnath/include/pjnath/turn_udp.h +++ b/pjnath/include/pjnath/turn_udp.h @@ -95,6 +95,13 @@ PJ_DECL(pj_status_t) pj_turn_udp_set_user_data(pj_turn_udp *udp_rel, */ PJ_DECL(void*) pj_turn_udp_get_user_data(pj_turn_udp *udp_rel); + +/** + * Get info. + */ +PJ_DECL(pj_status_t) pj_turn_udp_get_info(pj_turn_udp *udp_rel, + pj_turn_session_info *info); + /** * Initialize. */ diff --git a/pjnath/src/pjnath/turn_session.c b/pjnath/src/pjnath/turn_session.c index d09d7e42..05abc436 100644 --- a/pjnath/src/pjnath/turn_session.c +++ b/pjnath/src/pjnath/turn_session.c @@ -95,6 +95,8 @@ struct pj_turn_session pj_bool_t pending_alloc; pj_turn_alloc_param alloc_param; + pj_sockaddr relay_addr; + pj_hash_table_t *peer_table; /* tx_pkt must be 16bit aligned */ @@ -367,6 +369,33 @@ PJ_DEF(pj_status_t) pj_turn_session_destroy(pj_turn_session *sess) } +/* + * Get TURN session info. + */ +PJ_DEF(pj_status_t) pj_turn_session_get_info( pj_turn_session *sess, + pj_turn_session_info *info) +{ + pj_time_val now; + + PJ_ASSERT_RETURN(sess && info, PJ_EINVAL); + + pj_gettimeofday(&now); + + info->state = sess->state; + info->tp_type = sess->tp_type; + info->lifetime = sess->expiry.sec - now.sec; + + if (sess->srv_addr) + pj_memcpy(&info->server, sess->srv_addr, sizeof(info->server)); + else + pj_bzero(&info->server, sizeof(info->server)); + + pj_memcpy(&info->relay_addr, &sess->relay_addr, sizeof(sess->relay_addr)); + + return PJ_SUCCESS; +} + + /* * Re-assign user data. */ @@ -990,6 +1019,8 @@ static void on_allocate_success(pj_turn_session *sess, return; } + /* Save relayed address */ + pj_memcpy(&sess->relay_addr, &raddr_attr->sockaddr, sizeof(pj_sockaddr)); /* Success */ diff --git a/pjnath/src/pjnath/turn_udp.c b/pjnath/src/pjnath/turn_udp.c index 7e3bd4c5..a5d34b60 100644 --- a/pjnath/src/pjnath/turn_udp.c +++ b/pjnath/src/pjnath/turn_udp.c @@ -166,6 +166,23 @@ PJ_DEF(void*) pj_turn_udp_get_user_data(pj_turn_udp *udp_rel) return udp_rel->user_data; } +/** + * Get info. + */ +PJ_DEF(pj_status_t) pj_turn_udp_get_info(pj_turn_udp *udp_rel, + pj_turn_session_info *info) +{ + PJ_ASSERT_RETURN(udp_rel && info, PJ_EINVAL); + + if (udp_rel->sess) { + return pj_turn_session_get_info(udp_rel->sess, info); + } else { + pj_bzero(info, sizeof(*info)); + info->state = PJ_TURN_STATE_NULL; + return PJ_SUCCESS; + } +} + /* * Initialize. */ @@ -178,14 +195,19 @@ PJ_DEF(pj_status_t) pj_turn_udp_init( pj_turn_udp *udp_rel, { pj_status_t status; + PJ_ASSERT_RETURN(udp_rel && domain, PJ_EINVAL); + PJ_ASSERT_RETURN(udp_rel->sess, PJ_EINVALIDOP); + status = pj_turn_session_set_server(udp_rel->sess, domain, default_port, resolver); if (status != PJ_SUCCESS) return status; - status = pj_turn_session_set_cred(udp_rel->sess, cred); - if (status != PJ_SUCCESS) - return status; + if (cred) { + status = pj_turn_session_set_cred(udp_rel->sess, cred); + if (status != PJ_SUCCESS) + return status; + } status = pj_turn_session_alloc(udp_rel->sess, param); if (status != PJ_SUCCESS) @@ -203,6 +225,11 @@ PJ_DEF(pj_status_t) pj_turn_udp_sendto( pj_turn_udp *udp_rel, const pj_sockaddr_t *addr, unsigned addr_len) { + PJ_ASSERT_RETURN(udp_rel && addr && addr_len, PJ_EINVAL); + + if (udp_rel->sess == NULL) + return PJ_EINVALIDOP; + return pj_turn_session_sendto(udp_rel->sess, pkt, pkt_len, addr, addr_len); } @@ -214,6 +241,9 @@ PJ_DEF(pj_status_t) pj_turn_udp_bind_channel( pj_turn_udp *udp_rel, const pj_sockaddr_t *peer, unsigned addr_len) { + PJ_ASSERT_RETURN(udp_rel && peer && addr_len, PJ_EINVAL); + PJ_ASSERT_RETURN(udp_rel->sess != NULL, PJ_EINVALIDOP); + return pj_turn_session_bind_channel(udp_rel->sess, peer, addr_len); } @@ -232,7 +262,7 @@ static void on_read_complete(pj_ioqueue_key_t *key, do { /* Report incoming packet to TURN session */ - if (bytes_read > 0) { + if (bytes_read > 0 && udp_rel->sess) { pj_turn_session_on_rx_pkt(udp_rel->sess, udp_rel->pkt, bytes_read, PJ_TRUE); } @@ -317,6 +347,10 @@ static void turn_on_state(pj_turn_session *sess, if (udp_rel->cb.on_state) { (*udp_rel->cb.on_state)(udp_rel, old_state, new_state); } + + if (new_state > PJ_TURN_STATE_READY) { + udp_rel->sess = NULL; + } } diff --git a/pjnath/src/pjstun-client/client_main.c b/pjnath/src/pjstun-client/client_main.c deleted file mode 100644 index 6cea861e..00000000 --- a/pjnath/src/pjstun-client/client_main.c +++ /dev/null @@ -1,805 +0,0 @@ -/* $Id$ */ -/* - * Copyright (C) 2003-2007 Benny Prijono - * - * 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 -#include -#include - - -#define THIS_FILE "client_main.c" -#define LOCAL_PORT 1998 -#define BANDWIDTH 64 /* -1 to disable */ -#define LIFETIME 600 /* -1 to disable */ -#define REQ_TRANSPORT -1 /* 0: udp, 1: tcp, -1: disable */ -#define REQ_PORT_PROPS -1 /* -1 to disable */ -#define REQ_IP 0 /* IP address string */ - -//#define OPTIONS PJ_STUN_NO_AUTHENTICATE -#define OPTIONS 0 - - -static struct global -{ - pj_stun_config stun_config; - pj_pool_t *pool; - pj_caching_pool cp; - pj_timer_heap_t *th; - pj_stun_session *sess; - pj_sock_t sock; - pj_sock_t peer_sock; - pj_thread_t *thread; - pj_bool_t quit; - pj_sockaddr_in peer_addr; - pj_sockaddr_in srv_addr; - pj_sockaddr_in relay_addr; - char data_buf[256]; - char *data; - pj_bool_t detect; - pj_status_t detect_result; -} g; - -static struct options -{ - char *srv_addr; - char *srv_port; - char *realm; - char *user_name; - char *password; - char *nonce; - char *peer_addr; - pj_bool_t use_fingerprint; -} o; - - -static pj_status_t parse_addr(const char *input, pj_sockaddr_in *addr); - - -static void my_perror(const char *title, pj_status_t status) -{ - char errmsg[PJ_ERR_MSG_SIZE]; - pj_strerror(status, errmsg, sizeof(errmsg)); - - PJ_LOG(3,(THIS_FILE, "%s: %s", title, errmsg)); -} - -static pj_status_t on_send_msg(pj_stun_session *sess, - const void *pkt, - pj_size_t pkt_size, - const pj_sockaddr_t *srv_addr, - unsigned addr_len) -{ - pj_ssize_t len; - pj_status_t status; - - len = pkt_size; - status = pj_sock_sendto(g.sock, pkt, &len, 0, srv_addr, addr_len); - - if (status != PJ_SUCCESS) - my_perror("Error sending packet", status); - - return status; -} - -static void on_request_complete(pj_stun_session *sess, - pj_status_t status, - pj_stun_tx_data *tdata, - const pj_stun_msg *response, - const pj_sockaddr_t *src_addr, - unsigned src_addr_len) -{ - PJ_UNUSED_ARG(src_addr); - PJ_UNUSED_ARG(src_addr_len); - - if (status == PJ_SUCCESS) { - switch (response->hdr.type) { - case PJ_STUN_ALLOCATE_RESPONSE: - { - pj_stun_relay_addr_attr *ar; - pj_stun_lifetime_attr *al; - - al = (pj_stun_lifetime_attr*) - pj_stun_msg_find_attr(response, - PJ_STUN_ATTR_LIFETIME, 0); - if (!al) { - PJ_LOG(1,(THIS_FILE, "Error: LIFETIME attribute not present")); - return; - } - - ar = (pj_stun_relay_addr_attr*) - pj_stun_msg_find_attr(response, - PJ_STUN_ATTR_RELAY_ADDR, 0); - if (ar) { - pj_memcpy(&g.relay_addr, &ar->sockaddr.ipv4, - sizeof(pj_sockaddr_in)); - PJ_LOG(3,(THIS_FILE, "Relay address is %s:%d", - pj_inet_ntoa(g.relay_addr.sin_addr), - (int)pj_ntohs(g.relay_addr.sin_port))); - } else { - pj_memset(&g.relay_addr, 0, sizeof(g.relay_addr)); - } - - if (al->value == 0) { - PJ_LOG(3,(THIS_FILE, "Relay deallocated")); - } - } - break; - } - } else { - my_perror("Client transaction error", status); - } -} - -static int worker_thread(void *unused) -{ - PJ_UNUSED_ARG(unused); - - while (!g.quit) { - pj_time_val timeout = {0, 50}; - pj_fd_set_t readset; - int n; - - pj_timer_heap_poll(g.th, NULL); - pj_ioqueue_poll(g.stun_config.ioqueue, &timeout); - - PJ_FD_ZERO(&readset); - PJ_FD_SET(g.sock, &readset); - PJ_FD_SET(g.peer_sock, &readset); - - n = (g.peer_sock > g.sock) ? g.peer_sock : g.sock; - n = pj_sock_select(n+1, &readset, NULL, NULL, &timeout); - if (n > 0) { - if (PJ_FD_ISSET(g.sock, &readset)) { - pj_uint8_t buffer[512]; - pj_ssize_t len; - pj_sockaddr_in addr; - int addrlen; - pj_status_t rc; - - len = sizeof(buffer); - addrlen = sizeof(addr); - rc = pj_sock_recvfrom(g.sock, buffer, &len, 0, &addr, &addrlen); - if (rc != PJ_SUCCESS || len <= 0) - continue; - - if (pj_stun_msg_check(buffer, len, PJ_STUN_IS_DATAGRAM)==PJ_SUCCESS) { - rc = pj_stun_session_on_rx_pkt(g.sess, buffer, len, - OPTIONS, - NULL, &addr, addrlen); - if (rc != PJ_SUCCESS) - my_perror("Error processing packet", rc); - - } else { - buffer[len] = '\0'; - PJ_LOG(3,(THIS_FILE, "Received data on client sock: %s", (char*)buffer)); - } - - } else if (PJ_FD_ISSET(g.peer_sock, &readset)) { - pj_uint8_t buffer[512]; - pj_ssize_t len; - pj_sockaddr_in addr; - int addrlen; - pj_status_t rc; - - len = sizeof(buffer); - addrlen = sizeof(addr); - rc = pj_sock_recvfrom(g.peer_sock, buffer, &len, 0, &addr, &addrlen); - if (rc != PJ_SUCCESS || len <= 0) - continue; - - buffer[len] = '\0'; - - if (pj_stun_msg_check(buffer, len, PJ_STUN_IS_DATAGRAM)==PJ_SUCCESS) { - pj_stun_msg *msg; - - rc = pj_stun_msg_decode(g.pool, (pj_uint8_t*)buffer, len, 0, - &msg, NULL, NULL); - if (rc != PJ_SUCCESS) { - my_perror("Error decoding packet on peer sock", rc); - } else { - pj_stun_msg_dump(msg, (char*)buffer, sizeof(buffer), NULL); - PJ_LOG(3,(THIS_FILE, "Received STUN packet on peer sock: %s", - buffer)); - } - - } else { - PJ_LOG(3,(THIS_FILE, "Received data on peer sock: %s", (char*)buffer)); - } - - } - - } else if (n < 0) - pj_thread_sleep(50); - } - - return 0; -} - -static int init() -{ - pj_sockaddr addr; - pj_stun_session_cb stun_cb; - int len; - pj_status_t status; - - g.sock = PJ_INVALID_SOCKET; - - status = pj_init(); - status = pjlib_util_init(); - status = pjnath_init(); - - pj_caching_pool_init(&g.cp, &pj_pool_factory_default_policy, 0); - - if (o.srv_addr) { - pj_str_t s; - pj_uint16_t port; - - if (o.srv_port) - port = (pj_uint16_t) atoi(o.srv_port); - else - port = PJ_STUN_PORT; - - status = pj_sockaddr_in_init(&g.srv_addr, pj_cstr(&s, o.srv_addr), port); - if (status != PJ_SUCCESS) { - my_perror("Invalid address", status); - return status; - } - - printf("Destination address set to %s:%d\n", o.srv_addr, (int)port); - } else { - printf("Error: address must be specified\n"); - return PJ_EINVAL; - } - - g.pool = pj_pool_create(&g.cp.factory, NULL, 1000, 1000, NULL); - - status = pj_timer_heap_create(g.pool, 1000, &g.th); - pj_assert(status == PJ_SUCCESS); - - pj_stun_config_init(&g.stun_config, &g.cp.factory, 0, NULL, g.th); - pj_assert(status == PJ_SUCCESS); - - status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &g.peer_sock); - pj_assert(status == PJ_SUCCESS); - - status = pj_sock_bind_in(g.peer_sock, 0, 0); - pj_assert(status == PJ_SUCCESS); - - status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &g.sock); - pj_assert(status == PJ_SUCCESS); - - status = pj_sockaddr_in_init(&addr.ipv4, NULL, 0); - pj_assert(status == PJ_SUCCESS); - - addr.ipv4.sin_port = pj_htons((pj_uint16_t)LOCAL_PORT); - status = pj_sock_bind(g.sock, &addr, sizeof(addr)); - pj_assert(status == PJ_SUCCESS); - - len = sizeof(addr); - status = pj_sock_getsockname(g.sock, &addr, &len); - pj_assert(status == PJ_SUCCESS); - - PJ_LOG(3,(THIS_FILE, "Listening on port %d", (int)pj_ntohs(addr.ipv4.sin_port))); - - len = sizeof(g.peer_addr); - status = pj_sock_getsockname(g.peer_sock, &g.peer_addr, &len); - if (g.peer_addr.sin_addr.s_addr == 0) - pj_gethostip(pj_AF_INET(), (pj_sockaddr*)&g.peer_addr.sin_addr); - - PJ_LOG(3,(THIS_FILE, "Peer is on port %d", (int)pj_ntohs(g.peer_addr.sin_port))); - - pj_memset(&stun_cb, 0, sizeof(stun_cb)); - stun_cb.on_send_msg = &on_send_msg; - stun_cb.on_request_complete = &on_request_complete; - - status = pj_stun_session_create(&g.stun_config, NULL, &stun_cb, - o.use_fingerprint!=0, &g.sess); - pj_assert(status == PJ_SUCCESS); - - if (o.user_name) { - pj_stun_auth_cred cred; - - pj_bzero(&cred, sizeof(cred)); - - cred.type = PJ_STUN_AUTH_CRED_STATIC; - cred.data.static_cred.realm = pj_str(o.realm); - cred.data.static_cred.username = pj_str(o.user_name); - cred.data.static_cred.data_type = 0; - cred.data.static_cred.data = pj_str(o.password); - cred.data.static_cred.nonce = pj_str(o.nonce); - - pj_stun_session_set_credential(g.sess, &cred); - puts("Session credential set"); - } else { - puts("Credential not set"); - } - - if (o.peer_addr) { - if (parse_addr(o.peer_addr, &g.peer_addr)!=PJ_SUCCESS) - return -1; - } - - status = pj_ioqueue_create(g.pool, 16, &g.stun_config.ioqueue); - if (status != PJ_SUCCESS) - return status; - - status = pj_thread_create(g.pool, "stun", &worker_thread, NULL, - 0, 0, &g.thread); - if (status != PJ_SUCCESS) - return status; - - return PJ_SUCCESS; -} - - -static int shutdown() -{ - if (g.thread) { - g.quit = 1; - pj_thread_join(g.thread); - pj_thread_destroy(g.thread); - g.thread = NULL; - } - if (g.sess) - pj_stun_session_destroy(g.sess); - if (g.sock != PJ_INVALID_SOCKET) - pj_sock_close(g.sock); - if (g.th) - pj_timer_heap_destroy(g.th); - if (g.pool) - pj_pool_release(g.pool); - - pj_pool_factory_dump(&g.cp.factory, PJ_TRUE); - pj_caching_pool_destroy(&g.cp); - - return PJ_SUCCESS; -} - -static void send_bind_request(void) -{ - pj_stun_tx_data *tdata; - pj_status_t rc; - - rc = pj_stun_session_create_req(g.sess, PJ_STUN_BINDING_REQUEST, - PJ_STUN_MAGIC, NULL, &tdata); - pj_assert(rc == PJ_SUCCESS); - - rc = pj_stun_session_send_msg(g.sess, PJ_FALSE, - &g.srv_addr, sizeof(g.srv_addr), - tdata); - if (rc != PJ_SUCCESS) - my_perror("Error sending STUN request", rc); -} - -static void send_allocate_request(pj_bool_t allocate) -{ - pj_stun_tx_data *tdata; - pj_status_t rc; - - rc = pj_stun_session_create_req(g.sess, PJ_STUN_ALLOCATE_REQUEST, - PJ_STUN_MAGIC, NULL, &tdata); - pj_assert(rc == PJ_SUCCESS); - - - if (BANDWIDTH != -1) { - pj_stun_msg_add_uint_attr(tdata->pool, tdata->msg, - PJ_STUN_ATTR_BANDWIDTH, BANDWIDTH); - } - - if (!allocate) { - pj_stun_msg_add_uint_attr(tdata->pool, tdata->msg, - PJ_STUN_ATTR_LIFETIME, 0); - - } else { - if (LIFETIME != -1) { - pj_stun_msg_add_uint_attr(tdata->pool, tdata->msg, - PJ_STUN_ATTR_LIFETIME, LIFETIME); - } - - if (REQ_TRANSPORT != -1) { - pj_stun_msg_add_uint_attr(tdata->pool, tdata->msg, - PJ_STUN_ATTR_REQ_TRANSPORT, REQ_TRANSPORT); - } - - if (REQ_PORT_PROPS != -1) { - pj_stun_msg_add_uint_attr(tdata->pool, tdata->msg, - PJ_STUN_ATTR_REQ_PORT_PROPS, REQ_PORT_PROPS); - } - - if (REQ_IP) { - pj_sockaddr_in addr; - pj_str_t tmp; - - pj_sockaddr_in_init(&addr, pj_cstr(&tmp, REQ_IP), 0); - pj_stun_msg_add_sockaddr_attr(tdata->pool, tdata->msg, - PJ_STUN_ATTR_REQ_IP, PJ_FALSE, - &addr, sizeof(addr)); - } - } - - rc = pj_stun_session_send_msg(g.sess, PJ_FALSE, - &g.srv_addr, sizeof(g.srv_addr), - tdata); - pj_assert(rc == PJ_SUCCESS); -} - -static void send_sad_request(pj_bool_t set) -{ - pj_stun_tx_data *tdata; - pj_status_t rc; - - if (g.peer_addr.sin_addr.s_addr == 0 || - g.peer_addr.sin_port == 0) - { - puts("Error: peer address is not set"); - return; - } - - rc = pj_stun_session_create_req(g.sess, - PJ_STUN_SET_ACTIVE_DESTINATION_REQUEST, - PJ_STUN_MAGIC, NULL, &tdata); - pj_assert(rc == PJ_SUCCESS); - - if (set) { - pj_stun_msg_add_sockaddr_attr(tdata->pool, tdata->msg, - PJ_STUN_ATTR_REMOTE_ADDR, PJ_FALSE, - &g.peer_addr, sizeof(g.peer_addr)); - } - - rc = pj_stun_session_send_msg(g.sess, PJ_FALSE, - &g.srv_addr, sizeof(g.srv_addr), - tdata); - pj_assert(rc == PJ_SUCCESS); -} - -static void send_send_ind(void) -{ - pj_stun_tx_data *tdata; - int len; - pj_status_t rc; - - if (g.peer_addr.sin_addr.s_addr == 0 || - g.peer_addr.sin_port == 0) - { - puts("Error: peer address is not set"); - return; - } - - len = strlen(g.data); - if (len==0) { - puts("Error: data is not set"); - return; - } - - rc = pj_stun_session_create_ind(g.sess, PJ_STUN_SEND_INDICATION, &tdata); - pj_assert(rc == PJ_SUCCESS); - - pj_stun_msg_add_sockaddr_attr(tdata->pool, tdata->msg, - PJ_STUN_ATTR_REMOTE_ADDR, PJ_FALSE, - &g.peer_addr, sizeof(g.peer_addr)); - pj_stun_msg_add_binary_attr(tdata->pool, tdata->msg, - PJ_STUN_ATTR_DATA, (pj_uint8_t*)g.data, len); - - rc = pj_stun_session_send_msg(g.sess, PJ_FALSE, - &g.srv_addr, sizeof(g.srv_addr), - tdata); - pj_assert(rc == PJ_SUCCESS); - -} - -static void send_raw_data_to_srv(void) -{ - pj_ssize_t len; - - if (g.srv_addr.sin_addr.s_addr == 0 || - g.srv_addr.sin_port == 0) - { - puts("Error: server address is not set"); - return; - } - - len = strlen(g.data); - if (len==0) { - puts("Error: data is not set"); - return; - } - - len = strlen(g.data); - pj_sock_sendto(g.sock, g.data, &len, 0, &g.srv_addr, sizeof(g.srv_addr)); -} - -static void send_raw_data_to_relay(void) -{ - pj_ssize_t len; - - if (g.relay_addr.sin_addr.s_addr == 0 || - g.relay_addr.sin_port == 0) - { - puts("Error: relay address is not set"); - return; - } - - len = strlen(g.data); - if (len==0) { - puts("Error: data is not set"); - return; - } - - len = strlen(g.data); - pj_sock_sendto(g.peer_sock, g.data, &len, 0, &g.relay_addr, sizeof(g.relay_addr)); -} - -static pj_status_t parse_addr(const char *input, - pj_sockaddr_in *addr) -{ - const char *pos; - pj_str_t ip; - pj_uint16_t port; - pj_sockaddr tmp_addr; - - pos = pj_ansi_strchr(input, ':'); - if (pos==NULL) { - puts("Invalid format"); - return -1; - } - - ip.ptr = (char*)input; - ip.slen = pos - input; - port = (pj_uint16_t)atoi(pos+1); - - if (port==0) { - puts("Invalid port"); - return -1; - } - - if (pj_sockaddr_in_init(&tmp_addr.ipv4, &ip, port)!=PJ_SUCCESS) { - puts("Invalid address"); - return -1; - } - - pj_memcpy(addr, &tmp_addr, sizeof(pj_sockaddr_in)); - - return PJ_SUCCESS; -} - -static void set_peer_addr(void) -{ - char addr[64]; - - printf("Current peer address: %s:%d\n", - pj_inet_ntoa(g.peer_addr.sin_addr), - pj_ntohs(g.peer_addr.sin_port)); - - printf("Input peer address in IP:PORT format: "); - fflush(stdout); - fgets(addr, sizeof(addr), stdin); - - if (parse_addr(addr, &g.peer_addr) != PJ_SUCCESS) { - return; - } - -} - - -static void nat_detect_cb(void *user_data, - const pj_stun_nat_detect_result *res) -{ - g.detect_result = res->status; - - if (res->status == PJ_SUCCESS) { - PJ_LOG(3,(THIS_FILE, "NAT successfully detected as %s", res->nat_type_name)); - } else { - PJ_LOG(2,(THIS_FILE, "Error detecting NAT type: %s", res->status_text)); - } -} - -static pj_status_t perform_detection() -{ - pj_status_t status; - - g.detect_result = PJ_EPENDING; - status = pj_stun_detect_nat_type(&g.srv_addr, &g.stun_config, NULL, - &nat_detect_cb); - if (status != PJ_SUCCESS) - return status; - - while (g.detect_result == PJ_EPENDING) - pj_thread_sleep(100); - - status = g.detect_result; - - return status; -} - - -static void menu(void) -{ - puts("Menu:"); - puts(" d Perform NAT detection"); - printf(" pr Set peer address (currently %s:%d)\n", - pj_inet_ntoa(g.peer_addr.sin_addr), pj_ntohs(g.peer_addr.sin_port)); - printf(" dt Set data (currently \"%s\")\n", g.data); - puts(" br Send Bind request"); - puts(" ar Send Allocate request"); - puts(" dr Send de-Allocate request"); - puts(" sr Send Set Active Destination request"); - puts(" cr Send clear Active Destination request"); - puts(" si Send data with Send Indication"); - puts(" rw Send raw data to TURN server"); - puts(" rW Send raw data to relay address"); - puts(" q Quit"); - puts(""); - printf("Choice: "); -} - - -static void console_main(void) -{ - while (!g.quit) { - char input[10]; - - menu(); - - fgets(input, sizeof(input), stdin); - - if (input[0] == 'd' && (input[1]=='\r' || input[1]=='\n')) { - - perform_detection(); - - } else if (input[0]=='d' && input[1]=='t') { - printf("Input data: "); - fgets(g.data, sizeof(g.data_buf), stdin); - - } else if (input[0]=='p' && input[1]=='r') { - set_peer_addr(); - - } else if (input[0]=='b' && input[1]=='r') { - send_bind_request(); - - } else if (input[0]=='a' && input[1]=='r') { - send_allocate_request(PJ_TRUE); - - } else if (input[0]=='d' && input[1]=='r') { - send_allocate_request(PJ_FALSE); - - } else if (input[0]=='s' && input[1]=='r') { - send_sad_request(PJ_TRUE); - - } else if (input[0]=='c' && input[1]=='r') { - send_sad_request(PJ_FALSE); - - } else if (input[0]=='s' && input[1]=='i') { - send_send_ind(); - - } else if (input[0]=='r' && input[1]=='w') { - send_raw_data_to_srv(); - - } else if (input[0]=='r' && input[1]=='W') { - send_raw_data_to_relay(); - - } else if (input[0]=='q') { - g.quit = 1; - } - } -} - - -static void usage(void) -{ - puts("Usage: pjstun_client TARGET [OPTIONS]"); - puts(""); - puts("where TARGET is \"host[:port]\""); - puts(""); - puts("and OPTIONS:"); - puts(" --detect, -d Perform NAT type detection first"); - puts(" --realm, -r Set realm of the credential"); - puts(" --username, -u Set username of the credential"); - puts(" --password, -p Set password of the credential"); - puts(" --nonce, -N Set NONCE"); - puts(" --fingerprint, -F Use fingerprint for outgoing requests"); - puts(" --peer, -P Set peer address (address is in HOST:PORT format)"); - puts(" --data, -D Set data"); - puts(" --help, -h"); -} - -int main(int argc, char *argv[]) -{ - struct pj_getopt_option long_options[] = { - { "detect", 0, 0, 'd'}, - { "realm", 1, 0, 'r'}, - { "username", 1, 0, 'u'}, - { "password", 1, 0, 'p'}, - { "nonce", 1, 0, 'N'}, - { "fingerprint",0, 0, 'F'}, - { "peer", 1, 0, 'P'}, - { "data", 1, 0, 'D'}, - { "help", 0, 0, 'h'} - }; - int c, opt_id; - char *pos; - pj_status_t status; - - g.data = g.data_buf; - pj_ansi_strcpy(g.data, "Hello world"); - - while((c=pj_getopt_long(argc,argv, "r:u:p:N:dhF", long_options, &opt_id))!=-1) { - switch (c) { - case 'd': - g.detect = PJ_TRUE; - break; - case 'r': - o.realm = pj_optarg; - break; - case 'u': - o.user_name = pj_optarg; - break; - case 'p': - o.password = pj_optarg; - break; - case 'N': - o.nonce = pj_optarg; - break; - case 'h': - usage(); - return 0; - case 'F': - o.use_fingerprint = PJ_TRUE; - break; - case 'P': - o.peer_addr = pj_optarg; - break; - case 'D': - g.data = pj_optarg; - break; - - default: - printf("Argument \"%s\" is not valid. Use -h to see help", - argv[pj_optind]); - return 1; - } - } - - if (pj_optind == argc) { - puts("Error: TARGET is needed"); - return 1; - } - - if ((pos=pj_ansi_strchr(argv[pj_optind], ':')) != NULL) { - o.srv_addr = argv[pj_optind]; - *pos = '\0'; - o.srv_port = pos+1; - } else { - o.srv_addr = argv[pj_optind]; - } - - status = init(); - if (status != PJ_SUCCESS) - goto on_return; - - if (g.detect) { - status = perform_detection(); - if (status != PJ_SUCCESS) - goto on_return; - } - - console_main(); - -on_return: - shutdown(); - return status ? 1 : 0; -} - diff --git a/pjnath/src/pjturn-client/client_main.c b/pjnath/src/pjturn-client/client_main.c new file mode 100644 index 00000000..a9123319 --- /dev/null +++ b/pjnath/src/pjturn-client/client_main.c @@ -0,0 +1,546 @@ +/* $Id$ */ +/* + * Copyright (C) 2003-2007 Benny Prijono + * + * 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 +#include +#include + + +#define THIS_FILE "client_main.c" +#define LOCAL_PORT 1998 +#define BANDWIDTH 64 /* -1 to disable */ +#define LIFETIME 600 /* -1 to disable */ +#define REQ_TRANSPORT -1 /* 0: udp, 1: tcp, -1: disable */ +#define REQ_PORT_PROPS -1 /* -1 to disable */ +#define REQ_IP 0 /* IP address string */ + +//#define OPTIONS PJ_STUN_NO_AUTHENTICATE +#define OPTIONS 0 + + +struct peer +{ + pj_sock_t sock; + pj_sockaddr addr; +}; + + +static struct global +{ + pj_caching_pool cp; + pj_pool_t *pool; + pj_stun_config stun_config; + pj_thread_t *thread; + pj_bool_t quit; + + pj_turn_udp *udp_rel; + pj_sockaddr relay_addr; + + struct peer peer[2]; +} g; + +static struct options +{ + char *srv_addr; + char *srv_port; + char *realm; + char *user_name; + char *password; + char *nonce; + pj_bool_t use_fingerprint; +} o; + + +static int worker_thread(void *unused); +static pj_status_t parse_addr(const char *input, pj_sockaddr_in *addr); +static void turn_on_rx_data(pj_turn_udp *udp_rel, + const pj_uint8_t *pkt, + unsigned pkt_len, + const pj_sockaddr_t *peer_addr, + unsigned addr_len); +static void turn_on_state(pj_turn_udp *udp_rel, pj_turn_state_t old_state, + pj_turn_state_t new_state); + + +static void my_perror(const char *title, pj_status_t status) +{ + char errmsg[PJ_ERR_MSG_SIZE]; + pj_strerror(status, errmsg, sizeof(errmsg)); + + PJ_LOG(3,(THIS_FILE, "%s: %s", title, errmsg)); +} + +#define CHECK(expr) status=expr; \ + if (status!=PJ_SUCCESS) { \ + my_perror(#expr, status); \ + return status; \ + } + +static int init() +{ + int i; + pj_status_t status; + + CHECK( pj_init() ); + CHECK( pjlib_util_init() ); + CHECK( pjnath_init() ); + + /* Check that server is specified */ + if (!o.srv_addr) { + printf("Error: server must be specified\n"); + return PJ_EINVAL; + } + + pj_caching_pool_init(&g.cp, &pj_pool_factory_default_policy, 0); + + g.pool = pj_pool_create(&g.cp.factory, "main", 1000, 1000, NULL); + + /* Init global STUN config */ + pj_stun_config_init(&g.stun_config, &g.cp.factory, 0, NULL, NULL); + + /* Create global timer heap */ + CHECK( pj_timer_heap_create(g.pool, 1000, &g.stun_config.timer_heap) ); + + /* Create global ioqueue */ + CHECK( pj_ioqueue_create(g.pool, 16, &g.stun_config.ioqueue) ); + + /* + * Create peers + */ + for (i=0; i>> "); + fflush(stdout); +} + + +static void console_main(void) +{ + while (!g.quit) { + char input[32]; + struct peer *peer; + pj_ssize_t len; + pj_status_t status; + + menu(); + + fgets(input, sizeof(input), stdin); + + switch (input[0]) { + case 'A': + create_relay(); + break; + case 'S': + if (g.udp_rel == NULL) { + puts("Error: no relay"); + continue; + } + if (input[1] != '0' && input[1] != '1') { + puts("Usage: S0 or S1"); + continue; + } + peer = &g.peer[input[1]-'0']; + strcpy(input, "Hello from client"); + status = pj_turn_udp_sendto(g.udp_rel, input, strlen(input)+1, + &peer->addr, + pj_sockaddr_get_len(&peer->addr)); + if (status != PJ_SUCCESS) + my_perror("turn_udp_sendto() failed", status); + break; + case 'B': + if (g.udp_rel == NULL) { + puts("Error: no relay"); + continue; + } + if (input[1] != '0' && input[1] != '1') { + puts("Usage: B0 or B1"); + continue; + } + peer = &g.peer[input[1]-'0']; + status = pj_turn_udp_bind_channel(g.udp_rel, &peer->addr, + pj_sockaddr_get_len(&peer->addr)); + if (status != PJ_SUCCESS) + my_perror("turn_udp_bind_channel() failed", status); + break; + case 'X': + if (g.udp_rel == NULL) { + puts("Error: no relay"); + continue; + } + destroy_relay(); + break; + case '0': + case '1': + peer = &g.peer[input[1]-'0']; + sprintf(input, "Hello from peer%d", input[0]-'0'); + len = strlen(input)+1; + pj_sock_sendto(peer->sock, input, &len, 0, &g.relay_addr, + pj_sockaddr_get_len(&g.relay_addr)); + break; + case 'q': + g.quit = PJ_TRUE; + break; + } + } +} + + +static void usage(void) +{ + puts("Usage: pjturn_client TARGET [OPTIONS]"); + puts(""); + puts("where TARGET is \"host[:port]\""); + puts(""); + puts("and OPTIONS:"); + puts(" --realm, -r Set realm of the credential"); + puts(" --username, -u Set username of the credential"); + puts(" --password, -p Set password of the credential"); + puts(" --nonce, -N Set NONCE"); + puts(" --fingerprint, -F Use fingerprint for outgoing requests"); + puts(" --help, -h"); +} + +int main(int argc, char *argv[]) +{ + struct pj_getopt_option long_options[] = { + { "realm", 1, 0, 'r'}, + { "username", 1, 0, 'u'}, + { "password", 1, 0, 'p'}, + { "nonce", 1, 0, 'N'}, + { "fingerprint",0, 0, 'F'}, + { "data", 1, 0, 'D'}, + { "help", 0, 0, 'h'} + }; + int c, opt_id; + char *pos; + pj_status_t status; + + while((c=pj_getopt_long(argc,argv, "r:u:p:N:hF", long_options, &opt_id))!=-1) { + switch (c) { + case 'r': + o.realm = pj_optarg; + break; + case 'u': + o.user_name = pj_optarg; + break; + case 'p': + o.password = pj_optarg; + break; + case 'N': + o.nonce = pj_optarg; + break; + case 'h': + usage(); + return 0; + case 'F': + o.use_fingerprint = PJ_TRUE; + break; + + default: + printf("Argument \"%s\" is not valid. Use -h to see help", + argv[pj_optind]); + return 1; + } + } + + if (pj_optind == argc) { + puts("Error: TARGET is needed"); + return 1; + } + + if ((pos=pj_ansi_strchr(argv[pj_optind], ':')) != NULL) { + o.srv_addr = argv[pj_optind]; + *pos = '\0'; + o.srv_port = pos+1; + } else { + o.srv_addr = argv[pj_optind]; + } + + if ((status=init()) != 0) + goto on_return; + + if ((status=create_relay()) != 0) + goto on_return; + + console_main(); + +on_return: + shutdown(); + return status ? 1 : 0; +} + -- cgit v1.2.3