Some demo stuff
[handlervirt.git] / experiment5.c
blobbc27f52edd70f4272d8b11a8411d158c04598065
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 = 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 int print_forget() {
108 LINE *step = first;
109 while (step) {
110 int a, b, c, d;
111 char mac[9];
112 mac[8] = '\0';
113 LINE *this = step;
115 snprintf(mac, 8, "%06lX", this->mac);
116 strncpy(&mac[6], &mac[4], 2);
117 strncpy(&mac[3], &mac[2], 2);
118 mac[2] = ':';
119 mac[5] = ':';
121 ldiv_t ip = ldiv(this->ip, (256*256*256));
122 a = ip.quot;
123 ip = ldiv(ip.rem, (256*256));
124 b = ip.quot;
125 ip = ldiv(ip.rem, (256));
126 c = ip.quot;
127 d = ip.rem;
129 printf("dhcp-host=00:16:3E:%s,%s,%d.%d.%d.%d,24h\n", mac, this->fqdn, a, b, c, d);
132 free(this->fqdn);
134 step = this->next;
135 free(this);
138 return 0;
141 int ll_array() {
142 int i = 0;
143 array = malloc(sizeof(LINE) * listlen);
145 LINE *step = first;
146 while (step) {
147 array[i].mac = step->mac;
148 array[i].ip = step->ip;
149 array[i].fqdn = step->fqdn;
150 step = step->next;
151 i++;
154 return 0;
157 static int bymac(const void *a, const void *b) {
158 LINE *temp1 = (LINE *) a;
159 LINE *temp2 = (LINE *) b;
161 return temp1->mac - temp2->mac;
164 static int byip(const void *a, const void *b) {
165 LINE *temp1 = (LINE *) a;
166 LINE *temp2 = (LINE *) b;
168 return temp1->ip - temp2->ip;
171 static unsigned long int nextmac() {
172 int i;
173 for (i = 0; i < (listlen - 1); i++) {
174 if (array[i+1].mac != array[i].mac + 1) return array[i].mac + 1;
176 return array[listlen - 1].mac + 1;
179 static unsigned long int nextip() {
180 int i;
181 for (i = 0; i < (listlen - 1); i++) {
182 if (array[i+1].ip != array[i].ip + 1) return array[i].ip + 1;
184 return array[listlen - 1].ip + 1;
187 int main() {
188 unsigned long int new_mac, new_ip;
189 char line[1024];
190 FILE *fd;
191 if ((fd = fopen("/mnt/netapp/ipservices/dv28.conf", "r")) != NULL) {
192 while (!feof(fd)) {
193 if (fgets(line, 1024, fd) > 0)
194 add_line(line);
196 fclose(fd);
199 if (listlen > 0) {
200 ll_array();
202 qsort(array, listlen, sizeof(LINE), bymac);
203 new_mac = nextmac();
205 qsort(array, listlen, sizeof(LINE), byip);
206 new_ip = nextip();
209 add(new_mac, new_ip, strdup("test.xen"));
211 print_forget();
213 free(array);
215 return 0;