Updates for restoring/save.
[handlervirt.git] / experiment7.c
blob2713705fb7df20669a603d48291b63734a0e0a67
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <string.h>
6 typedef struct line LINE;
8 struct line {
9 unsigned long int mac;
10 unsigned long int ip;
11 char *fqdn;
12 LINE *next;
15 int add(LINE *first, LINE *last, unsigned long int mac, unsigned long int ip, char *fqdn) {
16 LINE *new = malloc(sizeof(LINE));
17 new->mac = mac;
18 new->ip = ip;
19 new->fqdn = strdup(fqdn);
20 new->next = NULL;
22 if (!first)
23 first = new;
24 else
25 last->next = new;
27 last = new;
29 return 0;
32 int add_line(LINE *first, LINE *last, char *line) {
33 if (strncmp(line, "dhcp-host=", 10) == 0 &&
34 line[12] == ':' && line[15] == ':' && line[18] == ':' && line[21] == ':' && line[24] == ':' &&
35 line[27] == ',') {
36 char *ip, *nextip;
37 char macstring[4];
38 LINE *new = malloc(sizeof(LINE));
40 if (new == NULL) return -1;
42 macstring[2] = '\0';
43 macstring[3] = '\0';
45 new->mac = 0;
46 new->ip = 0;
47 new->next= NULL;
49 strncpy(macstring, &line[19], 2);
50 new->mac += (strtoul(macstring, NULL, 16) * (unsigned long int)(256 * 256));
51 strncpy(macstring, &line[22], 2);
52 new->mac += (strtoul(macstring, NULL, 16) * (unsigned long int)(256));
53 strncpy(macstring, &line[25], 2);
54 new->mac += (strtoul(macstring, NULL, 16));
56 ip = strchr(&line[28], ',');
57 new->fqdn = strndup(&line[28], ip - &line[28]);
58 printf("%s\n", new->fqdn);
59 ip++;
61 macstring[1] = '\0'; macstring[2] = '\0';
62 strncpy(macstring, ip, (nextip = strchr(ip, '.')) - ip);
63 new->ip += (strtoul(macstring, NULL, 10) * (unsigned long int)(256 * 256 * 256));
64 // printf("%s %lu\n", macstring, new->ip);
65 ip = nextip + 1;
67 macstring[1] = '\0'; macstring[2] = '\0';
68 strncpy(macstring, ip, (nextip = strchr(ip, '.')) - ip);
69 new->ip += (strtoul(macstring, NULL, 10) * (unsigned long int)(256 * 256));
70 // printf("%s %lu\n", macstring, (strtoul(macstring, NULL, 10) * (unsigned long int)(256 * 256)));
71 ip = nextip + 1;
73 macstring[1] = '\0'; macstring[2] = '\0';
74 strncpy(macstring, ip, (nextip = strchr(ip, '.')) - ip);
75 new->ip += (strtoul(macstring, NULL, 10) * (unsigned long int)(256));
76 // printf("%s %lu\n", macstring, strtoul(macstring, NULL, 10) * (unsigned long int)(256));
77 ip = nextip + 1;
79 macstring[1] = '\0'; macstring[2] = '\0';
80 strncpy(macstring, ip, (nextip = strchr(ip, ',')) - ip);
81 new->ip += (strtoul(macstring, NULL, 10));
82 // printf("%s %lu\n", macstring, strtoul(macstring, NULL, 10));
84 // printf("%06lX %s %lu %lu\n", new->mac, new->fqdn, new->mac, new->ip);
85 // dhcp-host=00:1E:C9:B3:B3:85,xen001.xen,172.16.103.1,24h
88 if (!first)
89 first = new;
90 else
91 last->next = new;
93 last = new;
95 return 0;
98 return -1;
101 char * print_mac(unsigned long int mac) {
102 char *macstring = (char *) malloc(sizeof(char*) * 18);
103 strcpy(macstring, "00:16:3E:");
104 snprintf(&macstring[9], 8, "%06lX", mac);
105 strncpy(&macstring[15], &macstring[13], 2);
106 macstring[13] = macstring[12];
107 macstring[12] = macstring[11];
108 macstring[11] = ':';
109 macstring[14] = ':';
110 macstring[17] = '\0';
111 return macstring;
114 char * print_ip(unsigned long int ipaddress) {
115 char *dest = (char *) malloc(sizeof(char*) * 16);
116 int a, b, c, d;
117 ldiv_t ip = ldiv(ipaddress, (256*256*256));
118 a = ip.quot;
119 ip = ldiv(ip.rem, (256*256));
120 b = ip.quot;
121 ip = ldiv(ip.rem, (256));
122 c = ip.quot;
123 d = ip.rem;
125 snprintf(dest, 16, "%d.%d.%d.%d", a, b, c, d);
126 return dest;
129 int print_forget(LINE *first, char *path) {
130 FILE *fd = fopen(path, "w");
132 LINE *step = first;
133 while (step) {
134 LINE *this = step;
135 char *pretty_mac = print_mac(this->mac);
136 char *pretty_ip = print_ip(this->ip);
138 if (fd)
139 fprintf(fd, "dhcp-host=%s,%s,%s,24h\n", pretty_mac, this->fqdn, pretty_ip);
141 free(pretty_mac);
142 free(pretty_ip);
144 if (this->fqdn != NULL)
145 free(this->fqdn);
147 step = this->next;
148 free(this);
151 if (fd) {
152 fclose(fd);
153 return 0;
156 return -1;
159 static int bymac(const void *a, const void *b) {
160 LINE *temp1 = (LINE *) a;
161 LINE *temp2 = (LINE *) b;
163 return temp1->mac - temp2->mac;
166 static int byip(const void *a, const void *b) {
167 LINE *temp1 = (LINE *) a;
168 LINE *temp2 = (LINE *) b;
170 return temp1->ip - temp2->ip;
173 static unsigned long int nextmac(LINE *array, int listlen) {
174 int i;
175 for (i = 0; i < (listlen - 1); i++) {
176 if (array[i+1].mac != 0 && array[i+1].mac != array[i].mac + 1) return array[i].mac + 1;
178 return array[listlen - 1].mac + 1;
181 static unsigned long int nextip(LINE *array, int listlen) {
182 int i;
183 for (i = 0; i < (listlen - 1); i++) {
184 if (array[i+1].ip != array[i].ip + 1) return array[i].ip + 1;
186 return array[listlen - 1].ip + 1;
189 int getNewMac(char *cabinet, char *fqdn, char **mac, char **ip) {
190 if (fqdn == NULL)
191 return -1;
193 char *pad = malloc(sizeof(char) * 128);
195 if (pad == NULL) return -1;
197 LINE *first = NULL, *last = NULL, *array = NULL;
198 snprintf(pad, 128, "/mnt/netapp/ipservices/%s.conf", cabinet);
200 FILE *fd;
202 int listlen = 0;
204 if ((fd = fopen(pad, "r")) != NULL) {
205 char line[128];
206 while (!feof(fd)) {
207 if (fgets(line, 128, fd) > 0)
208 if (add_line(first, last, line) == 0)
209 listlen++;
211 fclose(fd);
214 if (listlen > 0) {
215 array = malloc(sizeof(LINE) * listlen);
216 if (array == NULL) {
217 // TODO: cleanup
218 free(pad);
219 return -1;
220 } else {
221 int i = 0;
222 LINE *step = first;
223 while (step) {
224 printf("i = %d\n", i);
225 array[i].mac = step->mac;
226 array[i].ip = step->ip;
227 array[i].fqdn = step->fqdn;
228 step = step->next;
229 i++;
232 unsigned long int new_mac, new_ip;
234 qsort(array, listlen, sizeof(LINE), bymac);
235 new_mac = nextmac(array, listlen);
236 if (mac)
237 *mac = print_mac(new_mac);
239 qsort(array, listlen, sizeof(LINE), byip);
240 new_ip = nextip(array, listlen);
242 if (ip)
243 *ip = print_ip(new_ip);
245 add(first, last, new_mac, new_ip, fqdn);
247 free(array);
249 print_forget(first, pad);
251 free(pad);
252 return 0;
254 free(pad);
256 return -1;
259 #ifdef WITH_MAIN
260 int main(int argc, char *argv[]) {
261 if (argc == 3) {
262 getNewMac(argv[1], argv[2], NULL, NULL);
264 return 0;
266 #endif