comparison src/http/modules/ngx_http_geo_module.c @ 4999:3f776d65c5b9

Geo: improved code readability.
author Ruslan Ermilov <ru@nginx.com>
date Thu, 27 Dec 2012 21:35:47 +0000
parents f95b3772d832
children f7fe817c92a2
comparison
equal deleted inserted replaced
4998:82336a9ce26d 4999:3f776d65c5b9
1014 1014
1015 static char * 1015 static char *
1016 ngx_http_geo_cidr(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx, 1016 ngx_http_geo_cidr(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,
1017 ngx_str_t *value) 1017 ngx_str_t *value)
1018 { 1018 {
1019 char *rv;
1019 ngx_int_t rc, del; 1020 ngx_int_t rc, del;
1020 ngx_str_t *net; 1021 ngx_str_t *net;
1021 ngx_cidr_t cidr; 1022 ngx_cidr_t cidr;
1022 1023
1023 if (ctx->tree == NULL) { 1024 if (ctx->tree == NULL) {
1039 if (ngx_strcmp(value[0].data, "default") == 0) { 1040 if (ngx_strcmp(value[0].data, "default") == 0) {
1040 cidr.family = AF_INET; 1041 cidr.family = AF_INET;
1041 cidr.u.in.addr = 0; 1042 cidr.u.in.addr = 0;
1042 cidr.u.in.mask = 0; 1043 cidr.u.in.mask = 0;
1043 1044
1044 if (ngx_http_geo_cidr_add(cf, ctx, &cidr, &value[1], &value[0]) 1045 rv = ngx_http_geo_cidr_add(cf, ctx, &cidr, &value[1], &value[0]);
1045 != NGX_CONF_OK) 1046
1046 { 1047 if (rv != NGX_CONF_OK) {
1047 return NGX_CONF_ERROR; 1048 return rv;
1048 } 1049 }
1049 1050
1050 #if (NGX_HAVE_INET6) 1051 #if (NGX_HAVE_INET6)
1051 cidr.family = AF_INET6; 1052 cidr.family = AF_INET6;
1052 ngx_memzero(&cidr.u.in6, sizeof(ngx_in6_cidr_t)); 1053 ngx_memzero(&cidr.u.in6, sizeof(ngx_in6_cidr_t));
1053 1054
1054 if (ngx_http_geo_cidr_add(cf, ctx, &cidr, &value[1], &value[0]) 1055 rv = ngx_http_geo_cidr_add(cf, ctx, &cidr, &value[1], &value[0]);
1055 != NGX_CONF_OK) 1056
1056 { 1057 if (rv != NGX_CONF_OK) {
1057 return NGX_CONF_ERROR; 1058 return rv;
1058 } 1059 }
1059 #endif 1060 #endif
1060 1061
1061 return NGX_CONF_OK; 1062 return NGX_CONF_OK;
1062 } 1063 }
1111 static char * 1112 static char *
1112 ngx_http_geo_cidr_add(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx, 1113 ngx_http_geo_cidr_add(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,
1113 ngx_cidr_t *cidr, ngx_str_t *value, ngx_str_t *net) 1114 ngx_cidr_t *cidr, ngx_str_t *value, ngx_str_t *net)
1114 { 1115 {
1115 ngx_int_t rc; 1116 ngx_int_t rc;
1116 ngx_uint_t i;
1117 ngx_http_variable_value_t *val, *old; 1117 ngx_http_variable_value_t *val, *old;
1118 1118
1119 val = ngx_http_geo_value(cf, ctx, value); 1119 val = ngx_http_geo_value(cf, ctx, value);
1120 1120
1121 if (val == NULL) { 1121 if (val == NULL) {
1124 1124
1125 switch (cidr->family) { 1125 switch (cidr->family) {
1126 1126
1127 #if (NGX_HAVE_INET6) 1127 #if (NGX_HAVE_INET6)
1128 case AF_INET6: 1128 case AF_INET6:
1129 for (i = 2; i; i--) { 1129 rc = ngx_radix128tree_insert(ctx->tree6, cidr->u.in6.addr.s6_addr,
1130 rc = ngx_radix128tree_insert(ctx->tree6, cidr->u.in6.addr.s6_addr, 1130 cidr->u.in6.mask.s6_addr,
1131 cidr->u.in6.mask.s6_addr, 1131 (uintptr_t) val);
1132 (uintptr_t) val); 1132
1133 1133 if (rc == NGX_OK) {
1134 if (rc == NGX_OK) { 1134 return NGX_CONF_OK;
1135 return NGX_CONF_OK; 1135 }
1136 } 1136
1137 1137 if (rc == NGX_ERROR) {
1138 if (rc == NGX_ERROR) { 1138 return NGX_CONF_ERROR;
1139 return NGX_CONF_ERROR; 1139 }
1140 } 1140
1141 1141 /* rc == NGX_BUSY */
1142 /* rc == NGX_BUSY */ 1142
1143 1143 old = (ngx_http_variable_value_t *)
1144 old = (ngx_http_variable_value_t *) 1144 ngx_radix128tree_find(ctx->tree6,
1145 ngx_radix128tree_find(ctx->tree6, 1145 cidr->u.in6.addr.s6_addr);
1146 cidr->u.in6.addr.s6_addr); 1146
1147 1147 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
1148 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, 1148 "duplicate network \"%V\", value: \"%v\", old value: \"%v\"",
1149 "duplicate network \"%V\", value: \"%v\", old value: \"%v\"", 1149 net, val, old);
1150 net, val, old); 1150
1151 1151 rc = ngx_radix128tree_delete(ctx->tree6,
1152 rc = ngx_radix128tree_delete(ctx->tree6, 1152 cidr->u.in6.addr.s6_addr,
1153 cidr->u.in6.addr.s6_addr, 1153 cidr->u.in6.mask.s6_addr);
1154 cidr->u.in6.mask.s6_addr); 1154
1155 1155 if (rc == NGX_ERROR) {
1156 if (rc == NGX_ERROR) { 1156 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid radix tree");
1157 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid radix tree"); 1157 return NGX_CONF_ERROR;
1158 return NGX_CONF_ERROR; 1158 }
1159 } 1159
1160 } 1160 rc = ngx_radix128tree_insert(ctx->tree6, cidr->u.in6.addr.s6_addr,
1161 cidr->u.in6.mask.s6_addr,
1162 (uintptr_t) val);
1161 1163
1162 break; 1164 break;
1163 #endif 1165 #endif
1164 1166
1165 default: /* AF_INET */ 1167 default: /* AF_INET */
1166 for (i = 2; i; i--) { 1168 rc = ngx_radix32tree_insert(ctx->tree, cidr->u.in.addr,
1167 rc = ngx_radix32tree_insert(ctx->tree, cidr->u.in.addr, 1169 cidr->u.in.mask, (uintptr_t) val);
1168 cidr->u.in.mask, (uintptr_t) val); 1170
1169 1171 if (rc == NGX_OK) {
1170 if (rc == NGX_OK) { 1172 return NGX_CONF_OK;
1171 return NGX_CONF_OK; 1173 }
1172 } 1174
1173 1175 if (rc == NGX_ERROR) {
1174 if (rc == NGX_ERROR) { 1176 return NGX_CONF_ERROR;
1175 return NGX_CONF_ERROR; 1177 }
1176 } 1178
1177 1179 /* rc == NGX_BUSY */
1178 /* rc == NGX_BUSY */ 1180
1179 1181 old = (ngx_http_variable_value_t *)
1180 old = (ngx_http_variable_value_t *) 1182 ngx_radix32tree_find(ctx->tree, cidr->u.in.addr);
1181 ngx_radix32tree_find(ctx->tree, cidr->u.in.addr); 1183
1182 1184 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
1183 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, 1185 "duplicate network \"%V\", value: \"%v\", old value: \"%v\"",
1184 "duplicate network \"%V\", value: \"%v\", old value: \"%v\"", 1186 net, val, old);
1185 net, val, old); 1187
1186 1188 rc = ngx_radix32tree_delete(ctx->tree,
1187 rc = ngx_radix32tree_delete(ctx->tree, 1189 cidr->u.in.addr, cidr->u.in.mask);
1188 cidr->u.in.addr, cidr->u.in.mask); 1190
1189 1191 if (rc == NGX_ERROR) {
1190 if (rc == NGX_ERROR) { 1192 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid radix tree");
1191 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid radix tree"); 1193 return NGX_CONF_ERROR;
1192 return NGX_CONF_ERROR; 1194 }
1193 } 1195
1194 } 1196 rc = ngx_radix32tree_insert(ctx->tree, cidr->u.in.addr,
1197 cidr->u.in.mask, (uintptr_t) val);
1195 1198
1196 break; 1199 break;
1200 }
1201
1202 if (rc == NGX_OK) {
1203 return NGX_CONF_OK;
1197 } 1204 }
1198 1205
1199 return NGX_CONF_ERROR; 1206 return NGX_CONF_ERROR;
1200 } 1207 }
1201 1208