From: <bc...@us...> - 2007-07-25 14:39:24
|
Revision: 1076 http://iaxclient.svn.sourceforge.net/iaxclient/?rev=1076&view=rev Author: bcholew Date: 2007-07-25 07:39:25 -0700 (Wed, 25 Jul 2007) Log Message: ----------- Modify iaxclient and libiax2 to ensure that the callno is properly (randomly) initialized. Courtesy of jpgrayson. Modified Paths: -------------- trunk/lib/iaxclient.h trunk/lib/iaxclient_lib.c trunk/lib/libiax2/src/iax.c Modified: trunk/lib/iaxclient.h =================================================================== --- trunk/lib/iaxclient.h 2007-07-20 18:34:36 UTC (rev 1075) +++ trunk/lib/iaxclient.h 2007-07-25 14:39:25 UTC (rev 1076) @@ -278,7 +278,7 @@ // NOTE: must be called before iaxc_initialize() EXPORT void iaxc_set_preferred_source_udp_port(int sourceUdpPort); -EXPORT unsigned short iaxc_get_bind_port(); +EXPORT short iaxc_get_bind_port(); EXPORT int iaxc_initialize(int num_calls); EXPORT void iaxc_shutdown(); EXPORT void iaxc_set_formats(int preferred, int allowed); Modified: trunk/lib/iaxclient_lib.c =================================================================== --- trunk/lib/iaxclient_lib.c 2007-07-20 18:34:36 UTC (rev 1075) +++ trunk/lib/iaxclient_lib.c 2007-07-25 14:39:25 UTC (rev 1076) @@ -92,7 +92,7 @@ static MUTEX iaxc_lock; static MUTEX event_queue_lock; -static int netfd; +static short iaxci_bound_port = -1; // default to use port 4569 unless set by iaxc_set_preferred_source_udp_port static int source_udp_port = IAX_DEFAULT_PORTNO; @@ -541,29 +541,16 @@ return iax_video_bypass_jitter(calls[selected_call].session,mode); } -EXPORT unsigned short iaxc_get_bind_port() +EXPORT short iaxc_get_bind_port() { - struct sockaddr_in addtmp; - socklen_t addlen; - int result; - - addlen = sizeof( addtmp ); - result = getsockname(netfd,(struct sockaddr *)&addtmp, &addlen ); - if ( result < 0 ) - { - iaxci_usermsg(IAXC_ERROR, "Fatal error: failed to get the iax port\n"); - return -1; - } - - return ntohs(addtmp.sin_port); + return iaxci_bound_port; } EXPORT int iaxc_initialize(int num_calls) { int i; + int port; - /* os-specific initializations: init gettimeofday fake stuff in - * Win32, etc) */ os_init(); setup_jb_output(); @@ -573,23 +560,27 @@ iaxc_set_audio_prefs(0); - if ( iaxc_sendto == (iaxc_sendto_t)sendto ) - { - int port; + if ( iaxc_recvfrom != (iaxc_recvfrom_t)recvfrom ) + iax_set_networking(iaxc_sendto, iaxc_recvfrom); - if ( (port = iax_init(source_udp_port)) < 0 ) - { - iaxci_usermsg(IAXC_ERROR, - "Fatal error: failed to initialize iax with port %d", - port); - return -1; - } - netfd = iax_get_fd(); - } else + /* Note that iax_init() only sets up the receive port when the + * sendto/recvfrom functions have not been replaced. We need + * to call iaxc_init in either case because there is other + * initialization beyond the socket setup that needs to be done. + */ + if ( (port = iax_init(source_udp_port)) < 0 ) { - iax_set_networking(iaxc_sendto, iaxc_recvfrom); + iaxci_usermsg(IAXC_ERROR, + "Fatal error: failed to initialize iax with port %d", + port); + return -1; } + if ( iaxc_recvfrom == (iaxc_recvfrom_t)recvfrom ) + iaxci_bound_port = (short)port; + else + iaxci_bound_port = -1; + /* tweak the jitterbuffer settings */ iax_set_jb_target_extra( jb_target_extra ); Modified: trunk/lib/libiax2/src/iax.c =================================================================== --- trunk/lib/libiax2/src/iax.c 2007-07-20 18:34:36 UTC (rev 1075) +++ trunk/lib/libiax2/src/iax.c 2007-07-25 14:39:25 UTC (rev 1076) @@ -885,16 +885,17 @@ int iax_init(int preferredportno) { int portno = preferredportno; - struct sockaddr_in sin; - socklen_t sinlen; - int flags; - int bufsize = 256 * 1024; - if(iax_recvfrom == (iax_recvfrom_t) recvfrom) + if (iax_recvfrom == (iax_recvfrom_t)recvfrom) { + struct sockaddr_in sin; + socklen_t sinlen; + int flags; + int bufsize = 256 * 1024; + if (netfd > -1) { - /* Sokay, just don't do anything */ + /* Okay, just don't do anything */ DEBU(G "Already initialized."); return 0; } @@ -906,9 +907,12 @@ return -1; } - if (preferredportno == 0) preferredportno = IAX_DEFAULT_PORTNO; - if (preferredportno < 0) preferredportno = 0; + if (preferredportno == 0) + preferredportno = IAX_DEFAULT_PORTNO; + if (preferredportno < 0) + preferredportno = 0; + sin.sin_family = AF_INET; sin.sin_addr.s_addr = 0; sin.sin_port = htons((short)preferredportno); @@ -973,18 +977,22 @@ return -1; } #endif + /* Mihai: increase UDP socket buffers to avoid packet loss. */ + if (setsockopt(netfd, SOL_SOCKET, SO_RCVBUF, (char *)&bufsize, + sizeof(bufsize)) < 0) + { + DEBU(G "Unable to set buffer size."); + IAXERROR "Unable to set buffer size."); + } + portno = ntohs(sin.sin_port); + DEBU(G "Started on port %d\n", portno); } - // Mihai: attempt to increase UDP socket buffers to avoid packet loss - if ( setsockopt(netfd, SOL_SOCKET, SO_RCVBUF, (char *)&bufsize,sizeof(bufsize)) < 0) - { - DEBU(G "Unable to set buffer size."); - IAXERROR "Unable to set buffer size."); - } + srand((unsigned int)time(0)); callnums = rand() % 32767 + 1; transfer_id = rand() % 32767 + 1; - DEBU(G "Started on port %d\n", portno); + return portno; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |