Klaus Demo nginx / 912fb44
Listen port ranges. A range is specified with a dash. For each port in a range a separate listen socket is created. Examples: listen 8080-9000; listen example.com:80-88; Roman Arutyunyan 1 year, 8 months ago
2 changed file(s) with 131 addition(s) and 41 deletion(s). Raw diff Collapse all Expand all
781781 static ngx_int_t
782782 ngx_parse_inet_url(ngx_pool_t *pool, ngx_url_t *u)
783783 {
784 u_char *host, *port, *last, *uri, *args;
784 u_char *host, *port, *last, *uri, *args, *dash;
785785 size_t len;
786786 ngx_int_t n;
787787 struct sockaddr_in *sin;
829829
830830 len = last - port;
831831
832 if (u->listen) {
833 dash = ngx_strlchr(port, last, '-');
834
835 if (dash) {
836 dash++;
837
838 n = ngx_atoi(dash, last - dash);
839
840 if (n < 1 || n > 65535) {
841 u->err = "invalid port";
842 return NGX_ERROR;
843 }
844
845 u->last_port = (in_port_t) n;
846
847 len = dash - port - 1;
848 }
849 }
850
832851 n = ngx_atoi(port, len);
833852
834853 if (n < 1 || n > 65535) {
836855 return NGX_ERROR;
837856 }
838857
858 if (u->last_port && n > u->last_port) {
859 u->err = "invalid port range";
860 return NGX_ERROR;
861 }
862
839863 u->port = (in_port_t) n;
840864 sin->sin_port = htons((in_port_t) n);
841865
842 u->port_text.len = len;
866 u->port_text.len = last - port;
843867 u->port_text.data = port;
844868
845869 last = port - 1;
851875
852876 /* test value as port only */
853877
854 n = ngx_atoi(host, last - host);
878 len = last - host;
879
880 dash = ngx_strlchr(host, last, '-');
881
882 if (dash) {
883 dash++;
884
885 n = ngx_atoi(dash, last - dash);
886
887 if (n == NGX_ERROR) {
888 goto no_port;
889 }
890
891 if (n < 1 || n > 65535) {
892 u->err = "invalid port";
893
894 } else {
895 u->last_port = (in_port_t) n;
896 }
897
898 len = dash - host - 1;
899 }
900
901 n = ngx_atoi(host, len);
855902
856903 if (n != NGX_ERROR) {
904
905 if (u->err) {
906 return NGX_ERROR;
907 }
857908
858909 if (n < 1 || n > 65535) {
859910 u->err = "invalid port";
860911 return NGX_ERROR;
861912 }
862913
914 if (u->last_port && n > u->last_port) {
915 u->err = "invalid port range";
916 return NGX_ERROR;
917 }
918
863919 u->port = (in_port_t) n;
864920 sin->sin_port = htons((in_port_t) n);
865921 sin->sin_addr.s_addr = INADDR_ANY;
875931 }
876932 }
877933
934 no_port:
935
936 u->err = NULL;
878937 u->no_port = 1;
879938 u->port = u->default_port;
880939 sin->sin_port = htons(u->default_port);
940 u->last_port = 0;
881941 }
882942
883943 len = last - host;
928988 ngx_parse_inet6_url(ngx_pool_t *pool, ngx_url_t *u)
929989 {
930990 #if (NGX_HAVE_INET6)
931 u_char *p, *host, *port, *last, *uri;
991 u_char *p, *host, *port, *last, *uri, *dash;
932992 size_t len;
933993 ngx_int_t n;
934994 struct sockaddr_in6 *sin6;
9741034
9751035 len = last - port;
9761036
1037 if (u->listen) {
1038 dash = ngx_strlchr(port, last, '-');
1039
1040 if (dash) {
1041 dash++;
1042
1043 n = ngx_atoi(dash, last - dash);
1044
1045 if (n < 1 || n > 65535) {
1046 u->err = "invalid port";
1047 return NGX_ERROR;
1048 }
1049
1050 u->last_port = (in_port_t) n;
1051
1052 len = dash - port - 1;
1053 }
1054 }
1055
9771056 n = ngx_atoi(port, len);
9781057
9791058 if (n < 1 || n > 65535) {
9811060 return NGX_ERROR;
9821061 }
9831062
1063 if (u->last_port && n > u->last_port) {
1064 u->err = "invalid port range";
1065 return NGX_ERROR;
1066 }
1067
9841068 u->port = (in_port_t) n;
9851069 sin6->sin6_port = htons((in_port_t) n);
9861070
987 u->port_text.len = len;
1071 u->port_text.len = last - port;
9881072 u->port_text.data = port;
9891073
9901074 } else {
11801264 {
11811265 u_char *p;
11821266 size_t len;
1267 ngx_uint_t i, nports;
11831268 ngx_addr_t *addr;
11841269 struct sockaddr *sa;
11851270
1271 nports = u->last_port ? u->last_port - u->port + 1 : 1;
1272
11861273 if (u->addrs == NULL) {
1187 u->addrs = ngx_palloc(pool, total * sizeof(ngx_addr_t));
1274 u->addrs = ngx_palloc(pool, total * nports * sizeof(ngx_addr_t));
11881275 if (u->addrs == NULL) {
11891276 return NGX_ERROR;
11901277 }
11911278 }
11921279
1193 sa = ngx_pcalloc(pool, socklen);
1194 if (sa == NULL) {
1195 return NGX_ERROR;
1196 }
1197
1198 ngx_memcpy(sa, sockaddr, socklen);
1199
1200 ngx_inet_set_port(sa, u->port);
1201
1202 switch (sa->sa_family) {
1203
1204 #if (NGX_HAVE_INET6)
1205 case AF_INET6:
1206 len = NGX_INET6_ADDRSTRLEN + sizeof("[]:65536") - 1;
1207 break;
1208 #endif
1209
1210 default: /* AF_INET */
1211 len = NGX_INET_ADDRSTRLEN + sizeof(":65535") - 1;
1212 }
1213
1214 p = ngx_pnalloc(pool, len);
1215 if (p == NULL) {
1216 return NGX_ERROR;
1217 }
1218
1219 len = ngx_sock_ntop(sa, socklen, p, len, 1);
1220
1221 addr = &u->addrs[u->naddrs++];
1222
1223 addr->sockaddr = sa;
1224 addr->socklen = socklen;
1225
1226 addr->name.len = len;
1227 addr->name.data = p;
1280 for (i = 0; i < nports; i++) {
1281 sa = ngx_pcalloc(pool, socklen);
1282 if (sa == NULL) {
1283 return NGX_ERROR;
1284 }
1285
1286 ngx_memcpy(sa, sockaddr, socklen);
1287
1288 ngx_inet_set_port(sa, u->port + i);
1289
1290 switch (sa->sa_family) {
1291
1292 #if (NGX_HAVE_INET6)
1293 case AF_INET6:
1294 len = NGX_INET6_ADDRSTRLEN + sizeof("[]:65536") - 1;
1295 break;
1296 #endif
1297
1298 default: /* AF_INET */
1299 len = NGX_INET_ADDRSTRLEN + sizeof(":65535") - 1;
1300 }
1301
1302 p = ngx_pnalloc(pool, len);
1303 if (p == NULL) {
1304 return NGX_ERROR;
1305 }
1306
1307 len = ngx_sock_ntop(sa, socklen, p, len, 1);
1308
1309 addr = &u->addrs[u->naddrs++];
1310
1311 addr->sockaddr = sa;
1312 addr->socklen = socklen;
1313
1314 addr->name.len = len;
1315 addr->name.data = p;
1316 }
12281317
12291318 return NGX_OK;
12301319 }
8585
8686 in_port_t port;
8787 in_port_t default_port;
88 in_port_t last_port;
8889 int family;
8990
9091 unsigned listen:1;