@@ -55,7 +55,7 @@ static void ngx_stream_lua_socket_udp_read_handler(ngx_stream_session_t *s,
5555 ngx_stream_lua_socket_udp_upstream_t * u );
5656static void ngx_stream_lua_socket_udp_handle_success (ngx_stream_session_t * s ,
5757 ngx_stream_lua_socket_udp_upstream_t * u );
58- static ngx_int_t ngx_stream_lua_udp_connect (lua_State * L ,
58+ static ngx_int_t ngx_stream_lua_udp_connect (
5959 ngx_stream_lua_udp_connection_t * uc );
6060static int ngx_stream_lua_socket_udp_close (lua_State * L );
6161static ngx_int_t ngx_stream_lua_socket_udp_resume (ngx_stream_session_t * s ,
@@ -224,6 +224,7 @@ ngx_stream_lua_socket_udp_setpeername(lua_State *L)
224224 ngx_stream_lua_udp_connection_t * uc ;
225225 int timeout ;
226226 ngx_stream_lua_co_ctx_t * coctx ;
227+ ngx_addr_t * local ;
227228
228229 ngx_stream_lua_socket_udp_upstream_t * u ;
229230
@@ -333,6 +334,14 @@ ngx_stream_lua_socket_udp_setpeername(lua_State *L)
333334
334335 dd ("lua peer connection log: %p" , & uc -> log );
335336
337+ lua_rawgeti (L , 1 , SOCKET_BIND_INDEX );
338+ local = lua_touserdata (L , -1 );
339+ lua_pop (L , 1 );
340+
341+ if (local ) {
342+ uc -> local = local ;
343+ }
344+
336345 lua_rawgeti (L , 1 , SOCKET_TIMEOUT_INDEX );
337346 timeout = (ngx_int_t ) lua_tointeger (L , -1 );
338347 lua_pop (L , 1 );
@@ -705,7 +714,7 @@ ngx_stream_lua_socket_resolve_retval_handler(ngx_stream_session_t *s,
705714 return 2 ;
706715 }
707716
708- rc = ngx_stream_lua_udp_connect (L , uc );
717+ rc = ngx_stream_lua_udp_connect (uc );
709718
710719 if (rc != NGX_OK ) {
711720 u -> socket_errno = ngx_socket_errno ;
@@ -1371,7 +1380,7 @@ ngx_stream_lua_socket_udp_handle_success(ngx_stream_session_t *s,
13711380
13721381
13731382static ngx_int_t
1374- ngx_stream_lua_udp_connect (lua_State * L , ngx_stream_lua_udp_connection_t * uc )
1383+ ngx_stream_lua_udp_connect (ngx_stream_lua_udp_connection_t * uc )
13751384{
13761385 int rc ;
13771386 ngx_int_t event ;
@@ -1447,19 +1456,17 @@ ngx_stream_lua_udp_connect(lua_State *L, ngx_stream_lua_udp_connection_t *uc)
14471456 }
14481457#endif
14491458
1450- lua_rawgeti ( L , 1 , SOCKET_BIND_INDEX ) ;
1451- local = lua_touserdata ( L , -1 );
1452- lua_pop ( L , 1 );
1459+ local = uc -> local ;
1460+ if ( local ) {
1461+ ngx_log_debug0 ( NGX_LOG_DEBUG_EVENT , & uc -> log , 0 , "udp socket bind" );
14531462
1454- if (local && (uc -> sockaddr -> sa_family == AF_INET
1455- || uc -> sockaddr -> sa_family == AF_INET6 )) {
1456- if (bind (uc -> connection -> fd ,
1457- local -> sockaddr , local -> socklen ) != 0 ) {
1458- ngx_log_error (NGX_LOG_CRIT , & uc -> log , ngx_socket_errno ,
1459- "bind(%V) failed" , & local -> name );
1460- return NGX_ERROR ;
1461- }
1462- }
1463+ if (bind (c -> fd , local -> sockaddr , local -> socklen ) != 0 ) {
1464+ ngx_log_error (NGX_LOG_CRIT , & uc -> log , ngx_socket_errno ,
1465+ "bind(%V) failed" , & local -> name );
1466+
1467+ return NGX_ERROR ;
1468+ }
1469+ }
14631470
14641471 ngx_log_debug3 (NGX_LOG_DEBUG_EVENT , & uc -> log , 0 ,
14651472 "connect to %V, fd:%d #%d" , & uc -> server , s , c -> number );
0 commit comments