]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/cap_debug/ip.c
Correction of the container map
[stg.git] / projects / stargazer / plugins / capture / cap_debug / ip.c
1 /* $Id: ip.c,v 1.1 2005/12/12 18:14:22 nobunaga Exp $ 
2
3 Copyright (C) 2002 Marc Kirchner <kirchner@stud.fh-heilbronn.de>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 #include "libpal.h"
21
22 int
23 pkt_ip_header(struct packet *pkt,
24                 unsigned int iphdr_len,
25                 unsigned int version,
26                 unsigned char tos,
27                 unsigned short int total_len,
28                 unsigned short int id,
29                 unsigned short int frag_off,
30                 unsigned char ttl,
31                 unsigned char protocol,
32                 unsigned short int cksum,
33                 unsigned int saddr,
34                 unsigned int daddr)
35 {
36         struct ip *ip;
37
38         if (!pkt)
39                 return EPKTINVALPTR;
40         
41         ip = (struct ip *) pkt->pkt_ptr;
42
43         ip->ip_hl = iphdr_len;
44         ip->ip_v = version;
45         ip->ip_tos = tos;
46         ip->ip_len = htons(total_len);
47         ip->ip_id = htons(id);
48         ip->ip_off = htons(frag_off);
49         ip->ip_ttl = ttl;
50         ip->ip_p = protocol;
51         ip->ip_src.s_addr = saddr;
52         ip->ip_dst.s_addr = daddr;
53
54         return 0;
55 }
56
57 int
58 pkt_ip_option_header(struct packet *pkt, unsigned char type, unsigned char len, unsigned char ptr, unsigned char oflw_flg, void *optval, size_t optlen)
59 {
60         unsigned char *vp;
61         
62         if (!pkt)
63                 return EPKTINVALPTR;
64         
65         vp = pkt->pkt_ptr;
66         *vp = type;             
67         switch (type) {
68                 case PKT_IP_OPT_END:
69                         break;
70                 case PKT_IP_OPT_NOP:
71                         break;
72                 case PKT_IP_OPT_SEC:
73                         if ((pkt->pkt_pos + 2 + optlen) <= pkt->pkt_size) {
74                                 *(vp+1) = len;
75                                 memcpy((void*)(vp+2), optval, optlen);
76                         } else {
77                                 return EPKTRANGE;
78                         }
79                         break;
80                 case PKT_IP_OPT_RR:
81                 case PKT_IP_OPT_LSRR:
82                 case PKT_IP_OPT_SSRR:
83                 case PKT_IP_OPT_SID:
84                         if ((pkt->pkt_pos + 3 + optlen) <= pkt->pkt_size) {
85                                 *(vp+1) = len;
86                                 *(vp+2) = ptr;
87                                 memcpy((void*)(vp+3), optval, optlen);
88                         } else {
89                                 return EPKTRANGE;
90                         }
91                         break;
92                 case PKT_IP_OPT_TS:
93                         if ((pkt->pkt_pos + 4 + optlen) <= pkt->pkt_size) {
94                                 *(vp+1) = len;
95                                 *(vp+2) = ptr;
96                                 *(vp+3) = oflw_flg;
97                                 memcpy((void*)(vp+4), optval, optlen);
98                         } else {
99                                 return EPKTRANGE;
100                         }
101                         break;
102                 default:
103                         return EPKTUNKNOWNTYPE;
104         }
105         return PKTOK;
106 }       
107         
108 int
109 pkt_ip_cksum(struct packet *pkt)
110 {
111         /*
112         * checksum should be calculated by kernel 
113         * when we are using SOCK_RAW access.
114         */
115         struct ip *ip;
116
117         if (!pkt)
118                 return EPKTINVALPTR;
119         
120         ip = (struct ip *) pkt->pkt_ptr;
121         ip->ip_sum = 0;
122         ip->ip_sum = in_cksum((unsigned short *)ip, sizeof(struct ip));
123         return 0;
124 }