source: mainline/uspace/srv/net/tcp/tcp.c@ fab2746

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since fab2746 was fab2746, checked in by Jiri Svoboda <jiri@…>, 10 years ago

New transport layer API. Only UDP implemented.

  • Property mode set to 100644
File size: 5.6 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 <stdlib.h>
45#include <task.h>
46
47#include "ncsim.h"
48#include "pdu.h"
49#include "rqueue.h"
50#include "std.h"
51#include "tcp.h"
52#include "test.h"
53
54#define NAME "tcp"
55
56static int tcp_inet_ev_recv(inet_dgram_t *dgram);
57static void tcp_received_pdu(tcp_pdu_t *pdu);
58
59static inet_ev_ops_t tcp_inet_ev_ops = {
60 .recv = tcp_inet_ev_recv
61};
62
63/** Received datagram callback */
64static int tcp_inet_ev_recv(inet_dgram_t *dgram)
65{
66 uint8_t *pdu_raw;
67 size_t pdu_raw_size;
68
69 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_inet_ev_recv()");
70
71 pdu_raw = dgram->data;
72 pdu_raw_size = dgram->size;
73
74 /* Split into header and payload. */
75
76 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_inet_ev_recv() - split header/payload");
77
78 tcp_pdu_t *pdu;
79 size_t hdr_size;
80 tcp_header_t *hdr;
81 uint32_t data_offset;
82
83 if (pdu_raw_size < sizeof(tcp_header_t)) {
84 log_msg(LOG_DEFAULT, LVL_WARN, "pdu_raw_size = %zu < sizeof(tcp_header_t) = %zu",
85 pdu_raw_size, sizeof(tcp_header_t));
86 return EINVAL;
87 }
88
89 hdr = (tcp_header_t *)pdu_raw;
90 data_offset = BIT_RANGE_EXTRACT(uint32_t, DF_DATA_OFFSET_h, DF_DATA_OFFSET_l,
91 uint16_t_be2host(hdr->doff_flags));
92
93 hdr_size = sizeof(uint32_t) * data_offset;
94
95 if (pdu_raw_size < hdr_size) {
96 log_msg(LOG_DEFAULT, LVL_WARN, "pdu_raw_size = %zu < hdr_size = %zu",
97 pdu_raw_size, hdr_size);
98 return EINVAL;
99 }
100
101 if (hdr_size < sizeof(tcp_header_t)) {
102 log_msg(LOG_DEFAULT, LVL_WARN, "hdr_size = %zu < sizeof(tcp_header_t) = %zu",
103 hdr_size, sizeof(tcp_header_t)); return EINVAL;
104 }
105
106 log_msg(LOG_DEFAULT, LVL_DEBUG, "pdu_raw_size=%zu, hdr_size=%zu",
107 pdu_raw_size, hdr_size);
108 pdu = tcp_pdu_create(pdu_raw, hdr_size, pdu_raw + hdr_size,
109 pdu_raw_size - hdr_size);
110 if (pdu == NULL) {
111 log_msg(LOG_DEFAULT, LVL_WARN, "Failed creating PDU. Dropped.");
112 return ENOMEM;
113 }
114
115 pdu->src = dgram->src;
116 pdu->dest = dgram->dest;
117
118 tcp_received_pdu(pdu);
119 tcp_pdu_delete(pdu);
120
121 return EOK;
122}
123
124/** Transmit PDU over network layer. */
125void tcp_transmit_pdu(tcp_pdu_t *pdu)
126{
127 int rc;
128 uint8_t *pdu_raw;
129 size_t pdu_raw_size;
130 inet_dgram_t dgram;
131
132 pdu_raw_size = pdu->header_size + pdu->text_size;
133 pdu_raw = malloc(pdu_raw_size);
134 if (pdu_raw == NULL) {
135 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed to transmit PDU. Out of memory.");
136 return;
137 }
138
139 memcpy(pdu_raw, pdu->header, pdu->header_size);
140 memcpy(pdu_raw + pdu->header_size, pdu->text,
141 pdu->text_size);
142
143 dgram.iplink = 0;
144 dgram.src = pdu->src;
145 dgram.dest = pdu->dest;
146 dgram.tos = 0;
147 dgram.data = pdu_raw;
148 dgram.size = pdu_raw_size;
149
150 rc = inet_send(&dgram, INET_TTL_MAX, 0);
151 if (rc != EOK)
152 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed to transmit PDU.");
153
154 free(pdu_raw);
155}
156
157/** Process received PDU. */
158static void tcp_received_pdu(tcp_pdu_t *pdu)
159{
160 tcp_segment_t *dseg;
161 tcp_sockpair_t rident;
162
163 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_received_pdu()");
164
165 if (tcp_pdu_decode(pdu, &rident, &dseg) != EOK) {
166 log_msg(LOG_DEFAULT, LVL_WARN, "Not enough memory. PDU dropped.");
167 return;
168 }
169
170 /* Insert decoded segment into rqueue */
171 tcp_rqueue_insert_seg(&rident, dseg);
172}
173
174static int tcp_init(void)
175{
176 int rc;
177
178 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_init()");
179
180 tcp_rqueue_init();
181 tcp_rqueue_fibril_start();
182
183 tcp_ncsim_init();
184 tcp_ncsim_fibril_start();
185
186 if (0) tcp_test();
187
188 rc = inet_init(IP_PROTO_TCP, &tcp_inet_ev_ops);
189 if (rc != EOK) {
190 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed connecting to internet service.");
191 return ENOENT;
192 }
193
194// rc = tcp_sock_init();
195// if (rc != EOK) {
196// log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing socket service.");
197// return ENOENT;
198// }
199
200 return EOK;
201}
202
203int main(int argc, char **argv)
204{
205 int rc;
206
207 printf(NAME ": TCP (Transmission Control Protocol) network module\n");
208
209 rc = log_init(NAME);
210 if (rc != EOK) {
211 printf(NAME ": Failed to initialize log.\n");
212 return 1;
213 }
214
215 rc = tcp_init();
216 if (rc != EOK)
217 return 1;
218
219 printf(NAME ": Accepting connections.\n");
220 task_retval(0);
221 async_manager();
222
223 /* Not reached */
224 return 0;
225}
226
227/**
228 * @}
229 */
Note: See TracBrowser for help on using the repository browser.