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

Revision 23740, 3.7 KB checked in by broder, 16 years ago (diff)
In moira: * New CVS snapshot (Trac: #195) * Drop patches that have been incorporated upstream. * Update to build without krb4 on systems that no longer have it. This doesn't build yet on squeeze, which lacks a krb4 library, but I'm committing now before I start hacking away at a patch to fix that.
Line 
1/* $Id: network.pc,v 1.6 2001-05-23 21:31:03 zacheiss Exp $
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 <stdio.h>
19#include <time.h>
20
21#include "util.h"
22
23EXEC SQL INCLUDE sqlca;
24
25RCSID("$Header: /afs/athena.mit.edu/astaff/project/moiradev/repository/moira/gen/network.pc,v 1.6 2001-05-23 21:31:03 zacheiss Exp $");
26
27char *whoami = "network.gen";
28char *db = "moira/moira";
29
30char *cidr_from_inaddr(struct in_addr in);
31
32int main(int argc, char **argv)
33{
34  FILE *out = stdout;
35  char *outf = NULL, *cidr = NULL, address[BUFSIZ], outft[MAXPATHLEN];
36  struct timeval now;
37  struct in_addr addr, maskaddr;
38  EXEC SQL BEGIN DECLARE SECTION;
39  int id, saddr;
40  char name[SUBNET_NAME_SIZE], description[SUBNET_DESCRIPTION_SIZE];
41  char mask[SUBNET_MASK_SIZE];
42  EXEC SQL END DECLARE SECTION;
43
44  EXEC SQL CONNECT :db;
45
46  if (argc == 2)
47    {
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);
54        }
55    }
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;
65
66  gettimeofday(&now, NULL);
67
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));
74
75  EXEC SQL DECLARE x CURSOR FOR SELECT
76    name, snet_id, saddr, mask, description
77    FROM subnet ORDER BY saddr;
78  EXEC SQL OPEN x;
79  while (1)
80    {
81      EXEC SQL FETCH x INTO :name, :id, :saddr, :mask, :description;
82      if (sqlca.sqlcode)
83        break;
84      if (id == 0)
85        continue;
86      if (!*strtrim(name))
87        continue;
88      addr.s_addr = htonl(saddr);
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));
99    }
100
101  EXEC SQL CLOSE x;
102
103  EXEC SQL COMMIT;
104
105  fprintf(out, "; End of automatically generated network table\n");
106  if (fclose(out))
107    {
108      perror("close failed");
109      exit(MR_CCONFIG);
110    }
111  if (outf)
112    fix_file(outf);
113  exit(MR_SUCCESS);
114
115sqlerr:
116  db_error(sqlca.sqlcode);
117  exit(MR_DBMS_ERR);
118}
119
120char *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}
Note: See TracBrowser for help on using the repository browser.