source: mainline/uspace/srv/net/udp/udp_inet.c@ 0d520a2

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

Move network-related servers to srv/net.

  • Property mode set to 100644
File size: 3.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 "assoc.h"
46#include "pdu.h"
47#include "std.h"
48#include "udp_inet.h"
49#include "udp_type.h"
50
51static int udp_inet_ev_recv(inet_dgram_t *dgram);
52static void udp_received_pdu(udp_pdu_t *pdu);
53
54static inet_ev_ops_t udp_inet_ev_ops = {
55 .recv = udp_inet_ev_recv
56};
57
58/** Received datagram callback */
59static int udp_inet_ev_recv(inet_dgram_t *dgram)
60{
61 udp_pdu_t *pdu;
62
63 log_msg(LVL_DEBUG, "udp_inet_ev_recv()");
64
65 pdu = udp_pdu_new();
66 pdu->data = dgram->data;
67 pdu->data_size = dgram->size;
68
69 pdu->src.ipv4 = dgram->src.ipv4;
70 pdu->dest.ipv4 = dgram->dest.ipv4;
71 log_msg(LVL_DEBUG, "src: 0x%08x, dest: 0x%08x",
72 pdu->src.ipv4, pdu->dest.ipv4);
73
74 udp_received_pdu(pdu);
75 udp_pdu_delete(pdu);
76
77 return EOK;
78}
79
80/** Transmit PDU over network layer. */
81int udp_transmit_pdu(udp_pdu_t *pdu)
82{
83 int rc;
84 inet_dgram_t dgram;
85
86 log_msg(LVL_DEBUG, "udp_transmit_pdu()");
87
88 dgram.src.ipv4 = pdu->src.ipv4;
89 dgram.dest.ipv4 = pdu->dest.ipv4;
90 dgram.tos = 0;
91 dgram.data = pdu->data;
92 dgram.size = pdu->data_size;
93
94 rc = inet_send(&dgram, INET_TTL_MAX, 0);
95 if (rc != EOK)
96 log_msg(LVL_ERROR, "Failed to transmit PDU.");
97
98 return rc;
99}
100
101/** Process received PDU. */
102static void udp_received_pdu(udp_pdu_t *pdu)
103{
104 udp_msg_t *dmsg;
105 udp_sockpair_t rident;
106
107 log_msg(LVL_DEBUG, "udp_received_pdu()");
108
109 if (udp_pdu_decode(pdu, &rident, &dmsg) != EOK) {
110 log_msg(LVL_WARN, "Not enough memory. PDU dropped.");
111 return;
112 }
113
114 /*
115 * Insert decoded message into appropriate receive queue.
116 * This transfers ownership of dmsg to the callee, we do not
117 * free it.
118 */
119 udp_assoc_received(&rident, dmsg);
120}
121
122int udp_inet_init(void)
123{
124 int rc;
125
126 log_msg(LVL_DEBUG, "udp_inet_init()");
127
128 rc = inet_init(IP_PROTO_UDP, &udp_inet_ev_ops);
129 if (rc != EOK) {
130 log_msg(LVL_ERROR, "Failed connecting to internet service.");
131 return ENOENT;
132 }
133
134 return EOK;
135}
136
137/**
138 * @}
139 */
Note: See TracBrowser for help on using the repository browser.