Changeset 0e94b979 in mainline


Ignore:
Timestamp:
2012-03-08T21:12:44Z (12 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
291c792
Parents:
a88a6eac
Message:

Listing configured addresses.

Location:
uspace
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/inetcfg/inetcfg.c

    ra88a6eac r0e94b979  
    4040#include <loc.h>
    4141#include <stdio.h>
     42#include <stdlib.h>
    4243#include <str_error.h>
    4344#include <sys/types.h>
     
    8687}
    8788
     89static int naddr_format(inet_naddr_t *naddr, char **bufp)
     90{
     91        int rc;
     92
     93        rc = asprintf(bufp, "%d.%d.%d.%d/%d", naddr->ipv4 >> 24,
     94            (naddr->ipv4 >> 16) & 0xff, (naddr->ipv4 >> 8) & 0xff,
     95            naddr->ipv4 & 0xff, naddr->bits);
     96
     97        if (rc < 0)
     98                return ENOMEM;
     99
     100        return EOK;
     101}
     102
    88103static int addr_create_static(int argc, char *argv[])
    89104{
     
    129144                printf(NAME ": Failed creating static address '%s' (%d)\n",
    130145                    "v4s", rc);
    131                 return 1;
    132         }
     146                return EIO;
     147        }
     148
     149        return EOK;
     150}
     151
     152static int addr_list(void)
     153{
     154        sysarg_t *addr_list;
     155        inet_addr_info_t ainfo;
     156        inet_link_info_t linfo;
     157
     158        size_t count;
     159        size_t i;
     160        int rc;
     161        char *astr;
     162
     163        rc = inetcfg_get_addr_list(&addr_list, &count);
     164        if (rc != EOK) {
     165                printf(NAME ": Failed getting address list.\n");
     166                return rc;
     167        }
     168
     169        for (i = 0; i < count; i++) {
     170                rc = inetcfg_addr_get(addr_list[i], &ainfo);
     171                if (rc != EOK) {
     172                        printf("Failed getting properties of address %zu.\n",
     173                            (size_t)addr_list[i]);
     174                        continue;
     175                }
     176
     177                rc = inetcfg_link_get(ainfo.ilink, &linfo);
     178                if (rc != EOK) {
     179                        printf("Failed getting properties of link %zu.\n",
     180                            (size_t)ainfo.ilink);
     181                        continue;
     182                }
     183
     184                rc = naddr_format(&ainfo.naddr, &astr);
     185                if (rc != EOK) {
     186                        printf("Memory allocation failed.\n");
     187                        goto out;
     188                }
     189
     190                printf("%s %s %s\n", astr, linfo.name,
     191                    ainfo.name);
     192
     193                free(astr);
     194                free(ainfo.name);
     195                free(linfo.name);
     196        }
     197out:
     198        free(addr_list);
    133199
    134200        return EOK;
     
    138204{
    139205        int rc;
    140 
    141         if (argc < 2) {
    142                 printf(NAME ": Missing argument.\n");
    143                 print_syntax();
    144                 return 1;
    145         }
    146206
    147207        rc = inetcfg_init();
     
    150210                    rc);
    151211                return 1;
     212        }
     213
     214        if (argc < 2) {
     215                rc = addr_list();
     216                if (rc != EOK)
     217                        return 1;
     218                return 0;
    152219        }
    153220
  • uspace/lib/c/generic/inetcfg.c

    ra88a6eac r0e94b979  
    3535#include <loc.h>
    3636#include <stdlib.h>
     37#include <str.h>
    3738
    3839static async_sess_t *inetcfg_sess = NULL;
     
    156157int inetcfg_addr_get(sysarg_t addr_id, inet_addr_info_t *ainfo)
    157158{
    158         sysarg_t ipv4, bits;
    159         async_exch_t *exch = async_exchange_begin(inetcfg_sess);
    160 
    161         int rc = async_req_1_2(exch, INETCFG_ADDR_GET, addr_id,
    162             &ipv4, &bits);
    163         async_exchange_end(exch);
    164 
    165         if (rc != EOK)
    166                 return rc;
    167 
    168         ainfo->naddr.ipv4 = ipv4;
    169         ainfo->naddr.bits = bits;
     159        ipc_call_t dreply;
     160        sysarg_t dretval;
     161        size_t act_size;
     162        char name_buf[LOC_NAME_MAXLEN + 1];
     163
     164        async_exch_t *exch = async_exchange_begin(inetcfg_sess);
     165
     166        ipc_call_t answer;
     167        aid_t req = async_send_1(exch, INETCFG_ADDR_GET, addr_id, &answer);
     168        aid_t dreq = async_data_read(exch, name_buf, LOC_NAME_MAXLEN, &dreply);
     169        async_wait_for(dreq, &dretval);
     170
     171        async_exchange_end(exch);
     172
     173        if (dretval != EOK) {
     174                async_wait_for(req, NULL);
     175                return dretval;
     176        }
     177
     178        sysarg_t retval;
     179        async_wait_for(req, &retval);
     180
     181        if (retval != EOK)
     182                return retval;
     183
     184        act_size = IPC_GET_ARG2(dreply);
     185        assert(act_size <= LOC_NAME_MAXLEN);
     186        name_buf[act_size] = '\0';
     187
     188        ainfo->naddr.ipv4 = IPC_GET_ARG1(answer);
     189        ainfo->naddr.bits = IPC_GET_ARG2(answer);
     190        ainfo->ilink = IPC_GET_ARG3(answer);
     191        ainfo->name = str_dup(name_buf);
     192
    170193        return EOK;
    171194}
     
    185208int inetcfg_link_get(sysarg_t link_id, inet_link_info_t *linfo)
    186209{
    187         async_exch_t *exch = async_exchange_begin(inetcfg_sess);
    188 
    189         int rc = async_req_1_0(exch, INETCFG_LINK_GET, link_id);
    190         async_exchange_end(exch);
    191 
    192         if (rc != EOK)
    193                 return rc;
    194 
    195         linfo->dummy = 0;
     210        ipc_call_t dreply;
     211        sysarg_t dretval;
     212        size_t act_size;
     213        char name_buf[LOC_NAME_MAXLEN + 1];
     214
     215        async_exch_t *exch = async_exchange_begin(inetcfg_sess);
     216
     217        ipc_call_t answer;
     218        aid_t req = async_send_1(exch, INETCFG_LINK_GET, link_id, &answer);
     219        aid_t dreq = async_data_read(exch, name_buf, LOC_NAME_MAXLEN, &dreply);
     220        async_wait_for(dreq, &dretval);
     221
     222        async_exchange_end(exch);
     223
     224        if (dretval != EOK) {
     225                async_wait_for(req, NULL);
     226                return dretval;
     227        }
     228
     229        sysarg_t retval;
     230        async_wait_for(req, &retval);
     231
     232        if (retval != EOK)
     233                return retval;
     234
     235        act_size = IPC_GET_ARG2(dreply);
     236        assert(act_size <= LOC_NAME_MAXLEN);
     237        name_buf[act_size] = '\0';
     238
     239        linfo->name = str_dup(name_buf);
     240
    196241        return EOK;
    197242}
  • uspace/lib/c/include/inet/inetcfg.h

    ra88a6eac r0e94b979  
    5151        /** Network address */
    5252        inet_naddr_t naddr;
     53        /** Link service ID */
     54        sysarg_t ilink;
     55        /** Address object name */
     56        char *name;
    5357} inet_addr_info_t;
    5458
    5559/** IP link info */
    5660typedef struct {
    57         int dummy;
     61        /** Link service name */
     62        char *name;
    5863} inet_link_info_t;
    5964
  • uspace/srv/inet/addrobj.c

    ra88a6eac r0e94b979  
    3636
    3737#include <bitops.h>
     38#include <errno.h>
    3839#include <fibril_synch.h>
    3940#include <io/log.h>
     
    4748static FIBRIL_MUTEX_INITIALIZE(addr_list_lock);
    4849static LIST_INITIALIZE(addr_list);
     50static sysarg_t addr_id = 0;
    4951
    5052static uint32_t inet_netmask(int bits)
     
    6769
    6870        link_initialize(&addr->addr_list);
     71        fibril_mutex_lock(&addr_list_lock);
     72        addr->id = ++addr_id;
     73        fibril_mutex_unlock(&addr_list_lock);
    6974
    7075        return addr;
     
    7378void inet_addrobj_delete(inet_addrobj_t *addr)
    7479{
     80        if (addr->name != NULL)
     81                free(addr->name);
    7582        free(addr);
    7683}
     
    123130}
    124131
     132/** Find address object matching address @a addr.
     133 *
     134 * @param id    Address object ID
     135 * @return      Address object
     136 */
     137inet_addrobj_t *inet_addrobj_get_by_id(sysarg_t id)
     138{
     139        log_msg(LVL_DEBUG, "inet_addrobj_get_by_id(%zu)", (size_t)id);
     140
     141        fibril_mutex_lock(&addr_list_lock);
     142
     143        list_foreach(addr_list, link) {
     144                inet_addrobj_t *naddr = list_get_instance(link,
     145                    inet_addrobj_t, addr_list);
     146
     147                if (naddr->id == id) {
     148                        fibril_mutex_unlock(&addr_list_lock);
     149                        return naddr;
     150                }
     151        }
     152
     153        fibril_mutex_unlock(&addr_list_lock);
     154
     155        return NULL;
     156}
     157
    125158/** Send datagram to directly reachable destination */
    126159int inet_addrobj_send_dgram(inet_addrobj_t *addr, inet_dgram_t *dgram,
     
    137170}
    138171
     172/** Get IDs of all address objects. */
     173int inet_addrobj_get_id_list(sysarg_t **rid_list, size_t *rcount)
     174{
     175        sysarg_t *id_list;
     176        size_t count, i;
     177
     178        fibril_mutex_lock(&addr_list_lock);
     179        count = list_count(&addr_list);
     180
     181        id_list = calloc(count, sizeof(sysarg_t));
     182        if (id_list == NULL) {
     183                fibril_mutex_unlock(&addr_list_lock);
     184                return ENOMEM;
     185        }
     186
     187        i = 0;
     188        list_foreach(addr_list, link) {
     189                inet_addrobj_t *addr = list_get_instance(link,
     190                    inet_addrobj_t, addr_list);
     191
     192                id_list[i++] = addr->id;
     193        }
     194
     195        fibril_mutex_unlock(&addr_list_lock);
     196
     197        *rid_list = id_list;
     198        *rcount = count;
     199
     200        return EOK;
     201}
     202
    139203/** @}
    140204 */
  • uspace/srv/inet/addrobj.h

    ra88a6eac r0e94b979  
    3838#define INET_ADDROBJ_H_
    3939
     40#include <sys/types.h>
    4041#include "inet.h"
    4142
     
    5253extern void inet_addrobj_remove(inet_addrobj_t *);
    5354extern inet_addrobj_t *inet_addrobj_find(inet_addr_t *, inet_addrobj_find_t);
     55extern inet_addrobj_t *inet_addrobj_get_by_id(sysarg_t);
    5456extern int inet_addrobj_send_dgram(inet_addrobj_t *, inet_dgram_t *,
    5557    uint8_t, uint8_t, int);
     58extern int inet_addrobj_get_id_list(sysarg_t **, size_t *);
     59
    5660
    5761#endif
  • uspace/srv/inet/inet.h

    ra88a6eac r0e94b979  
    6868        /** Network address */
    6969        inet_naddr_t naddr;
     70        /** Link service ID */
     71        sysarg_t ilink;
     72        /** Address object name */
     73        char *name;
    7074} inet_addr_info_t;
    7175
    7276/** IP link info */
    7377typedef struct {
    74         int dummy;
     78        /** Link service name */
     79        char *name;
    7580} inet_link_info_t;
    7681
     
    104109typedef struct {
    105110        link_t addr_list;
     111        sysarg_t id;
    106112        inet_naddr_t naddr;
    107113        inet_link_t *ilink;
     114        char *name;
    108115} inet_addrobj_t;
    109116
  • uspace/srv/inet/inet_link.c

    ra88a6eac r0e94b979  
    4242#include <loc.h>
    4343#include <stdlib.h>
     44#include <str.h>
    4445
    4546#include "addrobj.h"
     
    196197        addr->naddr.bits = 24;
    197198        addr->ilink = ilink;
     199        addr->name = str_dup("v4a");
    198200        inet_addrobj_add(addr);
    199201
  • uspace/srv/inet/inetcfg.c

    ra88a6eac r0e94b979  
    4242#include <loc.h>
    4343#include <stdlib.h>
     44#include <str.h>
    4445#include <sys/types.h>
    4546
     
    6768        addr->naddr = *naddr;
    6869        addr->ilink = ilink;
     70        addr->name = str_dup("foo");
    6971        inet_addrobj_add(addr);
    7072
     
    8890static int inetcfg_addr_get(sysarg_t addr_id, inet_addr_info_t *ainfo)
    8991{
     92        inet_addrobj_t *addr;
     93
     94        addr = inet_addrobj_get_by_id(addr_id);
     95        if (addr == NULL)
     96                return ENOENT;
     97
     98        ainfo->naddr = addr->naddr;
     99        ainfo->ilink = addr->ilink->svc_id;
     100        ainfo->name = str_dup(addr->name);
     101
     102        return EOK;
     103}
     104
     105static int inetcfg_get_addr_list(sysarg_t **addrs, size_t *count)
     106{
     107        return inet_addrobj_get_id_list(addrs, count);
     108}
     109
     110static int inetcfg_get_link_list(sysarg_t **addrs, size_t *count)
     111{
    90112        return ENOTSUP;
    91113}
    92114
    93 static int inetcfg_get_addr_list(sysarg_t **addrs, size_t *count)
    94 {
    95         return ENOTSUP;
    96 }
    97 
    98 static int inetcfg_get_link_list(sysarg_t **addrs, size_t *count)
    99 {
    100         return ENOTSUP;
    101 }
    102 
    103 static int inetcfg_link_get(sysarg_t addr_id, inet_link_info_t *ainfo)
    104 {
    105         return ENOTSUP;
     115static int inetcfg_link_get(sysarg_t link_id, inet_link_info_t *linfo)
     116{
     117        inet_link_t *ilink;
     118
     119        ilink = inet_link_get_by_id(link_id);
     120        if (ilink == NULL) {
     121                return ENOENT;
     122        }
     123
     124        linfo->name = str_dup(ilink->svc_name);
     125        return EOK;
    106126}
    107127
     
    140160static void inetcfg_addr_get_srv(ipc_callid_t callid, ipc_call_t *call)
    141161{
     162        ipc_callid_t rcallid;
     163        size_t max_size;
     164
    142165        sysarg_t addr_id;
    143166        inet_addr_info_t ainfo;
     
    147170        log_msg(LVL_DEBUG, "inetcfg_addr_get_srv()");
    148171
     172        ainfo.naddr.ipv4 = 0;
     173        ainfo.naddr.bits = 0;
     174        ainfo.ilink = 0;
     175        ainfo.name = NULL;
     176
     177        if (!async_data_read_receive(&rcallid, &max_size)) {
     178                async_answer_0(rcallid, EREFUSED);
     179                async_answer_0(callid, EREFUSED);
     180                return;
     181        }
     182
    149183        rc = inetcfg_addr_get(addr_id, &ainfo);
    150         async_answer_2(callid, rc, ainfo.naddr.ipv4, ainfo.naddr.bits);
     184        if (rc != EOK) {
     185                async_answer_0(callid, rc);
     186                return;
     187        }
     188
     189        sysarg_t retval = async_data_read_finalize(rcallid, ainfo.name,
     190            min(max_size, str_size(ainfo.name)));
     191        free(ainfo.name);
     192
     193        async_answer_3(callid, retval, ainfo.naddr.ipv4, ainfo.naddr.bits,
     194            ainfo.ilink);
    151195}
    152196
    153197
    154198static void inetcfg_get_addr_list_srv(ipc_callid_t callid, ipc_call_t *call)
     199{
     200        ipc_callid_t rcallid;
     201        size_t count;
     202        size_t max_size;
     203        size_t act_size;
     204        size_t size;
     205        sysarg_t *id_buf;
     206        int rc;
     207
     208        log_msg(LVL_DEBUG, "inetcfg_get_addr_list_srv()");
     209
     210        if (!async_data_read_receive(&rcallid, &max_size)) {
     211                async_answer_0(rcallid, EREFUSED);
     212                async_answer_0(callid, EREFUSED);
     213                return;
     214        }
     215
     216        rc = inetcfg_get_addr_list(&id_buf, &count);
     217        if (rc != EOK) {
     218                async_answer_0(rcallid, rc);
     219                async_answer_0(callid, rc);
     220                return;
     221        }
     222
     223        act_size = count * sizeof(sysarg_t);
     224        size = min(act_size, max_size);
     225
     226        sysarg_t retval = async_data_read_finalize(rcallid, id_buf, size);
     227        free(id_buf);
     228
     229        async_answer_1(callid, retval, act_size);
     230}
     231
     232static void inetcfg_link_get_srv(ipc_callid_t callid, ipc_call_t *call)
     233{
     234        ipc_callid_t rcallid;
     235        size_t max_size;
     236
     237        sysarg_t link_id;
     238        inet_link_info_t linfo;
     239        int rc;
     240
     241        link_id = IPC_GET_ARG1(*call);
     242        log_msg(LVL_DEBUG, "inetcfg_link_get_srv()");
     243
     244        linfo.name = NULL;
     245
     246        if (!async_data_read_receive(&rcallid, &max_size)) {
     247                async_answer_0(rcallid, EREFUSED);
     248                async_answer_0(callid, EREFUSED);
     249                return;
     250        }
     251
     252        rc = inetcfg_link_get(link_id, &linfo);
     253        if (rc != EOK) {
     254                async_answer_0(rcallid, rc);
     255                async_answer_0(callid, rc);
     256                return;
     257        }
     258
     259        sysarg_t retval = async_data_read_finalize(rcallid, linfo.name,
     260            min(max_size, str_size(linfo.name)));
     261        free(linfo.name);
     262
     263        async_answer_0(callid, retval);
     264}
     265
     266static void inetcfg_get_link_list_srv(ipc_callid_t callid, ipc_call_t *call)
    155267{
    156268        ipc_callid_t rcallid;
     
    161273        int rc;
    162274
    163         log_msg(LVL_DEBUG, "inetcfg_get_addr_list_srv()");
    164 
    165         if (!async_data_read_receive(&rcallid, &max_size)) {
    166                 async_answer_0(rcallid, EREFUSED);
    167                 async_answer_0(callid, EREFUSED);
    168                 return;
    169         }
    170 
    171         rc = inetcfg_get_addr_list(&id_buf, &act_size);
    172         if (rc != EOK) {
    173                 async_answer_0(rcallid, rc);
    174                 async_answer_0(callid, rc);
    175                 return;
    176         }
    177 
    178         size = min(act_size, max_size);
    179 
    180         sysarg_t retval = async_data_read_finalize(rcallid, id_buf, size);
    181         free(id_buf);
    182 
    183         async_answer_1(callid, retval, act_size);
    184 }
    185 
    186 static void inetcfg_link_get_srv(ipc_callid_t callid, ipc_call_t *call)
    187 {
    188         sysarg_t link_id;
    189         inet_link_info_t linfo;
    190         int rc;
    191 
    192         link_id = IPC_GET_ARG1(*call);
    193         log_msg(LVL_DEBUG, "inetcfg_link_get_srv()");
    194 
    195         rc = inetcfg_link_get(link_id, &linfo);
    196         async_answer_0(callid, rc);
    197 }
    198 
    199 static void inetcfg_get_link_list_srv(ipc_callid_t callid, ipc_call_t *call)
    200 {
    201         ipc_callid_t rcallid;
    202         size_t max_size;
    203         size_t act_size;
    204         size_t size;
    205         sysarg_t *id_buf;
    206         int rc;
    207 
    208275        log_msg(LVL_DEBUG, "inetcfg_get_link_list_srv()");
    209276
Note: See TracChangeset for help on using the changeset viewer.