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
#include "unix/zebraRouteMgr.h"

void ZebraRouteMgr::OnClientSocketEvent(ProtoSocket&       theSocket,
                                       ProtoSocket::Event theEvent)
{
     //printf("Socket Event\n");
}

ZebraRouteMgr::ZebraRouteMgr()
	:zPipe(ProtoPipe::STREAM)
{
}

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

bool ZebraRouteMgr::SetForwarding(bool state)
{
    return true;
}  // end ZebraRouteMgr::SetForwarding()


bool ZebraRouteMgr::Open(const void* /*userData*/)
{
    //printf("zebra mgr open\n");
    if ((descriptor = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0)
    {
        printf("ZebraRouteMgr::Open() socket(NETLINK_ROUTE) error: %s\n",
                strerror(errno));
        return false;
    }
    else
    {
        pid = (UINT32)getpid();
    }

    //printf("descriptor open\n");

    if (!zPipe.Connect("/var/run/zserv.api")) {
        if(!zPipe.Connect("/var/run/quagga/zserv.api")) {
	    printf("ZebraRouteMgr::Open fail to connect %s\n",zPipe.GetErrorString());
            printf("ZebraRouteMgr::Open tries to connect to /var/run/zserv.api and /var/run/quagga/zserv.api.  Is Zebra running?  Is its runs directory one of the above (set durring zebra configure)?\n");
            return false;
        }
    }
    return true;

}  // end ZebraRouteMgr::Open()
bool ZebraRouteMgr::IsOpen() const
{
    //return zSock.IsOpen();
    return zPipe.IsOpen();
}

void ZebraRouteMgr::Close()
{
    if (IsOpen())
    {
	//zSock.Close();
	zPipe.Close();
    }   
}  // end ZebraRouteMgr::Close()

/*
  * "xdr_encode"-like interface that allows daemon (client) to send
  * a message to zebra server for a route that needs to be
  * added/deleted to the kernel. Info about the route is specified
  * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
  * the info down the zclient socket using the stream_* functions.
  *
  * The corresponding read ("xdr_decode") function on the server
  * side is zread_ipv4_add()/zread_ipv4_delete().
  *
  *  0 1 2 3 4 5 6 7 8 9 A B C D E F 0 1 2 3 4 5 6 7 8 9 A B C D E F
  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  * |            Length (2)         |    Command    | Route Type    |
  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  * | ZEBRA Flags   | Message Flags | Prefix length |
  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  * | Destination IPv4 Prefix for route                             |
  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  * | Nexthop count |
  * +-+-+-+-+-+-+-+-+
  *
  *
  * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
  * described, as per the Nexthop count. Each nexthop described as:
  *
  * +-+-+-+-+-+-+-+-+
  * | Nexthop Type  |  Set to one of ZEBRA_NEXTHOP_*
  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  * |       IPv4 Nexthop address or Interface Index number          |
  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  *
  * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or
  * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_
  * nexthop information is provided, and the message describes a prefix
  * to blackhole or reject route.
  *
  * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
  * byte value.
  *
  * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
  * byte value.
  *
  * XXX: No attention paid to alignment.
  */
bool ZebraRouteMgr::SetRoute(const ProtoAddress&   dst,
                             unsigned int          prefixLen,
                             const ProtoAddress&   gw,
                             unsigned int          ifIndex,
                             int                   metric)
{
    //printf("enter setRoute\n");
    unsigned char *s = obuf;
    unsigned short *p ; 
    unsigned int len;

    //header
    p = (unsigned short *)s;
    *p = htons(ZEBRA_HEADER_SIZE);
    s +=2 ; // length
    *s++ = ZEBRA_HEADER_MARKER; // marker
    *s++ = ZEBRA_VERSION; //version
    p = (unsigned short *)s;
    *p = htons(ZEBRA_IPV4_ROUTE_ADD); // cmd
    s += 2;
    *s++ = 0 ; //type
    *s++ = 0 ; //flags
    *s++ = ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_DISTANCE; //message

    // destionation
    *s++ = prefixLen; //prefixlen
    unsigned long ipLong;
    ipLong = dst.IPv4GetAddress(); //tbd for ipv6 and others
    ipLong = htonl(ipLong);
    int prefixInBytes = (prefixLen +7 )/8;
    memcpy(s,&ipLong,prefixInBytes); // copy the prefix addr
    s += prefixInBytes;
    
    // nexthop num
    *s++ = 2; // two nexthop entries
    // nexthop
    *s++ = ZEBRA_NEXTHOP_IPV4; //type: ipv4
    ipLong = gw.IPv4GetAddress(); //tbd for ipv6 and others
    ipLong = htonl(ipLong);
    memcpy(s,&ipLong,4); // copy the prefix addr
    s += 4;

    // ifindex
    *s++ = ZEBRA_NEXTHOP_IFINDEX; // type: ifindex
    ipLong = htonl(ifIndex); 
    memcpy(s,&ipLong,4); // copy the ifindex 
    s += 4;

    // distance
    *s++ = metric; // metric 

    len = s - obuf;
    ipLong = htons(len); // length
    p = (unsigned short *)obuf;
    *p = htons(len);
    //PrintBuffer((char *)obuf,len,0);
    //printf("zPipe Send len=%d\n",len);

    zPipe.Send((char *)obuf,len);
    //printf("Send len=%d %s\n",len,zPipe.GetErrorString());
    return true;
}  // end ZebraRouteMgr::SetRoute()


bool ZebraRouteMgr::DeleteRoute(const ProtoAddress& dst,
                                unsigned int        prefixLen,
                                const ProtoAddress& gw,
                                unsigned int        ifIndex)
{
    //printf("enter delroute\n");
    unsigned char *s = obuf;
    unsigned short *p ; 
    unsigned int len;

    //header
    p = (unsigned short *)s;
    *p = htons(ZEBRA_HEADER_SIZE);
    s +=2 ; // length
    *s++ = ZEBRA_HEADER_MARKER; // marker
    *s++ = ZEBRA_VERSION; //version
    p = (unsigned short *)s;
    *p = htons(ZEBRA_IPV4_ROUTE_DELETE); // cmd
    s += 2;
    *s++ = 0 ; //type
    *s++ = 0 ; //flags
    *s++ = ZAPI_MESSAGE_NEXTHOP ; //message

    // destionation
    *s++ = prefixLen; //prefixlen
    unsigned long ipLong;
    ipLong = dst.IPv4GetAddress(); //tbd for ipv6 and others
    ipLong = htonl(ipLong);
    int prefixInBytes = (prefixLen +7 )/8;
    memcpy(s,&ipLong,prefixInBytes); // copy the prefix addr
    s += prefixInBytes;
    
    // nexthop num
    *s++ = 2; // two nexthop entries
    // nexthop
    *s++ = ZEBRA_NEXTHOP_IPV4; //type: ipv4
    ipLong = gw.IPv4GetAddress(); //tbd for ipv6 and others
    ipLong = htonl(ipLong);
    memcpy(s,&ipLong,4); // copy the prefix addr
    s += 4;

    // ifindex
    *s++ = ZEBRA_NEXTHOP_IFINDEX; // type: ifindex
    ipLong = htonl(ifIndex); 
    memcpy(s,&ipLong,4); // copy the ifindex 
    s += 4;

    len = s - obuf;
    ipLong = htons(len); // length
    p = (unsigned short *)obuf;
    *p = htons(len);
    //PrintBuffer((char *)obuf,len,0);
    //printf("zPipe Send len=%d\n",len);

    zPipe.Send((char *)obuf,len);
    //printf("Send len=%d %s\n",len,zPipe.GetErrorString());
    return true;
}  // end ZebraRouteMgr::DeleteRoute()

            
bool ZebraRouteMgr::GetRoute(const ProtoAddress& dst,
                              unsigned int       prefixLen,
                              ProtoAddress&      gw,
                              unsigned int&      ifIndex,
                              int&               metric)
{   
    return true;
}  // end ZebraRouteMgr::GetRoute()
            
            
bool ZebraRouteMgr::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

    return true;
}  // end ZebraRouteMgr::GetAllRoutes()
          
void
ZebraRouteMgr::PrintBuffer(char* buffer,int len,int dlevel)
{
    /*
     * @brief Will print out the buffer in hex format
     *
     * @param buffer the buffer to be printed
     * @param len the length of the buffer contents to be printed in bytes
     *
     */
    for(int i=0;i<(int)len;i++)
    {
        printf("%02X ",(unsigned char)buffer[i]);
        if(i % 4 == 3)
            printf("\n");
    }
}
bool ZebraRouteMgr::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 = pid;
    
    // 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[1024];
        int msgLen = recv(descriptor, buffer, 1024, 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 == (UINT32)pid) && (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()