| 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 "inet_link.h"
|
|---|
| 46 |
|
|---|
| 47 | static int inet_link_open(service_id_t sid);
|
|---|
| 48 | static int inet_iplink_recv(iplink_t *ilink, iplink_sdu_t *sdu);
|
|---|
| 49 |
|
|---|
| 50 | typedef struct {
|
|---|
| 51 | link_t link_list;
|
|---|
| 52 | service_id_t svc_id;
|
|---|
| 53 | char *svc_name;
|
|---|
| 54 | async_sess_t *sess;
|
|---|
| 55 | iplink_t *iplink;
|
|---|
| 56 | } inet_link_t;
|
|---|
| 57 |
|
|---|
| 58 | static iplink_ev_ops_t inet_iplink_ev_ops = {
|
|---|
| 59 | .recv = inet_iplink_recv
|
|---|
| 60 | };
|
|---|
| 61 |
|
|---|
| 62 | static LIST_INITIALIZE(inet_link_list);
|
|---|
| 63 | static FIBRIL_MUTEX_INITIALIZE(inet_discovery_lock);
|
|---|
| 64 |
|
|---|
| 65 | static int inet_iplink_recv(iplink_t *ilink, iplink_sdu_t *sdu)
|
|---|
| 66 | {
|
|---|
| 67 | log_msg(LVL_DEBUG, "inet_iplink_recv()");
|
|---|
| 68 | return EOK;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | static int inet_link_check_new(void)
|
|---|
| 72 | {
|
|---|
| 73 | bool already_known;
|
|---|
| 74 | category_id_t iplink_cat;
|
|---|
| 75 | service_id_t *svcs;
|
|---|
| 76 | size_t count, i;
|
|---|
| 77 | int rc;
|
|---|
| 78 |
|
|---|
| 79 | fibril_mutex_lock(&inet_discovery_lock);
|
|---|
| 80 |
|
|---|
| 81 | rc = loc_category_get_id("iplink", &iplink_cat, IPC_FLAG_BLOCKING);
|
|---|
| 82 | if (rc != EOK) {
|
|---|
| 83 | log_msg(LVL_ERROR, "Failed resolving category 'iplink'.");
|
|---|
| 84 | fibril_mutex_unlock(&inet_discovery_lock);
|
|---|
| 85 | return ENOENT;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | rc = loc_category_get_svcs(iplink_cat, &svcs, &count);
|
|---|
| 89 | if (rc != EOK) {
|
|---|
| 90 | log_msg(LVL_ERROR, "Failed getting list of IP links.");
|
|---|
| 91 | fibril_mutex_unlock(&inet_discovery_lock);
|
|---|
| 92 | return EIO;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | for (i = 0; i < count; i++) {
|
|---|
| 96 | already_known = false;
|
|---|
| 97 |
|
|---|
| 98 | list_foreach(inet_link_list, ilink_link) {
|
|---|
| 99 | inet_link_t *ilink = list_get_instance(ilink_link,
|
|---|
| 100 | inet_link_t, link_list);
|
|---|
| 101 | if (ilink->svc_id == svcs[i]) {
|
|---|
| 102 | already_known = true;
|
|---|
| 103 | break;
|
|---|
| 104 | }
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | if (!already_known) {
|
|---|
| 108 | log_msg(LVL_DEBUG, "Found IP link '%lu'",
|
|---|
| 109 | (unsigned long) svcs[i]);
|
|---|
| 110 | rc = inet_link_open(svcs[i]);
|
|---|
| 111 | if (rc != EOK)
|
|---|
| 112 | log_msg(LVL_ERROR, "Could not open IP link.");
|
|---|
| 113 | }
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | fibril_mutex_unlock(&inet_discovery_lock);
|
|---|
| 117 | return EOK;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | static inet_link_t *inet_link_new(void)
|
|---|
| 121 | {
|
|---|
| 122 | inet_link_t *ilink = calloc(1, sizeof(inet_link_t));
|
|---|
| 123 |
|
|---|
| 124 | if (ilink == NULL) {
|
|---|
| 125 | log_msg(LVL_ERROR, "Failed allocating link structure. "
|
|---|
| 126 | "Out of memory.");
|
|---|
| 127 | return NULL;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | link_initialize(&ilink->link_list);
|
|---|
| 131 |
|
|---|
| 132 | return ilink;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | static void inet_link_delete(inet_link_t *ilink)
|
|---|
| 136 | {
|
|---|
| 137 | if (ilink->svc_name != NULL)
|
|---|
| 138 | free(ilink->svc_name);
|
|---|
| 139 | free(ilink);
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | static int inet_link_open(service_id_t sid)
|
|---|
| 143 | {
|
|---|
| 144 | inet_link_t *ilink;
|
|---|
| 145 | int rc;
|
|---|
| 146 |
|
|---|
| 147 | log_msg(LVL_DEBUG, "inet_link_open()");
|
|---|
| 148 | ilink = inet_link_new();
|
|---|
| 149 | if (ilink == NULL)
|
|---|
| 150 | return ENOMEM;
|
|---|
| 151 |
|
|---|
| 152 | rc = loc_service_get_name(sid, &ilink->svc_name);
|
|---|
| 153 | if (rc != EOK) {
|
|---|
| 154 | log_msg(LVL_ERROR, "Failed getting service name.");
|
|---|
| 155 | goto error;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | ilink->sess = loc_service_connect(EXCHANGE_SERIALIZE, sid, 0);
|
|---|
| 159 | if (ilink->sess == NULL) {
|
|---|
| 160 | log_msg(LVL_ERROR, "Failed connecting '%s'", ilink->svc_name);
|
|---|
| 161 | goto error;
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | rc = iplink_open(ilink->sess, &inet_iplink_ev_ops, &ilink->iplink);
|
|---|
| 165 | if (rc != EOK) {
|
|---|
| 166 | log_msg(LVL_ERROR, "Failed opening IP link '%s'",
|
|---|
| 167 | ilink->svc_name);
|
|---|
| 168 | goto error;
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | log_msg(LVL_DEBUG, "Opened IP link '%s'", ilink->svc_name);
|
|---|
| 172 | list_append(&ilink->link_list, &inet_link_list);
|
|---|
| 173 |
|
|---|
| 174 | return EOK;
|
|---|
| 175 |
|
|---|
| 176 | error:
|
|---|
| 177 | inet_link_delete(ilink);
|
|---|
| 178 | return rc;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | static void inet_link_cat_change_cb(void)
|
|---|
| 182 | {
|
|---|
| 183 | (void) inet_link_check_new();
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | int inet_link_discovery_start(void)
|
|---|
| 187 | {
|
|---|
| 188 | int rc;
|
|---|
| 189 |
|
|---|
| 190 | rc = loc_register_cat_change_cb(inet_link_cat_change_cb);
|
|---|
| 191 | if (rc != EOK) {
|
|---|
| 192 | log_msg(LVL_ERROR, "Failed registering callback for IP link "
|
|---|
| 193 | "discovery (%d).", rc);
|
|---|
| 194 | return rc;
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | return inet_link_check_new();
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | /** @}
|
|---|
| 201 | */
|
|---|