source: trunk/third/moira/gen/network.pc @ 24319

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