arzmq-sys 0.6.3

Low-level bindings to the zeromq library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
#include <stdio.h>
#include "protoRouteMgr.h"
#include "protoDebug.h"
#include "unix/zebraRouteMgr.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <errno.h>

// linux specific includes
#include <asm/types.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>

class LinuxRouteMgr : public ProtoRouteMgr
{
    public:
        LinuxRouteMgr();
        ~LinuxRouteMgr();
        
        virtual bool Open(const void* userData = NULL);
        virtual bool IsOpen() const {return (descriptor >= 0);}
        virtual void Close();
        
        virtual bool GetAllRoutes(ProtoAddress::Type addrType,
                                  ProtoRouteTable&   routeTable);
        
        virtual bool GetRoute(const ProtoAddress&   dst, 
                              unsigned int          prefixLen,
                              ProtoAddress&         gw,
                              unsigned int&         ifIndex,
                              int&                  metric);
        
        virtual bool SetRoute(const ProtoAddress&   dst,
                              unsigned int          prefixLen,
                              const ProtoAddress&   gw,
                              unsigned int          ifIndex = 0,
                              int                   metric = -1);
        
        virtual bool DeleteRoute(const ProtoAddress& dst,
                                 unsigned int        prefixLen,
                                 const ProtoAddress& gw,
                                 unsigned int        ifIndex = 0);
        
        virtual bool SetForwarding(bool state);
        
        virtual unsigned int GetInterfaceIndex(const char* interfaceName)
            {return ProtoSocket::GetInterfaceIndex(interfaceName);}
        
        virtual bool GetInterfaceAddressList(unsigned int        ifIndex, 
                                            ProtoAddress::Type  addrType,
                                            ProtoAddressList& addrList);
        
        virtual bool GetInterfaceName(unsigned int  interfaceIndex, 
                                      char*         buffer, 
                                      unsigned int  buflen)
            {return ProtoSocket::GetInterfaceName(interfaceIndex, buffer, buflen);}
        
    private:        
        static bool NetlinkAddAttr(struct nlmsghdr* msg, 
                                   unsigned int     maxLen, 
                                   int              type, 
                                   const void*      data, 
                                   int              len);
        bool NetlinkCheckResponse(UINT32 seq);
                    
        int     descriptor;
        UINT32  port_id;  // netlink port id
        UINT32  sequence; // netlink request/response sequence number
        
};  // end class LinuxRouteMgr

ProtoRouteMgr* ProtoRouteMgr::Create(Type theType)
{
    // TBD - support alternative route mgr "types" (e.g. ZEBRA)
    ProtoRouteMgr* returnMgr = NULL;
    switch(theType)
    {
        case ZEBRA:
          returnMgr = (ProtoRouteMgr*)(new ZebraRouteMgr);
          break;
        case SYSTEM:
          returnMgr = (ProtoRouteMgr*)(new LinuxRouteMgr);
          break;
        default:
          return NULL;
    }
    return returnMgr;
}  // end ProtoRouteMgr::Create()

LinuxRouteMgr::LinuxRouteMgr()
 : descriptor(-1), port_id(0), sequence(0)
{
}

LinuxRouteMgr::~LinuxRouteMgr()
{
    Close();
}

bool LinuxRouteMgr::SetForwarding(bool state)
{
    int fd = open("/proc/sys/net/ipv4/ip_forward", O_WRONLY);
    if (fd < 0)
    {
        PLOG(PL_ERROR, "LinuxRouteMgr::SetForwarding() open(/proc/sys) error: %s\n",
                strerror(errno));
        return false;   
    }
    char value = state ? '1' : '0';
    if (1 == write(fd, &value, 1))
    {
        return true;
    }
    else
    {
        PLOG(PL_ERROR, "LinuxRouteMgr::SetForwarding() write(/proc/sys) error: %s\n",
            strerror(errno));
        return false;      
    }
}  // end LinuxRouteMgr::SetForwarding()

