source: mainline/uspace/srv/udp/udp_inet.c@ ee603c4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ee603c4 was ee603c4, checked in by Jiri Svoboda <jiri@…>, 14 years ago

UDP associations, sending datagrams.

  • Property mode set to 100644
File size: 4.4 KB
Line 
1/*
2 * Copyright (c) 2012 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 <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
45#include "std.h"
46#include "udp_inet.h"
47#include "udp_type.h"
48
49static int udp_inet_ev_recv(inet_dgram_t *dgram);
50//static void tcp_received_pdu(tcp_pdu_t *pdu);
51
52static inet_ev_ops_t udp_inet_ev_ops = {
53 .recv = udp_inet_ev_recv
54};
55
56/** Received datagram callback */
57static int udp_inet_ev_recv(inet_dgram_t *dgram)
58{
59 uint8_t *pdu_raw;
60 size_t pdu_raw_size;
61
62 log_msg(LVL_DEBUG, "udp_inet_ev_recv()");
63
64 pdu_raw = dgram->data;
65 pdu_raw_size = dgram->size;
66
67 (void)pdu_raw;
68 (void)pdu_raw_size;
69 /* Split into header and payload. */
70/*
71 log_msg(LVL_DEBUG, "tcp_inet_ev_recv() - split header/payload");
72
73 tcp_pdu_t *pdu;
74 size_t hdr_size;
75 tcp_header_t *hdr;
76 uint32_t data_offset;
77
78 if (pdu_raw_size < sizeof(tcp_header_t)) {
79 log_msg(LVL_WARN, "pdu_raw_size = %zu < sizeof(tcp_header_t) = %zu",
80 pdu_raw_size, sizeof(tcp_header_t));
81 return EINVAL;
82 }
83
84 hdr = (tcp_header_t *)pdu_raw;
85 data_offset = BIT_RANGE_EXTRACT(uint32_t, DF_DATA_OFFSET_h, DF_DATA_OFFSET_l,
86 uint16_t_be2host(hdr->doff_flags));
87
88 hdr_size = sizeof(uint32_t) * data_offset;
89
90 if (pdu_raw_size < hdr_size) {
91 log_msg(LVL_WARN, "pdu_raw_size = %zu < hdr_size = %zu",
92 pdu_raw_size, hdr_size);
93 return EINVAL;
94 }
95
96 if (hdr_size < sizeof(tcp_header_t)) {
97 log_msg(LVL_WARN, "hdr_size = %zu < sizeof(tcp_header_t) = %zu",
98 hdr_size, sizeof(tcp_header_t)); return EINVAL;
99 }
100
101 log_msg(LVL_DEBUG, "pdu_raw_size=%zu, hdr_size=%zu",
102 pdu_raw_size, hdr_size);
103 pdu = tcp_pdu_create(pdu_raw, hdr_size, pdu_raw + hdr_size,
104 pdu_raw_size - hdr_size);
105 if (pdu == NULL) {
106 log_msg(LVL_WARN, "Failed creating PDU. Dropped.");
107 return ENOMEM;
108 }
109
110 pdu->src_addr.ipv4 = dgram->src.ipv4;
111 pdu->dest_addr.ipv4 = dgram->dest.ipv4;
112 log_msg(LVL_DEBUG, "src: 0x%08x, dest: 0x%08x",
113 pdu->src_addr.ipv4, pdu->dest_addr.ipv4);
114
115 tcp_received_pdu(pdu);
116 tcp_pdu_delete(pdu);
117*/
118 return EOK;
119}
120
121/** Transmit PDU over network layer. */
122int udp_transmit_pdu(udp_pdu_t *pdu)
123{
124 int rc;
125 inet_dgram_t dgram;
126
127 dgram.src.ipv4 = pdu->src.ipv4;
128 dgram.dest.ipv4 = pdu->dest.ipv4;
129 dgram.tos = 0;
130 dgram.data = pdu->data;
131 dgram.size = pdu->data_size;
132
133 rc = inet_send(&dgram, INET_TTL_MAX, 0);
134 if (rc != EOK)
135 log_msg(LVL_ERROR, "Failed to transmit PDU.");
136
137 return rc;
138}
139
140/** Process received PDU. */
141/*
142static void tcp_received_pdu(tcp_pdu_t *pdu)
143{
144 tcp_segment_t *dseg;
145 tcp_sockpair_t rident;
146
147 log_msg(LVL_DEBUG, "tcp_received_pdu()");
148
149 if (tcp_pdu_decode(pdu, &rident, &dseg) != EOK) {
150 log_msg(LVL_WARN, "Not enough memory. PDU dropped.");
151 return;
152 }
153*/
154 /* Insert decoded segment into rqueue */
155/* tcp_rqueue_insert_seg(&rident, dseg);
156}
157*/
158int udp_inet_init(void)
159{
160 int rc;
161
162 log_msg(LVL_DEBUG, "udp_inet_init()");
163
164 rc = inet_init(IP_PROTO_UDP, &udp_inet_ev_ops);
165 if (rc != EOK) {
166 log_msg(LVL_ERROR, "Failed connecting to internet service.");
167 return ENOENT;
168 }
169
170 return EOK;
171}
172
173/**
174 * @}
175 */
Note: See TracBrowser for help on using the repository browser.