1 /* $Id: udp.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
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)
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);
42 pkt_udp_cksum(struct packet *pkt, char *saddr, char *daddr,
43 unsigned int udp_total_len)
46 struct pseudohdr *psh;
48 unsigned short int check=0;
51 if (!pkt || !saddr || !daddr)
54 if ((udp_total_len + pkt->pkt_pos) > pkt->pkt_size -1)
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;
64 if (inet_pton(AF_INET, saddr, &addr) < 0)
66 psh->saddr = addr.s_addr;
67 if (inet_pton(AF_INET, daddr, &addr) < 0)
69 psh->daddr = addr.s_addr;
71 psh->protocol = IPPROTO_UDP;
72 psh->length = htons(udp_total_len);
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;