bool LinuxRouteMgr::Open(const void* /*userData*/)
{
    if (IsOpen()) Close();
    // (TBD) We may need to dictate NETLINK_ROUTE6 for IPv6 ops ???
    if ((descriptor = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0)
    {
        PLOG(PL_ERROR, "LinuxRouteMgr::Open() socket(NETLINK_ROUTE) error: %s\n",
                strerror(errno));  
        return false; 
    }
    else
    {
        // Here we use "bind()" to get a unique netlink port id (pid) from the kernel
        // (with localAddr.nl_pid passed into bind() set as '0', kernel assigns us one
        struct sockaddr_nl localAddr;
        memset(&localAddr, 0, sizeof(localAddr));
        localAddr.nl_family = AF_NETLINK;
	    if (0 > bind(descriptor, (struct sockaddr*) &localAddr, sizeof(localAddr)))
        {
            PLOG(PL_ERROR, "ProtoNetlink::Open() bind() error: %s\n", GetErrorString());
            Close();
            return false;
        }
        // Get socket name so we know our port number (i.e. netlink pid)
        socklen_t addrLen = sizeof(localAddr);
        if (getsockname(descriptor, (struct sockaddr*)&localAddr, &addrLen) < 0) 
        {    
            PLOG(PL_ERROR, "ProtoNetlink::Open()  getsockname() error: %s\n", GetErrorString());
            Close();
            return false;
        }
        port_id = localAddr.nl_pid;
        return true;
    }
}  // end LinuxRouteMgr::Open()

void LinuxRouteMgr::Close()
{
    if (IsOpen())
    {
        close(descriptor);
        descriptor = -1;
    }   
}  // end LinuxRouteMgr::Close()

// Add an "attribute" (field) to netlink msg payload
bool LinuxRouteMgr::NetlinkAddAttr(struct nlmsghdr*    msg, 
                                   unsigned int        maxLen, 
                                   int                 type, 
                                   const void*         data, 
                                   int                 len)
{
    int rtaLen = RTA_LENGTH(len);
    if ((NLMSG_ALIGN(msg->nlmsg_len) + rtaLen) > maxLen) return false;
    struct rtattr* rta = (struct rtattr*)(((char*)msg) + NLMSG_ALIGN(msg->nlmsg_len));
    rta->rta_type = type;
    rta->rta_len = rtaLen;
    memcpy(RTA_DATA(rta), data, len);
    msg->nlmsg_len = NLMSG_ALIGN(msg->nlmsg_len) + rtaLen;
    return true;
}  // end LinuxRouteMgr::NetlinkAddAttr()

bool LinuxRouteMgr::NetlinkCheckResponse(UINT32 seq)
{
    while (1)
    {
        char buffer[1024];
        int msgLen = recv(descriptor, buffer, 1024, 0); 
        if (msgLen < 0)
        {
            if (errno == EINTR)
            {
                continue;
            }
            else
            {
                PLOG(PL_ERROR, "LinuxRouteMgr::NetlinkCheckResponse() recv() error: %s\n", strerror(errno)); 
                return false;  
            }
        } 
        struct nlmsghdr* msg = (struct nlmsghdr*)buffer;
        for (; 0 != NLMSG_OK(msg, (unsigned int)msgLen); msg = NLMSG_NEXT(msg, msgLen))
        {
// Justin Below is a semi-hack to get SMF to function correctly in core namespaces.  
// If when the msg->nlmsg_pid gets fixed this ifdef can be removed
#ifndef CORE_NAMESPACES
          if ((msg->nlmsg_pid == port_id) && (msg->nlmsg_seq == seq))
#else
    //DMSG(0,"J Namspaces comment in NetlinkCheckResponse\n");
          if (msg->nlmsg_seq == seq)
#endif // if/else !CORE_NAMESPACES
            {
                switch(msg->nlmsg_type)
                {
                    case NLMSG_ERROR:
                    {
                        struct nlmsgerr* errorMsg = (struct nlmsgerr*)NLMSG_DATA(msg);
                        if (0 != errorMsg->error)
                        {
                            PLOG(PL_DEBUG, "LinuxRouteMgr::NetlinkCheckResponse() recvd NLMSG_ERROR "
                                           "error seq:%d code:%d...\n", msg->nlmsg_seq, errorMsg->error);
                            return false;
                        }
                        else
                        {
                            return true;
                        }
                        break;
                    }   
                    default:
                        PLOG(PL_ERROR, "LinuxRouteMgr::NetlinkCheckResponse() recvd unexpected "
                                       "matching message type:%d\n", msg->nlmsg_type);
                        // Assume success ???
                        return true;
                        break;
                }
            }
        }
    }  // end while (1)
}  // end LinuxRouteMgr::NetlinkCheckResponse()

bool LinuxRouteMgr::SetRoute(const ProtoAddress&   dst,
                             unsigned int          prefixLen,
                             const ProtoAddress&   gw,
                             unsigned int          ifIndex,
                             int                   metric)
{
    // First, delete any pre-existing route(s) to this destination
    // (TBD) try to do a "make before break" routing change
    PLOG(PL_DEBUG, "LinuxRouteMgr::SetRoute() setting route to %s/%d via ",
            dst.GetHostString(), prefixLen);
    if (gw.IsValid())
        PLOG(PL_DEBUG, "gateway:%s\n", gw.GetHostString());
    else
        PLOG(PL_DEBUG, "direct if:%d\n", ifIndex);
    if (!DeleteRoute(dst, prefixLen, gw, ifIndex))
    {    
        // Limited to PL_DEBUG since route may _not_ pre-exist.  TBD - improve this by checking first???
        PLOG(PL_DEBUG, "LinuxRouteMgr::SetRoute() error deleting _possible_ pre-existing route to %s/%d\n",
                dst.GetHostString(), prefixLen);
    }    
    
    struct
    {
        struct nlmsghdr msg;
        struct rtmsg    rt;
        char            buf[1024];
    } req;
    memset(&req, 0, sizeof(req));
    
    // netlink message header
    req.msg.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
    req.msg.nlmsg_type = RTM_NEWROUTE;
    req.msg.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE  | NLM_F_ACK;
    UINT32 seq = sequence++;
    req.msg.nlmsg_seq = seq; 
    req.msg.nlmsg_pid = port_id;
    
    // route add request
    switch (dst.GetType())
    {
        case ProtoAddress::IPv4:
            req.rt.rtm_family = AF_INET;
            break;
#ifdef HAVE_IPV6
        case ProtoAddress::IPv6:
            req.rt.rtm_family = AF_INET6;
            break;
#endif // HAVE_IPV6
        default:
            PLOG(PL_ERROR, "LinuxRouteMgr::SetRoute() invalid destination address!\n");
            return false;
            break;
    }
    
    unsigned int addrBits = dst.GetLength() << 3;
    req.rt.rtm_dst_len = addrBits;
    req.rt.rtm_src_len = 0;
    req.rt.rtm_table = RT_TABLE_MAIN;
    req.rt.rtm_protocol = RTPROT_BOOT;
    if (gw.IsValid())
        req.rt.rtm_scope = RT_SCOPE_UNIVERSE; 
    else
        req.rt.rtm_scope = RT_SCOPE_LINK;
    if (dst.IsMulticast()) 
        req.rt.rtm_type = RTN_MULTICAST;
    else
        req.rt.rtm_type = RTN_UNICAST;
    
    req.rt.rtm_flags = 0;
    
    // Set destination rtattr
    if (dst.IsValid())
    {
        
        if (prefixLen < addrBits)
        {
            // address bits past mask _must_ be ZERO, so we test for that here
            unsigned int index = prefixLen >> 3;
            const char* bytes = dst.GetRawHostAddress();
            if (0 != ((0x00ff >> (prefixLen & 0x07)) & bytes[index]))
            {
                PLOG(PL_ERROR, "LinuxRouteMgr::SetRoute() invalid address for given mask\n");
                return false;   
            }
            while (++index < dst.GetLength())
            {
                if (0 != bytes[index] )
                {
                    PLOG(PL_ERROR, "LinuxRouteMgr::SetRoute() invalid address for given mask\n");
                    return false;
                }  
            }     
            req.rt.rtm_dst_len = prefixLen;       
        }
        else if (prefixLen > addrBits)
        {
            PLOG(PL_ERROR, "LinuxRouteMgr::SetRoute() invalid mask.\n");
            return false;
        }
        if (!NetlinkAddAttr(&req.msg, sizeof(req), RTA_DST, dst.GetRawHostAddress(), dst.GetLength()))
        {
            PLOG(PL_ERROR, "LinuxRouteMgr::SetRoute() error setting RTA_DST attr.\n");
            return false;
        }
    }
    else
    {
        PLOG(PL_ERROR, "LinuxRouteMgr::SetRoute() error: invalid destination address\n");
        return false;
    }
    
    // Set gateway rtattr
    if (gw.IsValid())
    {
        if (!NetlinkAddAttr(&req.msg, sizeof(req), RTA_GATEWAY, gw.GetRawHostAddress(), gw.GetLength()))
        {
            PLOG(PL_ERROR, "LinuxRouteMgr::SetRoute() error adding RTA_GATEWAY attr.\n");
            return false;
        }
    }
    else if (ifIndex == 0)
    {
        PLOG(PL_ERROR, "LinuxRouteMgr::SetRoute() error: invalid gateway address\n");
        return false;   
    }
    
    if (ifIndex != 0)
    {
        UINT32 value = ifIndex;
        if (!NetlinkAddAttr(&req.msg, sizeof(req), RTA_OIF, &value, sizeof(UINT32)))
        {
            PLOG(PL_ERROR, "LinuxRouteMgr::SetRoute() error adding RTA_OIF attr.\n");
            return false;
        }  
    }
    
    // Set default route metric values
    if (metric < 0)
    {
        if (gw.IsValid())
            metric = 2;
        else
            metric = 1;
    }
    
    if (metric >= 0)
    {
        UINT32 value = metric; 
        if (!NetlinkAddAttr(&req.msg, sizeof(req), RTA_PRIORITY, &value, sizeof(UINT32)))
        {
            PLOG(PL_ERROR, "LinuxRouteMgr::SetRoute() error adding RTA_PRIORITY attr.\n");
            return false;
        }  
    }
    
    // Send request to netlink socket
    int result = send(descriptor, &req, req.msg.nlmsg_len, 0);
    if ((int)req.msg.nlmsg_len != result)
    {
        PLOG(PL_ERROR, "LinuxRouteMgr::SetRoute() send() error: %s\n",
                strerror(errno));
        return false;   
    } 
     
    // Check response for error code    
    if (NetlinkCheckResponse(seq))
    {
        return true;
    }
    else
    {
        PLOG(PL_ERROR, "LinuxRouteMgr::SetRoute() error setting route\n");
        return false;
    }    
}  // end LinuxRouteMgr::SetRoute()

bool LinuxRouteMgr::DeleteRoute(const ProtoAddress& dst,
                                unsigned int        prefixLen,
                                const ProtoAddress& /*gateway*/,
                                unsigned int        ifIndex)
{
    ProtoAddress gw;
    unsigned int debugCount = 1;
    PLOG(PL_DEBUG, "LinuxRouteMgr::DeleteRoute() %u) getting route(s) to dst>%s/%d ",
            debugCount++, dst.GetHostString(), prefixLen);
    int metric;
    while (GetRoute(dst, prefixLen, gw, ifIndex, metric))
    {   
        PLOG(PL_DEBUG, "LinuxRouteMgr::DeleteRoute() %u) deleting dst>%s/%d ",
                debugCount++, dst.GetHostString(), prefixLen);
        if (gw.IsValid())
            PLOG(PL_DEBUG, "gw>%s (idx>%d)\n", gw.GetHostString(), ifIndex);
        else
            PLOG(PL_DEBUG, "direct idx>%d\n", ifIndex);      
        
        struct
        {
            struct nlmsghdr msg;
            struct rtmsg    rt;
            char            buf[1024];
        } req;
        memset(&req, 0, sizeof(req));

        // netlink message header
        req.msg.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
        req.msg.nlmsg_type = RTM_DELROUTE;
        req.msg.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_MATCH;
        UINT32 seq = sequence++;
        req.msg.nlmsg_seq = seq; 
        req.msg.nlmsg_pid = port_id;

        // route delete request
        switch (dst.GetType())
        {
            case ProtoAddress::IPv4:
                req.rt.rtm_family = AF_INET;
                break;
#ifdef HAVE_IPV6
            case ProtoAddress::IPv6:
                req.rt.rtm_family = AF_INET6;
                break;
#endif // HAVE_IPV6
            default:
                PLOG(PL_ERROR, "LinuxRouteMgr::DeleteRoute() invalid destination address!\n");
                return false;
                break;
        }

        req.rt.rtm_dst_len = prefixLen;
        req.rt.rtm_src_len = 0;
        req.rt.rtm_table = RT_TABLE_UNSPEC;
        req.rt.rtm_protocol = RTPROT_BOOT;

        if (gw.IsValid())
            req.rt.rtm_scope = RT_SCOPE_UNIVERSE;   
        else
            req.rt.rtm_scope = RT_SCOPE_LINK;
        if (dst.IsMulticast())
            req.rt.rtm_type = RTN_MULTICAST;
        else
            req.rt.rtm_type = RTN_UNICAST;

        req.rt.rtm_flags = 0;

        // Set destination rtattr
        if (dst.IsValid())
        {       
            unsigned int addrBits = dst.GetLength() << 3;
            if (prefixLen < addrBits)
            {
                // address bits past mask _must_ be ZERO, we test for that here
                unsigned int index = prefixLen >> 3;
                const char* bytes = dst.GetRawHostAddress();
                if (0 != ((0x00ff >> (prefixLen & 0x07)) & bytes[index]))
                {
                    PLOG(PL_ERROR, "LinuxRouteMgr::DeleteRoute() invalid address for given mask\n");
                    return false;   
                }
                while (++index < dst.GetLength())
                {
                    if (0 != bytes[index] )
                    {
                        PLOG(PL_ERROR, "LinuxRouteMgr::DeleteRoute() invalid address for given mask\n");
                        return false;
                    }  
                }            
            }
            else if (prefixLen > addrBits)
            {
                PLOG(PL_ERROR, "LinuxRouteMgr::DeleteRoute() invalid mask.\n");
                return false;
            }
            if (!NetlinkAddAttr(&req.msg, sizeof(req), RTA_DST, dst.GetRawHostAddress(), dst.GetLength()))
            {
                PLOG(PL_ERROR, "LinuxRouteMgr::DeleteRoute() error setting RTA_DST attr.\n");
                return false;
            }
        }
        else
        {
            PLOG(PL_ERROR, "LinuxRouteMgr::DeleteRoute() no valid destination given\n");
            return false;
        }

        if (gw.IsValid())
        {
            if (!NetlinkAddAttr(&req.msg, sizeof(req), RTA_GATEWAY, gw.GetRawHostAddress(), gw.GetLength()))
            {
                PLOG(PL_ERROR, "LinuxRouteMgr::DeleteRoute() error setting RTA_GATEWAY attr.\n");
                return false;
            }
        }

        if (0 != ifIndex)
        {
            UINT32 index = ifIndex;
            if (!NetlinkAddAttr(&req.msg, sizeof(req), RTA_OIF, &index, sizeof(UINT32)))
            {
                PLOG(PL_ERROR, "LinuxRouteMgr::DeleteRoute() error adding RTA_GATEWAY attr.\n");
                return false;
            }  
        }

        // Send request to netlink socket
        int result = send(descriptor, &req, req.msg.nlmsg_len, 0);
        if ((int)req.msg.nlmsg_len != result)
        {
            PLOG(PL_ERROR, "LinuxRouteMgr::DeleteRoute() send() error: %s\n",
                    strerror(errno));
            return false;   
        }

        // Check response for error code
        if (!NetlinkCheckResponse(seq))
        {
            PLOG(PL_DEBUG, "LinuxRouteMgr::DeleteRoute() error deleting route\n");
            return false;   
        }
    }
    return true;
}  // end LinuxRouteMgr::DeleteRoute()

            
bool LinuxRouteMgr::GetRoute(const ProtoAddress& dst, 
                              unsigned int       prefixLen,
                              ProtoAddress&      gw,
                              unsigned int&      ifIndex,
                              int&               metric)
{   
    bool routeFound = false;
    bool complete = false;
    int bufferSize = 4096;    
    do
    {
        // Init return values
        gw.Invalidate();
        ifIndex = 0;
        char* buffer = new char[bufferSize];
        if (!buffer)
        {
            PLOG(PL_ERROR, "LinuxRouteMgr::GetRoute() new buffer error: %s\n", strerror(errno));
            return false;   
        }
    
        // Construct request for route
        struct
        {
            struct nlmsghdr msg;
            struct rtmsg    rt;
            char            buf[512];   
        } req;
        memset(&req, 0, sizeof(req));

        // netlink message header
        req.msg.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
        req.msg.nlmsg_type = RTM_GETROUTE;
        req.msg.nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH;
        UINT32 seq = sequence++;
        req.msg.nlmsg_seq = seq; 
        req.msg.nlmsg_pid = port_id;

        // route dump request
        switch (dst.GetType())
        {
            case ProtoAddress::IPv4:
                req.rt.rtm_family = AF_INET;
                break;
#ifdef HAVE_IPV6
            case ProtoAddress::IPv6:
               req.rt.rtm_family = AF_INET6;
                break;
#endif // HAVE_IPV6
            default:
                PLOG(PL_ERROR, "LinuxRouteMgr::GetRoute() invalid destination address!\n");
                delete[] buffer;
                return false;
                break;
        }
        req.rt.rtm_dst_len = prefixLen;
        req.rt.rtm_src_len = 0;
        req.rt.rtm_table = RT_TABLE_UNSPEC;
        req.rt.rtm_protocol = RTPROT_UNSPEC;
        req.rt.rtm_scope = RT_SCOPE_UNIVERSE;
        req.rt.rtm_type = RTN_UNSPEC;

        req.rt.rtm_flags = 0;

        // Set destination rtattr
        if (dst.IsValid())
        {
            unsigned int addrBits = dst.GetLength() << 3;
            if (prefixLen < addrBits)
            {
                // address bits past mask _must_ be ZERO, we test for that here
                unsigned int index = prefixLen >> 3;
                const char* bytes = dst.GetRawHostAddress();
                if (0 != ((0x00ff >> (prefixLen & 0x07)) & bytes[index]))
                {
                    PLOG(PL_ERROR, "LinuxRouteMgr::GetRoute() invalid address for given mask\n");
                    delete[] buffer;
                    return false;   
                }
                while (++index < dst.GetLength())
                {
                    if (0 != bytes[index] )
                    {
                        PLOG(PL_ERROR, "LinuxRouteMgr::GetRoute() invalid address for given mask\n");
                        delete[] buffer;
                        return false;
                    }  
                }            
            }
            else if (prefixLen > addrBits)
            {
                PLOG(PL_ERROR, "LinuxRouteMgr::GetRoute() invalid mask.\n");
                delete[] buffer;
                return false;
            }
            if (!NetlinkAddAttr(&req.msg, sizeof(req), RTA_DST, dst.GetRawHostAddress(), dst.GetLength()))
            {
                PLOG(PL_ERROR, "LinuxRouteMgr::GetRoute() error setting RTA_DST attr.\n");
                delete[] buffer;
                return false;
            }
        }
        else
        {
            PLOG(PL_ERROR, "LinuxRouteMgr::GetRoute() no valid destination given\n");
            delete[] buffer;
            return false;
        }

        // write message to our netlink socket
        int result = send(descriptor, &req, req.msg.nlmsg_len, 0);
        if (result < 0)
        {
            PLOG(PL_ERROR, "LinuxRouteMgr::GetRoute() send() error: %s\n",
                    strerror(errno));
            delete[] buffer;
            return false;   
        }

        // read response(s) until NLMSG_DONE
        bool done = false;
        bool truncated = false;
        while(!done)
        {
            int msgLen = recv(descriptor, buffer, bufferSize, 0); 
            if (msgLen < 0)
            {
                PLOG(PL_ERROR, "LinuxRouteMgr::GetRoute() recv() error: %s\n", strerror(errno)); 
                delete[] buffer;
                return false;  
            }
            else if (msgLen == bufferSize)
            {
                bufferSize *= 2;
                truncated = true; 
                // TBD - should use struct msghdr.msg_flags MSG_TRUNC flag to detect this isntead 
            }
            
            struct nlmsghdr* msg = (struct nlmsghdr*)buffer;
            for (; 0 != NLMSG_OK(msg, (unsigned int)msgLen); msg = NLMSG_NEXT(msg, msgLen))
            {
//Justin comment oout pid part below if pid issues occur.  This can happen with some virtualized systems. ie core namespaces
//Justin Below is a semi-hack to get SMF to function correctly in core namespaces.  
//If when the msg->nlmsg_pid gets fixed this ifdef can be removed
#ifndef CORE_NAMESPACES
                if ((msg->nlmsg_pid == port_id) && (msg->nlmsg_seq == seq))
#else
                if (msg->nlmsg_seq == seq)
#endif // if/else !CORE_NAMESPACES
                {       
                    if (!truncated) complete = true;  // matching response within our buffer bounds   
                    switch (msg->nlmsg_type)
                    {
                        case NLMSG_NOOP:
                            TRACE("recvd NLMSG_NOOP ...\n");
                            break;
                        case NLMSG_ERROR:
                        {
                            struct nlmsgerr* errorMsg = (struct nlmsgerr*)NLMSG_DATA(msg);
                            PLOG(PL_DEBUG, "LinuxRouteMgr::GetRoute() recvd matching NLMSG_ERROR error seq:%d code:%d...\n", 
                                    msg->nlmsg_seq, errorMsg->error);
                            delete[] buffer;
                            return false;
                            break;
                        }
                        case NLMSG_DONE:
                            //TRACE("recvd NLMSG_DONE ...\n");
                            done = true;
                            break;
                        case NLMSG_OVERRUN:
                            TRACE("recvd NLMSG_OVERRUN ...\n");
                            break;
                        case RTM_GETROUTE:
                        {
                            TRACE("recvd RTM_GETROUTE ... \n");
                            break;
                        }
                        case RTM_NEWROUTE:
                        {
                            if (truncated) break;  // ignore "truncated" responses
                            ProtoAddress destination;
                            destination.Reset(dst.GetType());
                            unsigned int prefixLen = 0;
                            //bool havePrefSource = false;
                            ProtoAddress gateway;
                            gateway.Invalidate();
                            unsigned int interfaceIndex = 0;

                            struct rtmsg* rtm = (struct rtmsg*)NLMSG_DATA(msg);
                            struct rtattr* rta = RTM_RTA(rtm);
                            int rtaLen = msg->nlmsg_len - NLMSG_LENGTH(sizeof(struct rtmsg));
                            for (; RTA_OK(rta, rtaLen); 
                                 rta = RTA_NEXT(rta, rtaLen))
                            {
                                switch (rta->rta_type)
                                {
                                    case RTA_DST:
                                    {   
                                        prefixLen = rtm->rtm_dst_len;
                                        destination.SetRawHostAddress(dst.GetType(), (char*)RTA_DATA(rta), 
                                                                      (prefixLen+7)>>3);
                                        //TRACE("got dst: \"%s\"\n", destination.GetHostString());
                                        break;
                                    }
                                    case RTA_GATEWAY:
                                    {   
                                        gateway.SetRawHostAddress(dst.GetType(), (char*)RTA_DATA(rta), dst.GetLength());
                                        //TRACE("got gw: \"%s\"\n", gw.GetHostString());
                                        break;
                                    }

                                    case RTA_PREFSRC:
                                    {   
                                        //ProtoAddress addr;
                                        //addr.SetRawHostAddress(dst.GetType(), (char*)RTA_DATA(rta), dst.GetLength());
                                        //havePrefSource = true;
                                        break;
                                    }
                                    case RTA_OIF:
                                    {
                                        ASSERT(4 == RTA_PAYLOAD(rta));
                                        memcpy(&interfaceIndex, RTA_DATA(rta), 4);
                                        //TRACE("got ifIndex: %d\n", ifIndex);
                                        break;
                                    }
                                    case RTA_PRIORITY:
                                    {
                                        UINT32 priority;
                                        memcpy(&priority, RTA_DATA(rta), 4);
                                        metric = priority;
                                        //TRACE("RTA_PRIORITY value: %d\n", metric);
                                        break;
                                    }
                                    default:
                                        //TRACE("LinuxRouteMgr::GetRoute() unhandled rtattr type:%d len:%d\n", 
                                        //      rta->rta_type, RTA_PAYLOAD(rta));
                                    break;

                                }  // end switch(rta_type)
                            }  // end for(RTA_NEXT())
                            
                            if ((RTN_BROADCAST != rtm->rtm_type) &&
                                (RTN_UNREACHABLE != rtm->rtm_type) &&
                                //destination.IsValid() &&
                                destination.HostIsEqual(dst) &&
                                (RTN_LOCAL != rtm->rtm_type) &&
                                (0 == (RTM_F_CLONED & rtm->rtm_flags)))
                            {
                                if (!routeFound)
                                {
                                    if (gateway.IsValid() &&
                                        !gateway.HostIsEqual(dst) &&
                                        !gateway.IsUnspecified())
                                        gw = gateway;
                                    if (gw.IsValid() || (interfaceIndex > 0))
                                    {
                                        ifIndex = interfaceIndex;
                                        // (TBD) fill in real metric
                                        metric = -1;
                                        routeFound = true;  
                                        //delete[] buffer; return true;
                                    }
                                }
                            }
                            break;
                        }
                        default:
                            TRACE("matching reply type:%d len:%d bytes\n", 
                                  msg->nlmsg_type, msg->nlmsg_len);
                            break;
                    }  // end switch(nlmsg_type)
                }
                else
                {
                    if (NLMSG_ERROR == msg->nlmsg_type)
                    {
                        struct nlmsgerr* errorMsg = (struct nlmsgerr*)NLMSG_DATA(msg);
                        PLOG(PL_DEBUG, "LinuxRouteMgr::GetRoute() recvd non-matching NLMSG_ERROR seq:%d code:%d\n", 
                                msg->nlmsg_seq, errorMsg->error);  
                    }
                }  // end if/else (matching pid && seq)
            }  // end for(NLMSG_NEXT())
        }  // end while (!done)
        delete[] buffer;
        buffer = NULL;
    } while (!complete);
    return routeFound;
}  // end LinuxRouteMgr::GetRoute()
            
            
bool LinuxRouteMgr::GetAllRoutes(ProtoAddress::Type addrType,
                                 ProtoRouteTable&   routeTable)
{
    // The "do" loop is needed to make sure we get all of the routes
    // Routing tables can be big (particularly IPv6) and since the
    // Netlink does _not_ do multi-part messages properly (It only
    // gives you part of the routing table if your receive buffer 
    // isn't big enough), we repeat the request, increasing the buffer
    // size until it works

    bool complete = false;
    
    int bufferSize = 4096;
    int failSafeCount = 0;
    int failSafeMax = 10000;
    do
    {
        if(failSafeCount++>failSafeMax)
        {
            PLOG(PL_ERROR, "LinuxRouteMgr::GetAllRoutes() didn't find any routes!\n");
            return false;
        }
        char* buffer = new char[bufferSize];
        if (!buffer)
        {
            PLOG(PL_ERROR, "LinuxRouteMgr::GetAllRoutes() new buffer error: %s\n", strerror(errno));
            return false;   
        }
        // Construct request for all routes
        struct
        {
            struct nlmsghdr msg;
            struct rtmsg    rt;   
        } req;
        memset(&req, 0, sizeof(req));

        // netlink message header
        req.msg.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
        req.msg.nlmsg_type = RTM_GETROUTE;
        req.msg.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP | NLM_F_MULTI;
        UINT32 seq = sequence++;
        req.msg.nlmsg_seq = seq; 
        req.msg.nlmsg_pid = port_id;

        // route dump request
        switch (addrType)
        {
            case ProtoAddress::IPv4:
                req.rt.rtm_family = AF_INET;
                break;
#ifdef HAVE_IPV6
            case ProtoAddress::IPv6:
                req.rt.rtm_family = AF_INET6;
                break; 
#endif // HAVE_IPV6
            default:
                PLOG(PL_ERROR, "LinuxRouteMgr::GetAllRoutes() invalid address type\n");
                delete[] buffer;
                return false;
        }

        req.rt.rtm_dst_len = 0;
        req.rt.rtm_src_len = 0;
        req.rt.rtm_table = RT_TABLE_UNSPEC;
        req.rt.rtm_protocol = RTPROT_UNSPEC;
        req.rt.rtm_scope = RT_SCOPE_UNIVERSE;
        req.rt.rtm_type = RTN_UNSPEC; 
        req.rt.rtm_flags = 0;

        // write message to our netlink socket
        int result = send(descriptor, &req, req.msg.nlmsg_len, 0);
        if (result < 0)
        {
            PLOG(PL_ERROR, "LinuxRouteMgr::GetAllRoutes() send() error: %s\n", strerror(errno));
            delete[] buffer;
            return false;   
        }

        // read response(s) until NLMSG_DONE 
        // (if response is truncated, rescale "bufferSize" and try again
        bool done = false;
        bool truncated = false;
        while(!done)
        {
//fprintf(stderr,"tiger time %d is buffer size\n",bufferSize);
            int msgLen = recv(descriptor, buffer, bufferSize, 0); 
//fprintf(stderr,"tiger time sdfsdfi\n");
            if (msgLen < 0)
            {
//fprintf(stderr,"tiger time 2\n");
                PLOG(PL_ERROR, "LinuxRouteMgr::GetAllRoutes() recv() error: %s\n", 
                                strerror(errno));
                delete[] buffer;
                return false;  
            } 
            else if (msgLen == bufferSize)
            {
//fprintf(stderr,"tiger time 3\n");
                // assume our receive buffer wasn't big enough
                // mark response as "truncated"
                bufferSize *= 2;
                truncated = true;  
            }

            struct nlmsghdr* msg = (struct nlmsghdr*)buffer;
            for (; 0 != NLMSG_OK(msg, (unsigned int)msgLen); msg = NLMSG_NEXT(msg, msgLen))
            { 
// Justin Below is a semi-hack to get SMF to function correctly in core namespaces.  
// If when the msg->nlmsg_pid gets fixed this #ifdef can be removed
#ifndef CORE_NAMESPACES
                if ((msg->nlmsg_pid == port_id) && (msg->nlmsg_seq == seq))
#else
                if (msg->nlmsg_seq == seq)
#endif // if/else !CORE_NAMESPACES
                {       
                    if (!truncated) complete = true;  // we got a matching response _within_ our buffer bounds
                    // if (0 != (NLM_F_MULTI & msg->nlmsg_flags)) TRACE("multi part message ...\n");
                    switch (msg->nlmsg_type)
                    {
                        case NLMSG_NOOP:
                            TRACE("recvd NLMSG_NOOP ...\n");
                            break;
                        case NLMSG_ERROR:
                        {
                            struct nlmsgerr* errorMsg = (struct nlmsgerr*)NLMSG_DATA(msg);
                            PLOG(PL_ERROR, "LinuxRouteMgr::GetAllRoutes() recvd NLMSG_ERROR error seq:%d code:%d...\n", 
                                    msg->nlmsg_seq, errorMsg->error);
                            break;
                        }
                        case NLMSG_DONE:
                            done = true;
                            break;
                        case NLMSG_OVERRUN:
                            TRACE("recvd NLMSG_OVERRUN ...\n");
                            break;
                        case RTM_GETROUTE:
                        {
                            TRACE("recvd RTM_GETROUTE ...\n");
                            break;
                        }
                        case RTM_NEWROUTE:
                        {
                            if (truncated) break; // ignore truncated responses
                            //TRACE("matching RTM_NEWROUTE reply. len:%d bytes\n", msg->nlmsg_len);
                            ProtoAddress gateway;
                            gateway.Invalidate();
                            ProtoAddress destination;
                            destination.Invalidate();
                            unsigned int prefixLen = 0;
                            unsigned int ifIndex = 0;
                            int metric = -1;
                            struct rtmsg* rtm = (struct rtmsg*)NLMSG_DATA(msg);
                            struct rtattr* rta = RTM_RTA(rtm);
                            int rtaLen = msg->nlmsg_len - NLMSG_LENGTH(sizeof(struct rtmsg));
                            ProtoAddress::Type addrType = ProtoAddress::INVALID;
                            int addrLen = 0;
                            switch (rtm->rtm_family)
                            {
                                case AF_INET:
                                    addrType = ProtoAddress::IPv4;
                                    addrLen = 4;
                                    break;
#ifdef HAVE_IPV6        
                                case AF_INET6:
                                    addrType = ProtoAddress::IPv6;
                                    addrLen = 16;
                                    break;
#endif // HAVE_IPV6
                                default:
                                    PLOG(PL_ERROR, "LinuxRouteMgr::GetAllRoutes() recvd invalid addr family\n");
                                    delete[] buffer;
                                    return false;
                                    break;
                            }
                            for (; RTA_OK(rta, rtaLen); 
                                 rta = RTA_NEXT(rta, rtaLen))
                            {
                                switch (rta->rta_type)
                                {
                                    case RTA_DST:
                                    {   
                                        prefixLen = rtm->rtm_dst_len;
                                        destination.SetRawHostAddress(addrType, (char*)RTA_DATA(rta), (prefixLen+7)>>3);
                                        //TRACE("RTA_DST: %s/%d\n", destination.GetHostString(), 
                                        //                           rtm->rtm_dst_len);
                                        break;
                                    }
                                    case RTA_GATEWAY:
                                    {   
                                        gateway.SetRawHostAddress(addrType, (char*)RTA_DATA(rta), addrLen);
                                        //TRACE("RTA_GWY: %s\n", gateway.GetHostString());
                                        break;
                                    }

                                    case RTA_PREFSRC:
                                    {   
                                        //ProtoAddress addr;
                                        //addr.SetRawHostAddress(addrType, (char*)RTA_DATA(rta), addrLen);
                                        //TRACE("RTA_SRC: %s\n", addr.GetHostString());
                                        break;
                                    }
                                    case RTA_OIF:
                                    {
                                        memcpy(&ifIndex, RTA_DATA(rta), RTA_PAYLOAD(rta));
                                        //TRACE("RTA_OIF: %d\n", ifIndex);
                                        break;
                                    }
                                    case RTA_PRIORITY:
                                    {
                                        UINT32 priority;
                                        memcpy(&priority, RTA_DATA(rta), 4);
                                        metric = priority;
                                        //TRACE("LinuxRouteMgr::GetAllRoutes() RTA_PRIORITY value: %d\n", metric);
                                        break;
                                    }
                                    case RTA_METRICS:
                                    {
                                        //TRACE("get all routes got RTA_METRIC ...\n");
                                        break;
                                    }
                                    
                                    default:
                                        //TRACE("LinuxRouteMgr::GetAllRoutes() unhandled rtattr type:%d len:%d\n", 
                                        //      rta->rta_type, RTA_PAYLOAD(rta));
                                    break;

                                }
                            }  // end for(RTA_NEXT())

                            if ((RTN_BROADCAST != rtm->rtm_type) &&
                                (RTN_UNREACHABLE != rtm->rtm_type) &&
                                (RTN_LOCAL != rtm->rtm_type) &&
                                (0 == (RTM_F_CLONED & rtm->rtm_flags)))
                            {
                                if (!destination.IsValid()) 
                                {
                                    destination.Reset(addrType); 
                                    prefixLen = 0;
                                }
                                
                                if (gateway.IsValid() && (gateway.IsUnspecified() || gateway.HostIsEqual(destination))) 
                                {
                                    //TRACE("found UNSPECIFIED gateway\n");
                                    gateway.Invalidate();
                                }
                                
                                if (!routeTable.SetRoute(destination, prefixLen, gateway, ifIndex, metric))
                                {
                                    PLOG(PL_ERROR, "LinuxRouteMgr::GetAllRoutes() error creating table entry\n");
                                    delete[] buffer;
                                    return false;   
                                }
                            }
                            break;
                        }
                        default:
                            TRACE("matching reply type:%d len:%d bytes\n", 
                                  msg->nlmsg_type, msg->nlmsg_len);
                            break;
                    }  // end switch()
                }
                else
                {
            //fprintf(stderr,"NLMSG_OK if false\n");
            //fprintf(stderr,"%d %d is msg type\n",msg->nlmsg_type,NLMSG_ERROR);
                    if (NLMSG_ERROR == msg->nlmsg_type)
                    {
                    //fprintf(stderr,"before error message\n");
                        struct nlmsgerr* errorMsg = (struct nlmsgerr*)NLMSG_DATA(msg);
                        PLOG(PL_ERROR, "LinuxRouteMgr::GetAllRoutes() recvd NLMSG_ERROR seq:%d code:%d\n", 
                                msg->nlmsg_seq, errorMsg->error);  
                    }
                    //fprintf(stderr," bunny time 1\n");
                }
                    //fprintf(stderr," bunny time 2\n");
            }  // end for(NLMSG_NEXT())
                    //fprintf(stderr," bunny time 3\n");
        }  // end while (!done)
                    //fprintf(stderr," bunny time 4\n");
        delete[] buffer;
        buffer = NULL;
                    //fprintf(stderr," bunny time 5\n");
    } while (!complete);          
    return true;
}  // end LinuxRouteMgr::GetAllRoutes()
          
bool LinuxRouteMgr::GetInterfaceAddressList(unsigned int        ifIndex, 
                                            ProtoAddress::Type  addrType,
                                            ProtoAddressList&   addrList)
{
    ProtoAddressList localAddrList;  // keep link/site local addresses separate and add to "addrList" at end
    // Construct request for interface address
    struct
    {
        struct nlmsghdr     msg;
        struct ifaddrmsg    ifa;   
    } req;
    memset(&req, 0, sizeof(req));
      
//    DMSG(0,"Justin comment in getinterfaceaddress\n");
    // netlink message header
    req.msg.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
    req.msg.nlmsg_type = RTM_GETADDR;
    req.msg.nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH ;
    UINT32 seq = sequence++;
    req.msg.nlmsg_seq = seq; 
    req.msg.nlmsg_pid = port_id;
    
    // route dump request
    unsigned int addrLength = 0;
    switch (addrType)
    {
        case ProtoAddress::IPv4:
//    DMSG(0,"Justin comment in getinterfaceaddress is ipv4\n");
            req.ifa.ifa_family = AF_INET;
            req.ifa.ifa_prefixlen = 32;
            addrLength = 4;
            break;
#ifdef HAVE_IPV6
        case ProtoAddress::IPv6:
//    DMSG(0,"Justin comment in getinterfaceaddress is ipv6\n");
            req.ifa.ifa_family = AF_INET6;
            req.ifa.ifa_prefixlen = 128;
            addrLength = 16;
            break;
#endif // HAVE_IPV6
        default:
            PLOG(PL_ERROR, "LinuxRouteMgr::GetInterfaceAddressList() invalid destination address!\n");
            return false;
            break;
    }
    req.ifa.ifa_flags = 0;//IFA_F_SECONDARY;//0;//IFA_F_PERMANENT;
    req.ifa.ifa_scope = RT_SCOPE_UNIVERSE;
    req.ifa.ifa_index = ifIndex;
    
    // write message to our netlink socket
//    DMSG(0,"Justin before send comment in getinterfaceaddress is ipv4\n");
    int result = send(descriptor, &req, req.msg.nlmsg_len, 0);
//    DMSG(0,"Justin comment after send in getinterfaceaddress is ipv4\n");
    if (result < 0)
    {
        PLOG(PL_ERROR, "LinuxRouteMgr::GetInterfaceAddressList() send() error: %s\n",
                strerror(errno));
        return false;   
    }
    
    // read response(s)
    bool done = false;
    while(!done)
    {
//        DMSG(0,"Justin in while comment in getinterfaceaddress is ipv4\n");
        char buffer[4096];
        int msgLen = recv(descriptor, buffer, 4096, 0); 
        if (msgLen < 0)
        {
            PLOG(PL_ERROR, "LinuxRouteMgr::GetInterfaceAddressList() recv() error: %s\n", strerror(errno)); 
            return false;  
        }
        struct nlmsghdr* msg = (struct nlmsghdr*)buffer;
        for (; 0 != NLMSG_OK(msg, (unsigned int)msgLen); msg = NLMSG_NEXT(msg, msgLen))
        {
//Justin Below is a semi-hack to get SMF to function correctly in core namespaces.  
//If when the msg->nlmsg_pid gets fixed this #if can be removed
#ifndef CORE_NAMESPACES
          if ((msg->nlmsg_pid == port_id) && (msg->nlmsg_seq == seq))
#else
            if (msg->nlmsg_seq == seq)
#endif // if/else !CORE_NAMESPACES
            {          
                switch (msg->nlmsg_type)
                {
                    case NLMSG_NOOP:
                        //TRACE("recvd NLMSG_NOOP ...\n");
                        break;
                    case NLMSG_ERROR:
                    {
                        struct nlmsgerr* errorMsg = (struct nlmsgerr*)NLMSG_DATA(msg);
                        PLOG(PL_ERROR, "LinuxRouteMgr::GetInterfaceAddressList() recvd NLMSG_ERROR error seq:%d code:%d...\n", 
                                msg->nlmsg_seq, errorMsg->error);
                        return false;
                        break;
                    }
                    case NLMSG_DONE:
                        //TRACE("recvd NLMSG_DONE ...\n");
                        done = true;
                        break;
                    case RTM_NEWADDR:
                    {
                        //TRACE("recvd RTM_NEWADDR ... \n");
                        struct ifaddrmsg* ifa = (struct ifaddrmsg*)NLMSG_DATA(msg);
                        struct rtattr* rta = IFA_RTA(ifa);
                        int rtaLen = msg->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifaddrmsg));
                        for (; RTA_OK(rta, rtaLen); 
                             rta = RTA_NEXT(rta, rtaLen))
                        {
                            switch (rta->rta_type)
                            {
                                case IFA_ADDRESS:
                                case IFA_LOCAL:
                                {   
                                    //ProtoAddress addr;
                                    //addr.SetRawHostAddress(addrType, (char*)RTA_DATA(rta), addrLength);
                                    //TRACE("%s: %s (index:%d scope:%d)\n", (IFA_ADDRESS == rta->rta_type) ?
                                    //      "IFA_ADDRESS" : "IFA_LOCAL", addr.GetHostString(),
                                    //      ifa->ifa_index, ifa->ifa_scope);
                                    if (ifa->ifa_index == ifIndex)
                                    {
                                        switch (ifa->ifa_scope)
                                        {
                                            case RT_SCOPE_UNIVERSE: 
                                            {
                                                ProtoAddress theAddress;
                                                theAddress.SetRawHostAddress(addrType, (char*)RTA_DATA(rta), addrLength);
                                                if (theAddress.IsValid())
                                                {
                                                    if (!addrList.Insert(theAddress))
                                                    {
                                                        PLOG(PL_ERROR, "LinuxRouteMgr::GetInterfaceAddressList() error: couldn't add to addrList\n");
                                                        done = true;
                                                    }
                                                }
                                                break;
                                            }                             
                                            case RT_SCOPE_SITE:
                                            case RT_SCOPE_LINK:
                                            {
                                                // Keep site-local addresses in separate list and add at end.
                                                ProtoAddress theAddress;
                                                theAddress.SetRawHostAddress(addrType, (char*)RTA_DATA(rta), addrLength);
                                                if (theAddress.IsValid() && !localAddrList.Insert(theAddress))
                                                    PLOG(PL_ERROR, "LinuxRouteMgr::GetInterfaceAddressList() error: couldn't add to localAddrList\n");
                                                break;
                                            }
                                            default:
                                                // ignore other address types for now
                                                break;
                                        }
                                    }
                                    break;
                                }
                                case IFA_BROADCAST:
                                {
                                    //ProtoAddress addr;
                                    //addr.SetRawHostAddress(addrType, (char*)RTA_DATA(rta), addrLength);
                                    //TRACE("IFA_BROADCAST: %s\n", addr.GetHostString());
                                    break;
                                }
                                default:
                                    //TRACE("LinuxRouteMgr::GetInterfaceAddressList() unhandled rtattr type:%d len:%d\n", 
                                    //       rta->rta_type, RTA_PAYLOAD(rta));
                                break;
                                
                            }  // end switch(rta_type)
                        }  // end for(RTA_NEXT())
                        break;
                    }
                    default:
                        PLOG(PL_ERROR, "LinuxRouteMgr::GetInterfaceAddressList() matching reply type:%d len:%d bytes\n", 
                                msg->nlmsg_type, msg->nlmsg_len);
                        break;
                }  // end switch(nlmsg_type)
            }
            else
            {
                if (NLMSG_ERROR == msg->nlmsg_type)
                {
                    struct nlmsgerr* errorMsg = (struct nlmsgerr*)NLMSG_DATA(msg);
                    PLOG(PL_ERROR, "LinuxRouteMgr::GetInterfaceAddressList() recvd NLMSG_ERROR seq:%d code:%d\n", 
                            msg->nlmsg_seq, errorMsg->error);  
                }
            }  // end if/else (matching pid && seq)
        }  // end for(NLMSG_NEXT())
    }  // end while (!done)
    
    ProtoAddressList::Iterator iterator(localAddrList);
    ProtoAddress localAddr;
    while (iterator.GetNextAddress(localAddr))
    {
        if (!addrList.Insert(localAddr))
        {
            PLOG(PL_ERROR, "LinuxRouteMgr::GetInterfaceAddressList() error: couldn't add localAddr to addrList\n");
            break;
        }
    }    
    if (addrList.IsEmpty())
        PLOG(PL_WARN, "LinuxRouteMgr::GetInterfaceAddressList() warning: no addresses found\n");
    return true;
}  // end LinuxRouteMgr::GetInterfaceAddressList()