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