source: mainline/uspace/srv/net/tcp/tcp.c@ 02a09ed

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 02a09ed was 02a09ed, checked in by Martin Decky <martin@…>, 12 years ago

add basic infrastructure for IPv6 (inactive)
make inet_addr_t a universal address type

  • Property mode set to 100644
File size: 5.5 KB
Line 
1/*
2 * Copyright (c) 2012 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
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.
16 *
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.
27 */
28
29/** @addtogroup tcp
30 * @{
31 */
32
33/**
34 * @file TCP (Transmission Control Protocol) network module
35 */
36
37#include <async.h>
38#include <bitops.h>
39#include <byteorder.h>
40#include <errno.h>
41#include <inet/inet.h>
42#include <io/log.h>
43#include <stdio.h>
44#include <task.h>
45
46#include "ncsim.h"
47#include "pdu.h"
48#include "rqueue.h"
49#include "sock.h"
50#include "std.h"
51#include "tcp.h"
52#include "test.h"
53
54#define NAME "tcp"
55
56#define IP_PROTO_TCP 6
57
58static int tcp_inet_ev_recv(inet_dgram_t *dgram);
59static void tcp_received_pdu(tcp_pdu_t *pdu);
60
61static inet_ev_ops_t tcp_inet_ev_ops = {
62 .recv = tcp_inet_ev_recv
63};
64
65/** Received datagram callback */
66static int tcp_inet_ev_recv(inet_dgram_t *dgram)
67{
68 uint8_t *pdu_raw;
69 size_t pdu_raw_size;
70
71 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_inet_ev_recv()");
72
73 pdu_raw = dgram->data;
74 pdu_raw_size = dgram->size;
75
76 /* Split into header and payload. */
77
78 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_inet_ev_recv() - split header/payload");
79
80 tcp_pdu_t *pdu;
81 size_t hdr_size;
82 tcp_header_t *hdr;
83 uint32_t data_offset;
84
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));
88 return EINVAL;
89 }
90
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));
94
95 hdr_size = sizeof(uint32_t) * data_offset;
96
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);
100 return EINVAL;
101 }
102
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;
106 }
107
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);
112 if (pdu == NULL) {
113 log_msg(LOG_DEFAULT, LVL_WARN, "Failed creating PDU. Dropped.");
114 return ENOMEM;
115 }
116
117 pdu->src = dgram->src;
118 pdu->dest = dgram->dest;
119
120 tcp_received_pdu(pdu);
121 tcp_pdu_delete(pdu);
122
123 return EOK;
124}
125
126/** Transmit PDU over network layer. */
127void tcp_transmit_pdu(tcp_pdu_t *pdu)
128{
129 int rc;
130 uint8_t *pdu_raw;
131 size_t pdu_raw_size;
132 inet_dgram_t dgram;
133
134 pdu_raw_size = pdu->header_size + pdu->text_size;
135 pdu_raw = malloc(pdu_raw_size);
136 if (pdu_raw == NULL) {
137 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed to transmit PDU. Out of memory.");
138 return;
139 }
140
141 memcpy(pdu_raw, pdu->header, pdu->header_size);
142 memcpy(pdu_raw + pdu->header_size, pdu->text,
143 pdu->text_size);
144
145 dgram.src = pdu->src;
146 dgram.dest = pdu->dest;
147 dgram.tos = 0;
148 dgram.data = pdu_raw;
149 dgram.size = pdu_raw_size;
150
151 rc = inet_send(&dgram, INET_TTL_MAX, 0);
152 if (rc != EOK)
153 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed to transmit PDU.");
154}
155
156/** Process received PDU. */
157static void tcp_received_pdu(tcp_pdu_t *pdu)
158{
159 tcp_segment_t *dseg;
160 tcp_sockpair_t rident;
161
162 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_received_pdu()");
163
164 if (tcp_pdu_decode(pdu, &rident, &dseg) != EOK) {
165 log_msg(LOG_DEFAULT, LVL_WARN, "Not enough memory. PDU dropped.");
166 return;
167 }
168
169 /* Insert decoded segment into rqueue */
170 tcp_rqueue_insert_seg(&rident, dseg);
171}
172
173static int tcp_init(void)
174{
175 int rc;
176
177 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_init()");
178
179 tcp_rqueue_init();
180 tcp_rqueue_fibril_start();
181
182 tcp_ncsim_init();
183 tcp_ncsim_fibril_start();
184
185 if (0) tcp_test();
186
187 rc = inet_init(IP_PROTO_TCP, &tcp_inet_ev_ops);
188 if (rc != EOK) {
189 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed connecting to internet service.");
190 return ENOENT;
191 }
192
193 rc = tcp_sock_init();
194 if (rc != EOK) {
195 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing socket service.");
196 return ENOENT;
197 }
198
199 return EOK;
200}
201
202int main(int argc, char **argv)
203{
204 int rc;
205
206 printf(NAME ": TCP (Transmission Control Protocol) network module\n");
207
208 rc = log_init(NAME);
209 if (rc != EOK) {
210 printf(NAME ": Failed to initialize log.\n");
211 return 1;
212 }
213
214 rc = tcp_init();
215 if (rc != EOK)
216 return 1;
217
218 printf(NAME ": Accepting connections.\n");
219 task_retval(0);
220 async_manager();
221
222 /* Not reached */
223 return 0;
224}
225
226/**
227 * @}
228 */
Note: See TracBrowser for help on using the repository browser.