Dash:
[t2.git] / package / network / iproute2 / ipnm2nwbc.c
blob27c5a95eac660335b73ffe5c2e839cd607ed9536
1 // --- T2-COPYRIGHT-NOTE-BEGIN ---
2 // This copyright note is auto-generated by ./scripts/Create-CopyPatch.
3 //
4 // T2 SDE: package/.../iproute2/ipnm2nwbc.c
5 // Copyright (C) 2004 - 2005 The T2 SDE Project
6 // Copyright (C) 1998 - 2003 ROCK Linux Project
7 //
8 // More information can be found in the files COPYING and README.
9 //
10 // This program is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; version 2 of the License. A copy of the
13 // GNU General Public License can be found in the file COPYING.
14 // --- T2-COPYRIGHT-NOTE-END ---
16 #include <stdio.h>
18 int getbits(int n) {
19 int rc=0;
20 while (n) {
21 if (n&1) rc++;
22 n = n >> 1;
24 return rc;
27 int setbits(int n) {
28 int rc=0;
29 while (n--) {
30 rc = (rc>>1)|(1<<7);
32 return rc;
35 int main(int argc, char ** argv) {
36 int ip[4],nm[4],nw[4],bc[4],c;
37 int verbose=0,nmbits=0,n;
39 if ( argc > 1 && !strcmp(argv[1],"-v") ) verbose=1;
41 if ( argc == 3+verbose &&
42 sscanf(argv[1+verbose],"%d.%d.%d.%d",ip,ip+1,ip+2,ip+3) == 4 &&
43 sscanf(argv[2+verbose],"%d.%d.%d.%d",nm,nm+1,nm+2,nm+3) == 4 ) {
44 nmbits=getbits(nm[0])+getbits(nm[1])+
45 getbits(nm[2])+getbits(nm[3]);
46 } else if ( argc == 2+verbose && sscanf(argv[1+verbose],
47 "%d.%d.%d.%d/%d",ip,ip+1,ip+2,ip+3,&nmbits) == 5 ) {
48 n=nmbits;
49 if (n>0) { nm[0]=setbits(n>8?8:n); n-=8; } else nm[0]=0;
50 if (n>0) { nm[1]=setbits(n>8?8:n); n-=8; } else nm[1]=0;
51 if (n>0) { nm[2]=setbits(n>8?8:n); n-=8; } else nm[2]=0;
52 if (n>0) { nm[3]=setbits(n>8?8:n); n-=8; } else nm[3]=0;
53 } else {
54 fprintf(stderr,"\n"
55 "IP and Netmask to Network and Broadcast converter.\n"
56 "(C) under GPL, 1999 Clifford Wolf\n\n"
57 "Usage: %s [-v] <IP> <Netmask>\n"
58 " %s [-v] <IP>/<Mask>\n\n"
59 "Examples: %s -v 195.170.70.72/25\n"
60 " %s 195.170.70.72 255.255.255.128\n\n",
61 argv[0],argv[0],argv[0],argv[0]);
62 return 1;
65 for (c=0; c<4; c++) {
66 nw[c]=ip[c]&nm[c];
67 bc[c]=nw[c]|(255&~nm[c]);
70 if ( verbose ) {
71 printf("Network: %d.%d.%d.%d / %d\n",
72 nw[0],nw[1],nw[2],nw[3],nmbits);
73 printf("Netmask: %d.%d.%d.%d\n",
74 nm[0],nm[1],nm[2],nm[3]);
75 printf("Broadcast: %d.%d.%d.%d\n",
76 bc[0],bc[1],bc[2],bc[3]);
77 } else {
78 printf("%d.%d.%d.%d %d.%d.%d.%d\n",nw[0],nw[1],
79 nw[2],nw[3],bc[0],bc[1],bc[2],bc[3]);
81 return 0;