comparison src/http/ngx_http.c @ 2512:2e91aecb9e57

a prelimiary IPv6 support, HTTP listen
author Igor Sysoev <igor@sysoev.ru>
date Sat, 21 Feb 2009 07:02:02 +0000
parents f91c5c964e61
children 5d53ad3a8319
comparison
equal deleted inserted replaced
2511:f91c5c964e61 2512:2e91aecb9e57
18 ngx_http_core_main_conf_t *cmcf); 18 ngx_http_core_main_conf_t *cmcf);
19 19
20 static ngx_int_t ngx_http_init_server_lists(ngx_conf_t *cf, 20 static ngx_int_t ngx_http_init_server_lists(ngx_conf_t *cf,
21 ngx_array_t *servers, ngx_array_t *in_ports); 21 ngx_array_t *servers, ngx_array_t *in_ports);
22 static ngx_int_t ngx_http_add_ports(ngx_conf_t *cf, 22 static ngx_int_t ngx_http_add_ports(ngx_conf_t *cf,
23 ngx_http_core_srv_conf_t *cscf, ngx_array_t *in_ports, 23 ngx_http_core_srv_conf_t *cscf, ngx_array_t *ports,
24 ngx_http_listen_t *listen); 24 ngx_http_listen_t *listen);
25 static ngx_int_t ngx_http_add_addresses(ngx_conf_t *cf, 25 static ngx_int_t ngx_http_add_addresses(ngx_conf_t *cf,
26 ngx_http_core_srv_conf_t *cscf, ngx_http_conf_in_port_t *in_port, 26 ngx_http_core_srv_conf_t *cscf, ngx_http_conf_port_t *port,
27 ngx_http_listen_t *listen); 27 ngx_http_listen_t *listen);
28 static ngx_int_t ngx_http_add_address(ngx_conf_t *cf, 28 static ngx_int_t ngx_http_add_address(ngx_conf_t *cf,
29 ngx_http_core_srv_conf_t *cscf, ngx_http_conf_in_port_t *in_port, 29 ngx_http_core_srv_conf_t *cscf, ngx_http_conf_port_t *port,
30 ngx_http_listen_t *listen); 30 ngx_http_listen_t *listen);
31 static ngx_int_t ngx_http_add_names(ngx_conf_t *cf, 31 static ngx_int_t ngx_http_add_names(ngx_conf_t *cf,
32 ngx_http_core_srv_conf_t *cscf, ngx_http_conf_in_addr_t *in_addr); 32 ngx_http_core_srv_conf_t *cscf, ngx_http_conf_addr_t *addr);
33 33
34 static char *ngx_http_merge_locations(ngx_conf_t *cf, 34 static char *ngx_http_merge_locations(ngx_conf_t *cf,
35 ngx_queue_t *locations, void **loc_conf, ngx_http_module_t *module, 35 ngx_queue_t *locations, void **loc_conf, ngx_http_module_t *module,
36 ngx_uint_t ctx_index); 36 ngx_uint_t ctx_index);
37 static ngx_int_t ngx_http_init_locations(ngx_conf_t *cf, 37 static ngx_int_t ngx_http_init_locations(ngx_conf_t *cf,
47 static ngx_http_location_tree_node_t * 47 static ngx_http_location_tree_node_t *
48 ngx_http_create_locations_tree(ngx_conf_t *cf, ngx_queue_t *locations, 48 ngx_http_create_locations_tree(ngx_conf_t *cf, ngx_queue_t *locations,
49 size_t prefix); 49 size_t prefix);
50 50
51 static ngx_int_t ngx_http_optimize_servers(ngx_conf_t *cf, 51 static ngx_int_t ngx_http_optimize_servers(ngx_conf_t *cf,
52 ngx_http_core_main_conf_t *cmcf, ngx_array_t *in_ports); 52 ngx_http_core_main_conf_t *cmcf, ngx_array_t *ports);
53 static ngx_int_t ngx_http_server_names(ngx_conf_t *cf, 53 static ngx_int_t ngx_http_server_names(ngx_conf_t *cf,
54 ngx_http_core_main_conf_t *cmcf, ngx_http_conf_in_addr_t *in_addr); 54 ngx_http_core_main_conf_t *cmcf, ngx_http_conf_addr_t *addr);
55 static ngx_int_t ngx_http_cmp_conf_in_addrs(const void *one, const void *two); 55 static ngx_int_t ngx_http_cmp_conf_addrs(const void *one, const void *two);
56 static int ngx_libc_cdecl ngx_http_cmp_dns_wildcards(const void *one, 56 static int ngx_libc_cdecl ngx_http_cmp_dns_wildcards(const void *one,
57 const void *two); 57 const void *two);
58 58
59 static ngx_int_t ngx_http_init_listening(ngx_conf_t *cf, 59 static ngx_int_t ngx_http_init_listening(ngx_conf_t *cf,
60 ngx_http_conf_in_port_t *in_port); 60 ngx_http_conf_port_t *port);
61 static ngx_listening_t *ngx_http_add_listening(ngx_conf_t *cf,
62 ngx_http_conf_addr_t *addr);
63 static ngx_int_t ngx_http_add_addrs(ngx_conf_t *cf, ngx_http_port_t *hport,
64 ngx_http_conf_addr_t *addr);
65 #if (NGX_HAVE_INET6)
66 static ngx_int_t ngx_http_add_addrs6(ngx_conf_t *cf, ngx_http_port_t *hport,
67 ngx_http_conf_addr_t *addr);
68 #endif
61 69
62 ngx_uint_t ngx_http_max_module; 70 ngx_uint_t ngx_http_max_module;
63 71
64 72
65 ngx_int_t (*ngx_http_top_header_filter) (ngx_http_request_t *r); 73 ngx_int_t (*ngx_http_top_header_filter) (ngx_http_request_t *r);
357 /* 365 /*
358 * create the lists of ports, addresses and server names 366 * create the lists of ports, addresses and server names
359 * to find quickly the server core module configuration at run-time 367 * to find quickly the server core module configuration at run-time
360 */ 368 */
361 369
362 /* AF_INET only */
363
364 if (ngx_http_init_server_lists(cf, &cmcf->servers, &in_ports) != NGX_OK) { 370 if (ngx_http_init_server_lists(cf, &cmcf->servers, &in_ports) != NGX_OK) {
365 return NGX_CONF_ERROR; 371 return NGX_CONF_ERROR;
366 } 372 }
367 373
368 374
369 /* optimize the lists of ports, addresses and server names */ 375 /* optimize the lists of ports, addresses and server names */
370
371 /* AF_INET only */
372 376
373 if (ngx_http_optimize_servers(cf, cmcf, &in_ports) != NGX_OK) { 377 if (ngx_http_optimize_servers(cf, cmcf, &in_ports) != NGX_OK) {
374 return NGX_CONF_ERROR; 378 return NGX_CONF_ERROR;
375 } 379 }
376 380
1105 } 1109 }
1106 1110
1107 1111
1108 static ngx_int_t 1112 static ngx_int_t
1109 ngx_http_init_server_lists(ngx_conf_t *cf, ngx_array_t *servers, 1113 ngx_http_init_server_lists(ngx_conf_t *cf, ngx_array_t *servers,
1110 ngx_array_t *in_ports) 1114 ngx_array_t *ports)
1111 { 1115 {
1112 ngx_uint_t s, i; 1116 ngx_uint_t s, i;
1113 ngx_http_listen_t *listen; 1117 ngx_http_listen_t *listen;
1114 ngx_http_core_srv_conf_t **cscfp; 1118 ngx_http_core_srv_conf_t **cscfp;
1115 1119
1116 if (ngx_array_init(in_ports, cf->temp_pool, 2, 1120 if (ngx_array_init(ports, cf->temp_pool, 2, sizeof(ngx_http_conf_port_t))
1117 sizeof(ngx_http_conf_in_port_t))
1118 != NGX_OK) 1121 != NGX_OK)
1119 { 1122 {
1120 return NGX_ERROR; 1123 return NGX_ERROR;
1121 } 1124 }
1122 1125
1128 /* "listen" directives */ 1131 /* "listen" directives */
1129 1132
1130 listen = cscfp[s]->listen.elts; 1133 listen = cscfp[s]->listen.elts;
1131 for (i = 0; i < cscfp[s]->listen.nelts; i++) { 1134 for (i = 0; i < cscfp[s]->listen.nelts; i++) {
1132 1135
1133 /* AF_INET only */ 1136 if (ngx_http_add_ports(cf, cscfp[s], ports, &listen[i]) != NGX_OK) {
1134
1135 if (ngx_http_add_ports(cf, cscfp[s], in_ports, &listen[i])
1136 != NGX_OK)
1137 {
1138 return NGX_ERROR; 1137 return NGX_ERROR;
1139 } 1138 }
1140 } 1139 }
1141 } 1140 }
1142 1141
1143 return NGX_OK; 1142 return NGX_OK;
1144 } 1143 }
1145 1144
1146
1147 /* AF_INET only */
1148 1145
1149 static ngx_int_t 1146 static ngx_int_t
1150 ngx_http_add_ports(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf, 1147 ngx_http_add_ports(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
1151 ngx_array_t *in_ports, ngx_http_listen_t *listen) 1148 ngx_array_t *ports, ngx_http_listen_t *listen)
1152 { 1149 {
1150 in_port_t p;
1153 ngx_uint_t i; 1151 ngx_uint_t i;
1154 ngx_http_conf_in_port_t *in_port; 1152 struct sockaddr *sa;
1155 1153 struct sockaddr_in *sin;
1156 in_port = in_ports->elts; 1154 ngx_http_conf_port_t *port;
1157 for (i = 0; i < in_ports->nelts; i++) { 1155 #if (NGX_HAVE_INET6)
1158 1156 struct sockaddr_in6 *sin6;
1159 if (listen->port != in_port[i].port) { 1157 #endif
1158
1159 sa = (struct sockaddr *) &listen->sockaddr;
1160
1161 switch (sa->sa_family) {
1162
1163 #if (NGX_HAVE_INET6)
1164 case AF_INET6:
1165 sin6 = (struct sockaddr_in6 *) sa;
1166 p = sin6->sin6_port;
1167 break;
1168 #endif
1169
1170 default: /* AF_INET */
1171 sin = (struct sockaddr_in *) sa;
1172 p = sin->sin_port;
1173 break;
1174 }
1175
1176 port = ports->elts;
1177 for (i = 0; i < ports->nelts; i++) {
1178
1179 if (p != port[i].port || sa->sa_family != port[i].family) {
1160 continue; 1180 continue;
1161 } 1181 }
1162 1182
1163 /* a port is already in the in_port list */ 1183 /* a port is already in the in_port list */
1164 1184
1165 return ngx_http_add_addresses(cf, cscf, &in_port[i], listen); 1185 return ngx_http_add_addresses(cf, cscf, &port[i], listen);
1166 } 1186 }
1167 1187
1168 /* add a port to the in_port list */ 1188 /* add a port to the in_port list */
1169 1189
1170 in_port = ngx_array_push(in_ports); 1190 port = ngx_array_push(ports);
1171 if (in_port == NULL) { 1191 if (port == NULL) {
1172 return NGX_ERROR; 1192 return NGX_ERROR;
1173 } 1193 }
1174 1194
1175 in_port->port = listen->port; 1195 port->family = sa->sa_family;
1176 in_port->addrs.elts = NULL; 1196 port->port = p;
1177 1197 port->addrs.elts = NULL;
1178 return ngx_http_add_address(cf, cscf, in_port, listen); 1198
1199 return ngx_http_add_address(cf, cscf, port, listen);
1179 } 1200 }
1180 1201
1181 1202
1182 static ngx_int_t 1203 static ngx_int_t
1183 ngx_http_add_addresses(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf, 1204 ngx_http_add_addresses(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
1184 ngx_http_conf_in_port_t *in_port, ngx_http_listen_t *listen) 1205 ngx_http_conf_port_t *port, ngx_http_listen_t *listen)
1185 { 1206 {
1186 ngx_uint_t i; 1207 u_char *p;
1187 ngx_http_conf_in_addr_t *in_addr; 1208 size_t len, off;
1188 1209 ngx_uint_t i;
1189 in_addr = in_port->addrs.elts; 1210 struct sockaddr *sa;
1190 1211 ngx_http_conf_addr_t *addr;
1191 for (i = 0; i < in_port->addrs.nelts; i++) { 1212
1192 1213 /*
1193 if (listen->addr != in_addr[i].addr) { 1214 * we can not compare whole sockaddr struct's as kernel
1215 * may fill some fields in inherited sockaddr struct's
1216 */
1217
1218 sa = (struct sockaddr *) &listen->sockaddr;
1219
1220 switch (sa->sa_family) {
1221
1222 #if (NGX_HAVE_INET6)
1223 case AF_INET6:
1224 off = offsetof(struct sockaddr_in6, sin6_addr);
1225 len = 16;
1226 break;
1227 #endif
1228
1229 default: /* AF_INET */
1230 off = offsetof(struct sockaddr_in, sin_addr);
1231 len = 4;
1232 break;
1233 }
1234
1235 p = listen->sockaddr + off;
1236
1237 addr = port->addrs.elts;
1238
1239 for (i = 0; i < port->addrs.nelts; i++) {
1240
1241 if (ngx_memcmp(p, (u_char *) addr[i].sockaddr + off, len) != 0) {
1194 continue; 1242 continue;
1195 } 1243 }
1196 1244
1197 /* the address is already in the address list */ 1245 /* the address is already in the address list */
1198 1246
1199 if (ngx_http_add_names(cf, cscf, &in_addr[i]) != NGX_OK) { 1247 if (ngx_http_add_names(cf, cscf, &addr[i]) != NGX_OK) {
1200 return NGX_ERROR; 1248 return NGX_ERROR;
1201 } 1249 }
1202 1250
1203 /* 1251 /* check the duplicate "default" server for this address:port */
1204 * check the duplicate "default" server
1205 * for this address:port
1206 */
1207 1252
1208 if (listen->conf.default_server) { 1253 if (listen->conf.default_server) {
1209 1254
1210 if (in_addr[i].default_server) { 1255 if (addr[i].default_server) {
1211 ngx_log_error(NGX_LOG_ERR, cf->log, 0, 1256 ngx_log_error(NGX_LOG_ERR, cf->log, 0,
1212 "the duplicate default server in %s:%ui", 1257 "the duplicate default server in %s:%ui",
1213 listen->file_name, listen->line); 1258 listen->file_name, listen->line);
1214 1259
1215 return NGX_ERROR; 1260 return NGX_ERROR;
1216 } 1261 }
1217 1262
1218 in_addr[i].core_srv_conf = cscf; 1263 addr[i].core_srv_conf = cscf;
1219 in_addr[i].default_server = 1; 1264 addr[i].default_server = 1;
1220 #if (NGX_HTTP_SSL) 1265 #if (NGX_HTTP_SSL)
1221 in_addr[i].ssl = listen->conf.ssl; 1266 addr[i].ssl = listen->conf.ssl;
1222 #endif 1267 #endif
1223 in_addr[i].listen_conf = &listen->conf; 1268 addr[i].listen_conf = &listen->conf;
1224 } 1269 }
1225 1270
1226 return NGX_OK; 1271 return NGX_OK;
1227 } 1272 }
1228 1273
1229 /* 1274 /* add the address to the addresses list that bound to this port */
1230 * add the address to the addresses list that 1275
1231 * bound to this port 1276 return ngx_http_add_address(cf, cscf, port, listen);
1232 */
1233
1234 return ngx_http_add_address(cf, cscf, in_port, listen);
1235 } 1277 }
1236 1278
1237 1279
1238 /* 1280 /*
1239 * add the server address, the server names and the server core module 1281 * add the server address, the server names and the server core module
1240 * configurations to the port (in_port) 1282 * configurations to the port (in_port)
1241 */ 1283 */
1242 1284
1243 static ngx_int_t 1285 static ngx_int_t
1244 ngx_http_add_address(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf, 1286 ngx_http_add_address(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
1245 ngx_http_conf_in_port_t *in_port, ngx_http_listen_t *listen) 1287 ngx_http_conf_port_t *port, ngx_http_listen_t *listen)
1246 { 1288 {
1247 ngx_http_conf_in_addr_t *in_addr; 1289 ngx_http_conf_addr_t *addr;
1248 1290
1249 if (in_port->addrs.elts == NULL) { 1291 if (port->addrs.elts == NULL) {
1250 if (ngx_array_init(&in_port->addrs, cf->temp_pool, 4, 1292 if (ngx_array_init(&port->addrs, cf->temp_pool, 4,
1251 sizeof(ngx_http_conf_in_addr_t)) 1293 sizeof(ngx_http_conf_addr_t))
1252 != NGX_OK) 1294 != NGX_OK)
1253 { 1295 {
1254 return NGX_ERROR; 1296 return NGX_ERROR;
1255 } 1297 }
1256 } 1298 }
1257 1299
1258 in_addr = ngx_array_push(&in_port->addrs); 1300 addr = ngx_array_push(&port->addrs);
1259 if (in_addr == NULL) { 1301 if (addr == NULL) {
1260 return NGX_ERROR; 1302 return NGX_ERROR;
1261 } 1303 }
1262 1304
1263 in_addr->addr = listen->addr; 1305 addr->sockaddr = (struct sockaddr *) &listen->sockaddr;
1264 in_addr->hash.buckets = NULL; 1306 addr->socklen = listen->socklen;
1265 in_addr->hash.size = 0; 1307 addr->hash.buckets = NULL;
1266 in_addr->wc_head = NULL; 1308 addr->hash.size = 0;
1267 in_addr->wc_tail = NULL; 1309 addr->wc_head = NULL;
1268 in_addr->names.elts = NULL; 1310 addr->wc_tail = NULL;
1311 addr->names.elts = NULL;
1269 #if (NGX_PCRE) 1312 #if (NGX_PCRE)
1270 in_addr->nregex = 0; 1313 addr->nregex = 0;
1271 in_addr->regex = NULL; 1314 addr->regex = NULL;
1272 #endif 1315 #endif
1273 in_addr->core_srv_conf = cscf; 1316 addr->core_srv_conf = cscf;
1274 in_addr->default_server = listen->conf.default_server; 1317 addr->default_server = listen->conf.default_server;
1275 in_addr->bind = listen->conf.bind; 1318 addr->bind = listen->conf.bind;
1319 addr->wildcard = listen->conf.wildcard;
1276 #if (NGX_HTTP_SSL) 1320 #if (NGX_HTTP_SSL)
1277 in_addr->ssl = listen->conf.ssl; 1321 addr->ssl = listen->conf.ssl;
1278 #endif 1322 #endif
1279 in_addr->listen_conf = &listen->conf; 1323 addr->listen_conf = &listen->conf;
1280 1324
1281 return ngx_http_add_names(cf, cscf, in_addr); 1325 return ngx_http_add_names(cf, cscf, addr);
1282 } 1326 }
1283 1327
1284 1328
1285 /* 1329 /*
1286 * add the server names and the server core module 1330 * add the server names and the server core module
1287 * configurations to the address:port (in_addr) 1331 * configurations to the address:port
1288 */ 1332 */
1289 1333
1290 static ngx_int_t 1334 static ngx_int_t
1291 ngx_http_add_names(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf, 1335 ngx_http_add_names(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
1292 ngx_http_conf_in_addr_t *in_addr) 1336 ngx_http_conf_addr_t *addr)
1293 { 1337 {
1294 ngx_uint_t i; 1338 ngx_uint_t i;
1295 ngx_http_server_name_t *server_names, *name; 1339 ngx_http_server_name_t *server_names, *name;
1296 1340
1297 if (in_addr->names.elts == NULL) { 1341 if (addr->names.elts == NULL) {
1298 if (ngx_array_init(&in_addr->names, cf->temp_pool, 4, 1342 if (ngx_array_init(&addr->names, cf->temp_pool, 4,
1299 sizeof(ngx_http_server_name_t)) 1343 sizeof(ngx_http_server_name_t))
1300 != NGX_OK) 1344 != NGX_OK)
1301 { 1345 {
1302 return NGX_ERROR; 1346 return NGX_ERROR;
1303 } 1347 }
1311 server_names[i].name.len); 1355 server_names[i].name.len);
1312 1356
1313 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0, 1357 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0,
1314 "name: %V", &server_names[i].name); 1358 "name: %V", &server_names[i].name);
1315 1359
1316 name = ngx_array_push(&in_addr->names); 1360 name = ngx_array_push(&addr->names);
1317 if (name == NULL) { 1361 if (name == NULL) {
1318 return NGX_ERROR; 1362 return NGX_ERROR;
1319 } 1363 }
1320 1364
1321 *name = server_names[i]; 1365 *name = server_names[i];
1325 } 1369 }
1326 1370
1327 1371
1328 static ngx_int_t 1372 static ngx_int_t
1329 ngx_http_optimize_servers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf, 1373 ngx_http_optimize_servers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf,
1330 ngx_array_t *in_ports) 1374 ngx_array_t *ports)
1331 { 1375 {
1332 ngx_uint_t s, p, a; 1376 ngx_uint_t s, p, a;
1377 ngx_http_conf_port_t *port;
1378 ngx_http_conf_addr_t *addr;
1333 ngx_http_server_name_t *name; 1379 ngx_http_server_name_t *name;
1334 ngx_http_conf_in_port_t *in_port; 1380
1335 ngx_http_conf_in_addr_t *in_addr; 1381 port = ports->elts;
1336 1382 for (p = 0; p < ports->nelts; p++) {
1337 in_port = in_ports->elts; 1383
1338 for (p = 0; p < in_ports->nelts; p++) { 1384 ngx_sort(port[p].addrs.elts, (size_t) port[p].addrs.nelts,
1339 1385 sizeof(ngx_http_conf_addr_t), ngx_http_cmp_conf_addrs);
1340 ngx_sort(in_port[p].addrs.elts, (size_t) in_port[p].addrs.nelts,
1341 sizeof(ngx_http_conf_in_addr_t), ngx_http_cmp_conf_in_addrs);
1342 1386
1343 /* 1387 /*
1344 * check whether all name-based servers have the same 1388 * check whether all name-based servers have the same
1345 * configuraiton as a default server for given address:port 1389 * configuraiton as a default server for given address:port
1346 */ 1390 */
1347 1391
1348 in_addr = in_port[p].addrs.elts; 1392 addr = port[p].addrs.elts;
1349 for (a = 0; a < in_port[p].addrs.nelts; a++) { 1393 for (a = 0; a < port[p].addrs.nelts; a++) {
1350 1394
1351 name = in_addr[a].names.elts; 1395 name = addr[a].names.elts;
1352 for (s = 0; s < in_addr[a].names.nelts; s++) { 1396 for (s = 0; s < addr[a].names.nelts; s++) {
1353 1397
1354 if (in_addr[a].core_srv_conf == name[s].core_srv_conf) { 1398 if (addr[a].core_srv_conf == name[s].core_srv_conf) {
1355 continue; 1399 continue;
1356 } 1400 }
1357 1401
1358 if (ngx_http_server_names(cf, cmcf, &in_addr[a]) != NGX_OK) { 1402 if (ngx_http_server_names(cf, cmcf, &addr[a]) != NGX_OK) {
1359 return NGX_ERROR; 1403 return NGX_ERROR;
1360 } 1404 }
1361 1405
1362 break; 1406 break;
1363 } 1407 }
1364 } 1408 }
1365 1409
1366 if (ngx_http_init_listening(cf, &in_port[p]) != NGX_OK) { 1410 if (ngx_http_init_listening(cf, &port[p]) != NGX_OK) {
1367 return NGX_ERROR; 1411 return NGX_ERROR;
1368 } 1412 }
1369 } 1413 }
1370 1414
1371 return NGX_OK; 1415 return NGX_OK;
1372 } 1416 }
1373 1417
1374 1418
1375 static ngx_int_t 1419 static ngx_int_t
1376 ngx_http_server_names(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf, 1420 ngx_http_server_names(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf,
1377 ngx_http_conf_in_addr_t *in_addr) 1421 ngx_http_conf_addr_t *addr)
1378 { 1422 {
1379 ngx_int_t rc; 1423 ngx_int_t rc;
1380 ngx_uint_t s; 1424 ngx_uint_t s;
1381 ngx_hash_init_t hash; 1425 ngx_hash_init_t hash;
1382 ngx_http_server_name_t *name; 1426 ngx_http_server_name_t *name;
1398 1442
1399 if (ngx_hash_keys_array_init(&ha, NGX_HASH_LARGE) != NGX_OK) { 1443 if (ngx_hash_keys_array_init(&ha, NGX_HASH_LARGE) != NGX_OK) {
1400 goto failed; 1444 goto failed;
1401 } 1445 }
1402 1446
1403 name = in_addr->names.elts; 1447 name = addr->names.elts;
1404 1448
1405 for (s = 0; s < in_addr->names.nelts; s++) { 1449 for (s = 0; s < addr->names.nelts; s++) {
1406 1450
1407 #if (NGX_PCRE) 1451 #if (NGX_PCRE)
1408 if (name[s].regex) { 1452 if (name[s].regex) {
1409 regex++; 1453 regex++;
1410 continue; 1454 continue;
1419 } 1463 }
1420 1464
1421 if (rc == NGX_DECLINED) { 1465 if (rc == NGX_DECLINED) {
1422 ngx_log_error(NGX_LOG_EMERG, cf->log, 0, 1466 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
1423 "invalid server name or wildcard \"%V\" on %s", 1467 "invalid server name or wildcard \"%V\" on %s",
1424 &name[s].name, in_addr->listen_conf->addr); 1468 &name[s].name, addr->listen_conf->addr);
1425 return NGX_ERROR; 1469 return NGX_ERROR;
1426 } 1470 }
1427 1471
1428 if (rc == NGX_BUSY) { 1472 if (rc == NGX_BUSY) {
1429 ngx_log_error(NGX_LOG_WARN, cf->log, 0, 1473 ngx_log_error(NGX_LOG_WARN, cf->log, 0,
1430 "conflicting server name \"%V\" on %s, ignored", 1474 "conflicting server name \"%V\" on %s, ignored",
1431 &name[s].name, in_addr->listen_conf->addr); 1475 &name[s].name, addr->listen_conf->addr);
1432 } 1476 }
1433 } 1477 }
1434 1478
1435 hash.key = ngx_hash_key_lc; 1479 hash.key = ngx_hash_key_lc;
1436 hash.max_size = cmcf->server_names_hash_max_size; 1480 hash.max_size = cmcf->server_names_hash_max_size;
1437 hash.bucket_size = cmcf->server_names_hash_bucket_size; 1481 hash.bucket_size = cmcf->server_names_hash_bucket_size;
1438 hash.name = "server_names_hash"; 1482 hash.name = "server_names_hash";
1439 hash.pool = cf->pool; 1483 hash.pool = cf->pool;
1440 1484
1441 if (ha.keys.nelts) { 1485 if (ha.keys.nelts) {
1442 hash.hash = &in_addr->hash; 1486 hash.hash = &addr->hash;
1443 hash.temp_pool = NULL; 1487 hash.temp_pool = NULL;
1444 1488
1445 if (ngx_hash_init(&hash, ha.keys.elts, ha.keys.nelts) != NGX_OK) { 1489 if (ngx_hash_init(&hash, ha.keys.elts, ha.keys.nelts) != NGX_OK) {
1446 goto failed; 1490 goto failed;
1447 } 1491 }
1460 != NGX_OK) 1504 != NGX_OK)
1461 { 1505 {
1462 goto failed; 1506 goto failed;
1463 } 1507 }
1464 1508
1465 in_addr->wc_head = (ngx_hash_wildcard_t *) hash.hash; 1509 addr->wc_head = (ngx_hash_wildcard_t *) hash.hash;
1466 } 1510 }
1467 1511
1468 if (ha.dns_wc_tail.nelts) { 1512 if (ha.dns_wc_tail.nelts) {
1469 1513
1470 ngx_qsort(ha.dns_wc_tail.elts, (size_t) ha.dns_wc_tail.nelts, 1514 ngx_qsort(ha.dns_wc_tail.elts, (size_t) ha.dns_wc_tail.nelts,
1478 != NGX_OK) 1522 != NGX_OK)
1479 { 1523 {
1480 goto failed; 1524 goto failed;
1481 } 1525 }
1482 1526
1483 in_addr->wc_tail = (ngx_hash_wildcard_t *) hash.hash; 1527 addr->wc_tail = (ngx_hash_wildcard_t *) hash.hash;
1484 } 1528 }
1485 1529
1486 ngx_destroy_pool(ha.temp_pool); 1530 ngx_destroy_pool(ha.temp_pool);
1487 1531
1488 #if (NGX_PCRE) 1532 #if (NGX_PCRE)
1489 1533
1490 if (regex == 0) { 1534 if (regex == 0) {
1491 return NGX_OK; 1535 return NGX_OK;
1492 } 1536 }
1493 1537
1494 in_addr->nregex = regex; 1538 addr->nregex = regex;
1495 in_addr->regex = ngx_palloc(cf->pool, 1539 addr->regex = ngx_palloc(cf->pool, regex * sizeof(ngx_http_server_name_t));
1496 regex * sizeof(ngx_http_server_name_t)); 1540 if (addr->regex == NULL) {
1497 if (in_addr->regex == NULL) {
1498 return NGX_ERROR; 1541 return NGX_ERROR;
1499 } 1542 }
1500 1543
1501 for (i = 0, s = 0; s < in_addr->names.nelts; s++) { 1544 for (i = 0, s = 0; s < addr->names.nelts; s++) {
1502 if (name[s].regex) { 1545 if (name[s].regex) {
1503 in_addr->regex[i++] = name[s]; 1546 addr->regex[i++] = name[s];
1504 } 1547 }
1505 } 1548 }
1506 1549
1507 #endif 1550 #endif
1508 1551
1515 return NGX_ERROR; 1558 return NGX_ERROR;
1516 } 1559 }
1517 1560
1518 1561
1519 static ngx_int_t 1562 static ngx_int_t
1520 ngx_http_cmp_conf_in_addrs(const void *one, const void *two) 1563 ngx_http_cmp_conf_addrs(const void *one, const void *two)
1521 { 1564 {
1522 ngx_http_conf_in_addr_t *first, *second; 1565 ngx_http_conf_addr_t *first, *second;
1523 1566
1524 first = (ngx_http_conf_in_addr_t *) one; 1567 first = (ngx_http_conf_addr_t *) one;
1525 second = (ngx_http_conf_in_addr_t *) two; 1568 second = (ngx_http_conf_addr_t *) two;
1526 1569
1527 if (first->addr == INADDR_ANY) { 1570 if (first->wildcard) {
1528 /* the INADDR_ANY must be the last resort, shift it to the end */ 1571 /* a wildcard address must be the last resort, shift it to the end */
1529 return 1; 1572 return 1;
1530 } 1573 }
1531 1574
1532 if (first->bind && !second->bind) { 1575 if (first->bind && !second->bind) {
1533 /* shift explicit bind()ed addresses to the start */ 1576 /* shift explicit bind()ed addresses to the start */
1556 return ngx_strcmp(first->key.data, second->key.data); 1599 return ngx_strcmp(first->key.data, second->key.data);
1557 } 1600 }
1558 1601
1559 1602
1560 static ngx_int_t 1603 static ngx_int_t
1561 ngx_http_init_listening(ngx_conf_t *cf, ngx_http_conf_in_port_t *in_port) 1604 ngx_http_init_listening(ngx_conf_t *cf, ngx_http_conf_port_t *port)
1562 { 1605 {
1563 ngx_uint_t i, a, last, bind_all, done; 1606 ngx_uint_t i, a, last, bind_wildcard;
1564 ngx_listening_t *ls; 1607 ngx_listening_t *ls;
1565 ngx_http_in_port_t *hip; 1608 ngx_http_port_t *hport;
1566 ngx_http_conf_in_addr_t *in_addr; 1609 ngx_http_conf_addr_t *addr;
1567 ngx_http_virtual_names_t *vn; 1610
1611 addr = port->addrs.elts;
1612 last = port->addrs.nelts;
1613
1614 /*
1615 * If there is a binding to an "*:port" then we need to bind() to
1616 * the "*:port" only and ignore other implicit bindings. The bindings
1617 * have been already sorted: explicit bindings are on the start, then
1618 * implicit bindings go, and wildcard binding is in the end.
1619 */
1620
1621 if (addr[last - 1].wildcard) {
1622 addr[last - 1].bind = 1;
1623 bind_wildcard = 1;
1624
1625 } else {
1626 bind_wildcard = 0;
1627 }
1628
1629 a = 0;
1630
1631 while (a < last) {
1632
1633 if (bind_wildcard && !addr[a].bind) {
1634 a++;
1635 continue;
1636 }
1637
1638 ls = ngx_http_add_listening(cf, &addr[a]);
1639 if (ls == NULL) {
1640 return NGX_ERROR;
1641 }
1642
1643 hport = ngx_pcalloc(cf->pool, sizeof(ngx_http_port_t));
1644 if (hport == NULL) {
1645 return NGX_ERROR;
1646 }
1647
1648 ls->servers = hport;
1649
1650 hport->port = ntohs(port->port);
1651
1652 for (i = ls->addr_text.len - 1; i; i--) {
1653
1654 if (ls->addr_text.data[i] == ':') {
1655 hport->port_text.len = ls->addr_text.len - i;
1656 hport->port_text.data = &ls->addr_text.data[i];
1657 break;
1658 }
1659 }
1660
1661 if (a == last - 1) {
1662 hport->naddrs = last;
1663
1664 } else {
1665 hport->naddrs = 1;
1666 a = 0;
1667 }
1668
1669 switch (ls->sockaddr->sa_family) {
1670
1671 #if (NGX_HAVE_INET6)
1672 case AF_INET6:
1673 if (ngx_http_add_addrs6(cf, hport, addr) != NGX_OK) {
1674 return NGX_ERROR;
1675 }
1676 break;
1677 #endif
1678 default: /* AF_INET */
1679 if (ngx_http_add_addrs(cf, hport, addr) != NGX_OK) {
1680 return NGX_ERROR;
1681 }
1682 break;
1683 }
1684
1685 addr++;
1686 last--;
1687 }
1688
1689 return NGX_OK;
1690 }
1691
1692
1693 static ngx_listening_t *
1694 ngx_http_add_listening(ngx_conf_t *cf, ngx_http_conf_addr_t *addr)
1695 {
1696 ngx_listening_t *ls;
1697 struct sockaddr *sa;
1568 ngx_http_core_loc_conf_t *clcf; 1698 ngx_http_core_loc_conf_t *clcf;
1569 ngx_http_core_srv_conf_t *cscf; 1699 ngx_http_core_srv_conf_t *cscf;
1570 1700 u_char text[NGX_SOCKADDR_STRLEN];
1571 in_addr = in_port->addrs.elts; 1701
1572 last = in_port->addrs.nelts; 1702 ls = ngx_array_push(&cf->cycle->listening);
1573 1703 if (ls == NULL) {
1574 /* 1704 return NULL;
1575 * if there is a binding to a "*:port" then we need to bind() 1705 }
1576 * to the "*:port" only and ignore other bindings 1706
1577 */ 1707 ngx_memzero(ls, sizeof(ngx_listening_t));
1578 1708
1579 if (in_addr[last - 1].addr == INADDR_ANY) { 1709 sa = ngx_palloc(cf->pool, addr->socklen);
1580 in_addr[last - 1].bind = 1; 1710 if (sa == NULL) {
1581 bind_all = 0; 1711 return NULL;
1582 1712 }
1583 } else { 1713
1584 bind_all = 1; 1714 ngx_memcpy(sa, addr->sockaddr, addr->socklen);
1585 } 1715
1586 1716 ls->sockaddr = sa;
1587 a = 0; 1717 ls->socklen = addr->socklen;
1588 1718
1589 while (a < last) { 1719 ls->addr_text.len = ngx_sock_ntop(sa, text, NGX_SOCKADDR_STRLEN, 1);
1590 1720
1591 if (!bind_all && !in_addr[a].bind) { 1721 ls->addr_text.data = ngx_pnalloc(cf->pool, ls->addr_text.len);
1592 a++; 1722 if (ls->addr_text.data == NULL) {
1723 return NULL;
1724 }
1725
1726 ngx_memcpy(ls->addr_text.data, text, ls->addr_text.len);
1727
1728 ls->fd = (ngx_socket_t) -1;
1729 ls->type = SOCK_STREAM;
1730
1731 switch (ls->sockaddr->sa_family) {
1732 #if (NGX_HAVE_INET6)
1733 case AF_INET6:
1734 ls->addr_text_max_len = NGX_INET6_ADDRSTRLEN;
1735 break;
1736 #endif
1737 case AF_INET:
1738 ls->addr_text_max_len = NGX_INET_ADDRSTRLEN;
1739 break;
1740 default:
1741 ls->addr_text_max_len = NGX_SOCKADDR_STRLEN;
1742 break;
1743 }
1744
1745 ls->addr_ntop = 1;
1746
1747 ls->handler = ngx_http_init_connection;
1748
1749 cscf = addr->core_srv_conf;
1750 ls->pool_size = cscf->connection_pool_size;
1751 ls->post_accept_timeout = cscf->client_header_timeout;
1752
1753 clcf = cscf->ctx->loc_conf[ngx_http_core_module.ctx_index];
1754
1755 ls->log = *clcf->err_log;
1756 ls->log.data = &ls->addr_text;
1757 ls->log.handler = ngx_accept_log_error;
1758
1759 #if (NGX_WIN32)
1760 {
1761 ngx_iocp_conf_t *iocpcf;
1762
1763 iocpcf = ngx_event_get_conf(cf->cycle->conf_ctx, ngx_iocp_module);
1764 if (iocpcf->acceptex_read) {
1765 ls->post_accept_buffer_size = cscf->client_header_buffer_size;
1766 }
1767 }
1768 #endif
1769
1770 ls->backlog = addr->listen_conf->backlog;
1771 ls->rcvbuf = addr->listen_conf->rcvbuf;
1772 ls->sndbuf = addr->listen_conf->sndbuf;
1773
1774 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
1775 ls->accept_filter = addr->listen_conf->accept_filter;
1776 #endif
1777
1778 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
1779 ls->deferred_accept = addr->listen_conf->deferred_accept;
1780 #endif
1781
1782 return ls;
1783 }
1784
1785
1786 static ngx_int_t
1787 ngx_http_add_addrs(ngx_conf_t *cf, ngx_http_port_t *hport,
1788 ngx_http_conf_addr_t *addr)
1789 {
1790 ngx_uint_t i;
1791 ngx_http_in_addr_t *addrs;
1792 struct sockaddr_in *sin;
1793 ngx_http_virtual_names_t *vn;
1794
1795 hport->addrs = ngx_pcalloc(cf->pool,
1796 hport->naddrs * sizeof(ngx_http_in_addr_t));
1797 if (hport->addrs == NULL) {
1798 return NGX_ERROR;
1799 }
1800
1801 addrs = hport->addrs;
1802
1803 for (i = 0; i < hport->naddrs; i++) {
1804
1805 sin = (struct sockaddr_in *) addr[i].sockaddr;
1806 addrs[i].addr = sin->sin_addr.s_addr;
1807 addrs[i].conf.core_srv_conf = addr[i].core_srv_conf;
1808 #if (NGX_HTTP_SSL)
1809 addrs[i].conf.ssl = addr[i].ssl;
1810 #endif
1811
1812 if (addr[i].hash.buckets == NULL
1813 && (addr[i].wc_head == NULL
1814 || addr[i].wc_head->hash.buckets == NULL)
1815 && (addr[i].wc_head == NULL
1816 || addr[i].wc_head->hash.buckets == NULL))
1817 {
1593 continue; 1818 continue;
1594 } 1819 }
1595 1820
1596 ls = ngx_listening_inet_stream_socket(cf, in_addr[a].addr, 1821 vn = ngx_palloc(cf->pool, sizeof(ngx_http_virtual_names_t));
1597 in_port->port); 1822 if (vn == NULL) {
1598 if (ls == NULL) {
1599 return NGX_ERROR; 1823 return NGX_ERROR;
1600 } 1824 }
1601 1825
1602 ls->addr_ntop = 1; 1826 addrs[i].conf.virtual_names = vn;
1603 1827
1604 ls->handler = ngx_http_init_connection; 1828 vn->names.hash = addr[i].hash;
1605 1829 vn->names.wc_head = addr[i].wc_head;
1606 cscf = in_addr[a].core_srv_conf; 1830 vn->names.wc_tail = addr[i].wc_tail;
1607 ls->pool_size = cscf->connection_pool_size; 1831 #if (NGX_PCRE)
1608 ls->post_accept_timeout = cscf->client_header_timeout; 1832 vn->nregex = addr[i].nregex;
1609 1833 vn->regex = addr[i].regex;
1610 clcf = cscf->ctx->loc_conf[ngx_http_core_module.ctx_index]; 1834 #endif
1611 1835 }
1612 ls->log = *clcf->err_log; 1836
1613 ls->log.data = &ls->addr_text; 1837 return NGX_OK;
1614 ls->log.handler = ngx_accept_log_error; 1838 }
1615 1839
1616 #if (NGX_WIN32) 1840
1841 #if (NGX_HAVE_INET6)
1842
1843 static ngx_int_t
1844 ngx_http_add_addrs6(ngx_conf_t *cf, ngx_http_port_t *hport,
1845 ngx_http_conf_addr_t *addr)
1846 {
1847 ngx_uint_t i;
1848 ngx_http_in6_addr_t *addrs6;
1849 struct sockaddr_in6 *sin6;
1850 ngx_http_virtual_names_t *vn;
1851
1852 hport->addrs = ngx_pcalloc(cf->pool,
1853 hport->naddrs * sizeof(ngx_http_in6_addr_t));
1854 if (hport->addrs == NULL) {
1855 return NGX_ERROR;
1856 }
1857
1858 addrs6 = hport->addrs;
1859
1860 for (i = 0; i < hport->naddrs; i++) {
1861
1862 sin6 = (struct sockaddr_in6 *) addr[i].sockaddr;
1863 addrs6[i].addr6 = sin6->sin6_addr;
1864 addrs6[i].conf.core_srv_conf = addr[i].core_srv_conf;
1865 #if (NGX_HTTP_SSL)
1866 addrs6[i].conf.ssl = addr[i].ssl;
1867 #endif
1868
1869 if (addr[i].hash.buckets == NULL
1870 && (addr[i].wc_head == NULL
1871 || addr[i].wc_head->hash.buckets == NULL)
1872 && (addr[i].wc_head == NULL
1873 || addr[i].wc_head->hash.buckets == NULL))
1617 { 1874 {
1618 ngx_iocp_conf_t *iocpcf; 1875 continue;
1619 1876 }
1620 iocpcf = ngx_event_get_conf(cf->cycle->conf_ctx, ngx_iocp_module); 1877
1621 if (iocpcf->acceptex_read) { 1878 vn = ngx_palloc(cf->pool, sizeof(ngx_http_virtual_names_t));
1622 ls->post_accept_buffer_size = cscf->client_header_buffer_size; 1879 if (vn == NULL) {
1623 }
1624 }
1625 #endif
1626
1627 ls->backlog = in_addr[a].listen_conf->backlog;
1628 ls->rcvbuf = in_addr[a].listen_conf->rcvbuf;
1629 ls->sndbuf = in_addr[a].listen_conf->sndbuf;
1630
1631 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
1632 ls->accept_filter = in_addr[a].listen_conf->accept_filter;
1633 #endif
1634
1635 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
1636 ls->deferred_accept = in_addr[a].listen_conf->deferred_accept;
1637 #endif
1638
1639 hip = ngx_palloc(cf->pool, sizeof(ngx_http_in_port_t));
1640 if (hip == NULL) {
1641 return NGX_ERROR; 1880 return NGX_ERROR;
1642 } 1881 }
1643 1882
1644 hip->port = in_port->port; 1883 addrs6[i].conf.virtual_names = vn;
1645 1884
1646 hip->port_text.data = ngx_pnalloc(cf->pool, 7); 1885 vn->names.hash = addr[i].hash;
1647 if (hip->port_text.data == NULL) { 1886 vn->names.wc_head = addr[i].wc_head;
1648 return NGX_ERROR; 1887 vn->names.wc_tail = addr[i].wc_tail;
1649 }
1650
1651 ls->servers = hip;
1652
1653 hip->port_text.len = ngx_sprintf(hip->port_text.data, ":%d", hip->port)
1654 - hip->port_text.data;
1655
1656 in_addr = in_port->addrs.elts;
1657
1658 if (in_addr[a].bind && in_addr[a].addr != INADDR_ANY) {
1659 hip->naddrs = 1;
1660 done = 0;
1661
1662 } else if (in_port->addrs.nelts > 1
1663 && in_addr[last - 1].addr == INADDR_ANY)
1664 {
1665 hip->naddrs = last;
1666 done = 1;
1667
1668 } else {
1669 hip->naddrs = 1;
1670 done = 0;
1671 }
1672
1673 hip->addrs = ngx_pcalloc(cf->pool,
1674 hip->naddrs * sizeof(ngx_http_in_addr_t));
1675 if (hip->addrs == NULL) {
1676 return NGX_ERROR;
1677 }
1678
1679 for (i = 0; i < hip->naddrs; i++) {
1680 hip->addrs[i].addr = in_addr[i].addr;
1681 hip->addrs[i].core_srv_conf = in_addr[i].core_srv_conf;
1682
1683 #if (NGX_HTTP_SSL)
1684 hip->addrs[i].ssl = in_addr[i].ssl;
1685 #endif
1686
1687 if (in_addr[i].hash.buckets == NULL
1688 && (in_addr[i].wc_head == NULL
1689 || in_addr[i].wc_head->hash.buckets == NULL)
1690 && (in_addr[i].wc_head == NULL
1691 || in_addr[i].wc_head->hash.buckets == NULL))
1692 {
1693 continue;
1694 }
1695
1696 vn = ngx_palloc(cf->pool, sizeof(ngx_http_virtual_names_t));
1697 if (vn == NULL) {
1698 return NGX_ERROR;
1699 }
1700 hip->addrs[i].virtual_names = vn;
1701
1702 vn->names.hash = in_addr[i].hash;
1703 vn->names.wc_head = in_addr[i].wc_head;
1704 vn->names.wc_tail = in_addr[i].wc_tail;
1705 #if (NGX_PCRE) 1888 #if (NGX_PCRE)
1706 vn->nregex = in_addr[i].nregex; 1889 vn->nregex = addr[i].nregex;
1707 vn->regex = in_addr[i].regex; 1890 vn->regex = addr[i].regex;
1708 #endif 1891 #endif
1709 }
1710
1711 if (done) {
1712 return NGX_OK;
1713 }
1714
1715 in_addr++;
1716 in_port->addrs.elts = in_addr;
1717 last--;
1718
1719 a = 0;
1720 } 1892 }
1721 1893
1722 return NGX_OK; 1894 return NGX_OK;
1723 } 1895 }
1896
1897 #endif
1724 1898
1725 1899
1726 char * 1900 char *
1727 ngx_http_types_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) 1901 ngx_http_types_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1728 { 1902 {