PEP 55 Update license on source files to current license text and date
[tpot/pegasus/.git] / src / Providers / linux / ProviderSupport / DeviceLocator / IPV4NetInformation.cpp
1 //%2003////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2000, 2001, 2002  BMC Software, Hewlett-Packard Development
4 // Company, L. P., IBM Corp., The Open Group, Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L. P.;
6 // IBM Corp.; EMC Corporation, The Open Group.
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to
10 // deal in the Software without restriction, including without limitation the
11 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 // sell copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 // 
15 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
16 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
17 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
18 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
19 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 //
24 //==============================================================================
25 //
26 // Author: Christopher Neufeld <neufeld@linuxcare.com>
27 //         David Kennedy       <dkennedy@linuxcare.com>
28 //
29 // Modified By: David Kennedy       <dkennedy@linuxcare.com>
30 //              Christopher Neufeld <neufeld@linuxcare.com>
31 //              Al Stone            <ahs3@fc.hp.com>
32 //
33 //%////////////////////////////////////////////////////////////////////////////
34 //
35 //  IPV4 data classes
36 //
37
38 #include <stdio.h>
39
40 #include <Pegasus/Common/System.h>
41 #include <Pegasus/Common/String.h>
42 #include "IPV4NetInformation.h"
43
44
45 PEGASUS_NAMESPACE_BEGIN
46
47
48 //////////////////////////////////////////
49 //
50 //   IPV4NetInformation methods
51 //
52 //////////////////////////////////////////
53
54 void IPV4NetInformation::ParseDottedQuad(String const &quad_string, 
55                                          validated<dottedquad_t> *quad) const
56 {
57   int int_octets[4];
58   int fields, i;
59   dottedquad_t byte_octets;
60
61   fields = sscanf(quad_string.getCString(), "%d.%d.%d.%d",
62                   &int_octets[0], &int_octets[1],
63                   &int_octets[2], &int_octets[3]);
64
65
66   if (fields != 4)
67     return;
68
69   for (i = 0; i < 4; i++) {
70     if (int_octets[i] < 0 || int_octets[i] > 255)
71       return;
72     byte_octets.octets[i] = (unsigned char) int_octets[i];
73   }
74
75   /* If we make it here, we have a valid dotted quad, set the value on the
76    * validated type. */
77   quad->setValue(byte_octets);
78 }
79
80
81 #define IPV4_ADDR_MAXSIZE 20
82
83 String IPV4NetInformation::RenderDottedQuad(validated<dottedquad_t> const *quad) const
84 {
85   char renderbuffer[IPV4_ADDR_MAXSIZE];
86   
87   if (!quad->is_valid())
88     return INVALID_ADDRESS_STRING;
89
90   snprintf(renderbuffer, IPV4_ADDR_MAXSIZE, "%u.%u.%u.%u",
91            (int) quad->getValue().octets[0], (int) quad->getValue().octets[1],
92            (int) quad->getValue().octets[2], (int) quad->getValue().octets[3]);
93
94   return String(renderbuffer);
95 }
96
97
98 bool IPV4NetInformation::DottedQuadAllZeroes(validated<dottedquad_t> const *address) const
99 {
100   return (address->getValue().octets[0] == 0 &&
101           address->getValue().octets[1] == 0 &&
102           address->getValue().octets[2] == 0 &&
103           address->getValue().octets[3] == 0);
104 }
105
106
107 //////////////////////////////////////////
108 //
109 //   IPV4RouteInformation methods
110 //
111 //////////////////////////////////////////
112
113
114 IPV4RouteInformation::IPV4RouteInformation(void)
115 {
116   ptype = IPV4_ROUTE_INFO;
117
118   route_is_dynamic = false;
119   interface = "";
120 }
121
122
123 // Parses a dotted-quad format address and assigns it to destination.
124 // Returns 0 on success, -1 on failure.
125 int IPV4RouteInformation::SetDestination(String const &dest_string)
126 {
127   ParseDottedQuad(dest_string, &destination);
128
129   if (destination.is_valid())
130     return 0;
131   else
132     return -1;
133 }
134
135 // Parses a dotted-quad format address and assigns it to destination.
136 // Returns 0 on success, -1 on failure.
137 int IPV4RouteInformation::SetGateway(String const &gw_string)
138 {
139   ParseDottedQuad(gw_string, &gateway);
140
141   if (gateway.is_valid())
142     return 0;
143   else
144     return -1;
145 }
146
147 // Parses a dotted-quad format address and assigns it to destination.
148 // Returns 0 on success, -1 on failure.
149 int IPV4RouteInformation::SetMask(String const &mask_string)
150 {
151   ParseDottedQuad(mask_string, &mask);
152
153   if (mask.is_valid())
154     return 0;
155   else
156     return -1;
157 }
158
159
160 void IPV4RouteInformation::SetRouteDynamicFromFlags(String const &flags)
161 {
162   route_is_dynamic = (flags.find('D') != PEG_NOT_FOUND ||
163                       flags.find('R') != PEG_NOT_FOUND ||
164                       flags.find('M') != PEG_NOT_FOUND);
165 }
166
167
168 String IPV4RouteInformation::print(void) const
169 {
170   String retval = "";
171
172   if (!destination.is_valid() ||
173       !mask.is_valid() ||
174       !gateway.is_valid())
175     return "";
176
177   if (route_is_dynamic)
178     retval = "(dynamic) ";
179
180   if (DottedQuadAllZeroes(&destination)) {
181     if (!DottedQuadAllZeroes(&mask))
182       return "Unrecognized route.  default destination with non-default mask.";
183
184     retval.append("Default route:  out on interface \"" + GetInterface() + "\"");
185     retval.append(" through the gateway machine at " + RenderDottedQuad(&gateway));
186     return retval;
187   }
188
189   retval.append("Route to addresses " + RenderDottedQuad(&destination) + "/");
190   retval.append(RenderDottedQuad(&mask) + ":  out on interface \"");
191   retval.append(GetInterface() + "\"");
192   if (!DottedQuadAllZeroes(&gateway))
193     retval.append(" through the gateway machine at " + RenderDottedQuad(&gateway));
194
195   return retval;
196 }
197
198
199 //////////////////////////////////////////
200 //
201 //   IPV4IFInformation methods
202 //
203 //////////////////////////////////////////
204
205 IPV4IFInformation::IPV4IFInformation(void)
206 {
207   ptype = IPV4_INTERFACE_INFO;
208
209   interface = encapsulation = hwaddr = "";
210   status = flags = "";
211 }
212
213
214 int IPV4IFInformation::SetAddress(String const &addr_string)
215 {
216   ParseDottedQuad(addr_string, &address);
217
218   if (address.is_valid())
219     return 0;
220   else
221     return -1;
222 }
223
224 int IPV4IFInformation::SetBcast(String const &bcast_string)
225 {
226   ParseDottedQuad(bcast_string, &bcast);
227
228   if (bcast.is_valid())
229     return 0;
230   else
231     return -1;
232 }
233
234
235 int IPV4IFInformation::SetNetmask(String const &mask_string)
236 {
237   ParseDottedQuad(mask_string, &netmask);
238
239   if (netmask.is_valid())
240     return 0;
241   else
242     return -1;
243 }
244
245
246 int IPV4IFInformation::SetMTU(String const &mtu_string)
247 {
248   char *eptr;
249   Uint32 hold_mtu;
250
251   CString p;
252   p = mtu_string.getCString();
253   hold_mtu = strtoul(p, &eptr, 10);
254   
255   if (*eptr == 0) {
256     mtu.setValue(hold_mtu);
257     return 0;
258   } else {
259     return -1;
260   }
261 }
262
263
264 int IPV4IFInformation::SetInterrupt(String const &interrupt_string)
265 {
266   char *eptr;
267   CString p;
268   Uint32 int_hold;
269
270   p = interrupt_string.getCString();
271   int_hold = strtoul(p, &eptr, 10);
272   
273   if (*eptr == 0) {
274     interrupt.setValue(int_hold);
275     return 0;
276   } else {
277     return -1;
278   }
279 }
280
281
282 int IPV4IFInformation::SetBaseAddr(String const &base_address_string)
283 {
284   char *eptr;
285   CString p;
286   Uint32 hold_base;
287
288   p = base_address_string.getCString();
289   hold_base = strtoul(p, &eptr, 16);
290   
291   if (*eptr == 0) {
292     base_address.setValue(hold_base);
293     return 0;
294   } else {
295     return -1;
296   }
297 }
298
299 void IPV4IFInformation::SetRxErrStats(Uint32 err, Uint32 drop, 
300                                  Uint32 over, Uint32 frame)
301 {
302   rx_errors.setValue(err);
303   rx_drops.setValue(drop);
304   rx_overruns.setValue(over); 
305   rx_frame.setValue(frame);
306 }
307
308
309 void IPV4IFInformation::SetTxErrStats(Uint32 err, Uint32 drop, 
310                                  Uint32 over, Uint32 frame)
311 {
312   rx_errors.setValue(err); 
313   rx_drops.setValue(drop);
314   rx_overruns.setValue(over);
315   rx_frame.setValue(frame);
316 }
317
318 Uint32 IPV4IFInformation::GetMTU(void) const
319 {
320   return mtu.getValue();
321 }
322
323
324 Uint32 IPV4IFInformation::GetInterrupt(void) const
325 {
326   return interrupt.getValue();
327 }
328
329
330 Uint32 IPV4IFInformation::GetBaseAddr(void) const
331 {
332   return base_address.getValue();
333 }
334
335 String IPV4IFInformation::print(void) const
336 {
337   String retval, holdvar;
338   Uint32 hold_int;
339   char render_uint32[10];  /* big enough to hold a uint32 in decimal, plus
340                             * a few bytes */
341
342   retval = "Interface \"" + GetInterface() + "\":  " + GetEncapsulation();
343   if (GetHWAddr() != "")
344     retval.append(" on hardware address " + GetHWAddr());
345   retval.append(".");
346   if (address.is_valid()) {
347     retval.append("  Configured on address " + GetAddress());
348     if (bcast.is_valid()) {
349       retval.append(" with broadcast address " + GetBcast());
350     } else {
351       retval.append(" with no broadcast address ");
352     }
353     retval.append(" and netmask " + GetNetmask() + ".");
354   }
355
356   retval.append("  MTU is ");
357   try {
358     hold_int = GetMTU();
359     sprintf(render_uint32, "%u.", hold_int);
360     holdvar.append(render_uint32);
361   } 
362   catch (AccessedInvalidData &e) {
363     holdvar = "unset.";
364   }
365   retval.append(holdvar);
366
367   holdvar = "";
368   try {
369     hold_int = GetInterrupt();
370     sprintf(render_uint32, "%u", hold_int);
371     holdvar = "  Configured on interrupt " + String(render_uint32) + ".";
372   }
373   catch (AccessedInvalidData &e) {
374   }
375   retval.append(holdvar);
376
377   holdvar = "";
378   try {
379     hold_int = GetBaseAddr();
380     sprintf(render_uint32, "%x", hold_int);
381     holdvar = "  Located at base address 0x" + String(render_uint32) + ".";
382   }
383   catch (AccessedInvalidData &e) {
384   }
385   retval.append(holdvar);
386
387   return retval;
388 }
389
390
391 PEGASUS_NAMESPACE_END