Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 1 | /* support functions for ip_pool modules */ |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <string.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <ctype.h> |
| 7 | |
| 8 | #include <libippool/ip_pool_support.h> |
| 9 | |
| 10 | void ip_pool_init(void) |
| 11 | { |
| 12 | } |
| 13 | |
| 14 | ip_pool_t ip_pool_get_index(char *name) |
| 15 | { |
| 16 | FILE *fp; |
| 17 | char buf[256]; |
| 18 | |
| 19 | if (isdigit(*name)) |
| 20 | return atoi(name); |
| 21 | fp = fopen(IPPOOL_CONF, "r"); |
| 22 | if (!fp) exit_error(PARAMETER_PROBLEM, |
| 23 | "cannot open %s - no pool names", IPPOOL_CONF); |
| 24 | while (fgets(buf, sizeof(buf), fp)) { |
| 25 | char *p = strtok(buf, " \t\n"); |
| 26 | ip_pool_t index; |
| 27 | |
| 28 | if (!p || *p == '#') continue; |
| 29 | index = atoi(p); |
| 30 | p = strtok(0, " \t\n"); |
| 31 | if (p && 0 == strcmp(p, name)) { |
| 32 | fclose(fp); |
| 33 | return index; |
| 34 | } |
| 35 | } |
| 36 | exit_error(PARAMETER_PROBLEM, |
| 37 | "pool '%s' not found in %s\n", name, IPPOOL_CONF); |
| 38 | } |
| 39 | |
| 40 | char *ip_pool_get_name(char *buf, int size, ip_pool_t index, int numeric) |
| 41 | { |
| 42 | FILE *fp; |
| 43 | int ok = 0; |
| 44 | |
| 45 | /* make sure we have enough room, at least for a %d */ |
| 46 | if (size < 16) |
| 47 | exit_error(PARAMETER_PROBLEM, |
| 48 | "ip_pool_support:get_name buf too small (%d vs. 16)\n", |
| 49 | size); |
| 50 | if (numeric) |
| 51 | goto do_numeric; |
| 52 | fp = fopen(IPPOOL_CONF, "r"); |
| 53 | if (fp) { |
| 54 | while (fgets(buf, size, fp)) { |
| 55 | char *p = strtok(buf, " \t\n"); |
| 56 | |
| 57 | if (!p || *p == '#') continue; |
| 58 | if (index != atoi(p)) continue; |
| 59 | p = strtok(0, " \t\n"); |
| 60 | if (!p || *p == '#') continue; |
| 61 | memmove(buf, p, strlen(p)+1); |
| 62 | ok = 1; |
| 63 | break; |
| 64 | } |
| 65 | fclose(fp); |
| 66 | } |
| 67 | if (!ok) { |
| 68 | do_numeric: |
| 69 | sprintf(buf, "%d", index); |
| 70 | } |
| 71 | return buf; |
| 72 | } |