2 * Copyright (c) 2012 Jiri Svoboda
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * @file TCP (Transmission Control Protocol) network module
39 #include <byteorder.h>
41 #include <inet/inet.h>
56 #define IP_PROTO_TCP 6
58 static int tcp_inet_ev_recv(inet_dgram_t
*dgram
);
59 static void tcp_received_pdu(tcp_pdu_t
*pdu
);
61 static inet_ev_ops_t tcp_inet_ev_ops
= {
62 .recv
= tcp_inet_ev_recv
65 /** Received datagram callback */
66 static int tcp_inet_ev_recv(inet_dgram_t
*dgram
)
71 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "tcp_inet_ev_recv()");
73 pdu_raw
= dgram
->data
;
74 pdu_raw_size
= dgram
->size
;
76 /* Split into header and payload. */
78 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "tcp_inet_ev_recv() - split header/payload");
85 if (pdu_raw_size
< sizeof(tcp_header_t
)) {
86 log_msg(LOG_DEFAULT
, LVL_WARN
, "pdu_raw_size = %zu < sizeof(tcp_header_t) = %zu",
87 pdu_raw_size
, sizeof(tcp_header_t
));
91 hdr
= (tcp_header_t
*)pdu_raw
;
92 data_offset
= BIT_RANGE_EXTRACT(uint32_t, DF_DATA_OFFSET_h
, DF_DATA_OFFSET_l
,
93 uint16_t_be2host(hdr
->doff_flags
));
95 hdr_size
= sizeof(uint32_t) * data_offset
;
97 if (pdu_raw_size
< hdr_size
) {
98 log_msg(LOG_DEFAULT
, LVL_WARN
, "pdu_raw_size = %zu < hdr_size = %zu",
99 pdu_raw_size
, hdr_size
);
103 if (hdr_size
< sizeof(tcp_header_t
)) {
104 log_msg(LOG_DEFAULT
, LVL_WARN
, "hdr_size = %zu < sizeof(tcp_header_t) = %zu",
105 hdr_size
, sizeof(tcp_header_t
)); return EINVAL
;
108 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "pdu_raw_size=%zu, hdr_size=%zu",
109 pdu_raw_size
, hdr_size
);
110 pdu
= tcp_pdu_create(pdu_raw
, hdr_size
, pdu_raw
+ hdr_size
,
111 pdu_raw_size
- hdr_size
);
113 log_msg(LOG_DEFAULT
, LVL_WARN
, "Failed creating PDU. Dropped.");
117 pdu
->src_addr
.ipv4
= dgram
->src
.ipv4
;
118 pdu
->dest_addr
.ipv4
= dgram
->dest
.ipv4
;
119 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "src: 0x%08x, dest: 0x%08x",
120 pdu
->src_addr
.ipv4
, pdu
->dest_addr
.ipv4
);
122 tcp_received_pdu(pdu
);
128 /** Transmit PDU over network layer. */
129 void tcp_transmit_pdu(tcp_pdu_t
*pdu
)
136 pdu_raw_size
= pdu
->header_size
+ pdu
->text_size
;
137 pdu_raw
= malloc(pdu_raw_size
);
138 if (pdu_raw
== NULL
) {
139 log_msg(LOG_DEFAULT
, LVL_ERROR
, "Failed to transmit PDU. Out of memory.");
143 memcpy(pdu_raw
, pdu
->header
, pdu
->header_size
);
144 memcpy(pdu_raw
+ pdu
->header_size
, pdu
->text
,
147 dgram
.src
.ipv4
= pdu
->src_addr
.ipv4
;
148 dgram
.dest
.ipv4
= pdu
->dest_addr
.ipv4
;
150 dgram
.data
= pdu_raw
;
151 dgram
.size
= pdu_raw_size
;
153 rc
= inet_send(&dgram
, INET_TTL_MAX
, 0);
155 log_msg(LOG_DEFAULT
, LVL_ERROR
, "Failed to transmit PDU.");
158 /** Process received PDU. */
159 static void tcp_received_pdu(tcp_pdu_t
*pdu
)
162 tcp_sockpair_t rident
;
164 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "tcp_received_pdu()");
166 if (tcp_pdu_decode(pdu
, &rident
, &dseg
) != EOK
) {
167 log_msg(LOG_DEFAULT
, LVL_WARN
, "Not enough memory. PDU dropped.");
171 /* Insert decoded segment into rqueue */
172 tcp_rqueue_insert_seg(&rident
, dseg
);
175 static int tcp_init(void)
179 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "tcp_init()");
182 tcp_rqueue_fibril_start();
185 tcp_ncsim_fibril_start();
189 rc
= inet_init(IP_PROTO_TCP
, &tcp_inet_ev_ops
);
191 log_msg(LOG_DEFAULT
, LVL_ERROR
, "Failed connecting to internet service.");
195 rc
= tcp_sock_init();
197 log_msg(LOG_DEFAULT
, LVL_ERROR
, "Failed initializing socket service.");
204 int main(int argc
, char **argv
)
208 printf(NAME
": TCP (Transmission Control Protocol) network module\n");
212 printf(NAME
": Failed to initialize log.\n");
220 printf(NAME
": Accepting connections.\n");