00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 #ifndef _ADDRESS
00067 #define _ADDRESS
00068
00069
00070
00071 #include <string.h>
00072 #include <memory.h>
00073
00074 #include "snmp_pp/config_snmp_pp.h"
00075 #include "snmp_pp/smival.h"
00076 #include "snmp_pp/collect.h"
00077
00078
00079
00080
00081 #ifdef __unix
00082
00083
00084 #ifndef _AIX
00085 #include <unistd.h>
00086 #endif
00087 #include <sys/socket.h>
00088 #include <netinet/in.h>
00089 #include <netdb.h>
00090 #include <arpa/inet.h>
00091 #if defined _AIX
00092 #include <strings.h>
00093 #endif
00094
00095 #if !defined __CYGWIN32__ && !defined __hpux && !defined linux && !defined _AIX
00096 extern int h_errno;
00097 #endif
00098 #endif // __unix
00099
00100
00101 #ifdef WIN32
00102 #ifndef __unix // __unix overrides WIN32 if both options are present
00103 #include <winsock.h>
00104 #endif
00105 #endif
00106
00107 #ifdef SNMP_PP_NAMESPACE
00108 namespace Snmp_pp {
00109 #endif
00110
00111
00112 #define BUFSIZE 40 // worst case of address lens
00113 #define OUTBUFF 80 // worst case of output lens
00114
00115 #define IPLEN 4
00116 #define IP6LEN 16
00117 #define UDPIPLEN 6
00118 #define UDPIP6LEN 18
00119 #define IPXLEN 10
00120 #define IPXSOCKLEN 12
00121 #define MACLEN 6
00122 #define MAX_FRIENDLY_NAME 80
00123 #define HASH0 19
00124 #define HASH1 13
00125 #define HASH2 7
00126
00127
00128 class GenAddress;
00129
00130
00131
00132
00133
00134
00135 class DLLOPT Address : public SnmpSyntax
00136 {
00137 friend class GenAddress;
00138
00139 public:
00140
00141
00142
00143
00144 enum addr_type
00145 {
00146 type_ip,
00147 type_ipx,
00148 type_udp,
00149 type_ipxsock,
00150 type_mac,
00151 type_invalid
00152 };
00153
00154
00155
00156
00157
00158 enum version_type
00159 {
00160 version_ipv4,
00161 version_ipv6
00162 };
00163
00164
00165
00166
00167 Address();
00168
00169
00170
00171
00172 virtual ~Address() {};
00173
00174
00175 DLLOPT friend int operator==(const Address &lhs,const Address &rhs);
00176
00177
00178 DLLOPT friend int operator!=(const Address &lhs, const Address &rhs)
00179 { return !(lhs == rhs); };
00180
00181
00182 DLLOPT friend int operator>(const Address &lhs,const Address &rhs);
00183
00184
00185 DLLOPT friend int operator>=(const Address &lhs,const Address &rhs)
00186 { if ((lhs > rhs) || (lhs == rhs)) return true; return false; };
00187
00188
00189 DLLOPT friend int operator<(const Address &lhs,const Address &rhs);
00190
00191
00192 DLLOPT friend int operator<=(const Address &lhs, const Address &rhs)
00193 { if ((lhs < rhs) || (lhs == rhs)) return true; return false; };
00194
00195
00196 DLLOPT friend int operator==(const Address &lhs,const char *rhs);
00197
00198
00199 DLLOPT friend int operator!=(const Address &lhs,const char *rhs)
00200 { return !(lhs == rhs); };
00201
00202
00203 DLLOPT friend int operator>(const Address &lhs,const char *rhs);
00204
00205
00206 DLLOPT friend int operator>=(const Address &lhs,const char *rhs);
00207
00208
00209 DLLOPT friend int operator<(const Address &lhs,const char *rhs);
00210
00211
00212 DLLOPT friend int operator<=(const Address &lhs,const char *rhs);
00213
00214
00215
00216
00217
00218
00219 virtual operator const char *() const = 0;
00220
00221
00222
00223
00224
00225
00226 virtual bool valid() const { return valid_flag; };
00227
00228
00229
00230
00231 virtual int get_asn1_length() const = 0;
00232
00233
00234
00235
00236
00237
00238
00239
00240 unsigned char& operator[](const int position)
00241 { addr_changed = true; valid_flag = true;
00242 return (position < BUFSIZE) ? address_buffer[position]
00243 : address_buffer[0]; };
00244
00245
00246
00247
00248
00249
00250
00251
00252 unsigned char operator[](const int position) const
00253 { return (position < BUFSIZE) ? address_buffer[ position] : 0; }
00254
00255
00256
00257
00258
00259 virtual int get_length() const = 0;
00260
00261
00262
00263
00264
00265 virtual addr_type get_type() const = 0;
00266
00267
00268
00269
00270 virtual SnmpSyntax& operator=(const SnmpSyntax &val) = 0;
00271
00272
00273 virtual unsigned int hashFunction() const { return 0;};
00274
00275 protected:
00276 bool addr_changed;
00277 bool valid_flag;
00278 unsigned char address_buffer[BUFSIZE];
00279
00280
00281
00282 virtual bool parse_address(const char * inaddr) = 0;
00283
00284
00285
00286 virtual void format_output() const = 0;
00287
00288
00289
00290
00291
00292
00293 void trim_white_space(char * ptr);
00294
00295
00296
00297
00298 virtual bool is_gen_address() const { return false; };
00299
00300
00301
00302
00303 void clear();
00304 };
00305
00306
00307
00308
00309
00310 class DLLOPT IpAddress : public Address
00311 {
00312 public:
00313
00314
00315
00316 IpAddress();
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328 IpAddress(const char *inaddr);
00329
00330
00331
00332
00333
00334
00335 IpAddress(const IpAddress &ipaddr);
00336
00337
00338
00339
00340
00341
00342 IpAddress(const GenAddress &genaddr);
00343
00344
00345
00346
00347 ~IpAddress() {};
00348
00349
00350
00351
00352 SnmpSyntax& operator=(const SnmpSyntax &val);
00353
00354
00355
00356
00357 IpAddress& operator=(const IpAddress &ipaddress);
00358
00359
00360
00361
00362 IpAddress& operator=(const char *inaddr);
00363
00364
00365
00366
00367
00368
00369 SnmpSyntax *clone() const { return (SnmpSyntax *) new IpAddress(*this); };
00370
00371
00372
00373
00374
00375
00376
00377
00378 char *friendly_name(int &status);
00379
00380
00381
00382
00383
00384
00385 virtual const char *get_printable() const
00386 { if (addr_changed) format_output(); return output_buffer; };
00387
00388
00389
00390
00391
00392
00393 virtual operator const char *() const
00394 { if (addr_changed) format_output(); return output_buffer; };
00395
00396
00397
00398
00399
00400
00401 void mask(const IpAddress &ipaddr);
00402
00403
00404
00405
00406 virtual int get_length() const
00407 { return (ip_version == version_ipv4) ? IPLEN : IP6LEN; };
00408
00409
00410
00411
00412
00413
00414 virtual addr_type get_type() const { return type_ip; };
00415
00416
00417
00418
00419
00420
00421 virtual SmiUINT32 get_syntax() const { return sNMP_SYNTAX_IPADDR; };
00422
00423
00424
00425
00426 virtual int get_asn1_length() const
00427 { return (ip_version == version_ipv4) ? (IPLEN + 2) : (IP6LEN + 2); };
00428
00429
00430
00431
00432
00433
00434 virtual version_type get_ip_version() const { return ip_version; };
00435
00436
00437
00438
00439
00440
00441 virtual int map_to_ipv6();
00442
00443
00444
00445
00446 void clear();
00447
00448 protected:
00449 char output_buffer[OUTBUFF];
00450
00451
00452 char iv_friendly_name[MAX_FRIENDLY_NAME];
00453 int iv_friendly_name_status;
00454
00455
00456
00457 virtual bool parse_address(const char *inaddr);
00458
00459
00460
00461 virtual void format_output() const;
00462
00463
00464 int parse_dotted_ipstring(const char *inaddr);
00465
00466
00467 int parse_coloned_ipstring(const char *inaddr);
00468
00469
00470
00471 int addr_to_friendly();
00472
00473
00474 version_type ip_version;
00475 };
00476
00477
00478
00479
00480 class DLLOPT UdpAddress : public IpAddress
00481 {
00482 public:
00483
00484
00485
00486 UdpAddress();
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500 UdpAddress(const char *inaddr);
00501
00502
00503
00504
00505
00506
00507 UdpAddress(const UdpAddress &udpaddr);
00508
00509
00510
00511
00512
00513
00514 UdpAddress(const GenAddress &genaddr);
00515
00516
00517
00518
00519
00520
00521
00522 UdpAddress(const IpAddress &ipaddr);
00523
00524
00525
00526
00527 ~UdpAddress() {};
00528
00529
00530
00531
00532 SnmpSyntax& operator=(const SnmpSyntax &val);
00533
00534
00535
00536
00537 UdpAddress& operator=(const UdpAddress &udpaddr);
00538
00539
00540
00541
00542 UdpAddress& operator=(const IpAddress &ipaddr);
00543
00544
00545
00546
00547 UdpAddress& operator=(const char *inaddr);
00548
00549
00550
00551
00552
00553
00554 SmiUINT32 get_syntax() const { return sNMP_SYNTAX_OCTETS; };
00555
00556
00557
00558
00559 virtual int get_asn1_length() const
00560 { return (ip_version == version_ipv4) ? (UDPIPLEN + 2) : (UDPIP6LEN + 2);};
00561
00562
00563
00564
00565
00566
00567 SnmpSyntax *clone() const { return (SnmpSyntax *) new UdpAddress(*this); };
00568
00569
00570
00571
00572
00573
00574 virtual const char *get_printable() const
00575 { if (addr_changed) format_output(); return output_buffer; };
00576
00577
00578
00579
00580
00581
00582 virtual operator const char *() const
00583 { if (addr_changed) format_output(); return output_buffer; };
00584
00585
00586
00587
00588
00589
00590 void set_port(const unsigned short p);
00591
00592
00593
00594
00595
00596
00597 unsigned short get_port() const;
00598
00599
00600
00601
00602 virtual int get_length() const
00603 { return (ip_version == version_ipv4) ? UDPIPLEN : UDPIP6LEN; };
00604
00605
00606
00607
00608
00609
00610 virtual addr_type get_type() const { return type_udp; };
00611
00612
00613
00614
00615
00616
00617 virtual int map_to_ipv6();
00618
00619
00620
00621
00622 void clear()
00623 { Address::clear(); memset(output_buffer, 0, sizeof(output_buffer)); };
00624
00625 protected:
00626 char output_buffer[OUTBUFF];
00627 char sep;
00628
00629
00630
00631 virtual bool parse_address(const char *inaddr);
00632
00633
00634
00635 virtual void format_output() const;
00636 };
00637
00638 #ifdef _MAC_ADDRESS
00639
00640
00641
00642 class DLLOPT MacAddress : public Address {
00643
00644 public:
00645
00646 MacAddress();
00647
00648
00649 MacAddress(const char *inaddr);
00650
00651
00652 MacAddress(const MacAddress &macaddr);
00653
00654
00655 MacAddress(const GenAddress &genaddr);
00656
00657
00658 ~MacAddress() {};
00659
00660
00661
00662
00663
00664
00665 SmiUINT32 get_syntax() const { return sNMP_SYNTAX_OCTETS; };
00666
00667
00668
00669
00670 virtual int get_asn1_length() const { return MACLEN + 2; };
00671
00672
00673
00674
00675 SnmpSyntax& operator=(const SnmpSyntax &val);
00676
00677
00678 MacAddress& operator=(const MacAddress &macaddress);
00679
00680
00681
00682
00683
00684
00685 SnmpSyntax *clone() const { return (SnmpSyntax *) new MacAddress(*this); };
00686
00687
00688
00689
00690
00691
00692 virtual const char *get_printable() const
00693 { if (addr_changed) format_output(); return output_buffer; };
00694
00695
00696
00697
00698
00699
00700 virtual operator const char *() const
00701 { if (addr_changed) format_output(); return output_buffer; };
00702
00703
00704
00705
00706 virtual int get_length() const { return MACLEN; };
00707
00708
00709
00710
00711
00712
00713 virtual addr_type get_type() const { return type_mac; };
00714
00715
00716 unsigned int hashFunction() const;
00717
00718
00719
00720
00721 void clear()
00722 { Address::clear(); memset(output_buffer, 0, sizeof(output_buffer)); };
00723
00724 protected:
00725 char output_buffer[OUTBUFF];
00726
00727
00728 virtual bool parse_address(const char *inaddr);
00729
00730
00731 virtual void format_output() const;
00732 };
00733 #endif // _MAC_ADDRESS
00734
00735 #ifdef _IPX_ADDRESS
00736
00737
00738
00739 class DLLOPT IpxAddress : public Address {
00740
00741 public:
00742
00743 IpxAddress();
00744
00745
00746 IpxAddress(const char *inaddr);
00747
00748
00749 IpxAddress(const IpxAddress &ipxaddr);
00750
00751
00752 IpxAddress(const GenAddress &genaddr);
00753
00754
00755 ~IpxAddress() {};
00756
00757
00758
00759
00760
00761
00762 virtual SmiUINT32 get_syntax() const { return sNMP_SYNTAX_OCTETS; };
00763
00764
00765
00766
00767 virtual int get_asn1_length() const { return IPXLEN + 2; };
00768
00769
00770
00771
00772 SnmpSyntax& operator=(const SnmpSyntax &val);
00773
00774
00775 IpxAddress& operator=(const IpxAddress &ipxaddress);
00776
00777 #ifdef _MAC_ADDRESS
00778
00779 int get_hostid(MacAddress& mac) const;
00780 #endif
00781
00782
00783
00784
00785
00786
00787 SnmpSyntax *clone() const { return (SnmpSyntax *) new IpxAddress(*this); };
00788
00789
00790
00791
00792
00793
00794 virtual const char *get_printable() const
00795 { if (addr_changed) format_output(); return output_buffer; };
00796
00797
00798
00799
00800
00801
00802 virtual operator const char *() const
00803 { if (addr_changed) format_output(); return output_buffer; };
00804
00805
00806
00807
00808 virtual int get_length() const { return IPXLEN; };
00809
00810
00811
00812
00813
00814
00815 virtual addr_type get_type() const { return type_ipx; };
00816
00817
00818
00819
00820 void clear()
00821 { Address::clear(); memset(output_buffer, 0, sizeof(output_buffer)); };
00822
00823 protected:
00824
00825 char separator;
00826 char output_buffer[OUTBUFF];
00827
00828
00829 virtual bool parse_address(const char *inaddr);
00830
00831
00832
00833 virtual void format_output() const;
00834
00835 };
00836
00837
00838
00839
00840
00841
00842 class DLLOPT IpxSockAddress : public IpxAddress {
00843
00844 public:
00845
00846 IpxSockAddress();
00847
00848
00849 IpxSockAddress(const char *inaddr);
00850
00851
00852 IpxSockAddress(const IpxSockAddress &ipxaddr);
00853
00854
00855 IpxSockAddress(const GenAddress &genaddr);
00856
00857
00858
00859 IpxSockAddress(const IpxAddress &ipxaddr);
00860
00861
00862 ~IpxSockAddress() {};
00863
00864
00865
00866
00867
00868
00869
00870 virtual int get_asn1_length() const { return IPXSOCKLEN + 2; };
00871
00872
00873
00874
00875 SnmpSyntax& operator=(const SnmpSyntax &val);
00876
00877
00878 IpxSockAddress& operator=(const IpxSockAddress &ipxaddr);
00879
00880
00881
00882
00883
00884
00885 SnmpSyntax *clone() const { return (SnmpSyntax *)new IpxSockAddress(*this); };
00886
00887
00888 void set_socket(const unsigned short s);
00889
00890
00891 unsigned short get_socket() const;
00892
00893
00894
00895
00896
00897
00898 virtual const char *get_printable() const
00899 { if (addr_changed) format_output(); return output_buffer; };
00900
00901
00902
00903
00904
00905
00906 virtual operator const char *() const
00907 { if (addr_changed) format_output(); return output_buffer; };
00908
00909
00910
00911
00912 virtual int get_length() const { return IPXSOCKLEN; };
00913
00914
00915
00916
00917
00918
00919 virtual addr_type get_type() const { return type_ipxsock; };
00920
00921
00922
00923
00924 void clear()
00925 { Address::clear(); memset(output_buffer, 0, sizeof(output_buffer)); };
00926
00927 protected:
00928 char output_buffer[OUTBUFF];
00929
00930
00931 virtual bool parse_address(const char *inaddr);
00932
00933
00934
00935 virtual void format_output() const;
00936 };
00937 #endif // _IPX_ADDRESS
00938
00939
00940
00941
00942
00943
00944
00945 class DLLOPT GenAddress : public Address
00946 {
00947 public:
00948
00949
00950
00951 GenAddress();
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961
00962
00963 GenAddress(const char *addr,
00964 const Address::addr_type use_type = Address::type_invalid);
00965
00966
00967
00968
00969
00970
00971 GenAddress(const Address &addr);
00972
00973
00974
00975
00976
00977
00978 GenAddress(const GenAddress &addr);
00979
00980
00981
00982
00983 ~GenAddress() { if (address) delete address; };
00984
00985
00986
00987
00988
00989
00990
00991
00992 SmiUINT32 get_syntax() const
00993 { return address ? address->get_syntax() : sNMP_SYNTAX_NULL; };
00994
00995
00996
00997
00998 virtual int get_asn1_length() const
00999 { return address ? address->get_asn1_length() : 2; };
01000
01001
01002
01003
01004
01005
01006 SnmpSyntax *clone() const { return (SnmpSyntax *)new GenAddress(*this); };
01007
01008
01009
01010
01011 GenAddress& operator=(const GenAddress &addr);
01012
01013
01014
01015
01016 GenAddress& operator=(const Address &addr);
01017
01018
01019
01020
01021 SnmpSyntax& operator=(const SnmpSyntax &val);
01022
01023
01024
01025
01026
01027
01028 virtual const char *get_printable() const
01029 { return (address) ? address->get_printable() : output_buffer; };
01030
01031
01032
01033
01034
01035
01036 virtual operator const char *() const
01037 { return address ? (const char *)*address : output_buffer; };
01038
01039
01040
01041
01042 virtual int get_length() const
01043 { return (address) ? address->get_length() : 0; };
01044
01045
01046
01047
01048 void clear() { if (address) address->clear(); };
01049
01050
01051
01052
01053
01054
01055
01056 virtual addr_type get_type() const
01057 { return (valid()) ? address->get_type() : type_invalid; };
01058
01059
01060
01061
01062
01063
01064 const IpAddress &cast_ipaddress() const { return (IpAddress& )*address; };
01065
01066
01067
01068
01069
01070
01071 const UdpAddress &cast_udpaddress() const { return (UdpAddress&)*address; };
01072
01073 #ifdef _MAC_ADDRESS
01074
01075
01076
01077
01078
01079 const MacAddress &cast_macaddress() const { return (MacAddress&)*address; };
01080 #endif
01081
01082 #ifdef _IPX_ADDRESS
01083
01084
01085
01086
01087
01088 const IpxAddress &cast_ipxaddress() const { return (IpxAddress&)*address; };
01089
01090
01091
01092
01093
01094
01095 const IpxSockAddress &cast_ipxsockaddress() const
01096 { return (IpxSockAddress&)*address; };
01097 #endif
01098
01099 protected:
01100
01101 Address *address;
01102 char output_buffer[1];
01103
01104
01105 virtual bool parse_address(const char *addr)
01106 { return parse_address(addr, Address::type_invalid); };
01107
01108 virtual bool parse_address(const char *addr,
01109 const Address::addr_type use_type);
01110
01111
01112 virtual void format_output() const {};
01113
01114
01115
01116
01117 virtual bool is_gen_address() const { return true; };
01118 };
01119
01120
01121 typedef SnmpCollection <GenAddress> AddressCollection;
01122 typedef SnmpCollection <UdpAddress> UdpAddressCollection;
01123
01124 #ifdef SNMP_PP_NAMESPACE
01125 };
01126 #endif
01127
01128 #endif //_ADDRESS