1 /*
2 ctonm.c, Copyright (C) 2001-2003 Joe Laffey
3
4 $Revision: 1.17 $
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #else
25 #include "myTypes.h"
26 #endif
27
28
29 #include "ctonm.h"
30 #include "bitfill.h"
31 #include "bitcount.h"
32
ctonm_main(uint32 myInVal)33 int ctonm_main(uint32 myInVal)
34 {
35
36 uint32 myVal;
37
38 char myNetmask[NM_LEN];
39 char myNetmask2[NM_LEN];
40 char cidr[4];
41
42 uint32 usableIps;
43
44
45 if(myInVal >32) /* it's unsigned so won't be < 0 */
46 {
47 fprintf(stderr, "%s: CIDR notations must be a number between 0 and 32 inclusive!\n", PACKAGE);
48 return(EX_DATAERR);
49 }
50
51
52 myVal = bitfill_from_left(myInVal);
53
54
55 u_int_to_octets(myVal, (char*)myNetmask);
56 u_int_to_octets(~myVal, (char*)myNetmask2);
57
58 usableIps = ~myVal;
59
60 /* check for underflow - since we can't have an unsigned go negative */
61 if(usableIps > 0)
62 usableIps -= 1;
63
64
65 snprintf(cidr, 4,"%" U_INT_32_PRINTF_STRING, myInVal);
66 printdata(myVal, cidr, (char*) myNetmask, (char*) myNetmask2, usableIps);
67
68 return(0);
69 }
70
71