6 typedef struct line LINE
;
21 int add(unsigned long int mac
, unsigned long int ip
, char *fqdn
) {
22 LINE
*new = malloc(sizeof(LINE
));
25 new->fqdn
= strdup(fqdn
);
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
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];
116 macstring
[17] = '\0';
120 char * print_ip(unsigned long int ipaddress
) {
121 char *dest
= (char *) malloc(sizeof(char*) * 16);
123 ldiv_t ip
= ldiv(ipaddress
, (256*256*256));
125 ip
= ldiv(ip
.rem
, (256*256));
127 ip
= ldiv(ip
.rem
, (256));
131 snprintf(dest
, 16, "%d.%d.%d.%d", a
, b
, c
, d
);
135 int print_forget(char *path
) {
136 FILE *fd
= fopen(path
, "w+");
141 char *pretty_mac
= print_mac(this->mac
);
142 char *pretty_ip
= print_ip(this->ip
);
145 fprintf(fd
, "dhcp-host=%s,%s,%s,24h\n", pretty_mac
, this->fqdn
, pretty_ip
);
164 array
= malloc(sizeof(LINE
) * listlen
);
168 array
[i
].mac
= step
->mac
;
169 array
[i
].ip
= step
->ip
;
170 array
[i
].fqdn
= step
->fqdn
;
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() {
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() {
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
) {
210 char *pad
= malloc(sizeof(char) * 128);
217 if (pad
== NULL
) return -1;
219 snprintf(pad
, 128, "/mnt/netapp/ipservices/%s.conf", cabinet
);
221 if ((fd
= fopen(pad
, "r")) != NULL
) {
224 if (fgets(line
, 128, fd
) > 0)
230 unsigned long int new_mac
, new_ip
;
237 qsort(array
, listlen
, sizeof(LINE
), bymac
);
240 qsort(array
, listlen
, sizeof(LINE
), byip
);
245 *mac
= print_mac(new_mac
);
248 *ip
= print_ip(new_ip
);
250 add(new_mac
, new_ip
, fqdn
);
262 int main(int argc
, char *argv
[]) {
264 getNewMac(argv
[1], argv
[2], NULL
, NULL
);