]>
Commit | Line | Data |
---|---|---|
7ac48069 | 1 | /* $Id$ |
ff4ac895 | 2 | * |
3 | * This generates the network table. | |
4 | * | |
7ac48069 | 5 | * Copyright (C) 1994-1998 by the Massachusetts Institute of Technology. |
6 | * For copying and distribution information, please see the file | |
7 | * <mit-copyright.h>. | |
ff4ac895 | 8 | */ |
9 | ||
10 | #include <mit-copyright.h> | |
ff4ac895 | 11 | #include <moira.h> |
7ac48069 | 12 | |
ff4ac895 | 13 | #include <sys/stat.h> |
7ac48069 | 14 | |
9c04b191 | 15 | #include <netinet/in.h> |
7ac48069 | 16 | #include <arpa/inet.h> |
17 | ||
18 | #include <stdio.h> | |
19 | #include <time.h> | |
20 | ||
21 | #include "util.h" | |
ff4ac895 | 22 | |
23 | EXEC SQL INCLUDE sqlca; | |
24 | ||
7ac48069 | 25 | RCSID("$Header$"); |
26 | ||
ff4ac895 | 27 | char *whoami = "network.gen"; |
9c04b191 | 28 | char *db = "moira/moira"; |
ff4ac895 | 29 | |
4b150325 | 30 | char *cidr_from_inaddr(struct in_addr in); |
31 | ||
5eaef520 | 32 | int main(int argc, char **argv) |
ff4ac895 | 33 | { |
5eaef520 | 34 | FILE *out = stdout; |
4b150325 | 35 | char *outf = NULL, *cidr = NULL, address[BUFSIZ], outft[MAXPATHLEN]; |
5eaef520 | 36 | struct timeval now; |
4b150325 | 37 | struct in_addr addr, maskaddr; |
5eaef520 | 38 | EXEC SQL BEGIN DECLARE SECTION; |
39 | int id, saddr; | |
dfaf9b68 | 40 | char name[SUBNET_NAME_SIZE], description[SUBNET_DESCRIPTION_SIZE]; |
4b150325 | 41 | char mask[SUBNET_MASK_SIZE]; |
5eaef520 | 42 | EXEC SQL END DECLARE SECTION; |
ff4ac895 | 43 | |
5eaef520 | 44 | EXEC SQL CONNECT :db; |
ff4ac895 | 45 | |
5eaef520 | 46 | if (argc == 2) |
47 | { | |
5eaef520 | 48 | outf = argv[1]; |
49 | sprintf(outft, "%s~", outf); | |
50 | if (!(out = fopen(outft, "w"))) | |
51 | { | |
52 | fprintf(stderr, "unable to open %s for output\n", outf); | |
53 | exit(MR_OCONFIG); | |
ff4ac895 | 54 | } |
ff4ac895 | 55 | } |
5eaef520 | 56 | else if (argc != 1) |
57 | { | |
58 | fprintf(stderr, "usage: %s [outfile]\n", argv[0]); | |
59 | exit(MR_ARGS); | |
60 | } | |
61 | else | |
62 | outf = NULL; | |
63 | ||
64 | EXEC SQL WHENEVER SQLERROR GOTO sqlerr; | |
ff4ac895 | 65 | |
5eaef520 | 66 | gettimeofday(&now, NULL); |
ff4ac895 | 67 | |
5eaef520 | 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", | |
73 | ctime(&now.tv_sec)); | |
ff4ac895 | 74 | |
5eaef520 | 75 | EXEC SQL DECLARE x CURSOR FOR SELECT |
4b150325 | 76 | name, snet_id, saddr, mask, description |
5eaef520 | 77 | FROM subnet ORDER BY saddr; |
78 | EXEC SQL OPEN x; | |
79 | while (1) | |
80 | { | |
4b150325 | 81 | EXEC SQL FETCH x INTO :name, :id, :saddr, :mask, :description; |
5eaef520 | 82 | if (sqlca.sqlcode) |
83 | break; | |
84 | if (id == 0) | |
85 | continue; | |
86 | if (!*strtrim(name)) | |
87 | continue; | |
88 | addr.s_addr = htonl(saddr); | |
4b150325 | 89 | |
90 | maskaddr.s_addr = htonl(atoi(mask)); | |
91 | cidr = cidr_from_inaddr(maskaddr); | |
92 | ||
93 | strcpy(address, inet_ntoa(addr)); | |
94 | if (cidr) | |
95 | strcat(address, cidr); | |
96 | ||
97 | fprintf(out, "NETWORK : %-16.16s : %-15.15s : %s\n", name, | |
98 | address, strtrim(description)); | |
ff4ac895 | 99 | } |
100 | ||
5eaef520 | 101 | EXEC SQL CLOSE x; |
9c04b191 | 102 | |
5eaef520 | 103 | EXEC SQL COMMIT; |
ff4ac895 | 104 | |
5eaef520 | 105 | fprintf(out, "; End of automatically generated network table\n"); |
106 | if (fclose(out)) | |
107 | { | |
108 | perror("close failed"); | |
109 | exit(MR_CCONFIG); | |
ff4ac895 | 110 | } |
5eaef520 | 111 | if (outf) |
112 | fix_file(outf); | |
113 | exit(MR_SUCCESS); | |
ff4ac895 | 114 | |
5eaef520 | 115 | sqlerr: |
116 | db_error(sqlca.sqlcode); | |
117 | exit(MR_DBMS_ERR); | |
ff4ac895 | 118 | } |
4b150325 | 119 | |
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; | |
123 | int bits[32]; | |
124 | ||
125 | address = inet_ntoa(in); | |
126 | ||
127 | ptr1 = ptr2 = address; | |
128 | ptr2 = (char *)strchr(ptr1, '.'); | |
129 | if (!ptr2) | |
130 | return(NULL); | |
131 | a = atoi(ptr1); | |
132 | ||
133 | ptr1 = ptr2 + 1; | |
134 | ptr2 = (char *)strchr(ptr1, '.'); | |
135 | if (!ptr2) | |
136 | return(NULL); | |
137 | b = atoi(ptr1); | |
138 | ||
139 | ptr1 = ptr2 + 1; | |
140 | ptr2 = (char *)strchr(ptr1, '.'); | |
141 | if (!ptr2) | |
142 | return(NULL); | |
143 | c = atoi(ptr1); | |
144 | ||
145 | ptr1 = ptr2 + 1; | |
146 | d = atoi(ptr1); | |
147 | ||
148 | if (a < 0 || a > 255 || | |
149 | b < 0 || b > 255 || | |
150 | c < 0 || c > 255 || | |
151 | d < 0 || d > 255) | |
152 | return(NULL); | |
153 | ||
154 | addr = d + (c*256) + (b*256*256) + (a*256*256*256); | |
155 | ||
156 | for (i = 0; i < 32; i++) { | |
157 | bits[i] = (addr & 1); | |
158 | addr = (addr >> 1); | |
159 | } | |
160 | ||
161 | while (bits[j] == 0) { | |
162 | j++; | |
163 | if (j > 31) break; | |
164 | } | |
165 | while (bits[j] == 1) { | |
166 | j++; | |
167 | k++; | |
168 | if (j > 31) break; | |
169 | } | |
170 | ||
171 | if (j != 32) | |
172 | return(NULL); | |
173 | ||
174 | out = (char *)malloc(20); | |
175 | if (!out) | |
176 | exit(MR_NO_MEM); | |
177 | sprintf(out, "/%i", k); | |
178 | ||
179 | return(out); | |
180 | } |