Klaus Demo nginx / 5b201ac
Introduced ngx_inet_get_port() and ngx_inet_set_port() functions. Roman Arutyunyan 6 years ago
15 changed file(s) with 90 addition(s) and 317 deletion(s). Raw diff Collapse all Expand all
528528 ngx_parse_addr_port(ngx_pool_t *pool, ngx_addr_t *addr, u_char *text,
529529 size_t len)
530530 {
531 u_char *p, *last;
532 size_t plen;
533 ngx_int_t rc, port;
534 struct sockaddr_in *sin;
535 #if (NGX_HAVE_INET6)
536 struct sockaddr_in6 *sin6;
537 #endif
531 u_char *p, *last;
532 size_t plen;
533 ngx_int_t rc, port;
538534
539535 rc = ngx_parse_addr(pool, addr, text, len);
540536
584580 return rc;
585581 }
586582
587 switch (addr->sockaddr->sa_family) {
588
589 #if (NGX_HAVE_INET6)
590 case AF_INET6:
591 sin6 = (struct sockaddr_in6 *) addr->sockaddr;
592 sin6->sin6_port = htons((in_port_t) port);
593 break;
594 #endif
595
596 default: /* AF_INET */
597 sin = (struct sockaddr_in *) addr->sockaddr;
598 sin->sin_port = htons((in_port_t) port);
599 break;
600 }
583 ngx_inet_set_port(addr->sockaddr, port);
601584
602585 return NGX_OK;
603586 }
13551338
13561339 return NGX_OK;
13571340 }
1341
1342
1343 in_port_t
1344 ngx_inet_get_port(struct sockaddr *sa)
1345 {
1346 struct sockaddr_in *sin;
1347 #if (NGX_HAVE_INET6)
1348 struct sockaddr_in6 *sin6;
1349 #endif
1350
1351 switch (sa->sa_family) {
1352
1353 #if (NGX_HAVE_INET6)
1354 case AF_INET6:
1355 sin6 = (struct sockaddr_in6 *) sa;
1356 return ntohs(sin6->sin6_port);
1357 #endif
1358
1359 #if (NGX_HAVE_UNIX_DOMAIN)
1360 case AF_UNIX:
1361 return 0;
1362 #endif
1363
1364 default: /* AF_INET */
1365 sin = (struct sockaddr_in *) sa;
1366 return ntohs(sin->sin_port);
1367 }
1368 }
1369
1370
1371 void
1372 ngx_inet_set_port(struct sockaddr *sa, in_port_t port)
1373 {
1374 struct sockaddr_in *sin;
1375 #if (NGX_HAVE_INET6)
1376 struct sockaddr_in6 *sin6;
1377 #endif
1378
1379 switch (sa->sa_family) {
1380
1381 #if (NGX_HAVE_INET6)
1382 case AF_INET6:
1383 sin6 = (struct sockaddr_in6 *) sa;
1384 sin6->sin6_port = htons(port);
1385 break;
1386 #endif
1387
1388 #if (NGX_HAVE_UNIX_DOMAIN)
1389 case AF_UNIX:
1390 break;
1391 #endif
1392
1393 default: /* AF_INET */
1394 sin = (struct sockaddr_in *) sa;
1395 sin->sin_port = htons(port);
1396 break;
1397 }
1398 }
120120 ngx_int_t ngx_inet_resolve_host(ngx_pool_t *pool, ngx_url_t *u);
121121 ngx_int_t ngx_cmp_sockaddr(struct sockaddr *sa1, socklen_t slen1,
122122 struct sockaddr *sa2, socklen_t slen2, ngx_uint_t cmp_port);
123 in_port_t ngx_inet_get_port(struct sockaddr *sa);
124 void ngx_inet_set_port(struct sockaddr *sa, in_port_t port);
123125
124126
125127 #endif /* _NGX_INET_H_INCLUDED_ */
140140
141141 case AF_INET:
142142 buf = ngx_cpymem(buf, "PROXY TCP4 ", sizeof("PROXY TCP4 ") - 1);
143
144 port = ntohs(((struct sockaddr_in *) c->sockaddr)->sin_port);
145 lport = ntohs(((struct sockaddr_in *) c->local_sockaddr)->sin_port);
146
147143 break;
148144
149145 #if (NGX_HAVE_INET6)
150146 case AF_INET6:
151147 buf = ngx_cpymem(buf, "PROXY TCP6 ", sizeof("PROXY TCP6 ") - 1);
152
153 port = ntohs(((struct sockaddr_in6 *) c->sockaddr)->sin6_port);
154 lport = ntohs(((struct sockaddr_in6 *) c->local_sockaddr)->sin6_port);
155
156148 break;
157149 #endif
158150
168160 buf += ngx_sock_ntop(c->local_sockaddr, c->local_socklen, buf, last - buf,
169161 0);
170162
163 port = ngx_inet_get_port(c->sockaddr);
164 lport = ngx_inet_get_port(c->local_sockaddr);
165
171166 return ngx_slprintf(buf, last, " %ui %ui" CRLF, port, lport);
172167 }
29952995 ngx_addr_t *addrs;
29962996 ngx_resolver_t *r;
29972997 ngx_sockaddr_t *sockaddr;
2998 struct sockaddr_in *sin;
29992998 ngx_resolver_ctx_t *ctx;
30002999 ngx_resolver_srv_name_t *srv;
3001 #if (NGX_HAVE_INET6)
3002 struct sockaddr_in6 *sin6;
3003 #endif
30043000
30053001 r = cctx->resolver;
30063002 ctx = cctx->data;
30443040 ngx_memcpy(&sockaddr[i], cctx->addrs[i].sockaddr,
30453041 addrs[i].socklen);
30463042
3047 switch (addrs[i].sockaddr->sa_family) {
3048 #if (NGX_HAVE_INET6)
3049 case AF_INET6:
3050 sin6 = (struct sockaddr_in6 *) addrs[i].sockaddr;
3051 sin6->sin6_port = htons(srv->port);
3052 break;
3053 #endif
3054 default: /* AF_INET */
3055 sin = (struct sockaddr_in *) addrs[i].sockaddr;
3056 sin->sin_port = htons(srv->port);
3057 }
3043 ngx_inet_set_port(addrs[i].sockaddr, srv->port);
30583044 }
30593045
30603046 srv->addrs = addrs;
919919
920920 u_char *p;
921921 size_t len;
922 in_port_t port;
923922 socklen_t socklen;
924923 ngx_uint_t i;
925924 struct sockaddr *sockaddr;
961960 goto failed;
962961 }
963962
964 port = htons(ctx->port);
965
966963 for (i = 0; i < resolve->naddrs; i++) {
967964
968965 socklen = resolve->addrs[i].socklen;
973970 }
974971
975972 ngx_memcpy(sockaddr, resolve->addrs[i].sockaddr, socklen);
976
977 switch (sockaddr->sa_family) {
978 #if (NGX_HAVE_INET6)
979 case AF_INET6:
980 ((struct sockaddr_in6 *) sockaddr)->sin6_port = port;
981 break;
982 #endif
983 default: /* AF_INET */
984 ((struct sockaddr_in *) sockaddr)->sin_port = port;
985 }
973 ngx_inet_set_port(sockaddr, ctx->port);
986974
987975 ctx->addrs[i].sockaddr = sockaddr;
988976 ctx->addrs[i].socklen = socklen;
137137 ngx_list_part_t *part;
138138 ngx_table_elt_t *header;
139139 ngx_connection_t *c;
140 struct sockaddr_in *sin;
141 #if (NGX_HAVE_INET6)
142 struct sockaddr_in6 *sin6;
143 #endif
144140 ngx_http_realip_ctx_t *ctx;
145141 ngx_http_realip_loc_conf_t *rlcf;
146142
241237 != NGX_DECLINED)
242238 {
243239 if (rlcf->type == NGX_HTTP_REALIP_PROXY) {
244
245 switch (addr.sockaddr->sa_family) {
246
247 #if (NGX_HAVE_INET6)
248 case AF_INET6:
249 sin6 = (struct sockaddr_in6 *) addr.sockaddr;
250 sin6->sin6_port = htons(c->proxy_protocol_port);
251 break;
252 #endif
253
254 default: /* AF_INET */
255 sin = (struct sockaddr_in *) addr.sockaddr;
256 sin->sin_port = htons(c->proxy_protocol_port);
257 break;
258 }
240 ngx_inet_set_port(addr.sockaddr, c->proxy_protocol_port);
259241 }
260242
261243 return ngx_http_realip_set_addr(r, &addr);
577559 return NGX_ERROR;
578560 }
579561
580 switch (sa->sa_family) {
581
582 #if (NGX_HAVE_INET6)
583 case AF_INET6:
584 port = ntohs(((struct sockaddr_in6 *) sa)->sin6_port);
585 break;
586 #endif
587
588 #if (NGX_HAVE_UNIX_DOMAIN)
589 case AF_UNIX:
590 port = 0;
591 break;
592 #endif
593
594 default: /* AF_INET */
595 port = ntohs(((struct sockaddr_in *) sa)->sin_port);
596 break;
597 }
562 port = ngx_inet_get_port(sa);
598563
599564 if (port > 0 && port < 65536) {
600565 v->len = ngx_sprintf(v->data, "%ui", port) - v->data;
11431143 in_port_t p;
11441144 ngx_uint_t i;
11451145 struct sockaddr *sa;
1146 struct sockaddr_in *sin;
11471146 ngx_http_conf_port_t *port;
11481147 ngx_http_core_main_conf_t *cmcf;
1149 #if (NGX_HAVE_INET6)
1150 struct sockaddr_in6 *sin6;
1151 #endif
11521148
11531149 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
11541150
11611157 }
11621158
11631159 sa = &lsopt->sockaddr.sockaddr;
1164
1165 switch (sa->sa_family) {
1166
1167 #if (NGX_HAVE_INET6)
1168 case AF_INET6:
1169 sin6 = &lsopt->sockaddr.sockaddr_in6;
1170 p = sin6->sin6_port;
1171 break;
1172 #endif
1173
1174 #if (NGX_HAVE_UNIX_DOMAIN)
1175 case AF_UNIX:
1176 p = 0;
1177 break;
1178 #endif
1179
1180 default: /* AF_INET */
1181 sin = &lsopt->sockaddr.sockaddr_in;
1182 p = sin->sin_port;
1183 break;
1184 }
1160 p = ngx_inet_get_port(sa);
11851161
11861162 port = cmcf->ports->elts;
11871163 for (i = 0; i < cmcf->ports->nelts; i++) {
160160 ngx_connection_t *c;
161161 ngx_http_core_loc_conf_t *clcf;
162162 ngx_http_core_srv_conf_t *cscf;
163 struct sockaddr_in *sin;
164 #if (NGX_HAVE_INET6)
165 struct sockaddr_in6 *sin6;
166 #endif
167163 u_char addr[NGX_SOCKADDR_STRLEN];
168164
169165 if (r->header_sent) {
332328 }
333329 }
334330
335 switch (c->local_sockaddr->sa_family) {
336
337 #if (NGX_HAVE_INET6)
338 case AF_INET6:
339 sin6 = (struct sockaddr_in6 *) c->local_sockaddr;
340 port = ntohs(sin6->sin6_port);
341 break;
342 #endif
343 #if (NGX_HAVE_UNIX_DOMAIN)
344 case AF_UNIX:
345 port = 0;
346 break;
347 #endif
348 default: /* AF_INET */
349 sin = (struct sockaddr_in *) c->local_sockaddr;
350 port = ntohs(sin->sin_port);
351 break;
352 }
331 port = ngx_inet_get_port(c->local_sockaddr);
353332
354333 len += sizeof("Location: https://") - 1
355334 + host.len
353353 }
354354
355355 ngx_memcpy(sockaddr, ur->addrs[i].sockaddr, socklen);
356
357 switch (sockaddr->sa_family) {
358 #if (NGX_HAVE_INET6)
359 case AF_INET6:
360 ((struct sockaddr_in6 *) sockaddr)->sin6_port = htons(ur->port);
361 break;
362 #endif
363 default: /* AF_INET */
364 ((struct sockaddr_in *) sockaddr)->sin_port = htons(ur->port);
365 }
356 ngx_inet_set_port(sockaddr, ur->port);
366357
367358 p = ngx_pnalloc(r->pool, NGX_SOCKADDR_STRLEN);
368359 if (p == NULL) {
12001200 ngx_http_variable_remote_port(ngx_http_request_t *r,
12011201 ngx_http_variable_value_t *v, uintptr_t data)
12021202 {
1203 ngx_uint_t port;
1204 struct sockaddr_in *sin;
1205 #if (NGX_HAVE_INET6)
1206 struct sockaddr_in6 *sin6;
1207 #endif
1203 ngx_uint_t port;
12081204
12091205 v->len = 0;
12101206 v->valid = 1;
12161212 return NGX_ERROR;
12171213 }
12181214
1219 switch (r->connection->sockaddr->sa_family) {
1220
1221 #if (NGX_HAVE_INET6)
1222 case AF_INET6:
1223 sin6 = (struct sockaddr_in6 *) r->connection->sockaddr;
1224 port = ntohs(sin6->sin6_port);
1225 break;
1226 #endif
1227
1228 #if (NGX_HAVE_UNIX_DOMAIN)
1229 case AF_UNIX:
1230 port = 0;
1231 break;
1232 #endif
1233
1234 default: /* AF_INET */
1235 sin = (struct sockaddr_in *) r->connection->sockaddr;
1236 port = ntohs(sin->sin_port);
1237 break;
1238 }
1215 port = ngx_inet_get_port(r->connection->sockaddr);
12391216
12401217 if (port > 0 && port < 65536) {
12411218 v->len = ngx_sprintf(v->data, "%ui", port) - v->data;
13201297 ngx_http_variable_server_port(ngx_http_request_t *r,
13211298 ngx_http_variable_value_t *v, uintptr_t data)
13221299 {
1323 ngx_uint_t port;
1324 struct sockaddr_in *sin;
1325 #if (NGX_HAVE_INET6)
1326 struct sockaddr_in6 *sin6;
1327 #endif
1300 ngx_uint_t port;
13281301
13291302 v->len = 0;
13301303 v->valid = 1;
13401313 return NGX_ERROR;
13411314 }
13421315
1343 switch (r->connection->local_sockaddr->sa_family) {
1344
1345 #if (NGX_HAVE_INET6)
1346 case AF_INET6:
1347 sin6 = (struct sockaddr_in6 *) r->connection->local_sockaddr;
1348 port = ntohs(sin6->sin6_port);
1349 break;
1350 #endif
1351
1352 #if (NGX_HAVE_UNIX_DOMAIN)
1353 case AF_UNIX:
1354 port = 0;
1355 break;
1356 #endif
1357
1358 default: /* AF_INET */
1359 sin = (struct sockaddr_in *) r->connection->local_sockaddr;
1360 port = ntohs(sin->sin_port);
1361 break;
1362 }
1316 port = ngx_inet_get_port(r->connection->local_sockaddr);
13631317
13641318 if (port > 0 && port < 65536) {
13651319 v->len = ngx_sprintf(v->data, "%ui", port) - v->data;
136136 ngx_http_v2_out_frame_t *frame;
137137 ngx_http_core_loc_conf_t *clcf;
138138 ngx_http_core_srv_conf_t *cscf;
139 struct sockaddr_in *sin;
140 #if (NGX_HAVE_INET6)
141 struct sockaddr_in6 *sin6;
142 #endif
143139 u_char addr[NGX_SOCKADDR_STRLEN];
144140
145141 static const u_char nginx[5] = "\x84\xaa\x63\x55\xe7";
279275 }
280276 }
281277
282 switch (fc->local_sockaddr->sa_family) {
283
284 #if (NGX_HAVE_INET6)
285 case AF_INET6:
286 sin6 = (struct sockaddr_in6 *) fc->local_sockaddr;
287 port = ntohs(sin6->sin6_port);
288 break;
289 #endif
290 #if (NGX_HAVE_UNIX_DOMAIN)
291 case AF_UNIX:
292 port = 0;
293 break;
294 #endif
295 default: /* AF_INET */
296 sin = (struct sockaddr_in *) fc->local_sockaddr;
297 port = ntohs(sin->sin_port);
298 break;
299 }
278 port = ngx_inet_get_port(fc->local_sockaddr);
300279
301280 location.len = sizeof("https://") - 1 + host.len
302281 + r->headers_out.location->value.len;
227227 in_port_t p;
228228 ngx_uint_t i;
229229 struct sockaddr *sa;
230 struct sockaddr_in *sin;
231230 ngx_mail_conf_port_t *port;
232231 ngx_mail_conf_addr_t *addr;
233 #if (NGX_HAVE_INET6)
234 struct sockaddr_in6 *sin6;
235 #endif
236232
237233 sa = &listen->sockaddr.sockaddr;
238
239 switch (sa->sa_family) {
240
241 #if (NGX_HAVE_INET6)
242 case AF_INET6:
243 sin6 = &listen->sockaddr.sockaddr_in6;
244 p = sin6->sin6_port;
245 break;
246 #endif
247
248 #if (NGX_HAVE_UNIX_DOMAIN)
249 case AF_UNIX:
250 p = 0;
251 break;
252 #endif
253
254 default: /* AF_INET */
255 sin = &listen->sockaddr.sockaddr_in;
256 p = sin->sin_port;
257 break;
258 }
234 p = ngx_inet_get_port(sa);
259235
260236 port = ports->elts;
261237 for (i = 0; i < ports->nelts; i++) {
461461 ngx_mail_auth_http_process_headers(ngx_mail_session_t *s,
462462 ngx_mail_auth_http_ctx_t *ctx)
463463 {
464 u_char *p;
465 time_t timer;
466 size_t len, size;
467 ngx_int_t rc, port, n;
468 ngx_addr_t *peer;
469 struct sockaddr_in *sin;
470 #if (NGX_HAVE_INET6)
471 struct sockaddr_in6 *sin6;
472 #endif
464 u_char *p;
465 time_t timer;
466 size_t len, size;
467 ngx_int_t rc, port, n;
468 ngx_addr_t *peer;
473469
474470 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, s->connection->log, 0,
475471 "mail auth http process headers");
812808 return;
813809 }
814810
815 switch (peer->sockaddr->sa_family) {
816
817 #if (NGX_HAVE_INET6)
818 case AF_INET6:
819 sin6 = (struct sockaddr_in6 *) peer->sockaddr;
820 sin6->sin6_port = htons((in_port_t) port);
821 break;
822 #endif
823
824 default: /* AF_INET */
825 sin = (struct sockaddr_in *) peer->sockaddr;
826 sin->sin_port = htons((in_port_t) port);
827 break;
828 }
811 ngx_inet_set_port(peer->sockaddr, port);
829812
830813 len = ctx->addr.len + 1 + ctx->port.len;
831814
242242 in_port_t p;
243243 ngx_uint_t i;
244244 struct sockaddr *sa;
245 struct sockaddr_in *sin;
246245 ngx_stream_conf_port_t *port;
247246 ngx_stream_conf_addr_t *addr;
248 #if (NGX_HAVE_INET6)
249 struct sockaddr_in6 *sin6;
250 #endif
251247
252248 sa = &listen->sockaddr.sockaddr;
253
254 switch (sa->sa_family) {
255
256 #if (NGX_HAVE_INET6)
257 case AF_INET6:
258 sin6 = &listen->sockaddr.sockaddr_in6;
259 p = sin6->sin6_port;
260 break;
261 #endif
262
263 #if (NGX_HAVE_UNIX_DOMAIN)
264 case AF_UNIX:
265 p = 0;
266 break;
267 #endif
268
269 default: /* AF_INET */
270 sin = &listen->sockaddr.sockaddr_in;
271 p = sin->sin_port;
272 break;
273 }
249 p = ngx_inet_get_port(sa);
274250
275251 port = ports->elts;
276252 for (i = 0; i < ports->nelts; i++) {
445445 ngx_stream_proxy_set_local(ngx_stream_session_t *s, ngx_stream_upstream_t *u,
446446 ngx_stream_upstream_local_t *local)
447447 {
448 ngx_addr_t *addr;
449 ngx_connection_t *c;
450 struct sockaddr_in *sin;
451 #if (NGX_HAVE_INET6)
452 struct sockaddr_in6 *sin6;
453 #endif
448 ngx_addr_t *addr;
449 ngx_connection_t *c;
454450
455451 if (local == NULL) {
456452 u->peer.local = NULL;
483479 }
484480
485481 ngx_memcpy(addr->sockaddr, c->sockaddr, c->socklen);
486
487 switch (addr->sockaddr->sa_family) {
488
489 case AF_INET:
490 sin = (struct sockaddr_in *) addr->sockaddr;
491 sin->sin_port = 0;
492 break;
493
494 #if (NGX_HAVE_INET6)
495 case AF_INET6:
496 sin6 = (struct sockaddr_in6 *) addr->sockaddr;
497 sin6->sin6_port = 0;
498 break;
499 #endif
500 }
482 ngx_inet_set_port(addr->sockaddr, 0);
501483
502484 addr->name = c->addr_text;
503485 u->peer.local = addr;