]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/cap_debug/icmp.c
Fixed compilation issue.
[stg.git] / projects / stargazer / plugins / capture / cap_debug / icmp.c
1 /* $Id: icmp.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_icmp_header(struct packet *pkt, unsigned char type, unsigned char code, unsigned short int checksum) 
24 {
25
26         struct icmp *icmp;
27         if (pkt) {
28                 icmp = (struct icmp *) pkt->pkt_ptr;
29                 icmp->icmp_type = type;
30                 icmp->icmp_code = code;
31                 icmp->icmp_cksum = htons(checksum);
32                 return 0;
33         } else
34                 return EPKTINVALPTR;
35 }
36
37 int pkt_icmp_addr_mask(struct packet *pkt, unsigned short int id, unsigned short int seqno, unsigned int mask, char *cmask)
38 {
39         struct icmp *icmp;
40         struct in_addr inetaddr;
41         
42         if (pkt) {
43                 icmp = (struct icmp *)pkt->pkt_ptr;
44         } else
45                 return EPKTINVALPTR;
46         icmp->icmp_id = htons(id);
47         icmp->icmp_seq = htons(seqno);
48         if (!cmask) {
49                 icmp->icmp_mask = htons(mask);
50         } else {
51                 if (inet_aton(cmask, &inetaddr) != 0) {
52                         icmp->icmp_mask = inetaddr.s_addr;
53                 } else
54                         return EERRNO;
55         }
56         return 0;
57 }
58
59 int
60 pkt_icmp_cksum(struct packet *pkt, unsigned int len)
61 {
62         struct icmp *icmp;
63
64         if (!pkt)
65                 return EPKTINVALPTR;
66         
67         icmp = (struct icmp *) pkt->pkt_ptr;
68         icmp->icmp_cksum = 0;
69         icmp->icmp_cksum = in_cksum((unsigned short *)icmp, len);
70         return 0;
71 }
72
73 int 
74 pkt_icmp_dest_unreach(struct packet *pkt, unsigned int unused)
75 {
76         struct icmp *icmp;
77
78         if (pkt) {
79                 icmp = (struct icmp *)pkt->pkt_ptr;
80         } else
81                 return EPKTINVALPTR;
82         icmp->icmp_void = htons(unused);
83         return 0;
84 }
85
86 int 
87 pkt_icmp_source_quench(struct packet *pkt, unsigned int unused)
88 {
89         struct icmp *icmp;
90
91         if (pkt) {
92                 icmp = (struct icmp *)pkt->pkt_ptr;
93         } else
94                 return EPKTINVALPTR;
95         icmp->icmp_void = htons(unused);
96         return 0;
97 }
98
99 int
100 pkt_icmp_redirect(struct packet *pkt, unsigned int routerip, char *crouterip)
101 {
102         struct icmp *icmp;
103         struct in_addr inetaddr;
104
105         if (pkt) {
106                 icmp = (struct icmp *)pkt->pkt_ptr;
107         } else {
108                 return EPKTINVALPTR;
109         }
110         if (crouterip) {
111                 if (inet_aton(crouterip, &inetaddr) != 0) {
112                         icmp->icmp_gwaddr = inetaddr;
113                 } else
114                         return EERRNO;
115         } else {
116                 inetaddr.s_addr = htons(routerip);
117                 icmp->icmp_gwaddr = inetaddr;
118         }
119         return 0;
120 }
121
122 int
123 pkt_icmp_echo(struct packet *pkt, unsigned short int id, unsigned short int seqno, void *data, size_t data_len)
124 {
125         struct icmp *icmp;
126
127         if (pkt) {
128                 icmp = (struct icmp *) pkt->pkt_ptr;
129         } else
130                 return EPKTINVALPTR;
131         icmp->icmp_id = htons(id);
132         icmp->icmp_seq = htons(seqno);
133         if (data) {
134                 memcpy(icmp->icmp_data, data, data_len);
135         }
136         return 0;
137 }
138
139 int
140 pkt_icmp_timestamp(struct packet *pkt, unsigned short int id, unsigned short int seqno, unsigned int ts_otime, unsigned int ts_rtime, unsigned int ts_ttime)
141 {
142         struct icmp *icmp;
143
144         if (pkt)
145                 icmp = (struct icmp *) pkt->pkt_ptr;
146         else
147                 return EPKTINVALPTR;
148         icmp->icmp_id = htons(id);
149         icmp->icmp_seq = htons(seqno);
150         icmp->icmp_otime = htons(ts_otime);
151         icmp->icmp_rtime = htons(ts_rtime);
152         icmp->icmp_ttime = htons(ts_ttime);
153         return 0;
154 }