1 /* $Id: tcp.c,v 1.2 2009/06/19 12:50:47 faust 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
21 #include "types.h" /* internal types */
24 pkt_tcp_header(struct packet *pkt,
25 unsigned short int sport,
26 unsigned short int dport,
29 unsigned char headerlen,
30 unsigned char reserved,
32 unsigned short window,
33 unsigned short int checksum,
34 unsigned short int urgent)
41 tcp = (struct tcphdr *) pkt->pkt_ptr;
42 tcp->source = htons(sport);
43 tcp->dest = htons(dport);
44 tcp->seq = htonl(seq);
45 tcp->ack_seq = htonl(ackseq);
46 tcp->window = htons(window);
47 tcp->urg_ptr = htons(urgent);
49 tcp->doff = headerlen;
52 tcp->fin = ((flags & TH_FIN) != 0);
53 tcp->syn = ((flags & TH_SYN) != 0);
54 tcp->rst = ((flags & TH_RST) != 0);
55 tcp->psh = ((flags & TH_RST) != 0);
56 tcp->ack = ((flags & TH_ACK) != 0);
57 tcp->urg = ((flags & TH_URG) != 0);
58 # if __BYTE_ORDER == __LITTLE_ENDIAN
59 tcp->res2 = (flags & (TH_XMAS | TH_YMAS)) >> 6;
60 # elif __BYTE_ORDER == __BIG_ENDIAN
61 tcp->res2 = (flags & (TH_XMAS | TH_YMAS));
63 # error "Adjust your <bits/endian.h> defines"
74 tcp->check = htons(checksum);
79 pkt_tcp_cksum(struct packet *pkt, char *saddr, char *daddr,
80 unsigned int tcp_pkt_size)
83 struct pseudohdr *psh;
87 if (!pkt || !saddr || !daddr)
90 if ((tcp_pkt_size + pkt->pkt_pos) > pkt->pkt_size -1)
93 if ((tosum = (char *) malloc(tcp_pkt_size+sizeof(struct pseudohdr))) != NULL) {
94 memset(tosum, 0, tcp_pkt_size+sizeof(struct pseudohdr));
95 psh = (struct pseudohdr *) tosum;
96 tcp = (struct tcphdr *) pkt->pkt_ptr;
100 if (inet_pton(AF_INET, saddr, &addr) < 0)
102 psh->saddr = addr.s_addr;
103 if (inet_pton(AF_INET, daddr, &addr) < 0)
105 psh->daddr = addr.s_addr;
107 psh->protocol = IPPROTO_TCP;
108 psh->length = htons(tcp_pkt_size);
110 memcpy(tosum + sizeof(struct pseudohdr), tcp, tcp_pkt_size);
111 tcp->check = in_cksum((unsigned short *)tosum, tcp_pkt_size + sizeof(struct pseudohdr));
119 pkt_tcp_option(struct packet *pkt, unsigned char kind,
120 unsigned char len, void *optval, size_t optlen)
123 unsigned short int mss;
128 if ((pkt->pkt_size) < (pkt->pkt_pos+2+optlen))
131 vp = (void *)pkt->pkt_ptr;
133 memcpy(vp, &kind, 1);
144 if (kind == PKT_TCP_OPT_MSS) {
145 mss = htons(*(unsigned short int *)optval);
146 memcpy(vp, &mss, optlen);
147 } else if (kind == PKT_TCP_OPT_TIME) {
148 unsigned int time = htonl(*(unsigned int *)optval);
149 memcpy(vp, &time, optlen);
151 memcpy(vp, optval, optlen);