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
/**
* @file protoRouteMgr.cpp
* 
* @brief Base class for providing a consistent interface to manage operating system (or other) routing engines 
*/
#include "protoRouteMgr.h"
#include "protoDebug.h"

ProtoRouteMgr::ProtoRouteMgr()
 : savedRoutesIPv4(NULL), savedRoutesIPv6(NULL)
{
}

ProtoRouteMgr::~ProtoRouteMgr()
{
    if(NULL != savedRoutesIPv4) delete savedRoutesIPv4;
    if(NULL != savedRoutesIPv6) delete savedRoutesIPv6;
}


bool ProtoRouteMgr::DeleteAllRoutes()
{
    return DeleteAllRoutes(ProtoAddress::IPv4) && DeleteAllRoutes(ProtoAddress::IPv6);
}

bool ProtoRouteMgr::DeleteAllRoutes(ProtoAddress::Type addrType, unsigned int maxIterations)
{
    ProtoRouteTable rt;
    // Make multiple passes to get rid of possible
    // multiple routes to destinations
    // (TBD) extend ProtoRouteTable to support multiple routes per destination
    while (maxIterations-- > 0)
    {
        if (!GetAllRoutes(addrType, rt))
        {
            PLOG(PL_ERROR, "ProtoRouteMgr::DeleteAllRoutes() error getting routes\n");
            return false;   
        }
        if (rt.IsEmpty()) break;
        if (!DeleteRoutes(rt))
        {
            PLOG(PL_ERROR, "ProtoRouteMgr::DeleteAllRoutes() error deleting routes\n");
            return false;
        }
        rt.Destroy();
    }

    if (0 == maxIterations)
    {
        PLOG(PL_ERROR, "ProtoRouteMgr::DeleteAllRoutes() couldn't seem to delete everything!\n");
        return false;
    }
    else
    {
        return true;
    }
}  // end ProtoRouteMgr::DeleteAllRoutes()

/**
 * Set direct (interface) routes and gateway routes.
 */
