root/trunk/src/headers_mgmt.c @ 154

Revision 154, 4.5 KB (checked in by andreu, 11 years ago)

Typo correction and copyright update

  • Property svn:eol-style set to native
Line 
1/*
2 * File: headers_mgmt.c
3 *
4 * Authors: ANDREU Francois-Xavier
5 *
6 * Copyright (C) 2005-2011 GIP RENATER
7 */
8
9/*  This file is part of renetcol.
10 *
11 *  renetcol is free software; you can redistribute it and/or modify
12 *  it under the terms of the GNU General Public License as published by
13 *  the Free Software Foundation; either version 2 of the License, or
14 *  (at your option) any later version.
15 *
16 *  renetcol is distributed in the hope that it will be useful,
17 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 *  GNU General Public License for more details.
20 *
21 *  You should have received a copy of the GNU General Public License
22 *  along with renetcol; if not, write to the Free Software
23 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24 */
25
26#include <stdlib.h>
27#include "headers_mgmt.h"
28
29/*
30 * get IP Header
31 */
32unsigned short 
33getIpHeader(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 * few check on IP Header
67 */
68unsigned short 
69checkIpHeader(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/*   if (pckt.ipH.length_entete != 5) */
77/*     { */
78/*       syslog(LOG_INFO,"IHL != 5 (%d)", pckt.ipH.versionAndLength); */
79/*       return(1); */
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 * get UDP Header
108 */
109unsigned short 
110getUdpHeader(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 * few check on UDP Header
132 */
133unsigned short 
134checkUdpHeader(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/*   if(pckt->udp_header->length > 1472) */
157/*     { */
158/*       syslog(LOG_INFO,"UDP length > 1472"); */
159/*       return(1); */
160/*     } */
161  return(0);
162}
Note: See TracBrowser for help on using the browser.