]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/cap_debug/udp.c
238c95dba1a1657edba32ebb456f7117ed1af53e
[stg.git] / projects / stargazer / plugins / capture / cap_debug / udp.c
1 /* $Id: udp.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 #include "types.h"
22
23 int 
24 pkt_udp_header(struct packet *pkt, unsigned short int sport, unsigned short int dport, unsigned short int udp_total_len, unsigned short int checksum)
25 {
26         struct udphdr *udp;
27         
28         if (!pkt)
29                 return EPKTINVALPTR;
30         if (pkt->pkt_size >= (pkt->pkt_pos + sizeof(struct udphdr))) {
31                 udp = (struct udphdr *) pkt->pkt_ptr;
32                 udp->source = htons(sport);
33                 udp->dest = htons(dport);
34                 udp->len = htons(udp_total_len);
35                 udp->check = htons(checksum);
36                 return 0;
37         } else
38                 return EPKTRANGE;
39 }
40
41 int
42 pkt_udp_cksum(struct packet *pkt, char *saddr, char *daddr,
43                 unsigned int udp_total_len)
44 {
45         char *tosum;
46         struct pseudohdr *psh;
47         struct udphdr *udp;
48         unsigned short int check=0;
49         struct in_addr addr;
50
51         if (!pkt || !saddr || !daddr)
52                 return EPKTINVALPTR;
53
54         if ((udp_total_len + pkt->pkt_pos) > pkt->pkt_size -1)
55                 return EPKTRANGE;
56
57         if ((tosum = (char *) malloc(udp_total_len+sizeof(struct pseudohdr))) != NULL) {
58                 memset(tosum, 0, udp_total_len+sizeof(struct pseudohdr));
59                 psh = (struct pseudohdr *) tosum;
60                 udp = (struct udphdr *) pkt->pkt_ptr;
61
62                 udp->check = 0;
63
64                 if (inet_pton(AF_INET, saddr, &addr) < 0)
65                         return EERRNO;
66                 psh->saddr = addr.s_addr;
67                 if (inet_pton(AF_INET, daddr, &addr) < 0)
68                         return EERRNO;
69                 psh->daddr = addr.s_addr;
70                 psh->zero = 0x00;
71                 psh->protocol = IPPROTO_UDP;
72                 psh->length = htons(udp_total_len);
73
74                 memcpy(tosum + sizeof(struct pseudohdr), udp, udp_total_len);
75                 check = in_cksum((unsigned short *)tosum, udp_total_len + sizeof(struct pseudohdr));
76                 /* _no_ call to htons(), because tosum is in network byte order */
77                 udp->check = check == 0 ? 0xffff : check;
78                 free(tosum);
79                 return 0;
80         } else
81                 return EERRNO;
82 }