Skip to content

Commit 783fc17

Browse files
committed
Nginx's stream support transparent.
Also support kernel network stack while set `proxy_kernel_network_stack on`.
1 parent da5549f commit 783fc17

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

app/nginx-1.25.2/src/event/ngx_event_connect.c

+25-7
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,20 @@ ngx_event_connect_peer(ngx_peer_connection_t *pc)
353353

354354
#if (NGX_HAVE_TRANSPARENT_PROXY)
355355

356+
#if (NGX_HAVE_FSTACK)
357+
extern int is_fstack_fd(int sockfd);
358+
#ifndef IP_BINDANY
359+
#define IP_BINDANY 24
360+
#endif
361+
#endif
362+
356363
static ngx_int_t
357364
ngx_event_connect_set_transparent(ngx_peer_connection_t *pc, ngx_socket_t s)
358365
{
359366
int value;
367+
#if defined(NGX_HAVE_FSTACK)
368+
int optname;
369+
#endif
360370

361371
value = 1;
362372

@@ -376,23 +386,31 @@ ngx_event_connect_set_transparent(ngx_peer_connection_t *pc, ngx_socket_t s)
376386

377387
case AF_INET:
378388

379-
#if defined(IP_TRANSPARENT)
389+
#if defined(NGX_HAVE_FSTACK)
390+
/****
391+
FreeBSD define IP_BINDANY in freebsd/netinet/in.h
392+
Fstack should only support IP_BINDANY.
393+
****/
394+
if(is_fstack_fd(s)){
395+
optname = IP_BINDANY;
396+
} else {
397+
optname = IP_TRANSPARENT;
398+
}
380399

381-
if (setsockopt(s, IPPROTO_IP, IP_TRANSPARENT,
400+
if (setsockopt(s, IPPROTO_IP, optname,
382401
(const void *) &value, sizeof(int)) == -1)
383402
{
384403
ngx_log_error(NGX_LOG_ALERT, pc->log, ngx_socket_errno,
385-
"setsockopt(IP_TRANSPARENT) failed");
404+
"setsockopt(IP_BINDANY/IP_TRANSPARENT) failed");
386405
return NGX_ERROR;
387406
}
388407

389-
#elif defined(IP_BINDANY)
390-
391-
if (setsockopt(s, IPPROTO_IP, IP_BINDANY,
408+
#elif defined(IP_TRANSPARENT)
409+
if (setsockopt(s, IPPROTO_IP, IP_TRANSPARENT,
392410
(const void *) &value, sizeof(int)) == -1)
393411
{
394412
ngx_log_error(NGX_LOG_ALERT, pc->log, ngx_socket_errno,
395-
"setsockopt(IP_BINDANY) failed");
413+
"setsockopt(IP_TRANSPARENT) failed");
396414
return NGX_ERROR;
397415
}
398416

0 commit comments

Comments
 (0)