bool ProtoRouteMgr::SetRoutes(ProtoRouteTable& routeTable)
{
    bool result = true;
    ProtoRouteTable::Iterator iterator(routeTable);
    ProtoRouteTable::Entry* entry;
    // First, set direct (interface) routes 
    while ((entry = iterator.GetNextEntry()))
    {
        if (entry->IsDirectRoute())
        {
            if (!SetRoute(entry->GetDestination(),
                          entry->GetPrefixSize(),
                          entry->GetGateway(),
                          entry->GetInterfaceIndex(),
                          entry->GetMetric()))
            {
                PLOG(PL_ERROR, "ProtoRouteMgr::SetAllRoutes() failed to set direct route to: %s\n",
                        entry->GetDestination().GetHostString());
                result = false;   
            }
        }
    }
    // Second, set gateway routes 
    iterator.Reset();
    while ((entry = iterator.GetNextEntry()))
    {
        if (entry->IsGatewayRoute())
        {
            if (!SetRoute(entry->GetDestination(),
                          entry->GetPrefixSize(),
                          entry->GetGateway(),
                          entry->GetInterfaceIndex(),
                          entry->GetMetric()))
            {
                PLOG(PL_ERROR, "ProtoRouteMgr::SetAllRoutes() failed to set gateway route to: %s\n",
                        entry->GetDestination().GetHostString());
                result = false;   
            }
        }
    }
    
    return result;
}  // end ProtoRouteMgr::SetRoutes()
bool
ProtoRouteMgr::GetDiff(ProtoRouteTable& oldRouteTable, ProtoRouteTable& newRouteTable, ProtoRouteTable& settedRouteTable, ProtoRouteTable& deletedRouteTable)
{
    //this can be sped up by only going through a single list instead of both and adding routes directly instead of in sets. TBD
    ProtoRouteTable updateRoutesMetric; //this was added so we only get a diff on changed routes via the settedRoute table
    
    settedRouteTable.Init();
    deletedRouteTable.Init();
    ProtoRouteTable::Iterator it_old(oldRouteTable);
    ProtoRouteTable::Iterator it_new(newRouteTable);
    ProtoRouteTable::Entry* entry;
    
    ProtoAddress gwAddrLookup;
    unsigned int ifaceIndexLookup;
    int metricLookup;
    
    while(NULL != (entry = it_old.GetNextEntry()))
    {
        ProtoAddress dstAddr    = entry->GetDestination();
        ProtoAddress gwAddr     = entry->GetGateway();
        unsigned int prefixLen  = entry->GetPrefixSize();
        unsigned int ifaceIndex = entry->GetInterfaceIndex();
        int          metric     = entry->GetMetric();
        if(!newRouteTable.FindRoute(dstAddr,prefixLen,gwAddrLookup,ifaceIndexLookup,metricLookup))
        {
            if(!deletedRouteTable.SetRoute(dstAddr,prefixLen,gwAddr,ifaceIndex,metric)) 
            {
                PLOG(PL_ERROR,"ProtoRouteMgr::UpdateRoutes() failed add an old route to the removeRoutes table\n");
                return false;
            }
        }
    }
    while(NULL != (entry = it_new.GetNextEntry()))
    {
        ProtoAddress dstAddr    = entry->GetDestination();
        ProtoAddress gwAddr     = entry->GetGateway();
        unsigned int prefixLen  = entry->GetPrefixSize();
        unsigned int ifaceIndex = entry->GetInterfaceIndex();
        int          metric     = entry->GetMetric();
        if(!oldRouteTable.FindRoute(dstAddr,prefixLen,gwAddrLookup,ifaceIndexLookup,metricLookup))
        {
            if(!settedRouteTable.SetRoute(dstAddr,prefixLen,gwAddr,ifaceIndex,metric))
            {
                PLOG(PL_ERROR,"ProtoRouteMgr::UpdateRoutes() failed add an old route to the removeRoutes table in new section\n");
                return false;
            }
        } else if((gwAddrLookup != gwAddr) || (ifaceIndexLookup != ifaceIndex)) {
            if(!settedRouteTable.SetRoute(dstAddr,prefixLen,gwAddr,ifaceIndex,metric))
            {
                PLOG(PL_ERROR,"ProtoRouteMgr::UpdateRoutes() failed add an old route to the removeRoutes table in change section\n");
                return false;
            }
        } else if(metricLookup != metric) {
            if(!updateRoutesMetric.SetRoute(dstAddr,prefixLen,gwAddr,ifaceIndex,metric))
            {
                PLOG(PL_ERROR,"ProtoRouteMgr::UpdateRoutes() failed add an old route to the removeRoutes table in change section\n");
                return false;
            }
        }
    }
    return true;
}
bool
ProtoRouteMgr::UpdateRoutes(ProtoRouteTable& oldRouteTable, ProtoRouteTable& newRouteTable, ProtoRouteTable* settedRouteTable, ProtoRouteTable* deletedRouteTable)
{
    //this can be sped up by only going through a single list instead of both and adding routes directly instead of in sets. TBD
    ProtoRouteTable removeRoutes;
    ProtoRouteTable updateRoutes;
    ProtoRouteTable updateRoutesMetric; //this was added so we only get a diff on changed routes via the settedRoute table
    ProtoRouteTable* removeRoutesPtr = &removeRoutes;
    ProtoRouteTable* updateRoutesPtr = &updateRoutes;
    if( NULL != settedRouteTable)
    {
        updateRoutesPtr = settedRouteTable;
    }
    if( NULL != deletedRouteTable)
    {
        removeRoutesPtr = deletedRouteTable;
    }
    removeRoutesPtr->Init();
    updateRoutesPtr->Init();
    ProtoRouteTable::Iterator it_old(oldRouteTable);
    ProtoRouteTable::Iterator it_new(newRouteTable);
    ProtoRouteTable::Entry* entry;
    
    ProtoAddress gwAddrLookup;
    unsigned int ifaceIndexLookup;
    int metricLookup;
    
    while(NULL != (entry = it_old.GetNextEntry()))
    {
        ProtoAddress dstAddr    = entry->GetDestination();
        ProtoAddress gwAddr     = entry->GetGateway();
        unsigned int prefixLen  = entry->GetPrefixSize();
        unsigned int ifaceIndex = entry->GetInterfaceIndex();
        int          metric     = entry->GetMetric();
        if(!newRouteTable.FindRoute(dstAddr,prefixLen,gwAddrLookup,ifaceIndexLookup,metricLookup))
        {
            if(!removeRoutesPtr->SetRoute(dstAddr,prefixLen,gwAddr,ifaceIndex,metric)) 
            {
                PLOG(PL_ERROR,"ProtoRouteMgr::UpdateRoutes() failed add an old route to the removeRoutes table\n");
                return false;
            }
        }
    }
    while(NULL != (entry = it_new.GetNextEntry()))
    {
        ProtoAddress dstAddr    = entry->GetDestination();
        ProtoAddress gwAddr     = entry->GetGateway();
        unsigned int prefixLen  = entry->GetPrefixSize();
        unsigned int ifaceIndex = entry->GetInterfaceIndex();
        int          metric     = entry->GetMetric();
        if(!oldRouteTable.FindRoute(dstAddr,prefixLen,gwAddrLookup,ifaceIndexLookup,metricLookup))
        {
            if(!updateRoutesPtr->SetRoute(dstAddr,prefixLen,gwAddr,ifaceIndex,metric))
            {
                PLOG(PL_ERROR,"ProtoRouteMgr::UpdateRoutes() failed add an old route to the removeRoutes table in new section\n");
                return false;
            }
        } else if((gwAddrLookup != gwAddr) || (ifaceIndexLookup != ifaceIndex)) {
            if(!updateRoutesPtr->SetRoute(dstAddr,prefixLen,gwAddr,ifaceIndex,metric))
            {
                PLOG(PL_ERROR,"ProtoRouteMgr::UpdateRoutes() failed add an old route to the removeRoutes table in change section\n");
                return false;
            }
        } else if(metricLookup != metric) {
            if(!updateRoutesMetric.SetRoute(dstAddr,prefixLen,gwAddr,ifaceIndex,metric))
            {
                PLOG(PL_ERROR,"ProtoRouteMgr::UpdateRoutes() failed add an old route to the removeRoutes table in change section\n");
                return false;
            }
        }
    }
    if(!DeleteRoutes(*removeRoutesPtr)) {
        PLOG(PL_ERROR,"ProtoRouteMgr::UpdateRoutes() failed delete old routes\n");
        return false;
    }
    if(!SetRoutes(*updateRoutesPtr)) {
        PLOG(PL_ERROR,"ProtoRouteMgr::UpdateRoutes() failed update routes\n");
        return false;
    }
    if(!SetRoutes(updateRoutesMetric)) {
        PLOG(PL_ERROR,"ProtoRouteMgr::UpdateRoutes() failed update routes metric only\n");
        return false;
    }
    return true;
}
/**
 * Deletes gateway and direct (interface) routes and the
 * default entry if one exists.
 */
