* Parse the user input and obtain the list of forwarding ports * * @param[in] list * String containing the user input. User can specify * in these formats 1,3,5 or 1-3 or 1-2,5 or 3,5-6. * For example, if the user wants to use all the available * 4 ports in his system, then the input can be 0-3 or 0,1,2,3. * If the user wants to use only the ports 1,2 then the input * is 1,2.
| 5279 | * On failure, returns 0 |
| 5280 | */ |
| 5281 | static unsigned int |
| 5282 | parse_port_list(const char *list, unsigned int *values, unsigned int maxsize) |
| 5283 | { |
| 5284 | unsigned int count = 0; |
| 5285 | char *end = NULL; |
| 5286 | int min, max; |
| 5287 | int value, i; |
| 5288 | unsigned int marked[maxsize]; |
| 5289 | |
| 5290 | if (list == NULL || values == NULL) |
| 5291 | return 0; |
| 5292 | |
| 5293 | for (i = 0; i < (int)maxsize; i++) |
| 5294 | marked[i] = 0; |
| 5295 | |
| 5296 | min = INT_MAX; |
| 5297 | |
| 5298 | do { |
| 5299 | /*Remove the blank spaces if any*/ |
| 5300 | while (isblank(*list)) |
| 5301 | list++; |
| 5302 | if (*list == '\0') |
| 5303 | break; |
| 5304 | errno = 0; |
| 5305 | value = strtol(list, &end, 10); |
| 5306 | if (errno || end == NULL) |
| 5307 | return 0; |
| 5308 | if (value < 0 || value >= (int)maxsize) |
| 5309 | return 0; |
| 5310 | while (isblank(*end)) |
| 5311 | end++; |
| 5312 | if (*end == '-' && min == INT_MAX) { |
| 5313 | min = value; |
| 5314 | } else if ((*end == ',') || (*end == '\0')) { |
| 5315 | max = value; |
| 5316 | if (min == INT_MAX) |
| 5317 | min = value; |
| 5318 | for (i = min; i <= max; i++) { |
| 5319 | if (count < maxsize) { |
| 5320 | if (marked[i]) |
| 5321 | continue; |
| 5322 | values[count] = i; |
| 5323 | marked[i] = 1; |
| 5324 | count++; |
| 5325 | } |
| 5326 | } |
| 5327 | min = INT_MAX; |
| 5328 | } else |
| 5329 | return 0; |
| 5330 | list = end + 1; |
| 5331 | } while (*end != '\0'); |
| 5332 | |
| 5333 | return count; |
| 5334 | } |
| 5335 | |
| 5336 | void |
| 5337 | parse_fwd_portlist(const char *portlist) |
no test coverage detected