From ccfddb5b3f6cd1086450601ea846fa45d5ea8e00 Mon Sep 17 00:00:00 2001 From: Stefan de Konink Date: Tue, 22 Jul 2008 02:33:15 +0200 Subject: [PATCH] Some demo stuff --- experiment.c | 40 +++++++++++ experiment2.c | 33 +++++++++ experiment3.c | 21 ++++++ experiment4.c | 52 ++++++++++++++ experiment5.c | 216 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 362 insertions(+) create mode 100644 experiment.c create mode 100644 experiment2.c create mode 100644 experiment3.c create mode 100644 experiment4.c create mode 100644 experiment5.c diff --git a/experiment.c b/experiment.c new file mode 100644 index 0000000..7db2b74 --- /dev/null +++ b/experiment.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include +#include + +#include + + +virConnectPtr conn = NULL; /* the hypervisor connection */ + +int main(int argc, char*argv[]) { + int error; + int ret = 1; + + /* NULL means connect to local Xen hypervisor */ + conn = virConnectOpenReadOnly(NULL); + if (conn == NULL) { + fprintf(stderr, "Failed to connect to hypervisor\n"); + } + + if (argc == 2) { + unsigned long long required = strtoul(argv[1], (char **) NULL, 10); + unsigned long long free = virNodeGetFreeMemory(conn); + if (errno != ERANGE) + printf("%f\n", (float)required / (float)free); + } + + printf("%lu\n", virNodeGetFreeMemory(conn)); + + virDomainPtr domu = virDomainLookupByName(conn, "klant1_testdomein"); + printf("%lu\n", virDomainGetMaxMemory(domu)); + virDomainFree(domu); + + + virConnectClose(conn); + + return ret; +} diff --git a/experiment2.c b/experiment2.c new file mode 100644 index 0000000..1df8c7c --- /dev/null +++ b/experiment2.c @@ -0,0 +1,33 @@ +#include +#include +#include +#include +#include +#include + +int main() { + char filemac[13]; + unsigned long int mac; + int fd = open("/mnt/netapp/maccounter", O_RDWR|O_CREAT|O_TRUNC); + flock(fd, LOCK_EX); + FILE * fd2 = fdopen(fd, "r"); + + if (fd2 == NULL) printf("ga dood\n"); + if (fread(filemac, sizeof(char), 13, fd2) == 0) { + printf("read = 0\n"); + mac = 0x0; + } else { + mac++; + } + + mac = strtoul(filemac, NULL, 16); + printf("cur: %06X\n", mac); + mac++; + snprintf(filemac, 13, "%06X", mac); + printf("next: %s\n", filemac); + + lseek(fd, 0, SEEK_SET); + write(fd, &filemac, 13); + close(fd); + flock(fd, LOCK_UN); +} diff --git a/experiment3.c b/experiment3.c new file mode 100644 index 0000000..80d06b3 --- /dev/null +++ b/experiment3.c @@ -0,0 +1,21 @@ +#include + +int main() { + FILE *fd; + char mac[13]; + unsigned long int counter = 0; + + if ((fd = fopen("/tmp/maccounter", "r")) != NULL) { + fread(mac, 12, sizeof(char), fd); + mac[13] = '\0'; + counter = strtoul(mac, NULL, 16); + counter++; + fclose(fd); + } + + snprintf(mac, 13, "%012X\n", counter); + if ((fd = fopen("/tmp/maccounter", "w")) != NULL) { + fwrite(mac, 12, sizeof(char), fd); + fclose(fd); + } +} diff --git a/experiment4.c b/experiment4.c new file mode 100644 index 0000000..afa6da2 --- /dev/null +++ b/experiment4.c @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +int main() { + FILE *fd; + char mac[13]; + unsigned long int counter = 0; + + int locker = open("/mnt/netapp/maccounter", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + flock(locker, LOCK_EX); + + if ((fd = fdopen(locker, "w+")) != NULL) { + if (fread(mac, 12, sizeof(char), fd) == 1) { + mac[12] = '\0'; + counter = strtoul(mac, NULL, 16); + counter++; + } + snprintf(mac, 13, "%012lX\n", counter); + rewind(fd); + fwrite(mac, 12, sizeof(char), fd); + fclose(fd); + + char pretty[18]; + pretty[2] = ':'; pretty[5] = ':'; pretty[8] = ':'; pretty[11] = ':'; pretty[14] = ':'; pretty[17] = '\0'; + strncpy(&pretty[0], &mac[0], 2); + strncpy(&pretty[3], &mac[2], 2); + strncpy(&pretty[6], &mac[4], 2); + strncpy(&pretty[9], &mac[6], 2); + strncpy(&pretty[12], &mac[8], 2); + strncpy(&pretty[15], &mac[10], 2); + + // dhcp-host=00:1E:C9:B3:B3:85,xen001.xen,172.16.103.1,24h + printf("dhcp-host=%s,%s.xen,...,24h\n", pretty, "test"); + +// fd = fopen("/mnt/netapp/dnsmasq.conf", "a"); +// fprintf(fd, "dhcp-host=%s,%s,", 10, sizeof(char) +// fclose(fd); + } + + flock(locker, LOCK_UN); + close(locker); + + return 0; +} diff --git a/experiment5.c b/experiment5.c new file mode 100644 index 0000000..bc27f52 --- /dev/null +++ b/experiment5.c @@ -0,0 +1,216 @@ +#include +#include +#include +#include + +typedef struct line LINE; + +struct line { + unsigned long int mac; + unsigned long int ip; + char *fqdn; + LINE *next; +}; + +LINE *first = NULL; +LINE *last = NULL; +LINE *array = NULL; + +int listlen = 0; + +int add(unsigned long int mac, unsigned long int ip, char *fqdn) { + LINE *new = malloc(sizeof(LINE)); + new->mac = mac; + new->ip = ip; + new->fqdn = fqdn; + new->next = NULL; + + if (!first) + first = new; + else + last->next = new; + + last = new; + + return 0; +} + +int add_line(char *line) { + if (strncmp(line, "dhcp-host=", 10) == 0 && + line[12] == ':' && line[15] == ':' && line[18] == ':' && line[21] == ':' && line[24] == ':' && + line[27] == ',') { + char *ip, *nextip; + char macstring[4]; + LINE *new = malloc(sizeof(LINE)); + + if (new == NULL) return -1; + + listlen++; + macstring[2] = '\0'; + macstring[3] = '\0'; + + new->mac = 0; + new->ip = 0; + new->next= NULL; + + strncpy(macstring, &line[19], 2); + new->mac += (strtoul(macstring, NULL, 16) * (unsigned long int)(256 * 256)); + strncpy(macstring, &line[22], 2); + new->mac += (strtoul(macstring, NULL, 16) * (unsigned long int)(256)); + strncpy(macstring, &line[25], 2); + new->mac += (strtoul(macstring, NULL, 16)); + + ip = strchr(&line[28], ','); + new->fqdn = strndup(&line[28], ip - &line[28]); + ip++; + + macstring[1] = '\0'; macstring[2] = '\0'; + strncpy(macstring, ip, (nextip = strchr(ip, '.')) - ip); + new->ip += (strtoul(macstring, NULL, 10) * (unsigned long int)(256 * 256 * 256)); +// printf("%s %lu\n", macstring, new->ip); + ip = nextip + 1; + + macstring[1] = '\0'; macstring[2] = '\0'; + strncpy(macstring, ip, (nextip = strchr(ip, '.')) - ip); + new->ip += (strtoul(macstring, NULL, 10) * (unsigned long int)(256 * 256)); +// printf("%s %lu\n", macstring, (strtoul(macstring, NULL, 10) * (unsigned long int)(256 * 256))); + ip = nextip + 1; + + macstring[1] = '\0'; macstring[2] = '\0'; + strncpy(macstring, ip, (nextip = strchr(ip, '.')) - ip); + new->ip += (strtoul(macstring, NULL, 10) * (unsigned long int)(256)); +// printf("%s %lu\n", macstring, strtoul(macstring, NULL, 10) * (unsigned long int)(256)); + ip = nextip + 1; + + macstring[1] = '\0'; macstring[2] = '\0'; + strncpy(macstring, ip, (nextip = strchr(ip, ',')) - ip); + new->ip += (strtoul(macstring, NULL, 10)); +// printf("%s %lu\n", macstring, strtoul(macstring, NULL, 10)); + +// printf("%06lX %s %lu %lu\n", new->mac, new->fqdn, new->mac, new->ip); +// dhcp-host=00:1E:C9:B3:B3:85,xen001.xen,172.16.103.1,24h +// + + if (!first) + first = new; + else + last->next = new; + + last = new; + + return 0; + } + + return -1; +} + +int print_forget() { + LINE *step = first; + while (step) { + int a, b, c, d; + char mac[9]; + mac[8] = '\0'; + LINE *this = step; + + snprintf(mac, 8, "%06lX", this->mac); + strncpy(&mac[6], &mac[4], 2); + strncpy(&mac[3], &mac[2], 2); + mac[2] = ':'; + mac[5] = ':'; + + ldiv_t ip = ldiv(this->ip, (256*256*256)); + a = ip.quot; + ip = ldiv(ip.rem, (256*256)); + b = ip.quot; + ip = ldiv(ip.rem, (256)); + c = ip.quot; + d = ip.rem; + + printf("dhcp-host=00:16:3E:%s,%s,%d.%d.%d.%d,24h\n", mac, this->fqdn, a, b, c, d); + + + free(this->fqdn); + + step = this->next; + free(this); + } + + return 0; +} + +int ll_array() { + int i = 0; + array = malloc(sizeof(LINE) * listlen); + + LINE *step = first; + while (step) { + array[i].mac = step->mac; + array[i].ip = step->ip; + array[i].fqdn = step->fqdn; + step = step->next; + i++; + } + + return 0; +} + +static int bymac(const void *a, const void *b) { + LINE *temp1 = (LINE *) a; + LINE *temp2 = (LINE *) b; + + return temp1->mac - temp2->mac; +} + +static int byip(const void *a, const void *b) { + LINE *temp1 = (LINE *) a; + LINE *temp2 = (LINE *) b; + + return temp1->ip - temp2->ip; +} + +static unsigned long int nextmac() { + int i; + for (i = 0; i < (listlen - 1); i++) { + if (array[i+1].mac != array[i].mac + 1) return array[i].mac + 1; + } + return array[listlen - 1].mac + 1; +} + +static unsigned long int nextip() { + int i; + for (i = 0; i < (listlen - 1); i++) { + if (array[i+1].ip != array[i].ip + 1) return array[i].ip + 1; + } + return array[listlen - 1].ip + 1; +} + +int main() { + unsigned long int new_mac, new_ip; + char line[1024]; + FILE *fd; + if ((fd = fopen("/mnt/netapp/ipservices/dv28.conf", "r")) != NULL) { + while (!feof(fd)) { + if (fgets(line, 1024, fd) > 0) + add_line(line); + } + fclose(fd); + } + + if (listlen > 0) { + ll_array(); + + qsort(array, listlen, sizeof(LINE), bymac); + new_mac = nextmac(); + + qsort(array, listlen, sizeof(LINE), byip); + new_ip = nextip(); + } + + add(new_mac, new_ip, strdup("test.xen")); + + print_forget(); + + free(array); + + return 0; +} -- 2.11.4.GIT