Changeset 8bf672d in mainline for uspace/srv


Ignore:
Timestamp:
2012-03-30T17:42:11Z (14 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
66a272f8
Parents:
3b3c689
Message:

Static route configuration.

Location:
uspace/srv/inet
Files:
4 added
6 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/inet/Makefile

    r3b3c689 r8bf672d  
    3535        inet.c \
    3636        inet_link.c \
     37        inet_util.c \
    3738        inetcfg.c \
    3839        inetping.c \
    39         pdu.c
     40        pdu.c \
     41        sroute.c
    4042
    4143include $(USPACE_PREFIX)/Makefile.common
  • uspace/srv/inet/addrobj.c

    r3b3c689 r8bf672d  
    4646#include "inet.h"
    4747#include "inet_link.h"
     48#include "inet_util.h"
    4849
    4950static FIBRIL_MUTEX_INITIALIZE(addr_list_lock);
    5051static LIST_INITIALIZE(addr_list);
    5152static sysarg_t addr_id = 0;
    52 
    53 static uint32_t inet_netmask(int bits)
    54 {
    55         assert(bits >= 1);
    56         assert(bits < 32);
    57 
    58         return BIT_RANGE(uint32_t, 31, 31 - (bits - 1));
    59 }
    6053
    6154inet_addrobj_t *inet_addrobj_new(void)
     
    162155}
    163156
    164 /** Find address object matching address @a addr.
     157/** Find address object with the given ID.
    165158 *
    166159 * @param id    Address object ID
     
    188181}
    189182
    190 /** Send datagram to directly reachable destination */
    191 int inet_addrobj_send_dgram(inet_addrobj_t *addr, inet_dgram_t *dgram,
    192     uint8_t proto, uint8_t ttl, int df)
     183/** Send datagram from address object */
     184int inet_addrobj_send_dgram(inet_addrobj_t *addr, inet_addr_t *ldest,
     185    inet_dgram_t *dgram, uint8_t proto, uint8_t ttl, int df)
    193186{
    194187        inet_addr_t lsrc_addr;
  • uspace/srv/inet/addrobj.h

    r3b3c689 r8bf672d  
    5555extern inet_addrobj_t *inet_addrobj_find_by_name(const char *, inet_link_t *);
    5656extern inet_addrobj_t *inet_addrobj_get_by_id(sysarg_t);
    57 extern int inet_addrobj_send_dgram(inet_addrobj_t *, inet_dgram_t *,
    58     uint8_t, uint8_t, int);
     57extern int inet_addrobj_send_dgram(inet_addrobj_t *, inet_addr_t *,
     58    inet_dgram_t *, uint8_t, uint8_t, int);
    5959extern int inet_addrobj_get_id_list(sysarg_t **, size_t *);
    6060
  • uspace/srv/inet/inet.c

    r3b3c689 r8bf672d  
    5454#include "inetping.h"
    5555#include "inet_link.h"
     56#include "sroute.h"
    5657
    5758#define NAME "inet"
     
    120121}
    121122
     123static int inet_find_dir(inet_addr_t *src, inet_addr_t *dest, uint8_t tos,
     124    inet_dir_t *dir)
     125{
     126        inet_sroute_t *sr;
     127
     128        /* XXX Handle case where source address is specified */
     129        (void) src;
     130
     131        dir->aobj = inet_addrobj_find(dest, iaf_net);
     132        if (dir->aobj != NULL) {
     133                dir->ldest = *dest;
     134                dir->dtype = dt_direct;
     135        } else {
     136                /* No direct path, try using a static route */
     137                sr = inet_sroute_find(dest);
     138                if (sr != NULL) {
     139                        dir->aobj = inet_addrobj_find(&sr->router, iaf_net);
     140                        dir->ldest = sr->router;
     141                        dir->dtype = dt_router;
     142                }
     143        }
     144
     145        if (dir->aobj == NULL) {
     146                log_msg(LVL_DEBUG, "inet_send: No route to destination.");
     147                return ENOENT;
     148        }
     149
     150        return EOK;
     151}
     152
    122153int inet_route_packet(inet_dgram_t *dgram, uint8_t proto, uint8_t ttl,
    123154    int df)
    124155{
    125         inet_addrobj_t *addr;
    126 
    127         addr = inet_addrobj_find(&dgram->dest, iaf_net);
    128         if (addr != NULL) {
    129                 /* Destination is directly accessible */
    130                 return inet_addrobj_send_dgram(addr, dgram, proto, ttl, df);
    131         }
    132 
    133         /* TODO: Gateways */
    134         log_msg(LVL_DEBUG, "inet_send: No route to destination.");
    135         return ENOENT;
     156        inet_dir_t dir;
     157        int rc;
     158
     159        rc = inet_find_dir(&dgram->src, &dgram->dest, dgram->tos, &dir);
     160        if (rc != EOK)
     161                return rc;
     162
     163        return inet_addrobj_send_dgram(dir.aobj, &dir.ldest, dgram,
     164            proto, ttl, df);
    136165}
    137166
     
    144173int inet_get_srcaddr(inet_addr_t *remote, uint8_t tos, inet_addr_t *local)
    145174{
    146         inet_addrobj_t *addr;
    147 
    148         addr = inet_addrobj_find(remote, iaf_net);
    149         if (addr != NULL) {
    150                 /* Destination is directly accessible */
    151                 local->ipv4 = addr->naddr.ipv4;
    152                 return EOK;
    153         }
    154 
    155         return ENOENT;
     175        inet_dir_t dir;
     176        int rc;
     177
     178        rc = inet_find_dir(NULL, remote, tos, &dir);
     179        if (rc != EOK)
     180                return rc;
     181
     182        /* XXX dt_local? */
     183
     184        /* Take source address from the address object */
     185        local->ipv4 = dir.aobj->naddr.ipv4;
     186        return EOK;
    156187}
    157188
  • uspace/srv/inet/inet.h

    r3b3c689 r8bf672d  
    9090} inet_link_info_t;
    9191
     92/** Static route info */
     93typedef struct {
     94        /** Destination network address */
     95        inet_naddr_t dest;
     96        /** Router address */
     97        inet_addr_t router;
     98        /** Static route name */
     99        char *name;
     100} inet_sroute_info_t;
     101
    92102typedef struct {
    93103        inet_addr_t src;
     
    125135} inet_addrobj_t;
    126136
     137/** Static route configuration */
     138typedef struct {
     139        link_t sroute_list;
     140        sysarg_t id;
     141        /** Destination network */
     142        inet_naddr_t dest;
     143        /** Router via which to route packets */
     144        inet_addr_t router;
     145        char *name;
     146} inet_sroute_t;
     147
     148typedef enum {
     149        /** Destination is on this network node */
     150        dt_local,
     151        /** Destination is directly reachable */
     152        dt_direct,
     153        /** Destination is behind a router */
     154        dt_router
     155} inet_dir_type_t;
     156
     157/** Direction (next hop) to a destination */
     158typedef struct {
     159        /** Route type */
     160        inet_dir_type_t dtype;
     161        /** Address object (direction) */
     162        inet_addrobj_t *aobj;
     163        /** Local destination address */
     164        inet_addr_t ldest;
     165} inet_dir_t;
     166
    127167typedef struct {
    128168        inet_addr_t src;
  • uspace/srv/inet/inetcfg.c

    r3b3c689 r8bf672d  
    4949#include "inet_link.h"
    5050#include "inetcfg.h"
     51#include "sroute.h"
    5152
    5253static int inetcfg_addr_create_static(char *name, inet_naddr_t *naddr,
     
    6667
    6768        addr = inet_addrobj_new();
     69        if (addr == NULL) {
     70                *addr_id = 0;
     71                return ENOMEM;
     72        }
     73
    6874        addr->naddr = *naddr;
    6975        addr->ilink = ilink;
     
    143149}
    144150
     151static int inetcfg_get_sroute_list(sysarg_t **sroutes, size_t *count)
     152{
     153        return inet_sroute_get_id_list(sroutes, count);
     154}
     155
    145156static int inetcfg_link_get(sysarg_t link_id, inet_link_info_t *linfo)
    146157{
     
    153164
    154165        linfo->name = str_dup(ilink->svc_name);
     166        return EOK;
     167}
     168
     169static int inetcfg_sroute_create(char *name, inet_naddr_t *dest,
     170    inet_addr_t *router, sysarg_t *sroute_id)
     171{
     172        inet_sroute_t *sroute;
     173
     174        sroute = inet_sroute_new();
     175        if (sroute == NULL) {
     176                *sroute_id = 0;
     177                return ENOMEM;
     178        }
     179
     180        sroute->dest = *dest;
     181        sroute->router = *router;
     182        sroute->name = str_dup(name);
     183        inet_sroute_add(sroute);
     184
     185        *sroute_id = sroute->id;
     186        return EOK;
     187}
     188
     189static int inetcfg_sroute_delete(sysarg_t sroute_id)
     190{
     191        inet_sroute_t *sroute;
     192
     193        sroute = inet_sroute_get_by_id(sroute_id);
     194        if (sroute == NULL)
     195                return ENOENT;
     196
     197        inet_sroute_remove(sroute);
     198        inet_sroute_delete(sroute);
     199
     200        return EOK;
     201}
     202
     203static int inetcfg_sroute_get(sysarg_t sroute_id, inet_sroute_info_t *srinfo)
     204{
     205        inet_sroute_t *sroute;
     206
     207        sroute = inet_sroute_get_by_id(sroute_id);
     208        if (sroute == NULL)
     209                return ENOENT;
     210
     211        srinfo->dest = sroute->dest;
     212        srinfo->router = sroute->router;
     213        srinfo->name = str_dup(sroute->name);
     214
     215        return EOK;
     216}
     217
     218static int inetcfg_sroute_get_id(char *name, sysarg_t *sroute_id)
     219{
     220        inet_sroute_t *sroute;
     221
     222        sroute = inet_sroute_find_by_name(name);
     223        if (sroute == NULL) {
     224                log_msg(LVL_DEBUG, "Static route '%s' not found.", name);
     225                return ENOENT;
     226        }
     227
     228        *sroute_id = sroute->id;
    155229        return EOK;
    156230}
     
    257331        async_answer_1(callid, rc, addr_id);
    258332}
    259 
    260333
    261334static void inetcfg_get_addr_list_srv(ipc_callid_t callid, ipc_call_t *call)
     
    293366}
    294367
    295 static void inetcfg_link_get_srv(ipc_callid_t callid, ipc_call_t *call)
    296 {
    297         ipc_callid_t rcallid;
    298         size_t max_size;
    299 
    300         sysarg_t link_id;
    301         inet_link_info_t linfo;
    302         int rc;
    303 
    304         link_id = IPC_GET_ARG1(*call);
    305         log_msg(LVL_DEBUG, "inetcfg_link_get_srv()");
    306 
    307         linfo.name = NULL;
    308 
    309         if (!async_data_read_receive(&rcallid, &max_size)) {
    310                 async_answer_0(rcallid, EREFUSED);
    311                 async_answer_0(callid, EREFUSED);
    312                 return;
    313         }
    314 
    315         rc = inetcfg_link_get(link_id, &linfo);
    316         if (rc != EOK) {
    317                 async_answer_0(rcallid, rc);
    318                 async_answer_0(callid, rc);
    319                 return;
    320         }
    321 
    322         sysarg_t retval = async_data_read_finalize(rcallid, linfo.name,
    323             min(max_size, str_size(linfo.name)));
    324         free(linfo.name);
    325 
    326         async_answer_0(callid, retval);
    327 }
    328368
    329369static void inetcfg_get_link_list_srv(ipc_callid_t callid, ipc_call_t *call)
     
    359399}
    360400
     401static void inetcfg_get_sroute_list_srv(ipc_callid_t callid, ipc_call_t *call)
     402{
     403        ipc_callid_t rcallid;
     404        size_t count;
     405        size_t max_size;
     406        size_t act_size;
     407        size_t size;
     408        sysarg_t *id_buf;
     409        int rc;
     410
     411        log_msg(LVL_DEBUG, "inetcfg_get_sroute_list_srv()");
     412
     413        if (!async_data_read_receive(&rcallid, &max_size)) {
     414                async_answer_0(rcallid, EREFUSED);
     415                async_answer_0(callid, EREFUSED);
     416                return;
     417        }
     418
     419        rc = inetcfg_get_sroute_list(&id_buf, &count);
     420        if (rc != EOK) {
     421                async_answer_0(rcallid, rc);
     422                async_answer_0(callid, rc);
     423                return;
     424        }
     425
     426        act_size = count * sizeof(sysarg_t);
     427        size = min(act_size, max_size);
     428
     429        sysarg_t retval = async_data_read_finalize(rcallid, id_buf, size);
     430        free(id_buf);
     431
     432        async_answer_1(callid, retval, act_size);
     433}
     434
     435static void inetcfg_link_get_srv(ipc_callid_t callid, ipc_call_t *call)
     436{
     437        ipc_callid_t rcallid;
     438        size_t max_size;
     439
     440        sysarg_t link_id;
     441        inet_link_info_t linfo;
     442        int rc;
     443
     444        link_id = IPC_GET_ARG1(*call);
     445        log_msg(LVL_DEBUG, "inetcfg_link_get_srv()");
     446
     447        linfo.name = NULL;
     448
     449        if (!async_data_read_receive(&rcallid, &max_size)) {
     450                async_answer_0(rcallid, EREFUSED);
     451                async_answer_0(callid, EREFUSED);
     452                return;
     453        }
     454
     455        rc = inetcfg_link_get(link_id, &linfo);
     456        if (rc != EOK) {
     457                async_answer_0(rcallid, rc);
     458                async_answer_0(callid, rc);
     459                return;
     460        }
     461
     462        sysarg_t retval = async_data_read_finalize(rcallid, linfo.name,
     463            min(max_size, str_size(linfo.name)));
     464        free(linfo.name);
     465
     466        async_answer_0(callid, retval);
     467}
     468
     469static void inetcfg_sroute_create_srv(ipc_callid_t callid,
     470    ipc_call_t *call)
     471{
     472        char *name;
     473        inet_naddr_t dest;
     474        inet_addr_t router;
     475        sysarg_t sroute_id;
     476        int rc;
     477
     478        log_msg(LVL_DEBUG, "inetcfg_sroute_create_srv()");
     479
     480        rc = async_data_write_accept((void **) &name, true, 0, LOC_NAME_MAXLEN,
     481            0, NULL);
     482        if (rc != EOK) {
     483                async_answer_0(callid, rc);
     484                return;
     485        }
     486
     487        dest.ipv4   = IPC_GET_ARG1(*call);
     488        dest.bits   = IPC_GET_ARG2(*call);
     489        router.ipv4 = IPC_GET_ARG3(*call);
     490
     491        sroute_id = 0;
     492        rc = inetcfg_sroute_create(name, &dest, &router, &sroute_id);
     493        free(name);
     494        async_answer_1(callid, rc, sroute_id);
     495}
     496
     497static void inetcfg_sroute_delete_srv(ipc_callid_t callid, ipc_call_t *call)
     498{
     499        sysarg_t sroute_id;
     500        int rc;
     501
     502        log_msg(LVL_DEBUG, "inetcfg_sroute_delete_srv()");
     503
     504        sroute_id = IPC_GET_ARG1(*call);
     505
     506        rc = inetcfg_sroute_delete(sroute_id);
     507        async_answer_0(callid, rc);
     508}
     509
     510static void inetcfg_sroute_get_srv(ipc_callid_t callid, ipc_call_t *call)
     511{
     512        ipc_callid_t rcallid;
     513        size_t max_size;
     514
     515        sysarg_t sroute_id;
     516        inet_sroute_info_t srinfo;
     517        int rc;
     518
     519        sroute_id = IPC_GET_ARG1(*call);
     520        log_msg(LVL_DEBUG, "inetcfg_sroute_get_srv()");
     521
     522        srinfo.dest.ipv4 = 0;
     523        srinfo.dest.bits = 0;
     524        srinfo.router.ipv4 = 0;
     525        srinfo.name = NULL;
     526
     527        if (!async_data_read_receive(&rcallid, &max_size)) {
     528                async_answer_0(rcallid, EREFUSED);
     529                async_answer_0(callid, EREFUSED);
     530                return;
     531        }
     532
     533        rc = inetcfg_sroute_get(sroute_id, &srinfo);
     534        if (rc != EOK) {
     535                async_answer_0(callid, rc);
     536                return;
     537        }
     538
     539        sysarg_t retval = async_data_read_finalize(rcallid, srinfo.name,
     540            min(max_size, str_size(srinfo.name)));
     541        free(srinfo.name);
     542
     543        async_answer_3(callid, retval, srinfo.dest.ipv4, srinfo.dest.bits,
     544            srinfo.router.ipv4);
     545}
     546
     547static void inetcfg_sroute_get_id_srv(ipc_callid_t callid, ipc_call_t *call)
     548{
     549        char *name;
     550        sysarg_t sroute_id;
     551        int rc;
     552
     553        log_msg(LVL_DEBUG, "inetcfg_sroute_get_id_srv()");
     554
     555        rc = async_data_write_accept((void **) &name, true, 0, LOC_NAME_MAXLEN,
     556            0, NULL);
     557        if (rc != EOK) {
     558                async_answer_0(callid, rc);
     559                return;
     560        }
     561
     562        sroute_id = 0;
     563        rc = inetcfg_sroute_get_id(name, &sroute_id);
     564        free(name);
     565        async_answer_1(callid, rc, sroute_id);
     566}
     567
    361568void inet_cfg_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
    362569{
     
    396603                        inetcfg_get_link_list_srv(callid, &call);
    397604                        break;
     605                case INETCFG_GET_SROUTE_LIST:
     606                        inetcfg_get_sroute_list_srv(callid, &call);
     607                        break;
    398608                case INETCFG_LINK_GET:
    399609                        inetcfg_link_get_srv(callid, &call);
     610                        break;
     611                case INETCFG_SROUTE_CREATE:
     612                        inetcfg_sroute_create_srv(callid, &call);
     613                        break;
     614                case INETCFG_SROUTE_DELETE:
     615                        inetcfg_sroute_delete_srv(callid, &call);
     616                        break;
     617                case INETCFG_SROUTE_GET:
     618                        inetcfg_sroute_get_srv(callid, &call);
     619                        break;
     620                case INETCFG_SROUTE_GET_ID:
     621                        inetcfg_sroute_get_id_srv(callid, &call);
    400622                        break;
    401623                default:
Note: See TracChangeset for help on using the changeset viewer.