6 typedef struct line LINE
;
21 int add(unsigned long int mac
, unsigned long int ip
, char *fqdn
) {
22 LINE
*new = malloc(sizeof(LINE
));
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] == ':' &&
44 LINE
*new = malloc(sizeof(LINE
));
46 if (new == NULL
) return -1;
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]);
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);
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)));
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));
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
115 snprintf(mac
, 8, "%06lX", this->mac
);
116 strncpy(&mac
[6], &mac
[4], 2);
117 strncpy(&mac
[3], &mac
[2], 2);
121 ldiv_t ip
= ldiv(this->ip
, (256*256*256));
123 ip
= ldiv(ip
.rem
, (256*256));
125 ip
= ldiv(ip
.rem
, (256));
129 printf("dhcp-host=00:16:3E:%s,%s,%d.%d.%d.%d,24h\n", mac
, this->fqdn
, a
, b
, c
, d
);
143 array
= malloc(sizeof(LINE
) * listlen
);
147 array
[i
].mac
= step
->mac
;
148 array
[i
].ip
= step
->ip
;
149 array
[i
].fqdn
= step
->fqdn
;
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() {
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() {
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;
188 unsigned long int new_mac
, new_ip
;
191 if ((fd
= fopen("/mnt/netapp/ipservices/dv28.conf", "r")) != NULL
) {
193 if (fgets(line
, 1024, fd
) > 0)
202 qsort(array
, listlen
, sizeof(LINE
), bymac
);
205 qsort(array
, listlen
, sizeof(LINE
), byip
);
209 add(new_mac
, new_ip
, strdup("test.xen"));