source: mainline/uspace/srv/inet/addrobj.c@ bd8bfc5a

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

Sketch IP PDU encoding and decoding.
Unify IP packet routing.

  • Property mode set to 100644
File size: 3.7 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 inet
30 * @{
31 */
32/**
33 * @file
34 * @brief
35 */
36
37#include <bitops.h>
38#include <fibril_synch.h>
39#include <io/log.h>
40#include <ipc/loc.h>
41#include <stdlib.h>
42
43#include "addrobj.h"
44#include "inet.h"
45#include "inet_link.h"
46
47static FIBRIL_MUTEX_INITIALIZE(addr_list_lock);
48static LIST_INITIALIZE(addr_list);
49
50static uint32_t inet_netmask(int bits)
51{
52 assert(bits >= 1);
53 assert(bits < 32);
54
55 return BIT_RANGE(uint32_t, 31, 31 - (bits - 1));
56}
57
58inet_addrobj_t *inet_addrobj_new(void)
59{
60 inet_addrobj_t *addr = calloc(1, sizeof(inet_addrobj_t));
61
62 if (addr == NULL) {
63 log_msg(LVL_ERROR, "Failed allocating address object. "
64 "Out of memory.");
65 return NULL;
66 }
67
68 link_initialize(&addr->addr_list);
69
70 return addr;
71}
72
73void inet_addrobj_delete(inet_addrobj_t *addr)
74{
75 free(addr);
76}
77
78void inet_addrobj_add(inet_addrobj_t *addr)
79{
80 fibril_mutex_lock(&addr_list_lock);
81 list_append(&addr->addr_list, &addr_list);
82 fibril_mutex_unlock(&addr_list_lock);
83}
84
85void inet_addrobj_remove(inet_addrobj_t *addr)
86{
87 fibril_mutex_lock(&addr_list_lock);
88 list_remove(&addr->addr_list);
89 fibril_mutex_unlock(&addr_list_lock);
90}
91
92/** Find address object matching address @a addr.
93 *
94 * @param addr Address
95 * @oaram find iaf_net to find network (using mask),
96 * iaf_addr to find local address (exact match)
97 */
98inet_addrobj_t *inet_addrobj_find(inet_addr_t *addr, inet_addrobj_find_t find)
99{
100 uint32_t mask;
101
102 log_msg(LVL_DEBUG, "inet_addrobj_find(%x)", (unsigned)addr->ipv4);
103
104 fibril_mutex_lock(&addr_list_lock);
105
106 list_foreach(addr_list, link) {
107 inet_addrobj_t *naddr = list_get_instance(link,
108 inet_addrobj_t, addr_list);
109
110 mask = inet_netmask(naddr->naddr.bits);
111 if ((naddr->naddr.ipv4 & mask) == (addr->ipv4 & mask)) {
112 fibril_mutex_unlock(&addr_list_lock);
113 log_msg(LVL_DEBUG, "inet_addrobj_find: found %p",
114 addr);
115 return naddr;
116 }
117 }
118
119 log_msg(LVL_DEBUG, "inet_addrobj_find: Not found");
120 fibril_mutex_unlock(&addr_list_lock);
121
122 return NULL;
123}
124
125/** Send datagram to directly reachable destination */
126int inet_addrobj_send_dgram(inet_addrobj_t *addr, inet_dgram_t *dgram,
127 uint8_t ttl, int df)
128{
129 inet_addr_t lsrc_addr;
130 inet_addr_t *ldest_addr;
131
132 lsrc_addr.ipv4 = addr->naddr.ipv4;
133 ldest_addr = &dgram->dest;
134
135 return inet_link_send_dgram(addr->ilink, &lsrc_addr, ldest_addr, dgram,
136 ttl, df);
137}
138
139/** @}
140 */
Note: See TracBrowser for help on using the repository browser.