bool ProtoRouteMgr::DeleteRoutes(ProtoRouteTable& routeTable)
{
    bool result = true;
    ProtoRouteTable::Iterator iterator(routeTable);
    const ProtoRouteTable::Entry* entry;
    // First, delete gateway routes 
    while ((entry = iterator.GetNextEntry()))
    {
        if (entry->IsGatewayRoute())
        {
            if (!DeleteRoute(entry->GetDestination(),
			                 entry->GetPrefixSize(),
                             entry->GetGateway(),
                             entry->GetInterfaceIndex()))
            {
	            PLOG(PL_ERROR, "ProtoRouteMgr::DeleteAllRoutes() failed to delete gateway route to: %s\n",
	                    entry->GetDestination().GetHostString());
	            result = false;   
            }
        }
    }
    // Second, delete direct (interface) routes
    iterator.Reset(); 
    while ((entry = iterator.GetNextEntry()))
    {
        if (entry->IsDirectRoute())
        {
            if (!DeleteRoute(entry->GetDestination(),
			                 entry->GetPrefixSize(),
                             entry->GetGateway(),
                             entry->GetInterfaceIndex()))
            {
	            PLOG(PL_ERROR, "ProtoRouteMgr::DeleteAllRoutes() failed to delete direct route to: %s\n",
	                    entry->GetDestination().GetHostString());
	            result = false;   
            }
        }
    }
    // If there's a default entry delete it, too
    entry = routeTable.GetDefaultEntry();
    if (entry)
    {
        if (!DeleteRoute(entry->GetDestination(),
			             entry->GetPrefixSize(),
                         entry->GetGateway(),
                         entry->GetInterfaceIndex()))
        {
	        PLOG(PL_ERROR, "ProtoRouteMgr::DeleteAllRoutes() failed to delete default route\n");
	        result = false;   
        }
    }
    return result;
}  // end ProtoRouteMgr::DeleteRoutes()

