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 | #include <stdlib.h> |
---|
27 | #include "headers_mgmt.h" |
---|
28 | |
---|
29 | |
---|
30 | |
---|
31 | |
---|
32 | unsigned short |
---|
33 | getIpHeader(DatagramPtr pcktPtr, unsigned char * buf) |
---|
34 | { |
---|
35 | unsigned char buffer2[2]; |
---|
36 | unsigned char buffer4[4]; |
---|
37 | unsigned short offset = 0; |
---|
38 | pcktPtr->ipH->versionAndLength = (unsigned short)*(buf); |
---|
39 | pcktPtr->ipH->tos = (unsigned short)*(buf+offset+1); |
---|
40 | buffer2[1]= *(buf+offset+2); |
---|
41 | buffer2[0]= *(buf+offset+3); |
---|
42 | pcktPtr->ipH->length = *((unsigned short*)&buffer2); |
---|
43 | buffer2[1]= *(buf+offset+4); |
---|
44 | buffer2[0]= *(buf+offset+5); |
---|
45 | pcktPtr->ipH->ident = *((unsigned short*)&buffer2); |
---|
46 | pcktPtr->ipH->flagsAndOffset = (unsigned short)(*(buf+offset+7)); |
---|
47 | pcktPtr->ipH->ttl = (unsigned short)*(buf+offset+8); |
---|
48 | pcktPtr->ipH->protocol = (unsigned short)*(buf+offset+9); |
---|
49 | buffer2[1]= *(buf+offset+10); |
---|
50 | buffer2[0]= *(buf+offset+11); |
---|
51 | pcktPtr->ipH->checksum = *((unsigned short*)(&buffer2)); |
---|
52 | buffer4[3] = *(buf+offset+12)&0xff; |
---|
53 | buffer4[2] = *(buf+offset+13)&0xff; |
---|
54 | buffer4[1] = *(buf+offset+14)&0xff; |
---|
55 | buffer4[0] = *(buf+offset+15)&0xff; |
---|
56 | pcktPtr->ipH->srcAdd = *((unsigned long*)(&buffer4)); |
---|
57 | buffer4[3] = *(buf+offset+16)&0xff; |
---|
58 | buffer4[2] = *(buf+offset+17)&0xff; |
---|
59 | buffer4[1] = *(buf+offset+18)&0xff; |
---|
60 | buffer4[0] = *(buf+offset+19)&0xff; |
---|
61 | pcktPtr->ipH->dstAdd = *((unsigned long*)(&buffer4)); |
---|
62 | return(0); |
---|
63 | } |
---|
64 | |
---|
65 | |
---|
66 | |
---|
67 | |
---|
68 | unsigned short |
---|
69 | checkIpHeader(DatagramPtr pP, unsigned long *sortedRLPtr, int rNb) |
---|
70 | { |
---|
71 | if ((pP->ipH->versionAndLength>>4) != 4) |
---|
72 | { |
---|
73 | syslog(LOG_INFO,"IP version not 4 (%d)", (pP->ipH->versionAndLength>>4)); |
---|
74 | return(1); |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | |
---|
79 | |
---|
80 | |
---|
81 | if (pP->ipH->tos != 0) |
---|
82 | { |
---|
83 | syslog(LOG_INFO,"TOS(%d) != 0", |
---|
84 | pP->ipH->tos); |
---|
85 | } |
---|
86 | if (pP->ipH->length > 1492) |
---|
87 | { |
---|
88 | syslog(LOG_INFO,"IP packet length > 1492 (%d)", |
---|
89 | pP->ipH->length); |
---|
90 | return(1); |
---|
91 | } |
---|
92 | if (pP->ipH->protocol != 17) |
---|
93 | { |
---|
94 | syslog(LOG_INFO,"Not UDP protocol (%u)",pP->ipH->protocol); |
---|
95 | return(1); |
---|
96 | } |
---|
97 | if (bsearch(&pP->ipH->srcAdd, sortedRLPtr, rNb, sizeof(unsigned long), |
---|
98 | longCmp) == NULL ) |
---|
99 | { |
---|
100 | return(2); |
---|
101 | } |
---|
102 | return(0); |
---|
103 | } |
---|
104 | |
---|
105 | |
---|
106 | |
---|
107 | |
---|
108 | |
---|
109 | unsigned short |
---|
110 | getUdpHeader(DatagramPtr pckt, unsigned char * ptr_buffer) |
---|
111 | { |
---|
112 | unsigned char buffer2[2]; |
---|
113 | unsigned short offset = 20; |
---|
114 | buffer2[1]= *(ptr_buffer+offset+0); |
---|
115 | buffer2[0]= *(ptr_buffer+offset+1); |
---|
116 | pckt->udp_header->srcPort = *((unsigned short*)&buffer2); |
---|
117 | buffer2[1]= *(ptr_buffer+offset+2); |
---|
118 | buffer2[0]= *(ptr_buffer+offset+3); |
---|
119 | pckt->udp_header->dstPort = *((unsigned short*)&buffer2); |
---|
120 | buffer2[1]= *(ptr_buffer+offset+4); |
---|
121 | buffer2[0]= *(ptr_buffer+offset+5); |
---|
122 | pckt->udp_header->length = *((unsigned short*)&buffer2); |
---|
123 | buffer2[1]= *(ptr_buffer+offset+6); |
---|
124 | buffer2[0]= *(ptr_buffer+offset+7); |
---|
125 | pckt->udp_header->checksum = *((unsigned short*)&buffer2); |
---|
126 | return(0); |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | |
---|
131 | |
---|
132 | |
---|
133 | unsigned short |
---|
134 | checkUdpHeader(DatagramPtr pckt, int regRouter, unsigned short receptPort ) |
---|
135 | { |
---|
136 | if(pckt->udp_header->dstPort != receptPort) |
---|
137 | { |
---|
138 | if (regRouter == 0){ |
---|
139 | syslog(LOG_INFO,"Wrong destination port (%u)", |
---|
140 | pckt->udp_header->dstPort); |
---|
141 | return (1); |
---|
142 | } |
---|
143 | return (1); |
---|
144 | }else{ |
---|
145 | if (regRouter == 2) |
---|
146 | { |
---|
147 | syslog(LOG_INFO,"Router Address not registered : %lu.%lu.%lu.%lu)", |
---|
148 | (pckt->ipH->srcAdd>>24), |
---|
149 | (pckt->ipH->srcAdd<<8>>24), |
---|
150 | (pckt->ipH->srcAdd<<16>>24), |
---|
151 | (pckt->ipH->srcAdd<<24>>24)); |
---|
152 | return (1); |
---|
153 | } |
---|
154 | return (0); |
---|
155 | } |
---|
156 | |
---|
157 | |
---|
158 | |
---|
159 | |
---|
160 | |
---|
161 | return(0); |
---|
162 | } |
---|