Initial combination of announce and domudns
[handlervirt.git] / experiment5.c
blob133b4a956862040c3e2ad6d8bc64da50fe995671
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 LINE *first = NULL;
16 LINE *last = NULL;
17 LINE *array = NULL;
19 int listlen = 0;
21 int add(unsigned long int mac, unsigned long int ip, char *fqdn) {
22 LINE *new = malloc(sizeof(LINE));
23 new->mac = mac;
24 new->ip = ip;
25 new->fqdn = strdup(fqdn);
26 new->next = NULL;
28 if (!first)
29 first = new;
30 else
31 last->next = new;
33 last = new;
35 return 0;
38 int add_line(char *line) {
39 if (strncmp(line, "dhcp-host=", 10) == 0 &&
40 line[12] == ':' && line[15] == ':' && line[18] == ':' && line[21] == ':' && line[24] == ':' &&
41 line[27] == ',') {
42 char *ip, *nextip;
43 char macstring[4];
44 LINE *new = malloc(sizeof(LINE));
46 if (new == NULL) return -1;
48 listlen++;
49 macstring[2] = '\0';
50 macstring[3] = '\0';
52 new->mac = 0;
53 new->ip = 0;
54 new->next= NULL;
56 strncpy(macstring, &line[19], 2);
57 new->mac += (strtoul(macstring, NULL, 16) * (unsigned long int)(256 * 256));
58 strncpy(macstring, &line[22], 2);
59 new->mac += (strtoul(macstring, NULL, 16) * (unsigned long int)(256));
60 strncpy(macstring, &line[25], 2);
61 new->mac += (strtoul(macstring, NULL, 16));
63 ip = strchr(&line[28], ',');
64 new->fqdn = strndup(&line[28], ip - &line[28]);
65 ip++;
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 * 256));
70 // printf("%s %lu\n", macstring, new->ip);
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 * 256));
76 // printf("%s %lu\n", macstring, (strtoul(macstring, NULL, 10) * (unsigned long int)(256 * 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) * (unsigned long int)(256));
82 // printf("%s %lu\n", macstring, strtoul(macstring, NULL, 10) * (unsigned long int)(256));
83 ip = nextip + 1;
85 macstring[1] = '\0'; macstring[2] = '\0';
86 strncpy(macstring, ip, (nextip = strchr(ip, ',')) - ip);
87 new->ip += (strtoul(macstring, NULL, 10));
88 // printf("%s %lu\n", macstring, strtoul(macstring, NULL, 10));
90 // printf("%06lX %s %lu %lu\n", new->mac, new->fqdn, new->mac, new->ip);
91 // dhcp-host=00:1E:C9:B3:B3:85,xen001.xen,172.16.103.1,24h
94 if (!first)
95 first = new;
96 else
97 last->next = new;
99 last = new;
101 return 0;
104 return -1;
107 char * print_mac(unsigned long int mac) {
108 char *macstring = (char *) malloc(sizeof(char*) * 18);
109 strcpy(macstring, "00:16:3E:");
110 snprintf(&macstring[9], 8, "%06lX", mac);
111 strncpy(&macstring[15], &macstring[13], 2);
112 macstring[13] = macstring[12];
113 macstring[12] = macstring[11];
114 macstring[11] = ':';
115 macstring[14] = ':';
116 macstring[17] = '\0';
117 return macstring;
120 char * print_ip(unsigned long int ipaddress) {
121 char *dest = (char *) malloc(sizeof(char*) * 16);
122 int a, b, c, d;
123 ldiv_t ip = ldiv(ipaddress, (256*256*256));
124 a = ip.quot;
125 ip = ldiv(ip.rem, (256*256));
126 b = ip.quot;
127 ip = ldiv(ip.rem, (256));
128 c = ip.quot;
129 d = ip.rem;
131 snprintf(dest, 16, "%d.%d.%d.%d", a, b, c, d);
132 return dest;
135 int print_forget(char *path) {
136 FILE *fd = fopen(path, "w+");
138 LINE *step = first;
139 while (step) {
140 LINE *this = step;
141 char *pretty_mac = print_mac(this->mac);
142 char *pretty_ip = print_ip(this->ip);
144 if (fd)
145 fprintf(fd, "dhcp-host=%s,%s,%s,24h\n", pretty_mac, this->fqdn, pretty_ip);
147 free(pretty_mac);
148 free(pretty_ip);
150 free(this->fqdn);
152 step = this->next;
153 free(this);
156 if (fd)
157 fclose(fd);
159 return 0;
162 int ll_array() {
163 int i = 0;
164 array = malloc(sizeof(LINE) * listlen);
166 LINE *step = first;
167 while (step) {
168 array[i].mac = step->mac;
169 array[i].ip = step->ip;
170 array[i].fqdn = step->fqdn;
171 step = step->next;
172 i++;
175 return 0;
178 static int bymac(const void *a, const void *b) {
179 LINE *temp1 = (LINE *) a;
180 LINE *temp2 = (LINE *) b;
182 return temp1->mac - temp2->mac;
185 static int byip(const void *a, const void *b) {
186 LINE *temp1 = (LINE *) a;
187 LINE *temp2 = (LINE *) b;
189 return temp1->ip - temp2->ip;
192 static unsigned long int nextmac() {
193 int i;
194 for (i = 0; i < (listlen - 1); i++) {
195 if (array[i+1].mac != 0 && array[i+1].mac != array[i].mac + 1) return array[i].mac + 1;
197 return array[listlen - 1].mac + 1;
200 static unsigned long int nextip() {
201 int i;
202 for (i = 0; i < (listlen - 1); i++) {
203 if (array[i+1].ip != array[i].ip + 1) return array[i].ip + 1;
205 return array[listlen - 1].ip + 1;
208 int getNewMac(char *cabinet, char *fqdn, char **mac, char **ip) {
209 FILE *fd;
210 char *pad = malloc(sizeof(char) * 128);
212 listlen = 0;
213 first = NULL;
214 last = NULL;
215 array = NULL;
217 if (pad == NULL) return -1;
219 snprintf(pad, 128, "/mnt/netapp/ipservices/%s.conf", cabinet);
221 if ((fd = fopen(pad, "r")) != NULL) {
222 char line[128];
223 while (!feof(fd)) {
224 if (fgets(line, 128, fd) > 0)
225 add_line(line);
227 fclose(fd);
230 unsigned long int new_mac, new_ip;
231 if (listlen == 0) {
232 new_mac = 0;
233 new_ip = 0;
234 } else {
235 ll_array();
237 qsort(array, listlen, sizeof(LINE), bymac);
238 new_mac = nextmac();
240 qsort(array, listlen, sizeof(LINE), byip);
241 new_ip = nextip();
244 if (mac)
245 *mac = print_mac(new_mac);
247 if (ip)
248 *ip = print_ip(new_ip);
250 add(new_mac, new_ip, fqdn);
252 free(array);
254 print_forget(pad);
256 return 0;
258 free(pad);
261 #ifdef WITH_MAIN
262 int main(int argc, char *argv[]) {
263 if (argc == 3) {
264 getNewMac(argv[1], argv[2], NULL, NULL);
266 return 0;
268 #endif