6 typedef struct line LINE
;
15 int add(LINE
*first
, LINE
*last
, unsigned long int mac
, unsigned long int ip
, char *fqdn
) {
16 LINE
*new = malloc(sizeof(LINE
));
19 new->fqdn
= strdup(fqdn
);
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] == ':' &&
38 LINE
*new = malloc(sizeof(LINE
));
40 if (new == NULL
) return -1;
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
);
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);
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)));
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));
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
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];
110 macstring
[17] = '\0';
114 char * print_ip(unsigned long int ipaddress
) {
115 char *dest
= (char *) malloc(sizeof(char*) * 16);
117 ldiv_t ip
= ldiv(ipaddress
, (256*256*256));
119 ip
= ldiv(ip
.rem
, (256*256));
121 ip
= ldiv(ip
.rem
, (256));
125 snprintf(dest
, 16, "%d.%d.%d.%d", a
, b
, c
, d
);
129 int print_forget(LINE
*first
, char *path
) {
130 FILE *fd
= fopen(path
, "w");
135 char *pretty_mac
= print_mac(this->mac
);
136 char *pretty_ip
= print_ip(this->ip
);
139 fprintf(fd
, "dhcp-host=%s,%s,%s,24h\n", pretty_mac
, this->fqdn
, pretty_ip
);
144 if (this->fqdn
!= NULL
)
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
) {
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
) {
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
) {
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
);
204 if ((fd
= fopen(pad
, "r")) != NULL
) {
207 if (fgets(line
, 128, fd
) > 0)
208 if (add_line(first
, last
, line
) == 0)
215 array
= malloc(sizeof(LINE
) * listlen
);
224 printf("i = %d\n", i
);
225 array
[i
].mac
= step
->mac
;
226 array
[i
].ip
= step
->ip
;
227 array
[i
].fqdn
= step
->fqdn
;
232 unsigned long int new_mac
, new_ip
;
234 qsort(array
, listlen
, sizeof(LINE
), bymac
);
235 new_mac
= nextmac(array
, listlen
);
237 *mac
= print_mac(new_mac
);
239 qsort(array
, listlen
, sizeof(LINE
), byip
);
240 new_ip
= nextip(array
, listlen
);
243 *ip
= print_ip(new_ip
);
245 add(first
, last
, new_mac
, new_ip
, fqdn
);
249 print_forget(first
, pad
);
260 int main(int argc
, char *argv
[]) {
262 getNewMac(argv
[1], argv
[2], NULL
, NULL
);