| 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 | static int tcp_inet_ev_recv(inet_dgram_t *dgram);
|
|---|
| 57 | static void tcp_received_pdu(tcp_pdu_t *pdu);
|
|---|
| 58 |
|
|---|
| 59 | static inet_ev_ops_t tcp_inet_ev_ops = {
|
|---|
| 60 | .recv = tcp_inet_ev_recv
|
|---|
| 61 | };
|
|---|
| 62 |
|
|---|
| 63 | /** Received datagram callback */
|
|---|
| 64 | static 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. */
|
|---|
| 125 | void 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 |
|
|---|
| 155 | /** Process received PDU. */
|
|---|
| 156 | static void tcp_received_pdu(tcp_pdu_t *pdu)
|
|---|
| 157 | {
|
|---|
| 158 | tcp_segment_t *dseg;
|
|---|
| 159 | tcp_sockpair_t rident;
|
|---|
| 160 |
|
|---|
| 161 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_received_pdu()");
|
|---|
| 162 |
|
|---|
| 163 | if (tcp_pdu_decode(pdu, &rident, &dseg) != EOK) {
|
|---|
| 164 | log_msg(LOG_DEFAULT, LVL_WARN, "Not enough memory. PDU dropped.");
|
|---|
| 165 | return;
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | /* Insert decoded segment into rqueue */
|
|---|
| 169 | tcp_rqueue_insert_seg(&rident, dseg);
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | static int tcp_init(void)
|
|---|
| 173 | {
|
|---|
| 174 | int rc;
|
|---|
| 175 |
|
|---|
| 176 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_init()");
|
|---|
| 177 |
|
|---|
| 178 | tcp_rqueue_init();
|
|---|
| 179 | tcp_rqueue_fibril_start();
|
|---|
| 180 |
|
|---|
| 181 | tcp_ncsim_init();
|
|---|
| 182 | tcp_ncsim_fibril_start();
|
|---|
| 183 |
|
|---|
| 184 | if (0) tcp_test();
|
|---|
| 185 |
|
|---|
| 186 | rc = inet_init(IP_PROTO_TCP, &tcp_inet_ev_ops);
|
|---|
| 187 | if (rc != EOK) {
|
|---|
| 188 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed connecting to internet service.");
|
|---|
| 189 | return ENOENT;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | rc = tcp_sock_init();
|
|---|
| 193 | if (rc != EOK) {
|
|---|
| 194 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing socket service.");
|
|---|
| 195 | return ENOENT;
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | return EOK;
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | int main(int argc, char **argv)
|
|---|
| 202 | {
|
|---|
| 203 | int rc;
|
|---|
| 204 |
|
|---|
| 205 | printf(NAME ": TCP (Transmission Control Protocol) network module\n");
|
|---|
| 206 |
|
|---|
| 207 | rc = log_init(NAME);
|
|---|
| 208 | if (rc != EOK) {
|
|---|
| 209 | printf(NAME ": Failed to initialize log.\n");
|
|---|
| 210 | return 1;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | rc = tcp_init();
|
|---|
| 214 | if (rc != EOK)
|
|---|
| 215 | return 1;
|
|---|
| 216 |
|
|---|
| 217 | printf(NAME ": Accepting connections.\n");
|
|---|
| 218 | task_retval(0);
|
|---|
| 219 | async_manager();
|
|---|
| 220 |
|
|---|
| 221 | /* Not reached */
|
|---|
| 222 | return 0;
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | /**
|
|---|
| 226 | * @}
|
|---|
| 227 | */
|
|---|