1 //%2003////////////////////////////////////////////////////////////////////////
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.
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:
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.
24 //==============================================================================
26 // Author: Christopher Neufeld <neufeld@linuxcare.com>
27 // David Kennedy <dkennedy@linuxcare.com>
29 // Modified By: David Kennedy <dkennedy@linuxcare.com>
30 // Christopher Neufeld <neufeld@linuxcare.com>
31 // Al Stone <ahs3@fc.hp.com>
33 //%////////////////////////////////////////////////////////////////////////////
40 #include <Pegasus/Common/System.h>
41 #include <Pegasus/Common/String.h>
42 #include "IPV4NetInformation.h"
45 PEGASUS_NAMESPACE_BEGIN
48 //////////////////////////////////////////
50 // IPV4NetInformation methods
52 //////////////////////////////////////////
54 void IPV4NetInformation::ParseDottedQuad(String const &quad_string,
55 validated<dottedquad_t> *quad) const
59 dottedquad_t byte_octets;
61 fields = sscanf(quad_string.getCString(), "%d.%d.%d.%d",
62 &int_octets[0], &int_octets[1],
63 &int_octets[2], &int_octets[3]);
69 for (i = 0; i < 4; i++) {
70 if (int_octets[i] < 0 || int_octets[i] > 255)
72 byte_octets.octets[i] = (unsigned char) int_octets[i];
75 /* If we make it here, we have a valid dotted quad, set the value on the
77 quad->setValue(byte_octets);
81 #define IPV4_ADDR_MAXSIZE 20
83 String IPV4NetInformation::RenderDottedQuad(validated<dottedquad_t> const *quad) const
85 char renderbuffer[IPV4_ADDR_MAXSIZE];
87 if (!quad->is_valid())
88 return INVALID_ADDRESS_STRING;
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]);
94 return String(renderbuffer);
98 bool IPV4NetInformation::DottedQuadAllZeroes(validated<dottedquad_t> const *address) const
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);
107 //////////////////////////////////////////
109 // IPV4RouteInformation methods
111 //////////////////////////////////////////
114 IPV4RouteInformation::IPV4RouteInformation(void)
116 ptype = IPV4_ROUTE_INFO;
118 route_is_dynamic = false;
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)
127 ParseDottedQuad(dest_string, &destination);
129 if (destination.is_valid())
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)
139 ParseDottedQuad(gw_string, &gateway);
141 if (gateway.is_valid())
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)
151 ParseDottedQuad(mask_string, &mask);
160 void IPV4RouteInformation::SetRouteDynamicFromFlags(String const &flags)
162 route_is_dynamic = (flags.find('D') != PEG_NOT_FOUND ||
163 flags.find('R') != PEG_NOT_FOUND ||
164 flags.find('M') != PEG_NOT_FOUND);
168 String IPV4RouteInformation::print(void) const
172 if (!destination.is_valid() ||
177 if (route_is_dynamic)
178 retval = "(dynamic) ";
180 if (DottedQuadAllZeroes(&destination)) {
181 if (!DottedQuadAllZeroes(&mask))
182 return "Unrecognized route. default destination with non-default mask.";
184 retval.append("Default route: out on interface \"" + GetInterface() + "\"");
185 retval.append(" through the gateway machine at " + RenderDottedQuad(&gateway));
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));
199 //////////////////////////////////////////
201 // IPV4IFInformation methods
203 //////////////////////////////////////////
205 IPV4IFInformation::IPV4IFInformation(void)
207 ptype = IPV4_INTERFACE_INFO;
209 interface = encapsulation = hwaddr = "";
214 int IPV4IFInformation::SetAddress(String const &addr_string)
216 ParseDottedQuad(addr_string, &address);
218 if (address.is_valid())
224 int IPV4IFInformation::SetBcast(String const &bcast_string)
226 ParseDottedQuad(bcast_string, &bcast);
228 if (bcast.is_valid())
235 int IPV4IFInformation::SetNetmask(String const &mask_string)
237 ParseDottedQuad(mask_string, &netmask);
239 if (netmask.is_valid())
246 int IPV4IFInformation::SetMTU(String const &mtu_string)
252 p = mtu_string.getCString();
253 hold_mtu = strtoul(p, &eptr, 10);
256 mtu.setValue(hold_mtu);
264 int IPV4IFInformation::SetInterrupt(String const &interrupt_string)
270 p = interrupt_string.getCString();
271 int_hold = strtoul(p, &eptr, 10);
274 interrupt.setValue(int_hold);
282 int IPV4IFInformation::SetBaseAddr(String const &base_address_string)
288 p = base_address_string.getCString();
289 hold_base = strtoul(p, &eptr, 16);
292 base_address.setValue(hold_base);
299 void IPV4IFInformation::SetRxErrStats(Uint32 err, Uint32 drop,
300 Uint32 over, Uint32 frame)
302 rx_errors.setValue(err);
303 rx_drops.setValue(drop);
304 rx_overruns.setValue(over);
305 rx_frame.setValue(frame);
309 void IPV4IFInformation::SetTxErrStats(Uint32 err, Uint32 drop,
310 Uint32 over, Uint32 frame)
312 rx_errors.setValue(err);
313 rx_drops.setValue(drop);
314 rx_overruns.setValue(over);
315 rx_frame.setValue(frame);
318 Uint32 IPV4IFInformation::GetMTU(void) const
320 return mtu.getValue();
324 Uint32 IPV4IFInformation::GetInterrupt(void) const
326 return interrupt.getValue();
330 Uint32 IPV4IFInformation::GetBaseAddr(void) const
332 return base_address.getValue();
335 String IPV4IFInformation::print(void) const
337 String retval, holdvar;
339 char render_uint32[10]; /* big enough to hold a uint32 in decimal, plus
342 retval = "Interface \"" + GetInterface() + "\": " + GetEncapsulation();
343 if (GetHWAddr() != "")
344 retval.append(" on hardware address " + GetHWAddr());
346 if (address.is_valid()) {
347 retval.append(" Configured on address " + GetAddress());
348 if (bcast.is_valid()) {
349 retval.append(" with broadcast address " + GetBcast());
351 retval.append(" with no broadcast address ");
353 retval.append(" and netmask " + GetNetmask() + ".");
356 retval.append(" MTU is ");
359 sprintf(render_uint32, "%u.", hold_int);
360 holdvar.append(render_uint32);
362 catch (AccessedInvalidData &e) {
365 retval.append(holdvar);
369 hold_int = GetInterrupt();
370 sprintf(render_uint32, "%u", hold_int);
371 holdvar = " Configured on interrupt " + String(render_uint32) + ".";
373 catch (AccessedInvalidData &e) {
375 retval.append(holdvar);
379 hold_int = GetBaseAddr();
380 sprintf(render_uint32, "%x", hold_int);
381 holdvar = " Located at base address 0x" + String(render_uint32) + ".";
383 catch (AccessedInvalidData &e) {
385 retval.append(holdvar);
391 PEGASUS_NAMESPACE_END