comparison src/http/ngx_http_variables.c @ 6593:b3b7e33083ac

Introduced ngx_inet_get_port() and ngx_inet_set_port() functions.
author Roman Arutyunyan <arut@nginx.com>
date Mon, 20 Jun 2016 11:50:39 +0300
parents 28c76d9d75b7
children 8cd97c14b0e2
comparison
equal deleted inserted replaced
6592:2c7b488a61fb 6593:b3b7e33083ac
1199 1199
1200 static ngx_int_t 1200 static ngx_int_t
1201 ngx_http_variable_remote_port(ngx_http_request_t *r, 1201 ngx_http_variable_remote_port(ngx_http_request_t *r,
1202 ngx_http_variable_value_t *v, uintptr_t data) 1202 ngx_http_variable_value_t *v, uintptr_t data)
1203 { 1203 {
1204 ngx_uint_t port; 1204 ngx_uint_t port;
1205 struct sockaddr_in *sin;
1206 #if (NGX_HAVE_INET6)
1207 struct sockaddr_in6 *sin6;
1208 #endif
1209 1205
1210 v->len = 0; 1206 v->len = 0;
1211 v->valid = 1; 1207 v->valid = 1;
1212 v->no_cacheable = 0; 1208 v->no_cacheable = 0;
1213 v->not_found = 0; 1209 v->not_found = 0;
1215 v->data = ngx_pnalloc(r->pool, sizeof("65535") - 1); 1211 v->data = ngx_pnalloc(r->pool, sizeof("65535") - 1);
1216 if (v->data == NULL) { 1212 if (v->data == NULL) {
1217 return NGX_ERROR; 1213 return NGX_ERROR;
1218 } 1214 }
1219 1215
1220 switch (r->connection->sockaddr->sa_family) { 1216 port = ngx_inet_get_port(r->connection->sockaddr);
1221
1222 #if (NGX_HAVE_INET6)
1223 case AF_INET6:
1224 sin6 = (struct sockaddr_in6 *) r->connection->sockaddr;
1225 port = ntohs(sin6->sin6_port);
1226 break;
1227 #endif
1228
1229 #if (NGX_HAVE_UNIX_DOMAIN)
1230 case AF_UNIX:
1231 port = 0;
1232 break;
1233 #endif
1234
1235 default: /* AF_INET */
1236 sin = (struct sockaddr_in *) r->connection->sockaddr;
1237 port = ntohs(sin->sin_port);
1238 break;
1239 }
1240 1217
1241 if (port > 0 && port < 65536) { 1218 if (port > 0 && port < 65536) {
1242 v->len = ngx_sprintf(v->data, "%ui", port) - v->data; 1219 v->len = ngx_sprintf(v->data, "%ui", port) - v->data;
1243 } 1220 }
1244 1221
1319 1296
1320 static ngx_int_t 1297 static ngx_int_t
1321 ngx_http_variable_server_port(ngx_http_request_t *r, 1298 ngx_http_variable_server_port(ngx_http_request_t *r,
1322 ngx_http_variable_value_t *v, uintptr_t data) 1299 ngx_http_variable_value_t *v, uintptr_t data)
1323 { 1300 {
1324 ngx_uint_t port; 1301 ngx_uint_t port;
1325 struct sockaddr_in *sin;
1326 #if (NGX_HAVE_INET6)
1327 struct sockaddr_in6 *sin6;
1328 #endif
1329 1302
1330 v->len = 0; 1303 v->len = 0;
1331 v->valid = 1; 1304 v->valid = 1;
1332 v->no_cacheable = 0; 1305 v->no_cacheable = 0;
1333 v->not_found = 0; 1306 v->not_found = 0;
1339 v->data = ngx_pnalloc(r->pool, sizeof("65535") - 1); 1312 v->data = ngx_pnalloc(r->pool, sizeof("65535") - 1);
1340 if (v->data == NULL) { 1313 if (v->data == NULL) {
1341 return NGX_ERROR; 1314 return NGX_ERROR;
1342 } 1315 }
1343 1316
1344 switch (r->connection->local_sockaddr->sa_family) { 1317 port = ngx_inet_get_port(r->connection->local_sockaddr);
1345
1346 #if (NGX_HAVE_INET6)
1347 case AF_INET6:
1348 sin6 = (struct sockaddr_in6 *) r->connection->local_sockaddr;
1349 port = ntohs(sin6->sin6_port);
1350 break;
1351 #endif
1352
1353 #if (NGX_HAVE_UNIX_DOMAIN)
1354 case AF_UNIX:
1355 port = 0;
1356 break;
1357 #endif
1358
1359 default: /* AF_INET */
1360 sin = (struct sockaddr_in *) r->connection->local_sockaddr;
1361 port = ntohs(sin->sin_port);
1362 break;
1363 }
1364 1318
1365 if (port > 0 && port < 65536) { 1319 if (port > 0 && port < 65536) {
1366 v->len = ngx_sprintf(v->data, "%ui", port) - v->data; 1320 v->len = ngx_sprintf(v->data, "%ui", port) - v->data;
1367 } 1321 }
1368 1322