bool ProtoRouteMgr::SaveAllRoutes()
{
    return SaveAllRoutes(ProtoAddress::IPv4) || SaveAllRoutes(ProtoAddress::IPv6);
}  // end ProtoRouteMgr::SaveAllRoutes()

bool ProtoRouteMgr::SaveAllRoutes(ProtoAddress::Type addrType) 
{
    switch (addrType) 
    {
        case ProtoAddress::IPv4:
            if (NULL == savedRoutesIPv4) 
            {
                savedRoutesIPv4 = new ProtoRouteTable();
                if (NULL == savedRoutesIPv4) 
                {
                    PLOG(PL_ERROR, "ProtoRouteMgr::SaveAllRoutes() failed allocating a ProtoRouteTable\n");
                    return false;
                }
            }
            savedRoutesIPv4->Init();
            if (!GetAllRoutes(ProtoAddress::IPv4, *savedRoutesIPv4)) 
            {
                PLOG(PL_ERROR, "ProtoRouteMgr::SaveAllRoutes() failed getting all of the IPv4 routes");
                return false;
            }
            break;
        case ProtoAddress::IPv6:
            if (NULL == savedRoutesIPv6) 
            {
                savedRoutesIPv6 = new ProtoRouteTable();
                if (NULL == savedRoutesIPv6) 
                {
                    PLOG(PL_ERROR, "ProtoRouteMgr::SaveAllRoutes() failed allocating a ProtoRouteTable\n");
                    return false;
                }
            }
            savedRoutesIPv6->Init();
            if (!GetAllRoutes(ProtoAddress::IPv4, *savedRoutesIPv6)) 
            {
                PLOG(PL_ERROR, "ProtoRouteMgr::SaveAllRoutes() failed getting all of the IPv6 routes");
                return false;
            }
            break;
        default:
            PLOG(PL_ERROR, "ProtoRouteMgr::SaveAllRoutes() only supports saving route tables of types IPv6 and IPv4\n");
            return false;
    }
    return true;
}  // ProtoRouteMgr::SaveAllRoutes()

bool ProtoRouteMgr::RestoreSavedRoutes()
{
    bool returnvalue = false;
    if(NULL != savedRoutesIPv6)
    {
        returnvalue = RestoreSavedRoutes(ProtoAddress::IPv6);
    }
    if(NULL != savedRoutesIPv4)
    {
        returnvalue = returnvalue || RestoreSavedRoutes(ProtoAddress::IPv4);
    }
    if(!returnvalue)
    {
        PLOG(PL_ERROR,"ProtoRouteMgr::RestoreSavedRoutes() couldn't restore routes, did you save any first?");
    }
    return returnvalue;
}  // end ProtoRouteMgr::RestoreSavedRoutes()

bool ProtoRouteMgr::RestoreSavedRoutes(ProtoAddress::Type addrType)
{
    switch(addrType)
    {
        case ProtoAddress::IPv4:
            if(NULL == savedRoutesIPv4)
            {
                PLOG(PL_ERROR,"ProtoRouteMgr::RestoreSavedRoutes() can not restore IPv4 routes as none have been saved.\n");
                return false;
            }
            return SetRoutes(*savedRoutesIPv4);
            break;
        case ProtoAddress::IPv6:
            if(NULL == savedRoutesIPv6)
            {
                PLOG(PL_ERROR,"ProtoRouteMgr::RestoreSavedRoutes() can not restore IPv6 routes as none have been saved.\n");
                return false;
            }
            return SetRoutes(*savedRoutesIPv6);
            break;
        default:
            PLOG(PL_ERROR, "ProtoRouteMgr::RestoreSavedRoutes() only supports restoring route tables of types IPv6 and IPv4\n");
            return false;
    }
    return true;
}  // end ProtoRouteMgr::RestoreSavedRoutes()

void ProtoRouteMgr::ClearSavedRoutes()
{
    // the ProtoRouteMgr base class constructor now inits
    // these pointers to NULL as it should, so the Init()
    // method here is probably unecessary, but now can be
    // used to "clear" any saved route info
    if (NULL != savedRoutesIPv4)
    {
        delete savedRoutesIPv4;
        savedRoutesIPv4 = NULL;
    }
    if (NULL != savedRoutesIPv6)
    {
        delete savedRoutesIPv6;
        savedRoutesIPv6 = NULL;
    }
}  // end ProtoRouteMgr::ClearSavedRoutes()