[66a272f8] | 1 | /*
|
---|
[2f19103] | 2 | * Copyright (c) 2015 Jiri Svoboda
|
---|
[66a272f8] | 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 udp
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * @file
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <bitops.h>
|
---|
| 38 | #include <byteorder.h>
|
---|
| 39 | #include <errno.h>
|
---|
| 40 | #include <inet/inet.h>
|
---|
| 41 | #include <io/log.h>
|
---|
| 42 | #include <stdio.h>
|
---|
| 43 | #include <task.h>
|
---|
| 44 |
|
---|
[92b42442] | 45 | #include "assoc.h"
|
---|
| 46 | #include "pdu.h"
|
---|
[66a272f8] | 47 | #include "std.h"
|
---|
| 48 | #include "udp_inet.h"
|
---|
[ee603c4] | 49 | #include "udp_type.h"
|
---|
[66a272f8] | 50 |
|
---|
| 51 | static int udp_inet_ev_recv(inet_dgram_t *dgram);
|
---|
[92b42442] | 52 | static void udp_received_pdu(udp_pdu_t *pdu);
|
---|
[66a272f8] | 53 |
|
---|
| 54 | static inet_ev_ops_t udp_inet_ev_ops = {
|
---|
| 55 | .recv = udp_inet_ev_recv
|
---|
| 56 | };
|
---|
| 57 |
|
---|
| 58 | /** Received datagram callback */
|
---|
| 59 | static int udp_inet_ev_recv(inet_dgram_t *dgram)
|
---|
| 60 | {
|
---|
[92b42442] | 61 | udp_pdu_t *pdu;
|
---|
[66a272f8] | 62 |
|
---|
[a1a101d] | 63 | log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_inet_ev_recv()");
|
---|
[66a272f8] | 64 |
|
---|
[8d48c7e] | 65 | log_msg(LOG_DEFAULT, LVL_NOTE, "udp_inet_ev_recv: link=%zu",
|
---|
| 66 | dgram->iplink);
|
---|
| 67 |
|
---|
[92b42442] | 68 | pdu = udp_pdu_new();
|
---|
[8d48c7e] | 69 | pdu->iplink = dgram->iplink;
|
---|
[92b42442] | 70 | pdu->data = dgram->data;
|
---|
| 71 | pdu->data_size = dgram->size;
|
---|
[a2e3ee6] | 72 |
|
---|
| 73 | pdu->src = dgram->src;
|
---|
| 74 | pdu->dest = dgram->dest;
|
---|
[92b42442] | 75 |
|
---|
| 76 | udp_received_pdu(pdu);
|
---|
[f1a8c23] | 77 |
|
---|
| 78 | /* We don't want udp_pdu_delete() to free dgram->data */
|
---|
| 79 | pdu->data = NULL;
|
---|
[92b42442] | 80 | udp_pdu_delete(pdu);
|
---|
[66a272f8] | 81 |
|
---|
| 82 | return EOK;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | /** Transmit PDU over network layer. */
|
---|
[ee603c4] | 86 | int udp_transmit_pdu(udp_pdu_t *pdu)
|
---|
[66a272f8] | 87 | {
|
---|
| 88 | int rc;
|
---|
| 89 | inet_dgram_t dgram;
|
---|
| 90 |
|
---|
[a1a101d] | 91 | log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_transmit_pdu()");
|
---|
[92b42442] | 92 |
|
---|
[695b6ff] | 93 | dgram.iplink = pdu->iplink;
|
---|
[a2e3ee6] | 94 | dgram.src = pdu->src;
|
---|
| 95 | dgram.dest = pdu->dest;
|
---|
[66a272f8] | 96 | dgram.tos = 0;
|
---|
[ee603c4] | 97 | dgram.data = pdu->data;
|
---|
| 98 | dgram.size = pdu->data_size;
|
---|
[66a272f8] | 99 |
|
---|
| 100 | rc = inet_send(&dgram, INET_TTL_MAX, 0);
|
---|
| 101 | if (rc != EOK)
|
---|
[a1a101d] | 102 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed to transmit PDU.");
|
---|
[ee603c4] | 103 |
|
---|
| 104 | return rc;
|
---|
[66a272f8] | 105 | }
|
---|
[ee603c4] | 106 |
|
---|
[66a272f8] | 107 | /** Process received PDU. */
|
---|
[92b42442] | 108 | static void udp_received_pdu(udp_pdu_t *pdu)
|
---|
[66a272f8] | 109 | {
|
---|
[92b42442] | 110 | udp_msg_t *dmsg;
|
---|
[2f19103] | 111 | inet_ep2_t rident;
|
---|
[66a272f8] | 112 |
|
---|
[a1a101d] | 113 | log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_received_pdu()");
|
---|
[66a272f8] | 114 |
|
---|
[92b42442] | 115 | if (udp_pdu_decode(pdu, &rident, &dmsg) != EOK) {
|
---|
[a1a101d] | 116 | log_msg(LOG_DEFAULT, LVL_WARN, "Not enough memory. PDU dropped.");
|
---|
[66a272f8] | 117 | return;
|
---|
| 118 | }
|
---|
[92b42442] | 119 |
|
---|
| 120 | /*
|
---|
| 121 | * Insert decoded message into appropriate receive queue.
|
---|
| 122 | * This transfers ownership of dmsg to the callee, we do not
|
---|
| 123 | * free it.
|
---|
| 124 | */
|
---|
| 125 | udp_assoc_received(&rident, dmsg);
|
---|
[66a272f8] | 126 | }
|
---|
[92b42442] | 127 |
|
---|
[66a272f8] | 128 | int udp_inet_init(void)
|
---|
| 129 | {
|
---|
| 130 | int rc;
|
---|
| 131 |
|
---|
[a1a101d] | 132 | log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_inet_init()");
|
---|
[66a272f8] | 133 |
|
---|
| 134 | rc = inet_init(IP_PROTO_UDP, &udp_inet_ev_ops);
|
---|
| 135 | if (rc != EOK) {
|
---|
[a1a101d] | 136 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed connecting to internet service.");
|
---|
[66a272f8] | 137 | return ENOENT;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | return EOK;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | /**
|
---|
| 144 | * @}
|
---|
| 145 | */
|
---|