[c5808b41] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 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
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <adt/list.h>
|
---|
| 38 | #include <errno.h>
|
---|
| 39 | #include <io/log.h>
|
---|
| 40 | #include <stdlib.h>
|
---|
| 41 | #include "iqueue.h"
|
---|
| 42 | #include "seq_no.h"
|
---|
| 43 | #include "tcp_type.h"
|
---|
| 44 |
|
---|
| 45 | void tcp_iqueue_init(tcp_iqueue_t *iqueue, tcp_conn_t *conn)
|
---|
| 46 | {
|
---|
| 47 | list_initialize(&iqueue->list);
|
---|
| 48 | iqueue->conn = conn;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | void tcp_iqueue_insert_seg(tcp_iqueue_t *iqueue, tcp_segment_t *seg)
|
---|
| 52 | {
|
---|
| 53 | tcp_iqueue_entry_t *iqe;
|
---|
| 54 | log_msg(LVL_DEBUG, "tcp_iqueue_insert_seg()");
|
---|
| 55 |
|
---|
| 56 | iqe = calloc(1, sizeof(tcp_iqueue_entry_t));
|
---|
| 57 | if (iqe == NULL) {
|
---|
| 58 | log_msg(LVL_ERROR, "Failed allocating IQE.");
|
---|
| 59 | return;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | iqe->seg = seg;
|
---|
| 63 |
|
---|
| 64 | /* XXX Sort by sequence number */
|
---|
| 65 | list_append(&iqe->link, &iqueue->list);
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | int tcp_iqueue_get_ready_seg(tcp_iqueue_t *iqueue, tcp_segment_t **seg)
|
---|
| 69 | {
|
---|
| 70 | tcp_iqueue_entry_t *iqe;
|
---|
| 71 | link_t *link;
|
---|
| 72 |
|
---|
| 73 | log_msg(LVL_DEBUG, "tcp_get_ready_seg()");
|
---|
| 74 |
|
---|
| 75 | link = list_first(&iqueue->list);
|
---|
| 76 | if (link == NULL) {
|
---|
| 77 | log_msg(LVL_DEBUG, "iqueue is empty");
|
---|
| 78 | return ENOENT;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | iqe = list_get_instance(link, tcp_iqueue_entry_t, link);
|
---|
| 82 |
|
---|
| 83 | while (!seq_no_segment_acceptable(iqueue->conn, iqe->seg)) {
|
---|
| 84 | log_msg(LVL_DEBUG, "Skipping unacceptable segment");
|
---|
| 85 |
|
---|
| 86 | list_remove(&iqe->link);
|
---|
| 87 | /* XXX free segment */
|
---|
| 88 |
|
---|
| 89 | link = list_first(&iqueue->list);
|
---|
| 90 | if (link == NULL) {
|
---|
| 91 | log_msg(LVL_DEBUG, "iqueue is empty");
|
---|
| 92 | return ENOENT;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | iqe = list_get_instance(link, tcp_iqueue_entry_t, link);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | /* Do not return segments that are not ready for processing */
|
---|
| 99 | if (!seq_no_segment_ready(iqueue->conn, iqe->seg)) {
|
---|
| 100 | log_msg(LVL_DEBUG, "Next segment not ready: SEG.SEQ=%u, "
|
---|
| 101 | "RCV.NXT=%u, SEG.LEN=%u", iqe->seg->seq,
|
---|
| 102 | iqueue->conn->rcv_nxt, iqe->seg->len);
|
---|
| 103 | return ENOENT;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | log_msg(LVL_DEBUG, "Returning ready segment %p", iqe->seg);
|
---|
| 107 | list_remove(&iqe->link);
|
---|
| 108 | *seg = iqe->seg;
|
---|
| 109 |
|
---|
| 110 | return EOK;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | /**
|
---|
| 114 | * @}
|
---|
| 115 | */
|
---|