-
Notifications
You must be signed in to change notification settings - Fork 1
/
ip.h
969 lines (934 loc) · 20.2 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
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
#ifndef IP_H
#define IP_H
#define VM_IPV6 true
#if VM_IPV6
extern bool useIPv6;
#define VM_IPV6_B useIPv6
#else
#define VM_IPV6_B false
#endif
#define VM_IPV6_TYPE_MYSQL_COLUMN (VM_IPV6_B ? "varbinary(16)" : "int unsigned")
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <arpa/inet.h>
#include <string>
#include <math.h>
#include <iostream>
#include <iomanip>
#include <string.h>
#include <stdlib.h>
#include "endian.h"
#ifdef FREEBSD
#define __in6_u __u6_addr
#endif
#define IP_STR_MAX_LENGTH 50
struct vmIP {
inline vmIP(u_int32_t ip = 0) {
this->ip.v4.n = ip;
#if VM_IPV6
v6 = false;
#endif
}
#if VM_IPV6
inline vmIP(in6_addr ip) {
this->ip.v6 = ip;
v6 = true;
}
#endif
void setIP(u_char *data_ip, unsigned data_ip_length, bool varbinary) {
clear();
#if VM_IPV6
if(varbinary) {
switch(data_ip_length) {
case 4:
ip.v4.n = ntohl(*(u_int32_t*)data_ip);
v6 = false;
break;
case 8:
case 9:
case 10:
{
unsigned int ipl = 0;
for(unsigned i = 0; i < data_ip_length; i++) {
ipl = ipl * 10 + (data_ip[i] - 0x30);
}
ip.v4.n = ipl;
v6 = false;
}
break;
case 16:
memcpy((u_char*)&ip.v6 + (sizeof(ip.v6) - data_ip_length), data_ip, data_ip_length);
for(unsigned i = 0; i < 4; i++) {
ip.v6.__in6_u.__u6_addr32[i] = ntohl(ip.v6.__in6_u.__u6_addr32[i]);
}
v6 = true;
break;
}
} else {
#endif
ip.v4.n = atol((char*)data_ip);
#if VM_IPV6
}
#endif
}
void setIP(void *db_row, const char *field);
inline void setIPv4(u_int32_t ip, bool ntoh = false) {
this->ip.v4.n = ip;
if(ntoh) {
this->ip.v4.n = ntohl(this->ip.v4.n);
}
#if VM_IPV6
v6 = false;
#endif
}
#if VM_IPV6
inline void setIPv6(in6_addr ip, bool ntoh = false) {
this->ip.v6 = ip;
if(ntoh) {
for(unsigned i = 0; i < 4; i++) {
this->ip.v6.__in6_u.__u6_addr32[i] = ntohl(this->ip.v6.__in6_u.__u6_addr32[i]);
}
}
v6 = true;
}
#endif
inline u_int32_t getIPv4(bool hton = false) const {
return(hton ?
htonl(this->ip.v4.n) :
this->ip.v4.n);
}
#if VM_IPV6
inline in6_addr getIPv6(bool hton = false) const {
if(hton) {
in6_addr _ip = this->ip.v6;
for(unsigned i = 0; i < 4; i++) {
_ip.__in6_u.__u6_addr32[i] = htonl(_ip.__in6_u.__u6_addr32[i]);
}
return(_ip);
} else {
return(this->ip.v6);
}
}
#endif
bool setFromString(const char *ip);
std::string getString() const;
std::string getStringForMysqlIpColumn(const char *table, const char *column) const;
std::string _getStringForMysqlIpColumn(int IPv) const;
inline bool isLocalhost() {
#if VM_IPV6
if(!v6) {
#endif
return((ip.v4.n >> 8) == 0x7F0000);
#if VM_IPV6
} else {
return(ip.v6.__in6_u.__u6_addr32[0] == 0 &&
ip.v6.__in6_u.__u6_addr32[1] == 0 &&
ip.v6.__in6_u.__u6_addr32[2] == 0 &&
ip.v6.__in6_u.__u6_addr32[3] == 1);
}
#endif
}
bool isLocalIP();
inline bool operator == (const vmIP& other) const {
#if VM_IPV6
return(this->v6 == other.v6 &&
(this->v6 ?
!memcmp(&this->ip.v6, &other.ip.v6, sizeof(this->ip.v6)) :
this->ip.v4.n == other.ip.v4.n));
#else
return(this->ip.v4.n == other.ip.v4.n);
#endif
}
inline bool operator != (const vmIP& other) const {
return(!(*this == other));
}
inline bool operator < (const vmIP& other) const {
#if VM_IPV6
return(this->v6 != other.v6 ?
this->v6 < other.v6 :
(this->v6 ?
memcmp(&this->ip.v6, &other.ip.v6, sizeof(this->ip.v6)) < 0 :
this->ip.v4.n < other.ip.v4.n));
#else
return(this->ip.v4.n < other.ip.v4.n);
#endif
}
inline bool operator <= (const vmIP& other) const {
return(*this < other ||
*this == other);
}
inline bool operator > (const vmIP& other) const {
return(!(*this <= other));
}
inline bool operator >= (const vmIP& other) const {
return(*this > other ||
*this == other);
}
inline bool isSet() const {
#if VM_IPV6
if(!v6) {
#endif
return(ip.v4.n != 0);
#if VM_IPV6
} else {
return(ip.v6.__in6_u.__u6_addr32[0] != 0 ||
ip.v6.__in6_u.__u6_addr32[1] != 0 ||
ip.v6.__in6_u.__u6_addr32[2] != 0 ||
ip.v6.__in6_u.__u6_addr32[3] != 0);
}
#endif
}
inline void clear(u_int32_t set = 0) {
#if VM_IPV6
v6 = false;
ip.v6.__in6_u.__u6_addr32[0] = set;
ip.v6.__in6_u.__u6_addr32[1] = set;
ip.v6.__in6_u.__u6_addr32[2] = set;
ip.v6.__in6_u.__u6_addr32[3] = set;
#else
ip.v4.n = set;
#endif
}
inline vmIP _and(vmIP mask) {
vmIP ip = *this;
#if VM_IPV6
if(!v6) {
#endif
ip.ip.v4.n &= mask.ip.v4.n;
#if VM_IPV6
} else {
for(unsigned i = 0; i < 4; i++) {
ip.ip.v6.__in6_u.__u6_addr32[i] &= mask.ip.v6.__in6_u.__u6_addr32[i];
}
}
#endif
return(ip);
}
inline vmIP _or(vmIP mask) {
vmIP ip = *this;
#if VM_IPV6
if(!v6) {
#endif
ip.ip.v4.n |= mask.ip.v4.n;
#if VM_IPV6
} else {
for(unsigned i = 0; i < 4; i++) {
ip.ip.v6.__in6_u.__u6_addr32[i] |= mask.ip.v6.__in6_u.__u6_addr32[i];
}
}
#endif
return(ip);
}
inline vmIP mask(vmIP mask) {
return(_and(mask));
}
inline vmIP network_mask(unsigned mask) {
vmIP ip;
#if VM_IPV6
if(!v6) {
ip.v6 = false;
#endif
if(!mask) {
mask = 32;
}
ip.ip.v4.n = ((u_int32_t)-1 << (32 - mask)) & (u_int32_t)-1;
#if VM_IPV6
} else {
if(!mask) {
mask = 128;
}
ip.v6 = true;
for(unsigned i = 0; i < 4; i++) {
int _mask = mask - i * 32;
if(_mask >= 32) {
ip.ip.v6.__in6_u.__u6_addr32[i] = (u_int32_t)-1;
} else if(_mask <= 0) {
ip.ip.v6.__in6_u.__u6_addr32[i] = 0;
} else {
ip.ip.v6.__in6_u.__u6_addr32[i] = ((u_int32_t)-1 << (32 - _mask)) & (u_int32_t)-1;
}
}
}
#endif
return(ip);
}
inline vmIP wildcard_mask(unsigned mask) {
vmIP ip;
#if VM_IPV6
if(!v6) {
ip.v6 = false;
#endif
if(!mask) {
mask = 32;
}
ip.ip.v4.n = (u_int32_t)(pow(2, 32 - mask) - 1);
#if VM_IPV6
} else {
if(!mask) {
mask = 128;
}
ip.v6 = true;
for(unsigned i = 0; i < 4; i++) {
int _mask = mask - i * 32;
if(_mask >= 32) {
ip.ip.v6.__in6_u.__u6_addr32[i] = 0;
} else if(_mask <= 0) {
ip.ip.v6.__in6_u.__u6_addr32[i] = (u_int32_t)-1;
} else {
ip.ip.v6.__in6_u.__u6_addr32[i] = (u_int32_t)(pow(2, 32 -_mask) - 1);
}
}
}
#endif
return(ip);
}
inline vmIP network(unsigned mask) {
return(this->_and(this->network_mask(mask)));
}
inline vmIP broadcast(unsigned mask) {
return(this->_or(this->wildcard_mask(mask)));
}
inline u_int32_t getHashNumber() {
#if VM_IPV6
if(!v6) {
#endif
return(ip.v4.n);
#if VM_IPV6
} else {
return(ip.v6.__in6_u.__u6_addr32[3]);
}
#endif
}
inline void *getPointerToIP() {
#if VM_IPV6
if(!v6) {
#endif
return(&ip.v4.n);
#if VM_IPV6
} else {
return(&ip.v6);
}
#endif
}
inline bool is_v6() {
#if VM_IPV6
return(v6);
#else
return(false);
#endif
}
inline u_int8_t bits() {
#if VM_IPV6
return(v6 ? 128 : 32);
#else
return(32);
#endif
}
inline bool is_net_mask(int bits) {
return(bits > 0 && bits < this->bits());
}
#if VM_IPV6
u_int8_t v6;
#endif
union {
struct {
#if VM_IPV6
u_int32_t filler[3];
#endif
u_int32_t n;
} v4;
#if VM_IPV6
in6_addr v6;
#endif
} ip;
};
#define IP6_EXT_HEADERS_MAX 10
#if VM_IPV6
struct ip6hdr2 {
inline vmIP get_saddr() {
in6_addr __saddr = _saddr;
for(unsigned i = 0; i < 4; i++) {
__saddr.__in6_u.__u6_addr32[i] = ntohl(__saddr.__in6_u.__u6_addr32[i]);
}
return(__saddr);
}
inline vmIP get_daddr() {
in6_addr __daddr = _daddr;
for(unsigned i = 0; i < 4; i++) {
__daddr.__in6_u.__u6_addr32[i] = ntohl(__daddr.__in6_u.__u6_addr32[i]);
}
return(__daddr);
}
inline void set_saddr(vmIP &ip) {
_saddr = ip.getIPv6();
for(unsigned i = 0; i < 4; i++) {
_saddr.__in6_u.__u6_addr32[i] = htonl(_saddr.__in6_u.__u6_addr32[i]);
}
}
inline void set_daddr(vmIP &ip) {
_daddr = ip.getIPv6();
for(unsigned i = 0; i < 4; i++) {
_daddr.__in6_u.__u6_addr32[i] = htonl(_daddr.__in6_u.__u6_addr32[i]);
}
}
static inline bool is_ext_header(u_int8_t header_id) {
return(header_id == IPPROTO_HOPOPTS ||
header_id == IPPROTO_ROUTING ||
header_id == IPPROTO_FRAGMENT ||
header_id == IPPROTO_ICMPV6 ||
header_id == IPPROTO_NONE ||
header_id == IPPROTO_DSTOPTS
#ifdef IPPROTO_MH
|| header_id == IPPROTO_MH
#endif
);
}
static inline u_int16_t get_ext_header_size(u_char *header, u_int8_t header_id) {
return(header_id == IPPROTO_HOPOPTS ? ((ip6_hbh*)header)->ip6h_len :
header_id == IPPROTO_ROUTING ? ((ip6_rthdr*)header)->ip6r_len :
header_id == IPPROTO_FRAGMENT ? sizeof(ip6_frag) :
header_id == IPPROTO_ICMPV6 ? ((ip6_ext*)header)->ip6e_len :
header_id == IPPROTO_NONE ? ((ip6_ext*)header)->ip6e_len :
header_id == IPPROTO_DSTOPTS ? ((ip6_dest*)header)->ip6d_len :
#ifdef IPPROTO_MH
header_id == IPPROTO_MH ? ((ip6_ext*)header)->ip6e_len :
#endif
0);
}
static inline u_int16_t get_ext_header_nxt(u_char *header, u_int8_t header_id) {
return(header_id == IPPROTO_HOPOPTS ? ((ip6_hbh*)header)->ip6h_nxt :
header_id == IPPROTO_ROUTING ? ((ip6_rthdr*)header)->ip6r_nxt :
header_id == IPPROTO_FRAGMENT ? ((ip6_frag*)header)->ip6f_nxt :
header_id == IPPROTO_ICMPV6 ? ((ip6_ext*)header)->ip6e_nxt :
header_id == IPPROTO_NONE ? ((ip6_ext*)header)->ip6e_nxt :
header_id == IPPROTO_DSTOPTS ? ((ip6_dest*)header)->ip6d_nxt :
#ifdef IPPROTO_MH
header_id == IPPROTO_MH ? ((ip6_ext*)header)->ip6e_nxt :
#endif
0);
}
inline u_int32_t get_tot_len() {
return(sizeof(ip6hdr2) + ntohs(this->plen));
}
inline void set_tot_len(u_int32_t tot_len) {
this->plen = htons(tot_len - sizeof(ip6hdr2));
}
inline u_int8_t get_tos() {
return(0);
}
inline u_int32_t get_frag_id() {
ip6_frag *frag = (ip6_frag*)get_ext_header(IPPROTO_FRAGMENT);
return(frag ? ntohl(frag->ip6f_ident) : 0);
}
inline u_int16_t get_frag_data() {
ip6_frag *frag = (ip6_frag*)get_ext_header(IPPROTO_FRAGMENT);
return(frag ? ntohs(frag->ip6f_offlg) : 0);
}
inline bool is_more_frag(u_int16_t frag_data) {
return(frag_data & 1);
}
inline u_int16_t get_frag_offset(u_int16_t frag_data) {
return(frag_data >> 3 << 3);
}
inline void clear_frag_data() {
ip6_frag *frag = (ip6_frag*)get_ext_header(IPPROTO_FRAGMENT);
if(frag) {
frag->ip6f_offlg = 0;
}
}
inline u_int8_t get_protocol() {
return(get_ext_headers(NULL, 0, NULL));
}
inline u_int16_t get_hdr_size() {
return(get_total_headers_len());
}
inline bool is_ext_headers() {
return(is_ext_header(nxt));
}
static inline bool is_ext_headers(u_char *packet, u_int16_t iph_offset) {
return(((ip6hdr2*)(packet + iph_offset))->is_ext_headers());
}
u_int8_t get_ext_headers(u_int8_t *ext_headers_type, u_int8_t ext_headers_max, u_int8_t *ext_headers_count);
u_int16_t get_ext_headers_len();
inline u_int16_t get_total_headers_len() {
u_int16_t ext_headers_len = get_ext_headers_len();
if(ext_headers_len == (u_int16_t)-1) {
return(-1);
}
return(ext_headers_len + sizeof(ip6hdr2));
}
u_int16_t get_ext_header_offset(u_int8_t header_id);
inline u_char *get_ext_header(u_int8_t header_id) {
u_int16_t offset = get_ext_header_offset(header_id);
return(offset == (u_int16_t)-1 ? NULL : (u_char*)this + offset);
}
#if __BYTE_ORDER == __LITTLE_ENDIAN
unsigned tc1:4;
unsigned version:4;
unsigned flow_id_1:4;
unsigned tc2:4;
unsigned flow_id_2:16;
#elif __BYTE_ORDER == __BIG_ENDIAN
unsigned version:4;
unsigned tc1:4;
unsigned tc2:4;
unsigned flow_id_1:4;
unsigned flow_id_2:16;
#endif
u_int16_t plen;
u_int8_t nxt;
u_int8_t hlim;
in6_addr _saddr;
in6_addr _daddr;
};
#endif
struct iphdr2 {
inline vmIP get_saddr() {
#if VM_IPV6
if(version == 4) {
#endif
return(ntohl(_saddr));
#if VM_IPV6
} else {
return(((ip6hdr2*)this)->get_saddr());
}
#endif
}
inline vmIP get_daddr() {
#if VM_IPV6
if(version == 4) {
#endif
return(ntohl(_daddr));
#if VM_IPV6
} else {
return(((ip6hdr2*)this)->get_daddr());
}
#endif
}
inline void set_saddr(vmIP &ip) {
#if VM_IPV6
if(version == 4) {
#endif
_saddr = htonl(ip.getIPv4());
#if VM_IPV6
} else {
((ip6hdr2*)this)->set_saddr(ip);
}
#endif
}
inline void set_daddr(vmIP &ip) {
#if VM_IPV6
if(version == 4) {
#endif
_daddr = htonl(ip.getIPv4());
#if VM_IPV6
} else {
((ip6hdr2*)this)->set_daddr(ip);
}
#endif
}
inline void _set_saddr(u_int32_t ip) {
#if VM_IPV6
if(version == 4) {
#endif
_saddr = ip;
#if VM_IPV6
} else {
}
#endif
}
inline void _set_daddr(u_int32_t ip) {
#if VM_IPV6
if(version == 4) {
#endif
_daddr = ip;
#if VM_IPV6
} else {
}
#endif
}
inline u_int32_t get_tot_len() {
#if VM_IPV6
if(version == 4) {
#endif
return(ntohs(_tot_len));
#if VM_IPV6
} else {
return(((ip6hdr2*)this)->get_tot_len());
}
#endif
}
inline void set_tot_len(u_int32_t tot_len) {
#if VM_IPV6
if(version == 4) {
#endif
_tot_len = htons(tot_len);
#if VM_IPV6
} else {
((ip6hdr2*)this)->set_tot_len(tot_len);
}
#endif
}
inline u_int8_t get_tos() {
#if VM_IPV6
if(version == 4) {
#endif
return(_tos);
#if VM_IPV6
} else {
return(((ip6hdr2*)this)->get_tos());
}
#endif
}
inline u_int32_t get_frag_id() {
#if VM_IPV6
if(version == 4) {
#endif
return(ntohs(_id));
#if VM_IPV6
} else {
return(((ip6hdr2*)this)->get_frag_id());
}
#endif
}
inline u_int16_t get_frag_data() {
#if VM_IPV6
if(version == 4) {
#endif
return(ntohs(_frag_off));
#if VM_IPV6
} else {
return(((ip6hdr2*)this)->get_frag_data());
}
#endif
}
inline bool is_more_frag(u_int16_t frag_data) {
#if VM_IPV6
if(version == 4) {
#endif
return(frag_data & IP_MF);
#if VM_IPV6
} else {
return(((ip6hdr2*)this)->is_more_frag(frag_data));
}
#endif
}
inline u_int16_t get_frag_offset(u_int16_t frag_data) {
#if VM_IPV6
if(version == 4) {
#endif
return((frag_data & IP_OFFMASK) << 3);
#if VM_IPV6
} else {
return(((ip6hdr2*)this)->get_frag_offset(frag_data));
}
#endif
}
inline void clear_frag_data() {
#if VM_IPV6
if(version == 4) {
#endif
_frag_off = 0;
#if VM_IPV6
} else {
((ip6hdr2*)this)->clear_frag_data();
}
#endif
}
inline u_int8_t get_ttl() {
#if VM_IPV6
if(version == 4) {
#endif
return(_ttl);
#if VM_IPV6
} else {
return(0);
}
#endif
}
inline void set_ttl(u_int8_t ttl) {
#if VM_IPV6
if(version == 4) {
#endif
_ttl = ttl;
#if VM_IPV6
} else {
}
#endif
}
inline u_int8_t get_protocol() {
#if VM_IPV6
if(version == 4) {
#endif
return(_protocol);
#if VM_IPV6
} else {
return(((ip6hdr2*)this)->get_protocol());
}
#endif
}
inline u_int16_t get_hdr_size() {
#if VM_IPV6
if(version == 4) {
#endif
return(sizeof(iphdr2));
#if VM_IPV6
} else {
return(((ip6hdr2*)this)->get_hdr_size());
}
#endif
}
inline u_int16_t get_check() {
#if VM_IPV6
if(version == 4) {
#endif
return(ntohs(_check));
#if VM_IPV6
} else {
return(0);
}
#endif
}
inline void set_check(u_int16_t check) {
#if VM_IPV6
if(version == 4) {
#endif
_check = htons(check);
#if VM_IPV6
} else {
}
#endif
}
inline u_int8_t get_ihl() {
#if VM_IPV6
if(version == 4) {
#endif
return(_ihl);
#if VM_IPV6
} else {
return(0);
}
#endif
}
inline bool version_is_ok() {
return(version == 4
#if VM_IPV6
|| version == 6
#endif
);
}
#if __BYTE_ORDER == __LITTLE_ENDIAN
unsigned int _ihl:4;
unsigned int version:4;
#elif __BYTE_ORDER == __BIG_ENDIAN
unsigned int version:4;
unsigned int _ihl:4;
#else
# error "Please fix <bits/endian.h>"
#endif
u_int8_t _tos;
u_int16_t _tot_len;
u_int16_t _id;
u_int16_t _frag_off;
u_int8_t _ttl;
u_int8_t _protocol;
u_int16_t _check;
u_int32_t _saddr;
u_int32_t _daddr;
/*The options start here. */
#ifdef PACKED
} __attribute__((packed));
#else
};
#endif
struct vmPort {
inline vmPort(u_int16_t port = 0) {
this->port = port;
}
inline void setPort(u_int16_t port, bool ntoh = false) {
this->port = port;
if(ntoh) {
this->port = ntohs(this->port);
}
}
inline u_int16_t getPort(bool hton = false) const {
return(hton ?
htons(this->port) :
this->port);
}
inline operator int() const {
return(port);
}
vmPort &setFromString(const char *port, bool inv = false) {
this->port = atoi(port);
if(inv) {
this->port = htons(this->port);
}
return(*this);
}
std::string getString();
inline bool operator == (const vmPort& other) const {
return(this->port == other.port);
}
inline bool operator != (const vmPort& other) const {
return(this->port != other.port);
}
inline bool operator < (const vmPort& other) const {
return(this->port < other.port);
}
inline bool operator > (const vmPort& other) const {
return(this->port > other.port);
}
inline bool isSet() const {
return(port != 0);
}
inline void clear() {
port = 0;
}
inline vmPort inc(int inc = 1) {
vmPort port = *this;
port.port += inc;
return(port);
}
inline vmPort dec(int dec = 1) {
vmPort port = *this;
port.port -= dec;
return(port);
}
u_int16_t port;
};
inline std::ostream& operator << (std::ostream& os, const vmPort &port) {
os << port.getPort();
return(os);
}
struct tcphdr2{
inline vmPort get_source() {
return(ntohs(_source));
}
inline vmPort get_dest() {
return(ntohs(_dest));
}
inline void set_source(vmPort port) {
_source = htons(port.port);
}
inline void set_dest(vmPort port) {
_dest = htons(port.port);
}
u_int16_t _source;
u_int16_t _dest;
u_int32_t seq;
u_int32_t ack_seq;
# if __BYTE_ORDER == __LITTLE_ENDIAN
u_int16_t res1:4;
u_int16_t doff:4;
u_int16_t fin:1;
u_int16_t syn:1;
u_int16_t rst:1;
u_int16_t psh:1;
u_int16_t ack:1;
u_int16_t urg:1;
u_int16_t res2:2;
# elif __BYTE_ORDER == __BIG_ENDIAN
u_int16_t doff:4;
u_int16_t res1:4;
u_int16_t res2:2;
u_int16_t urg:1;
u_int16_t ack:1;
u_int16_t psh:1;
u_int16_t rst:1;
u_int16_t syn:1;
u_int16_t fin:1;
# else
# error "Adjust your <bits/endian.h> defines"
# endif
u_int16_t window;
u_int16_t check;
u_int16_t urg_ptr;
};
struct udphdr2 {
inline vmPort get_source() {
return(ntohs(_source));
}
inline vmPort get_dest() {
return(ntohs(_dest));
}
inline void set_source(vmPort port) {
_source = htons(port.port);
}
inline void set_dest(vmPort port) {
_dest = htons(port.port);
}
uint16_t _source;
uint16_t _dest;
uint16_t len;
uint16_t check;
};
struct vmIPport {
inline vmIPport() {
}
inline vmIPport(vmIP ip, vmPort port) {
this->ip = ip;
this->port = port;
}
inline bool operator == (const vmIPport& other) const {
return(this->ip == other.ip &&
this->port == other.port);
}
inline bool operator < (const vmIPport& other) const {
return(this->ip < other.ip ||
(this->ip == other.ip && this->port < other.port));
}
inline bool operator > (const vmIPport& other) const {
return(this->ip > other.ip ||
(this->ip == other.ip && this->port > other.port));
}
vmIP ip;
vmPort port;
};
struct vmIPmask {
vmIPmask() {
ip.clear();
mask = 0;
}
vmIPmask(vmIP ip, u_int16_t mask) {
this->ip = ip;
this->mask = mask;
}
vmIP ip;
u_int16_t mask;
};
inline vmIP str_2_vmIP(const char *str) {
vmIP vm_ip;
vm_ip.setFromString(str);
return(vm_ip);
}
inline vmIP ipv4_2_vmIP(u_int32_t ip, bool ntoh = false) {
vmIP vm_ip;
vm_ip.setIPv4(ip, ntoh);
return(vm_ip);
}
inline bool ip_is_v4(const char *ips) {
sockaddr_in sa;
return(inet_pton(AF_INET, ips, &sa.sin_addr) != 0);
}
inline bool ip_is_v6(const char *ips) {
struct sockaddr_in6 sa;
return(inet_pton(AF_INET6, ips, &sa.sin6_addr) != 0);
}
inline int ip_is_valid(const char *ips) {
return(ip_is_v4(ips) ? 4 :
ip_is_v6(ips) ? 6 : 0);
}
vmIP mysql_ip_2_vmIP(void *row, const char *column);
void socket_set_saddr(sockaddr_in *addr, vmIP ip, vmPort port);
#if VM_IPV6
void socket_set_saddr(sockaddr_in6 *addr, vmIP ip, vmPort port);
#endif
int socket_create(vmIP ip, int type, int protocol);
int socket_connect(int socket, vmIP ip, vmPort port);
int socket_bind(int socket, vmIP ip, vmPort port);
int socket_accept(int socket, vmIP *ip, vmPort *port);
#endif //IP_H