[21580dd] | 1 | /*
|
---|
[2f19103] | 2 | * Copyright (c) 2015 Jiri Svoboda
|
---|
[21580dd] | 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
|
---|
[89e57cee] | 30 | * @{
|
---|
[21580dd] | 31 | */
|
---|
| 32 |
|
---|
[c5808b41] | 33 | /**
|
---|
| 34 | * @file TCP (Transmission Control Protocol) network module
|
---|
[21580dd] | 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <async.h>
|
---|
[fe5e9629] | 38 | #include <bitops.h>
|
---|
[1812a0d] | 39 | #include <byteorder.h>
|
---|
[e98b1d5] | 40 | #include <errno.h>
|
---|
[c76e926] | 41 | #include <inet/inet.h>
|
---|
[c5808b41] | 42 | #include <io/log.h>
|
---|
| 43 | #include <stdio.h>
|
---|
[fab2746] | 44 | #include <stdlib.h>
|
---|
[c5808b41] | 45 | #include <task.h>
|
---|
[21580dd] | 46 |
|
---|
[2989c7e] | 47 | #include "conn.h"
|
---|
[8218b6b] | 48 | #include "ncsim.h"
|
---|
[762b48a] | 49 | #include "pdu.h"
|
---|
[c5808b41] | 50 | #include "rqueue.h"
|
---|
[779541b] | 51 | #include "service.h"
|
---|
[1812a0d] | 52 | #include "std.h"
|
---|
[014dd57b] | 53 | #include "tcp.h"
|
---|
[c5808b41] | 54 | #include "test.h"
|
---|
[21580dd] | 55 |
|
---|
[c5808b41] | 56 | #define NAME "tcp"
|
---|
[21580dd] | 57 |
|
---|
[c76e926] | 58 | static int tcp_inet_ev_recv(inet_dgram_t *dgram);
|
---|
[1812a0d] | 59 | static void tcp_received_pdu(tcp_pdu_t *pdu);
|
---|
[21580dd] | 60 |
|
---|
[c76e926] | 61 | static inet_ev_ops_t tcp_inet_ev_ops = {
|
---|
| 62 | .recv = tcp_inet_ev_recv
|
---|
| 63 | };
|
---|
[7c8267b] | 64 |
|
---|
[c76e926] | 65 | /** Received datagram callback */
|
---|
| 66 | static int tcp_inet_ev_recv(inet_dgram_t *dgram)
|
---|
[7c8267b] | 67 | {
|
---|
[1812a0d] | 68 | uint8_t *pdu_raw;
|
---|
[c76e926] | 69 | size_t pdu_raw_size;
|
---|
| 70 |
|
---|
[a1a101d] | 71 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_inet_ev_recv()");
|
---|
[7c8267b] | 72 |
|
---|
[c76e926] | 73 | pdu_raw = dgram->data;
|
---|
| 74 | pdu_raw_size = dgram->size;
|
---|
[7c8267b] | 75 |
|
---|
[1812a0d] | 76 | /* Split into header and payload. */
|
---|
[21580dd] | 77 |
|
---|
[a1a101d] | 78 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_inet_ev_recv() - split header/payload");
|
---|
[7c8267b] | 79 |
|
---|
[1812a0d] | 80 | tcp_pdu_t *pdu;
|
---|
| 81 | size_t hdr_size;
|
---|
[fe5e9629] | 82 | tcp_header_t *hdr;
|
---|
| 83 | uint32_t data_offset;
|
---|
[7c8267b] | 84 |
|
---|
[fe5e9629] | 85 | if (pdu_raw_size < sizeof(tcp_header_t)) {
|
---|
[a1a101d] | 86 | log_msg(LOG_DEFAULT, LVL_WARN, "pdu_raw_size = %zu < sizeof(tcp_header_t) = %zu",
|
---|
[fe5e9629] | 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;
|
---|
[7c8267b] | 96 |
|
---|
[1812a0d] | 97 | if (pdu_raw_size < hdr_size) {
|
---|
[a1a101d] | 98 | log_msg(LOG_DEFAULT, LVL_WARN, "pdu_raw_size = %zu < hdr_size = %zu",
|
---|
[1812a0d] | 99 | pdu_raw_size, hdr_size);
|
---|
| 100 | return EINVAL;
|
---|
[7c8267b] | 101 | }
|
---|
| 102 |
|
---|
[fe5e9629] | 103 | if (hdr_size < sizeof(tcp_header_t)) {
|
---|
[a1a101d] | 104 | log_msg(LOG_DEFAULT, LVL_WARN, "hdr_size = %zu < sizeof(tcp_header_t) = %zu",
|
---|
[c76e926] | 105 | hdr_size, sizeof(tcp_header_t)); return EINVAL;
|
---|
[fe5e9629] | 106 | }
|
---|
| 107 |
|
---|
[a1a101d] | 108 | log_msg(LOG_DEFAULT, LVL_DEBUG, "pdu_raw_size=%zu, hdr_size=%zu",
|
---|
[1812a0d] | 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) {
|
---|
[a1a101d] | 113 | log_msg(LOG_DEFAULT, LVL_WARN, "Failed creating PDU. Dropped.");
|
---|
[1812a0d] | 114 | return ENOMEM;
|
---|
[21580dd] | 115 | }
|
---|
| 116 |
|
---|
[02a09ed] | 117 | pdu->src = dgram->src;
|
---|
| 118 | pdu->dest = dgram->dest;
|
---|
[21580dd] | 119 |
|
---|
[1812a0d] | 120 | tcp_received_pdu(pdu);
|
---|
| 121 | tcp_pdu_delete(pdu);
|
---|
[7c8267b] | 122 |
|
---|
[21580dd] | 123 | return EOK;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[1812a0d] | 126 | /** Transmit PDU over network layer. */
|
---|
| 127 | void tcp_transmit_pdu(tcp_pdu_t *pdu)
|
---|
[7c8267b] | 128 | {
|
---|
[0578271] | 129 | int rc;
|
---|
[c76e926] | 130 | uint8_t *pdu_raw;
|
---|
| 131 | size_t pdu_raw_size;
|
---|
| 132 | inet_dgram_t dgram;
|
---|
[7c8267b] | 133 |
|
---|
[c76e926] | 134 | pdu_raw_size = pdu->header_size + pdu->text_size;
|
---|
| 135 | pdu_raw = malloc(pdu_raw_size);
|
---|
| 136 | if (pdu_raw == NULL) {
|
---|
[a1a101d] | 137 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed to transmit PDU. Out of memory.");
|
---|
[1812a0d] | 138 | return;
|
---|
[7c8267b] | 139 | }
|
---|
[21580dd] | 140 |
|
---|
[c76e926] | 141 | memcpy(pdu_raw, pdu->header, pdu->header_size);
|
---|
| 142 | memcpy(pdu_raw + pdu->header_size, pdu->text,
|
---|
[1812a0d] | 143 | pdu->text_size);
|
---|
[7c8267b] | 144 |
|
---|
[695b6ff] | 145 | dgram.iplink = 0;
|
---|
[02a09ed] | 146 | dgram.src = pdu->src;
|
---|
| 147 | dgram.dest = pdu->dest;
|
---|
[c76e926] | 148 | dgram.tos = 0;
|
---|
| 149 | dgram.data = pdu_raw;
|
---|
| 150 | dgram.size = pdu_raw_size;
|
---|
| 151 |
|
---|
| 152 | rc = inet_send(&dgram, INET_TTL_MAX, 0);
|
---|
| 153 | if (rc != EOK)
|
---|
[a1a101d] | 154 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed to transmit PDU.");
|
---|
[9787a93] | 155 |
|
---|
| 156 | free(pdu_raw);
|
---|
[21580dd] | 157 | }
|
---|
| 158 |
|
---|
[1812a0d] | 159 | /** Process received PDU. */
|
---|
| 160 | static void tcp_received_pdu(tcp_pdu_t *pdu)
|
---|
[7c8267b] | 161 | {
|
---|
[1812a0d] | 162 | tcp_segment_t *dseg;
|
---|
[2f19103] | 163 | inet_ep2_t rident;
|
---|
[fb04cba8] | 164 |
|
---|
[a1a101d] | 165 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_received_pdu()");
|
---|
[fb04cba8] | 166 |
|
---|
[1812a0d] | 167 | if (tcp_pdu_decode(pdu, &rident, &dseg) != EOK) {
|
---|
[a1a101d] | 168 | log_msg(LOG_DEFAULT, LVL_WARN, "Not enough memory. PDU dropped.");
|
---|
[7c8267b] | 169 | return;
|
---|
| 170 | }
|
---|
[fb04cba8] | 171 |
|
---|
[1812a0d] | 172 | /* Insert decoded segment into rqueue */
|
---|
| 173 | tcp_rqueue_insert_seg(&rident, dseg);
|
---|
[21580dd] | 174 | }
|
---|
| 175 |
|
---|
[c76e926] | 176 | static int tcp_init(void)
|
---|
[7c8267b] | 177 | {
|
---|
[1812a0d] | 178 | int rc;
|
---|
[21580dd] | 179 |
|
---|
[a1a101d] | 180 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_init()");
|
---|
[1812a0d] | 181 |
|
---|
[2989c7e] | 182 | rc = tcp_conns_init();
|
---|
| 183 | if (rc != EOK) {
|
---|
| 184 | assert(rc == ENOMEM);
|
---|
| 185 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing connections");
|
---|
| 186 | return ENOMEM;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
[c76e926] | 189 | tcp_rqueue_init();
|
---|
[88a6819] | 190 | tcp_rqueue_fibril_start();
|
---|
[1812a0d] | 191 |
|
---|
[c76e926] | 192 | tcp_ncsim_init();
|
---|
[88a6819] | 193 | tcp_ncsim_fibril_start();
|
---|
[a4ee3ab2] | 194 |
|
---|
[c76e926] | 195 | if (0) tcp_test();
|
---|
| 196 |
|
---|
[59157eb] | 197 | rc = inet_init(IP_PROTO_TCP, &tcp_inet_ev_ops);
|
---|
[ecff3d9] | 198 | if (rc != EOK) {
|
---|
[a1a101d] | 199 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed connecting to internet service.");
|
---|
[1812a0d] | 200 | return ENOENT;
|
---|
[ecff3d9] | 201 | }
|
---|
[21580dd] | 202 |
|
---|
[779541b] | 203 | rc = tcp_service_init();
|
---|
| 204 | if (rc != EOK) {
|
---|
| 205 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing service.");
|
---|
| 206 | return ENOENT;
|
---|
| 207 | }
|
---|
[1812a0d] | 208 |
|
---|
| 209 | return EOK;
|
---|
[21580dd] | 210 | }
|
---|
| 211 |
|
---|
[c5808b41] | 212 | int main(int argc, char **argv)
|
---|
[7c8267b] | 213 | {
|
---|
[0578271] | 214 | int rc;
|
---|
[2e99277] | 215 |
|
---|
[c5808b41] | 216 | printf(NAME ": TCP (Transmission Control Protocol) network module\n");
|
---|
[7c8267b] | 217 |
|
---|
[267f235] | 218 | rc = log_init(NAME);
|
---|
[0578271] | 219 | if (rc != EOK) {
|
---|
[c5808b41] | 220 | printf(NAME ": Failed to initialize log.\n");
|
---|
| 221 | return 1;
|
---|
[21580dd] | 222 | }
|
---|
[7c8267b] | 223 |
|
---|
[ecff3d9] | 224 | rc = tcp_init();
|
---|
| 225 | if (rc != EOK)
|
---|
| 226 | return 1;
|
---|
| 227 |
|
---|
| 228 | printf(NAME ": Accepting connections.\n");
|
---|
[c76e926] | 229 | task_retval(0);
|
---|
[c5808b41] | 230 | async_manager();
|
---|
[849ed54] | 231 |
|
---|
[c5808b41] | 232 | /* Not reached */
|
---|
| 233 | return 0;
|
---|
[014dd57b] | 234 | }
|
---|
| 235 |
|
---|
[c5808b41] | 236 | /**
|
---|
| 237 | * @}
|
---|
[21580dd] | 238 | */
|
---|