source: mainline/uspace/srv/net/udp/udp_inet.c

Last change on this file was 89ba88c, checked in by Jiri Svoboda <jiri@…>, 6 years ago

Test udp_assoc_send()

This required virtualizing inet_get_src_addr() / udp_transmit_msg for the
association module

  • Property mode set to 100644
File size: 4.2 KB
Line 
1/*
2 * Copyright (c) 2015 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 udp
30 * @{
31 */
32
33/**
34 * @file
35 */
36
37#include <errno.h>
38#include <inet/inet.h>
39#include <io/log.h>
40
41#include "assoc.h"
42#include "pdu.h"
43#include "std.h"
44#include "udp_inet.h"
45#include "udp_type.h"
46
47static errno_t udp_inet_ev_recv(inet_dgram_t *dgram);
48static void udp_received_pdu(udp_pdu_t *pdu);
49
50static inet_ev_ops_t udp_inet_ev_ops = {
51 .recv = udp_inet_ev_recv
52};
53
54/** Received datagram callback */
55static errno_t udp_inet_ev_recv(inet_dgram_t *dgram)
56{
57 udp_pdu_t *pdu;
58
59 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_inet_ev_recv()");
60
61 pdu = udp_pdu_new();
62 pdu->iplink = dgram->iplink;
63 pdu->data = dgram->data;
64 pdu->data_size = dgram->size;
65
66 pdu->src = dgram->src;
67 pdu->dest = dgram->dest;
68
69 udp_received_pdu(pdu);
70
71 /* We don't want udp_pdu_delete() to free dgram->data */
72 pdu->data = NULL;
73 udp_pdu_delete(pdu);
74
75 return EOK;
76}
77
78/** Transmit PDU over network layer. */
79errno_t udp_transmit_pdu(udp_pdu_t *pdu)
80{
81 errno_t rc;
82 inet_dgram_t dgram;
83
84 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_transmit_pdu()");
85
86 dgram.iplink = pdu->iplink;
87 dgram.src = pdu->src;
88 dgram.dest = pdu->dest;
89 dgram.tos = 0;
90 dgram.data = pdu->data;
91 dgram.size = pdu->data_size;
92
93 rc = inet_send(&dgram, INET_TTL_MAX, 0);
94 if (rc != EOK)
95 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed to transmit PDU.");
96
97 return rc;
98}
99
100/** Process received PDU. */
101static void udp_received_pdu(udp_pdu_t *pdu)
102{
103 udp_msg_t *dmsg;
104 inet_ep2_t rident;
105
106 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_received_pdu()");
107
108 if (udp_pdu_decode(pdu, &rident, &dmsg) != EOK) {
109 log_msg(LOG_DEFAULT, LVL_WARN, "Not enough memory. PDU dropped.");
110 return;
111 }
112
113 /*
114 * Insert decoded message into appropriate receive queue.
115 * This transfers ownership of dmsg to the callee, we do not
116 * free it.
117 */
118 udp_assoc_received(&rident, dmsg);
119}
120
121errno_t udp_inet_init(void)
122{
123 errno_t rc;
124
125 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_inet_init()");
126
127 rc = inet_init(IP_PROTO_UDP, &udp_inet_ev_ops);
128 if (rc != EOK) {
129 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed connecting to internet service.");
130 return ENOENT;
131 }
132
133 return EOK;
134}
135
136/** Get source address.
137 *
138 * @param remote Remote address
139 * @param tos Type of service
140 * @param local Place to store local address
141 * @return EOK on success or an error code
142 */
143errno_t udp_get_srcaddr(inet_addr_t *remote, uint8_t tos, inet_addr_t *local)
144{
145 return inet_get_srcaddr(remote, tos, local);
146}
147
148/** Transmit message over network layer. */
149errno_t udp_transmit_msg(inet_ep2_t *epp, udp_msg_t *msg)
150{
151 udp_pdu_t *pdu;
152 errno_t rc;
153
154 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_transmit_msg()");
155
156 rc = udp_pdu_encode(epp, msg, &pdu);
157 if (rc != EOK) {
158 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed encoding PDU");
159 return rc;
160 }
161
162 rc = udp_transmit_pdu(pdu);
163 udp_pdu_delete(pdu);
164
165 return rc;
166}
167
168/**
169 * @}
170 */
Note: See TracBrowser for help on using the repository browser.