-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathip.h
762 lines (644 loc) · 18.4 KB
/
ip.h
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
/*
Copyright (c) 2014, Charles Smith
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this
list of conditions and the following disclaimer in the documentation
and/or
other materials provided with the distribution.
* Neither the name of Vallona Networks nor the names of its contributors
may be used to endorse or promote products derived from this software
without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _IP_H
#define _IP_H
#include <cstring>
#include <inttypes.h>
#include <iostream>
#include <list>
#include <string>
/* Socket handling includes */
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <linux/if_ether.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <linux/if_ether.h>
#include <netax25/ax25.h>
#include <netax25/axconfig.h>
#include <netax25/axlib.h>
#include "sarflags.h"
#include "screen.h"
#include "timer.h"
#include "timestamp.h"
using namespace std;
using namespace timer_group;
using namespace saratoga;
// timer c;
// Return greatest of 2 fd numbers
template <typename T>
inline T
FDMAX(const T& x, const T& y)
{
return (x > y) ? x : y;
}
namespace sarnet {
// We don't do jumbo frames
#ifndef JUMBO
#undef JUMBO
#endif // JUMBO
/*
**********************************************************************
* IP
**********************************************************************
*/
// Multicast direction
enum mcast_dir
{
MCAST_IN = 0x00,
MCAST_OUT = 0x01
};
union in_storage
{
struct in_addr v4;
struct in6_addr v6;
};
// typedef union inv4or6 IPVER;
class ip
{
private:
const int OTHER = -1;
union in_storage _ip; // The IP in_addr or in6_addr data
string _ax25addr;
int _family; // What are V4 or V6 or OTHER
public:
// Blank placeholder
ip()
{
bzero(&_ip, sizeof(union in_storage));
_family = OTHER;
};
// An IP Address filled in
ip(string addr);
// A blank placeholder for a certain protocol
ip(int protocol)
{
_family = protocol;
if (_family == AF_INET)
bzero(&_ip.v4, sizeof(struct in_addr));
else if (_family == AF_INET6)
bzero(&_ip.v6, sizeof(struct in6_addr));
else if (_family == AF_AX25)
bzero(&_ip, sizeof(union in_storage));
else
bzero(&_ip, sizeof(union in_storage));
return;
};
// Given a pointer to another ip create an identical one
ip(ip* p)
{
_family = p->family();
bzero(&_ip, sizeof(union in_storage));
if (_family == AF_INET)
bcopy(&p->_ip.v4, &_ip.v4, sizeof(struct in_addr));
else if (_family == AF_INET6)
bcopy(&p->_ip.v6, &_ip.v6, sizeof(struct in6_addr));
else if (_family == AF_AX25)
_ax25addr = p->_ax25addr;
}
~ip()
{
bzero(&_ip, sizeof(union in_storage));
_family = OTHER;
};
void zap()
{
bzero(&_ip, sizeof(union in_storage));
_family = OTHER;
}
ip(struct in_addr* a)
{
_family = AF_INET;
bzero(&_ip, sizeof(union in_storage));
bcopy(a, &_ip.v4, sizeof(struct in_addr));
}
ip(struct in6_addr* a)
{
_family = AF_INET6;
bzero(&_ip, sizeof(union in_storage));
bcopy(a, &_ip.v6, sizeof(struct in6_addr));
}
// Given a soxket get me the ip address info
ip(struct sockaddr_storage* s)
{
struct sockaddr_in* pv4;
struct sockaddr_in6* pv6;
switch (s->ss_family) {
case AF_INET:
_family = AF_INET;
pv4 = (struct sockaddr_in*)s;
bzero(&_ip, sizeof(union in_storage));
bcopy(&(pv4->sin_addr), &_ip.v4, sizeof(struct in_addr));
return;
case AF_INET6:
_family = AF_INET6;
pv6 = (struct sockaddr_in6*)s;
bzero(&_ip, sizeof(union in_storage));
bcopy(&(pv6->sin6_addr), &_ip.v6, sizeof(struct in6_addr));
return;
default:
_family = OTHER;
bzero(&_ip, sizeof(union in_storage));
return;
}
}
// Assignment via x = y;
ip& operator=(const ip& addr)
{
_family = addr._family;
bzero(&_ip, sizeof(union in_storage));
bcopy(&addr._ip, &_ip, sizeof(union in_storage));
_ax25addr = addr._ax25addr;
return (*this);
};
// True is ip x == y
bool operator==(const ip& rhs)
{
if (_family != rhs._family)
return (false);
if (_family == AF_AX25)
return _ax25addr == rhs._ax25addr;
if (_family == AF_INET) {
if (memcmp(&_ip, &rhs._ip.v4, sizeof(struct in_addr)) != 0)
return (false);
return (true);
}
if (_family == AF_INET6) {
if (memcmp(&_ip, &rhs._ip.v6, sizeof(struct in6_addr)) != 0)
return (false);
return (true);
}
if (_family == OTHER)
return (false);
return (false);
};
bool operator==(const string s)
{
ip tmp(s);
if (tmp._family != AF_INET && tmp._family != AF_INET6)
return (false);
if (_family != tmp._family)
return (false);
if (_family == AF_AX25)
return _ax25addr == tmp._ax25addr;
if (_family == AF_INET) {
if (memcmp(&_ip, &tmp._ip.v4, sizeof(struct in_addr)) != 0)
return (false);
return (true);
}
if (_family == AF_INET6) {
if (memcmp(&_ip, &tmp._ip.v6, sizeof(struct in6_addr)) != 0)
return (false);
return (true);
}
return (false);
}
// True is ip x != y
bool operator!=(const ip& rhs)
{
if (_family != rhs._family)
return (true);
if (_family == AF_AX25)
return _ax25addr != rhs._ax25addr;
if (_family == AF_INET) {
if (memcmp(&_ip, &rhs._ip.v4, sizeof(struct in_addr)) != 0)
return (true);
return (false);
}
if (_family == AF_INET6) {
if (memcmp(&_ip, &rhs._ip.v6, sizeof(struct in6_addr)) != 0)
return (true);
return (false);
}
if (_family == OTHER)
return (true);
return (true);
}
bool operator!=(const string s)
{
ip tmp(s);
if (tmp._family != AF_INET && tmp._family != AF_INET6)
return (true);
if (_family != tmp._family)
return (true);
if (_family == AF_AX25)
return _ax25addr != tmp._ax25addr;
if (_family == AF_INET) {
if (memcmp(&_ip, &tmp._ip.v4, sizeof(struct in_addr)) != 0)
return (true);
return (false);
}
if (_family == AF_INET6) {
if (memcmp(&_ip, &tmp._ip.v6, sizeof(struct in6_addr)) != 0)
return (true);
return (false);
}
return (true);
}
// What are we v4 or v6
int family() { return (_family); };
bool isother() { return (_family == OTHER); };
bool isax25() { return (_family == AF_AX25); };
bool isv4() { return (_family == AF_INET); };
bool isv6() { return (_family == AF_INET6); };
struct in_addr* addr4() { return &_ip.v4; };
struct in6_addr* addr6() { return &_ip.v6; };
string addrax25() { return _ax25addr; };
virtual string straddr();
virtual string print();
};
/*
*********************************************************************************
* INTERFACES lo0, eth0 ...
*********************************************************************************
*/
class netiface
{
private:
ip* _ifip; // IP Address
int _ifindex; // What index # it is used for V6 multicast
string _ifname; // What interface name it is on eg. eth0
int _mtu; // Ethernet MTU of the interface
char _mac[6]; // What is the MAC address
public:
netiface(string ipaddr, string ifname)
{
extern char* if_nametomac(char*, string);
extern int if_mtu(string);
char s[IF_NAMESIZE];
_ifip = new ip(ipaddr);
strcpy(s, ifname.c_str());
_ifindex = if_nametoindex(s);
_ifname = ifname;
if_nametomac(_mac, ifname);
_mtu = if_mtu(ifname);
};
~netiface(){};
string ifname() { return _ifname; };
int ifindex() { return _ifindex; };
ip* ifip() { return (_ifip); };
string mac();
int mtu() { return _mtu; };
bool ifmatch(string s) { return (s == _ifname); };
bool ifmatch(ip* myip) { return (*myip == *_ifip); };
bool familymatch(int fam) { return (fam == _ifip->family()); };
string print();
};
// System list of network interfaces
class netifaces
{
private:
std::list<netiface*> _iface; // List of local IP Addresses
// associated to interfaces
public:
netifaces();
~netifaces() { this->zap(); };
std::list<netiface*>::iterator begin() { return (_iface.begin()); };
std::list<netiface*>::iterator end() { return (_iface.end()); };
void zap()
{
while (!_iface.empty())
_iface.pop_front();
};
// Is the address given in our local iface list
bool islocal(string s)
{
ip tmp(s);
for (std::list<netiface*>::iterator i = _iface.begin(); i != _iface.end();
i++) {
if ((*i)->ifmatch(&tmp))
return (true);
}
return (false);
}
// Return first entry in list for AF_INET or AF_INET6
// ignoring loopbacks, but as a last resort send the loopback
netiface* first(int fam)
{
for (std::list<netiface*>::iterator i = _iface.begin(); i != _iface.end();
i++) {
if (!(*i)->familymatch(fam))
continue;
ip tmp((*i)->ifip());
if (tmp != "127.0.0.1" && tmp != "::1")
return ((*i));
}
// No active interfaces so lets send back the loopback
for (std::list<netiface*>::iterator i = _iface.begin(); i != _iface.end();
i++) {
if (!(*i)->familymatch(fam))
continue;
ip tmp((*i)->ifip());
if (tmp == "127.0.0.1" || tmp == "::1")
return ((*i));
}
return (nullptr);
}
string print()
{
string s;
for (std::list<netiface*>::iterator i = _iface.begin(); i != _iface.end();
i++) {
s += (*i)->print();
s += "\n";
}
return (s);
};
netiface* ipmatch(string ipaddr)
{
for (std::list<netiface*>::iterator i = _iface.begin(); i != _iface.end();
i++) {
ip tmp((*i)->ifip());
if (tmp.straddr() == ipaddr)
return ((*i));
}
return (nullptr);
}
};
/*
**********************************************************************
* UDP
**********************************************************************
*/
class udp
{
private:
// AX25
static struct full_sockaddr_ax25 ax25src;
static char* ax25portcall;
static int ax25slen;
static char* dev;
static constexpr char* ax25port = "spacelink";
static constexpr char* ax25multidestcall = "ALL";
struct full_sockaddr_ax25 ax25dest;
char* ax25destcall;
string ax25addr;
int ax25dlen;
void constructax25(char* destcall);
void constructax25(string destcall)
{
char* cstr = new char[destcall.length() + 1];
strcpy(cstr, destcall.c_str());
this->constructax25(cstr);
}
int ax25send();
ssize_t ax25rx(char* b, sarnet::ip* from);
bool _isax25 = false;
// END
struct sockaddr_storage
_sa; // sockaddr info and it is big enough to hold v4 & v6 info
int _fd; // file descriptor
std::list<saratoga::buffer> _buf; // Frames queued to send
bool _readytotx; // Sets FD_SET() or FD_CLR() for tx
timer_group::timer*
_delay; // Used to implement a delay between sending frames
static const ssize_t _jumbosize = 8192; // Jumbo ethernet frame size
static const ssize_t _ethsize = 1500; // Normal ethernet frame size
static const ssize_t _udpheader = 8; // Size of the udp header
static const ssize_t _v4header = 20; // Max Size of an ipv4 header
static const ssize_t _v6header = 40; // Size of an ipv6 header
static const ssize_t _ax25size = 255;
ssize_t _maxbuff = _ethsize;
// We will ALWAYS send or recv a minumum of 4 bytes as this is the size of
// the saratoga flags header so set the watermarks to this
const int _rcvlowat = 4;
const int _sndlowat = 4;
ssize_t _maxframesize()
{
if (this->family() == AF_INET)
return (_maxbuff - _v4header - _udpheader);
else if (this->family() == AF_AX25)
return _maxbuff;
else
return (_maxbuff - _v6header - _udpheader);
};
public:
// AX25
static int ax25outsock;
static int ax25insock;
static bool ax25available;
static char* ax25srcaddress;
static int initax25();
udp(char* dest);
udp(bool imanidiot);
// END
udp()
{
_fd = -1;
_buf.empty();
bzero(&_sa, sizeof(struct sockaddr_storage));
_readytotx = false;
_delay = new timer_group::timer();
};
// Create a socket to a particular address and port
udp(string addr, int port);
udp(sarnet::ip* addr, int port);
// Create & listen (bind) to inbound sockets on a port
udp(int proto, int port);
udp(struct sockaddr_storage* p);
// Use for multicast to bind to existing input or output socket
udp(enum mcast_dir direction, udp* p, string addr, int port);
~udp() { this->zap(); };
void zap()
{
// Clear the buffers
_buf.clear();
if (_fd > 2) {
shutdown(_fd, SHUT_RDWR);
close(_fd);
_fd = -1;
bzero(&_sa, sizeof(struct sockaddr_storage));
_readytotx = false;
;
}
}
// Copy constructor
udp(udp* b)
{
_fd = b->fd();
_readytotx = b->_readytotx;
_buf = b->_buf;
_isax25 = b->_isax25;
ax25destcall = b->ax25destcall;
ax25addr = b->ax25addr;
ax25dlen = b->ax25dlen;
ax25dest = b->ax25dest;
bzero(&_sa, sizeof(struct sockaddr_storage));
bcopy(&_sa, b->addr(), sizeof(struct sockaddr_storage));
}
// Copy a socket assignment
udp& operator=(udp& s1)
{
_fd = s1.fd();
_readytotx = s1.ready();
_isax25 = s1._isax25;
ax25destcall = s1.ax25destcall;
ax25addr = s1.ax25addr;
ax25dlen = s1.ax25dlen;
ax25dest = s1.ax25dest;
bzero(&_sa, sizeof(struct sockaddr_storage));
bcopy(&_sa, s1.addr(), sizeof(struct sockaddr_storage));
return (*this);
};
bool operator==(const udp& s)
{
if (!_isax25) {
return ((_fd == s._fd) && (_readytotx == s._readytotx) &&
// (_buf == s._buf) &&
(memcmp(&_sa, &s._sa, sizeof(struct sockaddr_storage)) == 0));
} else
return ax25addr == s.ax25addr;
};
ssize_t framesize() { return _maxframesize(); };
// What are we v4 or v6
int family()
{
if (_isax25)
return AF_AX25;
struct sockaddr* s = (struct sockaddr*)&_sa;
return (s->sa_family);
};
// Handle integer setsockopt
bool setopt(int option, void* optval, socklen_t optsize);
// Is the option set OK ?
bool getopt(int option, void* optval, socklen_t* optlen);
bool set(string addr, int port);
// Add a buffer to the list to be sent
virtual ssize_t tx(char* buf, size_t buflen)
{
// Add the frame to the end of the list of buffers
// alloc the memory for it and then push it
saratoga::buffer* tmp = new saratoga::buffer(buf, buflen);
_buf.push_back(*tmp);
// make FD_SET ready() test to true
// that way we can call send() when ready
_readytotx = true;
// delete tmp;
return (buflen);
}
// Receive a buffer, return # chars sent -
// You catch the error if <0
virtual ssize_t rx(char*, sarnet::ip*);
// Socket #
int port();
// fd
virtual int fd() { return (_fd); };
// Actually transmit the frames in the buffers
virtual int send();
// Are we ready to tx (controls select()
bool ready() { return _readytotx; };
bool ready(bool x)
{
_readytotx = x;
return _readytotx;
};
// Printable IP Address
virtual string straddr();
// Printable Port Number
string strport();
struct in_addr* addr4()
{
struct sockaddr_in* p = (struct sockaddr_in*)&_sa;
return (&p->sin_addr);
}
struct in6_addr* addr6()
{
struct sockaddr_in6* p = (struct sockaddr_in6*)&_sa;
return (&p->sin6_addr);
}
struct sockaddr_storage* addr() { return &_sa; };
struct sockaddr* saptr() { return (struct sockaddr*)&_sa; };
virtual string print();
};
// List of currently opened udp peers
class peers
{
private:
const size_t _max = 100; // Maximum # of sockets
std::list<udp> _peers; // List of open peers
bool _fdchange; // We have added/removed a peer used for select()
public:
peers(){};
~peers() { this->zap(); };
sarnet::udp* add(sarnet::udp* p); // Previos opened socket put it in list
sarnet::udp* add(string ipaddr,
int port); // Create & open a peer and put it in list
sarnet::udp* add(sarnet::ip* addr,
int port); // Create & open a peer and put it in list
void remove(int fd); // Remove the peer from the list with matching fd
void remove(
string ipaddr,
int port); // Remove the peer from the list with matching ip & port
void zap()
{
_fdchange = true;
// Close down all of the peer sockets
for (std::list<udp>::iterator p = this->begin(); p != this->end(); p++)
p->zap();
// Clear the list
_peers.clear();
};
std::list<udp>::iterator begin() { return (_peers.begin()); };
std::list<udp>::iterator end() { return (_peers.end()); };
// What is the largest fd in the list (for select())
int largestfd();
// Is the fd in the current list
bool exists(int fd);
bool fdchange() { return (_fdchange); };
bool fdchange(bool state)
{
_fdchange = state;
return (_fdchange);
};
// Does the setsockoptpeer match an IP address
sarnet::udp* matchsetsockopt(ip* host);
sarnet::udp* match(ip* host);
sarnet::udp* match(string addr);
// Print all the open sockets info
string print();
};
}; // Namespace sarnet
#endif // _IP_H