3 * This generates the network table.
5 * Copyright (C) 1994-1998 by the Massachusetts Institute of Technology.
6 * For copying and distribution information, please see the file
10 #include <mit-copyright.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
23 EXEC SQL INCLUDE sqlca;
27 char *whoami = "network.gen";
28 char *db = "moira/moira";
30 char *cidr_from_inaddr(struct in_addr in);
32 int main(int argc, char **argv)
35 char *outf = NULL, *cidr = NULL, address[BUFSIZ], outft[MAXPATHLEN];
37 struct in_addr addr, maskaddr;
38 EXEC SQL BEGIN DECLARE SECTION;
40 char name[SUBNET_NAME_SIZE], description[SUBNET_DESCRIPTION_SIZE];
41 char mask[SUBNET_MASK_SIZE];
42 EXEC SQL END DECLARE SECTION;
49 sprintf(outft, "%s~", outf);
50 if (!(out = fopen(outft, "w")))
52 fprintf(stderr, "unable to open %s for output\n", outf);
58 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
64 EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
66 gettimeofday(&now, NULL);
68 fprintf(out, "; MIT Network Table\n;\n");
69 fprintf(out, "; \t%cAuthor: $\n", '$');
70 fprintf(out, "; \t%cDate: $\n", '$');
71 fprintf(out, "; \t%cRevision: $\n;\n", '$');
72 fprintf(out, "; Network table generated by Moira at %s;\n",
75 EXEC SQL DECLARE x CURSOR FOR SELECT
76 name, snet_id, saddr, mask, description
77 FROM subnet ORDER BY saddr;
81 EXEC SQL FETCH x INTO :name, :id, :saddr, :mask, :description;
88 addr.s_addr = htonl(saddr);
90 maskaddr.s_addr = htonl(atoi(mask));
91 cidr = cidr_from_inaddr(maskaddr);
93 strcpy(address, inet_ntoa(addr));
95 strcat(address, cidr);
97 fprintf(out, "NETWORK : %-16.16s : %-15.15s : %s\n", name,
98 address, strtrim(description));
105 fprintf(out, "; End of automatically generated network table\n");
108 perror("close failed");
116 db_error(sqlca.sqlcode);
120 char *cidr_from_inaddr(struct in_addr in) {
121 char *ptr1, *ptr2, *address, *out;
122 int a, b, c, d, addr, i, j = 0, k = 0;
125 address = inet_ntoa(in);
127 ptr1 = ptr2 = address;
128 ptr2 = (char *)strchr(ptr1, '.');
134 ptr2 = (char *)strchr(ptr1, '.');
140 ptr2 = (char *)strchr(ptr1, '.');
148 if (a < 0 || a > 255 ||
154 addr = d + (c*256) + (b*256*256) + (a*256*256*256);
156 for (i = 0; i < 32; i++) {
157 bits[i] = (addr & 1);
161 while (bits[j] == 0) {
165 while (bits[j] == 1) {
174 out = (char *)malloc(20);
177 sprintf(out, "/%i", k);