| 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 <bool.h>
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 | #include <fibril_synch.h>
|
|---|
| 40 | #include <inet/iplink.h>
|
|---|
| 41 | #include <io/log.h>
|
|---|
| 42 | #include <loc.h>
|
|---|
| 43 | #include <stdlib.h>
|
|---|
| 44 |
|
|---|
| 45 | #include "addrobj.h"
|
|---|
| 46 | #include "inet.h"
|
|---|
| 47 | #include "inet_link.h"
|
|---|
| 48 | #include "pdu.h"
|
|---|
| 49 |
|
|---|
| 50 | static int inet_link_open(service_id_t sid);
|
|---|
| 51 | static int inet_iplink_recv(iplink_t *ilink, iplink_sdu_t *sdu);
|
|---|
| 52 |
|
|---|
| 53 | static iplink_ev_ops_t inet_iplink_ev_ops = {
|
|---|
| 54 | .recv = inet_iplink_recv
|
|---|
| 55 | };
|
|---|
| 56 |
|
|---|
| 57 | static LIST_INITIALIZE(inet_link_list);
|
|---|
| 58 | static FIBRIL_MUTEX_INITIALIZE(inet_discovery_lock);
|
|---|
| 59 |
|
|---|
| 60 | static int inet_iplink_recv(iplink_t *iplink, iplink_sdu_t *sdu)
|
|---|
| 61 | {
|
|---|
| 62 | inet_dgram_t dgram;
|
|---|
| 63 | uint8_t ttl;
|
|---|
| 64 | int df;
|
|---|
| 65 | int rc;
|
|---|
| 66 |
|
|---|
| 67 | log_msg(LVL_DEBUG, "inet_iplink_recv()");
|
|---|
| 68 | rc = inet_pdu_decode(sdu->data, sdu->size, &dgram, &ttl, &df);
|
|---|
| 69 | if (rc != EOK)
|
|---|
| 70 | return rc;
|
|---|
| 71 |
|
|---|
| 72 | return inet_recv_packet(&dgram, ttl, df);
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | static int inet_link_check_new(void)
|
|---|
| 76 | {
|
|---|
| 77 | bool already_known;
|
|---|
| 78 | category_id_t iplink_cat;
|
|---|
| 79 | service_id_t *svcs;
|
|---|
| 80 | size_t count, i;
|
|---|
| 81 | int rc;
|
|---|
| 82 |
|
|---|
| 83 | fibril_mutex_lock(&inet_discovery_lock);
|
|---|
| 84 |
|
|---|
| 85 | rc = loc_category_get_id("iplink", &iplink_cat, IPC_FLAG_BLOCKING);
|
|---|
| 86 | if (rc != EOK) {
|
|---|
| 87 | log_msg(LVL_ERROR, "Failed resolving category 'iplink'.");
|
|---|
| 88 | fibril_mutex_unlock(&inet_discovery_lock);
|
|---|
| 89 | return ENOENT;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | rc = loc_category_get_svcs(iplink_cat, &svcs, &count);
|
|---|
| 93 | if (rc != EOK) {
|
|---|
| 94 | log_msg(LVL_ERROR, "Failed getting list of IP links.");
|
|---|
| 95 | fibril_mutex_unlock(&inet_discovery_lock);
|
|---|
| 96 | return EIO;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | for (i = 0; i < count; i++) {
|
|---|
| 100 | already_known = false;
|
|---|
| 101 |
|
|---|
| 102 | list_foreach(inet_link_list, ilink_link) {
|
|---|
| 103 | inet_link_t *ilink = list_get_instance(ilink_link,
|
|---|
| 104 | inet_link_t, link_list);
|
|---|
| 105 | if (ilink->svc_id == svcs[i]) {
|
|---|
| 106 | already_known = true;
|
|---|
| 107 | break;
|
|---|
| 108 | }
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | if (!already_known) {
|
|---|
| 112 | log_msg(LVL_DEBUG, "Found IP link '%lu'",
|
|---|
| 113 | (unsigned long) svcs[i]);
|
|---|
| 114 | rc = inet_link_open(svcs[i]);
|
|---|
| 115 | if (rc != EOK)
|
|---|
| 116 | log_msg(LVL_ERROR, "Could not open IP link.");
|
|---|
| 117 | }
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | fibril_mutex_unlock(&inet_discovery_lock);
|
|---|
| 121 | return EOK;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | static inet_link_t *inet_link_new(void)
|
|---|
| 125 | {
|
|---|
| 126 | inet_link_t *ilink = calloc(1, sizeof(inet_link_t));
|
|---|
| 127 |
|
|---|
| 128 | if (ilink == NULL) {
|
|---|
| 129 | log_msg(LVL_ERROR, "Failed allocating link structure. "
|
|---|
| 130 | "Out of memory.");
|
|---|
| 131 | return NULL;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | link_initialize(&ilink->link_list);
|
|---|
| 135 |
|
|---|
| 136 | return ilink;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | static void inet_link_delete(inet_link_t *ilink)
|
|---|
| 140 | {
|
|---|
| 141 | if (ilink->svc_name != NULL)
|
|---|
| 142 | free(ilink->svc_name);
|
|---|
| 143 | free(ilink);
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | static int inet_link_open(service_id_t sid)
|
|---|
| 147 | {
|
|---|
| 148 | inet_link_t *ilink;
|
|---|
| 149 | int rc;
|
|---|
| 150 |
|
|---|
| 151 | log_msg(LVL_DEBUG, "inet_link_open()");
|
|---|
| 152 | ilink = inet_link_new();
|
|---|
| 153 | if (ilink == NULL)
|
|---|
| 154 | return ENOMEM;
|
|---|
| 155 |
|
|---|
| 156 | rc = loc_service_get_name(sid, &ilink->svc_name);
|
|---|
| 157 | if (rc != EOK) {
|
|---|
| 158 | log_msg(LVL_ERROR, "Failed getting service name.");
|
|---|
| 159 | goto error;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | ilink->sess = loc_service_connect(EXCHANGE_SERIALIZE, sid, 0);
|
|---|
| 163 | if (ilink->sess == NULL) {
|
|---|
| 164 | log_msg(LVL_ERROR, "Failed connecting '%s'", ilink->svc_name);
|
|---|
| 165 | goto error;
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | rc = iplink_open(ilink->sess, &inet_iplink_ev_ops, &ilink->iplink);
|
|---|
| 169 | if (rc != EOK) {
|
|---|
| 170 | log_msg(LVL_ERROR, "Failed opening IP link '%s'",
|
|---|
| 171 | ilink->svc_name);
|
|---|
| 172 | goto error;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | log_msg(LVL_DEBUG, "Opened IP link '%s'", ilink->svc_name);
|
|---|
| 176 | list_append(&ilink->link_list, &inet_link_list);
|
|---|
| 177 |
|
|---|
| 178 | inet_addrobj_t *addr;
|
|---|
| 179 |
|
|---|
| 180 | /* XXX For testing: set static IP address 192.168.0.4/24 */
|
|---|
| 181 | addr = inet_addrobj_new();
|
|---|
| 182 | addr->naddr.ipv4 = (192 << 24) + (168 << 16) + (0 << 8) + 4;
|
|---|
| 183 | addr->naddr.bits = 24;
|
|---|
| 184 | addr->ilink = ilink;
|
|---|
| 185 | inet_addrobj_add(addr);
|
|---|
| 186 |
|
|---|
| 187 | return EOK;
|
|---|
| 188 |
|
|---|
| 189 | error:
|
|---|
| 190 | inet_link_delete(ilink);
|
|---|
| 191 | return rc;
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | static void inet_link_cat_change_cb(void)
|
|---|
| 195 | {
|
|---|
| 196 | (void) inet_link_check_new();
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | int inet_link_discovery_start(void)
|
|---|
| 200 | {
|
|---|
| 201 | int rc;
|
|---|
| 202 |
|
|---|
| 203 | rc = loc_register_cat_change_cb(inet_link_cat_change_cb);
|
|---|
| 204 | if (rc != EOK) {
|
|---|
| 205 | log_msg(LVL_ERROR, "Failed registering callback for IP link "
|
|---|
| 206 | "discovery (%d).", rc);
|
|---|
| 207 | return rc;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | return inet_link_check_new();
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | /** Send datagram over Internet link */
|
|---|
| 214 | int inet_link_send_dgram(inet_link_t *ilink, inet_addr_t *lsrc,
|
|---|
| 215 | inet_addr_t *ldest, inet_dgram_t *dgram, uint8_t ttl, int df)
|
|---|
| 216 | {
|
|---|
| 217 | iplink_sdu_t sdu;
|
|---|
| 218 | int rc;
|
|---|
| 219 |
|
|---|
| 220 | sdu.lsrc.ipv4 = lsrc->ipv4;
|
|---|
| 221 | sdu.ldest.ipv4 = ldest->ipv4;
|
|---|
| 222 | rc = inet_pdu_encode(dgram, ttl, df, &sdu.data, &sdu.size);
|
|---|
| 223 | if (rc != EOK)
|
|---|
| 224 | return rc;
|
|---|
| 225 |
|
|---|
| 226 | rc = iplink_send(ilink->iplink, &sdu);
|
|---|
| 227 | free(sdu.data);
|
|---|
| 228 |
|
|---|
| 229 | return rc;
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 | /** @}
|
|---|
| 234 | */
|
|---|