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