1 /* $Id: ip.c,v 1.1 2005/12/12 18:14:22 nobunaga Exp $
3 Copyright (C) 2002 Marc Kirchner <kirchner@stud.fh-heilbronn.de>
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.
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.
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
23 pkt_ip_header(struct packet *pkt,
24 unsigned int iphdr_len,
27 unsigned short int total_len,
28 unsigned short int id,
29 unsigned short int frag_off,
31 unsigned char protocol,
32 unsigned short int cksum,
41 ip = (struct ip *) pkt->pkt_ptr;
43 ip->ip_hl = iphdr_len;
46 ip->ip_len = htons(total_len);
47 ip->ip_id = htons(id);
48 ip->ip_off = htons(frag_off);
51 ip->ip_src.s_addr = saddr;
52 ip->ip_dst.s_addr = daddr;
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)
73 if ((pkt->pkt_pos + 2 + optlen) <= pkt->pkt_size) {
75 memcpy((void*)(vp+2), optval, optlen);
84 if ((pkt->pkt_pos + 3 + optlen) <= pkt->pkt_size) {
87 memcpy((void*)(vp+3), optval, optlen);
93 if ((pkt->pkt_pos + 4 + optlen) <= pkt->pkt_size) {
97 memcpy((void*)(vp+4), optval, optlen);
103 return EPKTUNKNOWNTYPE;
109 pkt_ip_cksum(struct packet *pkt)
112 * checksum should be calculated by kernel
113 * when we are using SOCK_RAW access.
120 ip = (struct ip *) pkt->pkt_ptr;
122 ip->ip_sum = in_cksum((unsigned short *)ip, sizeof(struct ip));