source: mainline/uspace/srv/net/tcp/tcp.c@ 2f19103

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

TCP and UDP servers can make use of inet/endpoint.h types internally.

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