[21580dd] | 1 | /*
|
---|
[c76e926] | 2 | * Copyright (c) 2012 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>
|
---|
| 44 | #include <task.h>
|
---|
[21580dd] | 45 |
|
---|
[8218b6b] | 46 | #include "ncsim.h"
|
---|
[762b48a] | 47 | #include "pdu.h"
|
---|
[c5808b41] | 48 | #include "rqueue.h"
|
---|
[04cd242] | 49 | #include "sock.h"
|
---|
[1812a0d] | 50 | #include "std.h"
|
---|
[014dd57b] | 51 | #include "tcp.h"
|
---|
[c5808b41] | 52 | #include "test.h"
|
---|
[21580dd] | 53 |
|
---|
[c5808b41] | 54 | #define NAME "tcp"
|
---|
[21580dd] | 55 |
|
---|
[59157eb] | 56 | #define IP_PROTO_TCP 6
|
---|
| 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 |
|
---|
| 71 | log_msg(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 |
|
---|
[c76e926] | 78 | log_msg(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)) {
|
---|
| 86 | log_msg(LVL_WARN, "pdu_raw_size = %zu < sizeof(tcp_header_t) = %zu",
|
---|
| 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) {
|
---|
| 98 | log_msg(LVL_WARN, "pdu_raw_size = %zu < hdr_size = %zu",
|
---|
| 99 | pdu_raw_size, hdr_size);
|
---|
| 100 | return EINVAL;
|
---|
[7c8267b] | 101 | }
|
---|
| 102 |
|
---|
[fe5e9629] | 103 | if (hdr_size < sizeof(tcp_header_t)) {
|
---|
| 104 | log_msg(LVL_WARN, "hdr_size = %zu < sizeof(tcp_header_t) = %zu",
|
---|
[c76e926] | 105 | hdr_size, sizeof(tcp_header_t)); return EINVAL;
|
---|
[fe5e9629] | 106 | }
|
---|
| 107 |
|
---|
[1812a0d] | 108 | log_msg(LVL_DEBUG, "pdu_raw_size=%zu, hdr_size=%zu",
|
---|
| 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) {
|
---|
| 113 | log_msg(LVL_WARN, "Failed creating PDU. Dropped.");
|
---|
| 114 | return ENOMEM;
|
---|
[21580dd] | 115 | }
|
---|
| 116 |
|
---|
[c76e926] | 117 | pdu->src_addr.ipv4 = dgram->src.ipv4;
|
---|
| 118 | pdu->dest_addr.ipv4 = dgram->dest.ipv4;
|
---|
[1812a0d] | 119 | log_msg(LVL_DEBUG, "src: 0x%08x, dest: 0x%08x",
|
---|
| 120 | pdu->src_addr.ipv4, pdu->dest_addr.ipv4);
|
---|
[21580dd] | 121 |
|
---|
[1812a0d] | 122 | tcp_received_pdu(pdu);
|
---|
| 123 | tcp_pdu_delete(pdu);
|
---|
[7c8267b] | 124 |
|
---|
[21580dd] | 125 | return EOK;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[1812a0d] | 128 | /** Transmit PDU over network layer. */
|
---|
| 129 | void tcp_transmit_pdu(tcp_pdu_t *pdu)
|
---|
[7c8267b] | 130 | {
|
---|
[0578271] | 131 | int rc;
|
---|
[c76e926] | 132 | uint8_t *pdu_raw;
|
---|
| 133 | size_t pdu_raw_size;
|
---|
| 134 | inet_dgram_t dgram;
|
---|
[7c8267b] | 135 |
|
---|
[c76e926] | 136 | pdu_raw_size = pdu->header_size + pdu->text_size;
|
---|
| 137 | pdu_raw = malloc(pdu_raw_size);
|
---|
| 138 | if (pdu_raw == NULL) {
|
---|
| 139 | log_msg(LVL_ERROR, "Failed to transmit PDU. Out of memory.");
|
---|
[1812a0d] | 140 | return;
|
---|
[7c8267b] | 141 | }
|
---|
[21580dd] | 142 |
|
---|
[c76e926] | 143 | memcpy(pdu_raw, pdu->header, pdu->header_size);
|
---|
| 144 | memcpy(pdu_raw + pdu->header_size, pdu->text,
|
---|
[1812a0d] | 145 | pdu->text_size);
|
---|
[7c8267b] | 146 |
|
---|
[c76e926] | 147 | dgram.src.ipv4 = pdu->src_addr.ipv4;
|
---|
| 148 | dgram.dest.ipv4 = pdu->dest_addr.ipv4;
|
---|
| 149 | dgram.tos = 0;
|
---|
| 150 | dgram.data = pdu_raw;
|
---|
| 151 | dgram.size = pdu_raw_size;
|
---|
| 152 |
|
---|
| 153 | rc = inet_send(&dgram, INET_TTL_MAX, 0);
|
---|
| 154 | if (rc != EOK)
|
---|
| 155 | log_msg(LVL_ERROR, "Failed to transmit PDU.");
|
---|
[21580dd] | 156 | }
|
---|
| 157 |
|
---|
[1812a0d] | 158 | /** Process received PDU. */
|
---|
| 159 | static void tcp_received_pdu(tcp_pdu_t *pdu)
|
---|
[7c8267b] | 160 | {
|
---|
[1812a0d] | 161 | tcp_segment_t *dseg;
|
---|
| 162 | tcp_sockpair_t rident;
|
---|
[fb04cba8] | 163 |
|
---|
[1812a0d] | 164 | log_msg(LVL_DEBUG, "tcp_received_pdu()");
|
---|
[fb04cba8] | 165 |
|
---|
[1812a0d] | 166 | if (tcp_pdu_decode(pdu, &rident, &dseg) != EOK) {
|
---|
| 167 | log_msg(LVL_WARN, "Not enough memory. PDU dropped.");
|
---|
[7c8267b] | 168 | return;
|
---|
| 169 | }
|
---|
[fb04cba8] | 170 |
|
---|
[1812a0d] | 171 | /* Insert decoded segment into rqueue */
|
---|
| 172 | tcp_rqueue_insert_seg(&rident, dseg);
|
---|
[21580dd] | 173 | }
|
---|
| 174 |
|
---|
[c76e926] | 175 | static int tcp_init(void)
|
---|
[7c8267b] | 176 | {
|
---|
[1812a0d] | 177 | int rc;
|
---|
[21580dd] | 178 |
|
---|
[c76e926] | 179 | log_msg(LVL_DEBUG, "tcp_init()");
|
---|
[1812a0d] | 180 |
|
---|
[c76e926] | 181 | tcp_rqueue_init();
|
---|
[88a6819] | 182 | tcp_rqueue_fibril_start();
|
---|
[1812a0d] | 183 |
|
---|
[c76e926] | 184 | tcp_ncsim_init();
|
---|
[88a6819] | 185 | tcp_ncsim_fibril_start();
|
---|
[a4ee3ab2] | 186 |
|
---|
[c76e926] | 187 | if (0) tcp_test();
|
---|
| 188 |
|
---|
[59157eb] | 189 | rc = inet_init(IP_PROTO_TCP, &tcp_inet_ev_ops);
|
---|
[ecff3d9] | 190 | if (rc != EOK) {
|
---|
| 191 | log_msg(LVL_ERROR, "Failed connecting to internet service.");
|
---|
[1812a0d] | 192 | return ENOENT;
|
---|
[ecff3d9] | 193 | }
|
---|
[21580dd] | 194 |
|
---|
[c76e926] | 195 | rc = tcp_sock_init();
|
---|
[ecff3d9] | 196 | if (rc != EOK) {
|
---|
| 197 | log_msg(LVL_ERROR, "Failed initializing socket service.");
|
---|
[c76e926] | 198 | return ENOENT;
|
---|
[ecff3d9] | 199 | }
|
---|
[1812a0d] | 200 |
|
---|
| 201 | return EOK;
|
---|
[21580dd] | 202 | }
|
---|
| 203 |
|
---|
[c5808b41] | 204 | int main(int argc, char **argv)
|
---|
[7c8267b] | 205 | {
|
---|
[0578271] | 206 | int rc;
|
---|
[2e99277] | 207 |
|
---|
[c5808b41] | 208 | printf(NAME ": TCP (Transmission Control Protocol) network module\n");
|
---|
[7c8267b] | 209 |
|
---|
[06a1d077] | 210 | rc = log_init(NAME, LVL_WARN);
|
---|
[0578271] | 211 | if (rc != EOK) {
|
---|
[c5808b41] | 212 | printf(NAME ": Failed to initialize log.\n");
|
---|
| 213 | return 1;
|
---|
[21580dd] | 214 | }
|
---|
[7c8267b] | 215 |
|
---|
[ecff3d9] | 216 | rc = tcp_init();
|
---|
| 217 | if (rc != EOK)
|
---|
| 218 | return 1;
|
---|
| 219 |
|
---|
| 220 | printf(NAME ": Accepting connections.\n");
|
---|
[c76e926] | 221 | task_retval(0);
|
---|
[c5808b41] | 222 | async_manager();
|
---|
[849ed54] | 223 |
|
---|
[c5808b41] | 224 | /* Not reached */
|
---|
| 225 | return 0;
|
---|
[014dd57b] | 226 | }
|
---|
| 227 |
|
---|
[c5808b41] | 228 | /**
|
---|
| 229 | * @}
|
---|
[21580dd] | 230 | */
|
---|