source: mainline/uspace/srv/udp/udp_inet.c@ 66a272f8

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

Add UDP skeleton.

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