Klaus Demo nginx / 48a1646
Use ngx_cmp_sockaddr() where appropriate. Ruslan Ermilov 6 years ago
6 changed file(s) with 16 addition(s) and 136 deletion(s). Raw diff Collapse all Expand all
12141214 ngx_http_add_addresses(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
12151215 ngx_http_conf_port_t *port, ngx_http_listen_opt_t *lsopt)
12161216 {
1217 u_char *p;
1218 size_t len, off;
12191217 ngx_uint_t i, default_server, proxy_protocol;
1220 struct sockaddr *sa;
12211218 ngx_http_conf_addr_t *addr;
1222 #if (NGX_HAVE_UNIX_DOMAIN)
1223 struct sockaddr_un *saun;
1224 #endif
12251219 #if (NGX_HTTP_SSL)
12261220 ngx_uint_t ssl;
12271221 #endif
12341228 * may fill some fields in inherited sockaddr struct's
12351229 */
12361230
1237 sa = &lsopt->u.sockaddr;
1238
1239 switch (sa->sa_family) {
1240
1241 #if (NGX_HAVE_INET6)
1242 case AF_INET6:
1243 off = offsetof(struct sockaddr_in6, sin6_addr);
1244 len = 16;
1245 break;
1246 #endif
1247
1248 #if (NGX_HAVE_UNIX_DOMAIN)
1249 case AF_UNIX:
1250 off = offsetof(struct sockaddr_un, sun_path);
1251 len = sizeof(saun->sun_path);
1252 break;
1253 #endif
1254
1255 default: /* AF_INET */
1256 off = offsetof(struct sockaddr_in, sin_addr);
1257 len = 4;
1258 break;
1259 }
1260
1261 p = lsopt->u.sockaddr_data + off;
1262
12631231 addr = port->addrs.elts;
12641232
12651233 for (i = 0; i < port->addrs.nelts; i++) {
12661234
1267 if (ngx_memcmp(p, addr[i].opt.u.sockaddr_data + off, len) != 0) {
1235 if (ngx_cmp_sockaddr(&lsopt->u.sockaddr, lsopt->socklen,
1236 &addr[i].opt.u.sockaddr, addr[i].opt.socklen, 0)
1237 != NGX_OK)
1238 {
12681239 continue;
12691240 }
12701241
6666 #if (NGX_HAVE_UNIX_DOMAIN)
6767 struct sockaddr_un sockaddr_un;
6868 #endif
69 u_char sockaddr_data[NGX_SOCKADDRLEN];
7069 } u;
7170
7271 socklen_t socklen;
3535 #if (NGX_HAVE_UNIX_DOMAIN)
3636 struct sockaddr_un sockaddr_un;
3737 #endif
38 u_char sockaddr_data[NGX_SOCKADDRLEN];
3938 } u;
4039
4140 socklen_t socklen;
287287 {
288288 ngx_mail_core_srv_conf_t *cscf = conf;
289289
290 size_t len, off;
291 in_port_t port;
292290 ngx_str_t *value;
293291 ngx_url_t u;
294292 ngx_uint_t i, m;
295 struct sockaddr *sa;
296293 ngx_mail_listen_t *ls;
297294 ngx_mail_module_t *module;
298 struct sockaddr_in *sin;
299295 ngx_mail_core_main_conf_t *cmcf;
300 #if (NGX_HAVE_INET6)
301 struct sockaddr_in6 *sin6;
302 #endif
303296
304297 value = cf->args->elts;
305298
324317
325318 for (i = 0; i < cmcf->listen.nelts; i++) {
326319
327 sa = &ls[i].u.sockaddr;
328
329 if (sa->sa_family != u.family) {
330 continue;
331 }
332
333 switch (sa->sa_family) {
334
335 #if (NGX_HAVE_INET6)
336 case AF_INET6:
337 off = offsetof(struct sockaddr_in6, sin6_addr);
338 len = 16;
339 sin6 = &ls[i].u.sockaddr_in6;
340 port = ntohs(sin6->sin6_port);
341 break;
342 #endif
343
344 #if (NGX_HAVE_UNIX_DOMAIN)
345 case AF_UNIX:
346 off = offsetof(struct sockaddr_un, sun_path);
347 len = sizeof(((struct sockaddr_un *) sa)->sun_path);
348 port = 0;
349 break;
350 #endif
351
352 default: /* AF_INET */
353 off = offsetof(struct sockaddr_in, sin_addr);
354 len = 4;
355 sin = &ls[i].u.sockaddr_in;
356 port = ntohs(sin->sin_port);
357 break;
358 }
359
360 if (ngx_memcmp(ls[i].u.sockaddr_data + off, u.sockaddr + off, len)
361 != 0)
320 if (ngx_cmp_sockaddr(&ls[i].u.sockaddr, ls[i].socklen,
321 (struct sockaddr *) &u.sockaddr, u.socklen, 1)
322 != NGX_OK)
362323 {
363 continue;
364 }
365
366 if (port != u.port) {
367324 continue;
368325 }
369326
433390
434391 if (ngx_strncmp(value[i].data, "ipv6only=o", 10) == 0) {
435392 #if (NGX_HAVE_INET6 && defined IPV6_V6ONLY)
393 size_t len;
436394 u_char buf[NGX_SOCKADDR_STRLEN];
437395
438 sa = &ls->u.sockaddr;
439
440 if (sa->sa_family == AF_INET6) {
396 if (ls->u.sockaddr.sa_family == AF_INET6) {
441397
442398 if (ngx_strcmp(&value[i].data[10], "n") == 0) {
443399 ls->ipv6only = 1;
455411 ls->bind = 1;
456412
457413 } else {
458 len = ngx_sock_ntop(sa, ls->socklen, buf,
414 len = ngx_sock_ntop(&ls->u.sockaddr, ls->socklen, buf,
459415 NGX_SOCKADDR_STRLEN, 1);
460416
461417 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3939 #if (NGX_HAVE_UNIX_DOMAIN)
4040 struct sockaddr_un sockaddr_un;
4141 #endif
42 u_char sockaddr_data[NGX_SOCKADDRLEN];
4342 } u;
4443
4544 socklen_t socklen;
247247 static char *
248248 ngx_stream_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
249249 {
250 size_t len, off;
251 in_port_t port;
252250 ngx_str_t *value;
253251 ngx_url_t u;
254252 ngx_uint_t i, backlog;
255 struct sockaddr *sa;
256 struct sockaddr_in *sin;
257253 ngx_stream_listen_t *ls;
258254 ngx_stream_core_main_conf_t *cmcf;
259 #if (NGX_HAVE_INET6)
260 struct sockaddr_in6 *sin6;
261 #endif
262255
263256 value = cf->args->elts;
264257
283276
284277 for (i = 0; i < cmcf->listen.nelts; i++) {
285278
286 sa = &ls[i].u.sockaddr;
287
288 if (sa->sa_family != u.family) {
289 continue;
290 }
291
292 switch (sa->sa_family) {
293
294 #if (NGX_HAVE_INET6)
295 case AF_INET6:
296 off = offsetof(struct sockaddr_in6, sin6_addr);
297 len = 16;
298 sin6 = &ls[i].u.sockaddr_in6;
299 port = sin6->sin6_port;
300 break;
301 #endif
302
303 #if (NGX_HAVE_UNIX_DOMAIN)
304 case AF_UNIX:
305 off = offsetof(struct sockaddr_un, sun_path);
306 len = sizeof(((struct sockaddr_un *) sa)->sun_path);
307 port = 0;
308 break;
309 #endif
310
311 default: /* AF_INET */
312 off = offsetof(struct sockaddr_in, sin_addr);
313 len = 4;
314 sin = &ls[i].u.sockaddr_in;
315 port = sin->sin_port;
316 break;
317 }
318
319 if (ngx_memcmp(ls[i].u.sockaddr_data + off, u.sockaddr + off, len)
320 != 0)
279 if (ngx_cmp_sockaddr(&ls[i].u.sockaddr, ls[i].socklen,
280 (struct sockaddr *) &u.sockaddr, u.socklen, 1)
281 != NGX_OK)
321282 {
322 continue;
323 }
324
325 if (port != u.port) {
326283 continue;
327284 }
328285
383340
384341 if (ngx_strncmp(value[i].data, "ipv6only=o", 10) == 0) {
385342 #if (NGX_HAVE_INET6 && defined IPV6_V6ONLY)
343 size_t len;
386344 u_char buf[NGX_SOCKADDR_STRLEN];
387345
388 sa = &ls->u.sockaddr;
389
390 if (sa->sa_family == AF_INET6) {
346 if (ls->u.sockaddr.sa_family == AF_INET6) {
391347
392348 if (ngx_strcmp(&value[i].data[10], "n") == 0) {
393349 ls->ipv6only = 1;
405361 ls->bind = 1;
406362
407363 } else {
408 len = ngx_sock_ntop(sa, ls->socklen, buf,
364 len = ngx_sock_ntop(&ls->u.sockaddr, ls->socklen, buf,
409365 NGX_SOCKADDR_STRLEN, 1);
410366
411367 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,