source: mainline/uspace/srv/net/tcp/inet.c@ b7fd2a0

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b7fd2a0 was b7fd2a0, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Use errno_t in all uspace and kernel code.

Change type of every variable, parameter and return value that holds an
<errno.h> constant to either errno_t (the usual case), or sys_errno_t
(some places in kernel). This is for the purpose of self-documentation,
as well as for type-checking with a bit of type definition hackery.

Although this is a massive commit, it is a simple text replacement, and thus
is very easy to verify. Simply do the following:

`
git checkout <this commit's hash>
git reset HEAD
git add .
tools/srepl '\berrno_t\b' int
git add .
tools/srepl '\bsys_errno_t\b' sysarg_t
git reset
git diff
`

While this doesn't ensure that the replacements are correct, it does ensure
that the commit doesn't do anything except those replacements. Since errno_t
is typedef'd to int in the usual case (and sys_errno_t to sysarg_t), even if
incorrect, this commit cannot change behavior.

  • Property mode set to 100644
File size: 4.9 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 inet interfacing
35 */
36
37#include <bitops.h>
38#include <byteorder.h>
39#include <errno.h>
40#include <inet/inet.h>
41#include <mem.h>
42#include <io/log.h>
43#include <stdlib.h>
44
45#include "inet.h"
46#include "pdu.h"
47#include "rqueue.h"
48#include "std.h"
49
50#define NAME "tcp"
51
52static errno_t tcp_inet_ev_recv(inet_dgram_t *dgram);
53static void tcp_received_pdu(tcp_pdu_t *pdu);
54
55static inet_ev_ops_t tcp_inet_ev_ops = {
56 .recv = tcp_inet_ev_recv
57};
58
59/** Received datagram callback */
60static errno_t tcp_inet_ev_recv(inet_dgram_t *dgram)
61{
62 uint8_t *pdu_raw;
63 size_t pdu_raw_size;
64
65 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_inet_ev_recv()");
66
67 pdu_raw = dgram->data;
68 pdu_raw_size = dgram->size;
69
70 /* Split into header and payload. */
71
72 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_inet_ev_recv() - split header/payload");
73
74 tcp_pdu_t *pdu;
75 size_t hdr_size;
76 tcp_header_t *hdr;
77 uint32_t data_offset;
78
79 if (pdu_raw_size < sizeof(tcp_header_t)) {
80 log_msg(LOG_DEFAULT, LVL_WARN, "pdu_raw_size = %zu < sizeof(tcp_header_t) = %zu",
81 pdu_raw_size, sizeof(tcp_header_t));
82 return EINVAL;
83 }
84
85 hdr = (tcp_header_t *)pdu_raw;
86 data_offset = BIT_RANGE_EXTRACT(uint32_t, DF_DATA_OFFSET_h, DF_DATA_OFFSET_l,
87 uint16_t_be2host(hdr->doff_flags));
88
89 hdr_size = sizeof(uint32_t) * data_offset;
90
91 if (pdu_raw_size < hdr_size) {
92 log_msg(LOG_DEFAULT, LVL_WARN, "pdu_raw_size = %zu < hdr_size = %zu",
93 pdu_raw_size, hdr_size);
94 return EINVAL;
95 }
96
97 if (hdr_size < sizeof(tcp_header_t)) {
98 log_msg(LOG_DEFAULT, LVL_WARN, "hdr_size = %zu < sizeof(tcp_header_t) = %zu",
99 hdr_size, sizeof(tcp_header_t)); return EINVAL;
100 }
101
102 log_msg(LOG_DEFAULT, LVL_DEBUG, "pdu_raw_size=%zu, hdr_size=%zu",
103 pdu_raw_size, hdr_size);
104 pdu = tcp_pdu_create(pdu_raw, hdr_size, pdu_raw + hdr_size,
105 pdu_raw_size - hdr_size);
106 if (pdu == NULL) {
107 log_msg(LOG_DEFAULT, LVL_WARN, "Failed creating PDU. Dropped.");
108 return ENOMEM;
109 }
110
111 pdu->src = dgram->src;
112 pdu->dest = dgram->dest;
113
114 tcp_received_pdu(pdu);
115 tcp_pdu_delete(pdu);
116
117 return EOK;
118}
119
120/** Transmit PDU over network layer. */
121void tcp_transmit_pdu(tcp_pdu_t *pdu)
122{
123 errno_t rc;
124 uint8_t *pdu_raw;
125 size_t pdu_raw_size;
126 inet_dgram_t dgram;
127
128 pdu_raw_size = pdu->header_size + pdu->text_size;
129 pdu_raw = malloc(pdu_raw_size);
130 if (pdu_raw == NULL) {
131 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed to transmit PDU. Out of memory.");
132 return;
133 }
134
135 memcpy(pdu_raw, pdu->header, pdu->header_size);
136 memcpy(pdu_raw + pdu->header_size, pdu->text,
137 pdu->text_size);
138
139 dgram.iplink = 0;
140 dgram.src = pdu->src;
141 dgram.dest = pdu->dest;
142 dgram.tos = 0;
143 dgram.data = pdu_raw;
144 dgram.size = pdu_raw_size;
145
146 rc = inet_send(&dgram, INET_TTL_MAX, 0);
147 if (rc != EOK)
148 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed to transmit PDU.");
149
150 free(pdu_raw);
151}
152
153/** Process received PDU. */
154static void tcp_received_pdu(tcp_pdu_t *pdu)
155{
156 tcp_segment_t *dseg;
157 inet_ep2_t rident;
158
159 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_received_pdu()");
160
161 if (tcp_pdu_decode(pdu, &rident, &dseg) != EOK) {
162 log_msg(LOG_DEFAULT, LVL_WARN, "Not enough memory. PDU dropped.");
163 return;
164 }
165
166 /* Insert decoded segment into rqueue */
167 tcp_rqueue_insert_seg(&rident, dseg);
168}
169
170/** Initialize TCP inet interface. */
171errno_t tcp_inet_init(void)
172{
173 errno_t rc;
174
175 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_inet_init()");
176
177 rc = inet_init(IP_PROTO_TCP, &tcp_inet_ev_ops);
178 if (rc != EOK) {
179 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed connecting to internet service.");
180 return ENOENT;
181 }
182
183 return EOK;
184}
185
186/**
187 * @}
188 */
Note: See TracBrowser for help on using the repository browser.