1 /*
2 main.c, Copyright (C) 2001-2003 Joe Laffey
3 Main for Whatmask
4
5 $Revision: 1.21 $
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #else
25 #include "myTypes.h"
26 #endif
27
28
29 #include "whatnet.h"
30 #include "usage.h"
31
32 #include "main.h"
33
34 #if __dest_os == __mac_os
main(void)35 int main(void)
36 #else
37 int main(int argc, char** argv)
38 #endif
39 {
40
41 uint32 val1;
42 char *inStrPtr;
43 int retVal;
44 char* slashPos;
45
46 #if __dest_os == __mac_os
47 char inStr[256];
48 char line[256];
49 #endif
50
51 #ifdef HAVE_LOCALE_H
52 if ( !setlocale(LC_NUMERIC, "") )
53 {
54 fprintf(stderr, "* %s: WARNING: Unable to set locale to default locale!\n* Please check your LANG and/or LC_NUMERIC environment variables.\n* US users set LC_NUMERIC to 'en_US'\n* Or, you may want to recompile without local support.\n", PACKAGE);
55 /* return(EX_SOFTWARE); */
56 if ( !setlocale(LC_NUMERIC, "C") )
57 {
58 fprintf(stderr, "%s: Unable to set locale to default 'C' locale! \nYou may want to recompile without local support.\n", PACKAGE);
59 return(EX_SOFTWARE);
60 }
61 }
62 #endif
63
64
65 #if __dest_os == __mac_os
66 char inStr[256];
67 char line[256];
68
69 inStrPtr = inStr;
70
71 printf("%s v%s, Copyright (C) 2001-2003 Joe Laffey <joe@laffeycomputer.com>\n", PACKAGE, VERSION);
72 printf("Visit: http://www.laffeycomputer.com/software.html for updates.\n\n");
73
74 printf("%s comes with ABSOLUTELY NO WARRANTY; for details see the COPYING file\nthat accompained this distribution. ", PACKAGE);
75 printf("This is free software, and you are welcome\nto redistribute it");
76 printf(" under the terms of GNU PUBLIC LICENSE.\n");
77
78 usage();
79 printf("\n");
80
81 /* Loop for ever for Mac OS Classic because there is no command line */
82 while(1)
83 {
84
85 printf("\nEnter the netmask or ip/netmask: ");
86 fgets(line, 255, stdin);
87 if(sscanf(line,"%255s", inStrPtr) != 1)
88 {
89 usage();
90 continue;
91 }
92
93 #else
94
95
96 if(argc < 2 || argc > 3)
97 return(usage());
98
99 inStrPtr = argv[1];
100
101
102 #endif
103
104
105
106
107
108
109 #if __dest_os != __mac_os
110 if(argc == 3)
111 {
112 /* if argv[2] starts with a slash '/' we skip it */
113 retVal = whatnet_main(argv[1], ((argv[2][0] == '/')?argv[2]+1:argv[2]) );
114 }
115 else
116 {
117 #endif
118
119 if(inStrPtr[0] == '/') /* drop a leading slash */
120 inStrPtr += 1;
121
122 slashPos = strchr(inStrPtr, '/');
123 if(slashPos != NULL )
124 {
125 /* This is an ip/netmask combo to be processed as net info */
126
127 /* assume the input is ip/mask */
128
129 slashPos[0] = '\0'; /* terminate the ip string by putting a null in place of the slash */
130
131 retVal = whatnet_main(inStrPtr, slashPos + 1);
132 }
133 else
134 {
135
136 if( (inStrPtr[0] == '0')
137 && (inStrPtr[1]=='x' || inStrPtr[1]=='X')
138 )
139 {
140
141 if(
142 !( (inStrPtr[2] >= 'a' && inStrPtr[2] <= 'f')
143 ||(inStrPtr[2] >= 'A' && inStrPtr[2] <= 'F')
144 ||(inStrPtr[2] >= '0' && inStrPtr[2] <= '9')
145 ) ||
146 !( (inStrPtr[3] >= 'a' && inStrPtr[3] <= 'f')
147 ||(inStrPtr[3] >= 'A' && inStrPtr[3] <= 'F')
148 ||(inStrPtr[3] >= '0' && inStrPtr[3] <= '9')
149 ) ||
150 !( (inStrPtr[4] >= 'a' && inStrPtr[4] <= 'f')
151 ||(inStrPtr[4] >= 'A' && inStrPtr[4] <= 'F')
152 ||(inStrPtr[4] >= '0' && inStrPtr[4] <= '9')
153 ) ||
154 !( (inStrPtr[5] >= 'a' && inStrPtr[5] <= 'f')
155 ||(inStrPtr[5] >= 'A' && inStrPtr[5] <= 'F')
156 ||(inStrPtr[5] >= '0' && inStrPtr[5] <= '9')
157 ) ||
158 !( (inStrPtr[6] >= 'a' && inStrPtr[6] <= 'f')
159 ||(inStrPtr[6] >= 'A' && inStrPtr[6] <= 'F')
160 ||(inStrPtr[6] >= '0' && inStrPtr[6] <= '9')
161 ) ||
162 !( (inStrPtr[7] >= 'A' && inStrPtr[7] <= 'f')
163 ||(inStrPtr[7] >= 'A' && inStrPtr[7] <= 'F')
164 ||(inStrPtr[7] >= '0' && inStrPtr[7] <= '9')
165 ) ||
166 !( (inStrPtr[8] >= 'a' && inStrPtr[8] <= 'f')
167 ||(inStrPtr[8] >= 'A' && inStrPtr[8] <= 'F')
168 ||(inStrPtr[8] >= '0' && inStrPtr[8] <= '9')
169 ) ||
170 !( (inStrPtr[9] >= 'a' && inStrPtr[9] <= 'f')
171 ||(inStrPtr[9] >= 'A' && inStrPtr[9] <= 'F')
172 ||(inStrPtr[9] >= '0' && inStrPtr[9] <= '9')
173 ) ||
174 ( strlen(inStrPtr) != 10)
175 )
176 {
177 /*doesn't have all 8 hex chars*/
178 fprintf(stderr, "%s: \"%s\" is not a valid subnet mask!\n(Hex values need 8 chars [0-9][a-f][A-F])\n", PACKAGE, inStrPtr);
179 retVal = EX_DATAERR;
180
181 }
182 else if( sscanf((char*)(inStrPtr+2), "%08x", &val1) == 1)
183 {
184 /* hex conversion succeeded so we have a netmask */
185 retVal = nmtoc_main(inStrPtr,val1);
186 }
187 }
188 else
189 {
190 /* try to convert the octets - this also returns 1 if the conversion fails */
191 if(octets_to_u_int(&val1, inStrPtr))
192 {
193 /*assume CIDR */
194 if((strchr(inStrPtr, '.') != NULL) || sscanf(inStrPtr, "%" U_INT_32_PRINTF_STRING, &val1) != 1)
195 {
196 /* not CIDR */
197 fprintf(stderr, "%s: \"%s\" is not a valid subnet mask or wildcard bit mask!\n", PACKAGE, inStrPtr);
198 retVal = EX_DATAERR;
199 }
200 else
201 {
202 /*try CIDR */
203 retVal = ctonm_main(val1);
204 }
205 }
206 else
207 {
208 /* octet conversion succeeded so we have a netmask */
209 retVal = nmtoc_main(inStrPtr,val1);
210 }
211 }
212 }
213
214
215 } /**** */ /* This closing brace is double purpose! */
216 /* It serves as the close for the else of if(argc == 3) and also as the */
217 /* end of while(1) loop forever for Mac OS classic */
218
219
220 return(retVal);
221 }
222
223