Changeset a62ceaf in mainline


Ignore:
Timestamp:
2016-02-29T19:19:19Z (8 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
129b92c6
Parents:
5147ff1
Message:

Need better interfaces for handling internet host and host:port specifications.

Location:
uspace
Files:
8 added
15 edited

Legend:

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

    r5147ff1 ra62ceaf  
    7272       
    7373        inet_addr_t addr;
    74         int rc = inet_addr_parse(srv_addr, &addr);
     74        int rc = inet_addr_parse(srv_addr, &addr, NULL);
    7575       
    7676        if (rc != EOK) {
  • uspace/app/inet/inet.c

    r5147ff1 ra62ceaf  
    8888        }
    8989
    90         rc = inet_naddr_parse(addr_spec, &naddr);
     90        rc = inet_naddr_parse(addr_spec, &naddr, NULL);
    9191        if (rc != EOK) {
    9292                printf(NAME ": Invalid network address format '%s'.\n",
     
    177177        route_name = argv[2];
    178178
    179         rc = inet_naddr_parse(dest_str, &dest);
     179        rc = inet_naddr_parse(dest_str, &dest, NULL);
    180180        if (rc != EOK) {
    181181                printf(NAME ": Invalid network address format '%s'.\n",
     
    184184        }
    185185
    186         rc = inet_addr_parse(router_str, &router);
     186        rc = inet_addr_parse(router_str, &router, NULL);
    187187        if (rc != EOK) {
    188188                printf(NAME ": Invalid address format '%s'.\n", router_str);
  • uspace/app/netecho/comm.c

    r5147ff1 ra62ceaf  
    3737#include <errno.h>
    3838#include <fibril.h>
    39 #include <inet/dnsr.h>
    4039#include <inet/endpoint.h>
     40#include <inet/hostport.h>
    4141#include <inet/udp.h>
    4242#include <macros.h>
     
    109109}
    110110
    111 int comm_open(const char *host, const char *port_s)
     111int comm_open_listen(const char *port_s)
    112112{
    113113        inet_ep2_t epp;
    114         inet_addr_t iaddr;
    115114        int rc;
    116 
    117         if (host != NULL) {
    118                 /* Interpret as address */
    119                 inet_addr_t iaddr;
    120                 int rc = inet_addr_parse(host, &iaddr);
    121 
    122                 if (rc != EOK) {
    123                         /* Interpret as a host name */
    124                         dnsr_hostinfo_t *hinfo = NULL;
    125                         rc = dnsr_name2host(host, &hinfo, ip_any);
    126 
    127                         if (rc != EOK) {
    128                                 printf("Error resolving host '%s'.\n", host);
    129                                 goto error;
    130                         }
    131 
    132                         iaddr = hinfo->addr;
    133                 }
    134         }
    135115
    136116        char *endptr;
     
    142122
    143123        inet_ep2_init(&epp);
    144         if (host != NULL) {
    145                 /* Talk to remote host */
    146                 remote.addr = iaddr;
    147                 remote.port = port;
     124        epp.local.port = port;
    148125
    149                 printf("Talking to host %s port %u\n", host, port);
    150         } else {
    151                 /* Listen on local port */
    152                 epp.local.port = port;
     126        printf("Listening on port %u\n", port);
    153127
    154                 printf("Listening on port %u\n", port);
     128        rc = udp_create(&udp);
     129        if (rc != EOK)
     130                goto error;
     131
     132        rc = udp_assoc_create(udp, &epp, &comm_udp_cb, NULL, &assoc);
     133        if (rc != EOK)
     134                goto error;
     135
     136        return EOK;
     137error:
     138        udp_assoc_destroy(assoc);
     139        udp_destroy(udp);
     140
     141        return EIO;
     142}
     143
     144int comm_open_talkto(const char *hostport)
     145{
     146        inet_ep2_t epp;
     147        const char *errmsg;
     148        int rc;
     149
     150        inet_ep2_init(&epp);
     151        rc = inet_hostport_plookup_one(hostport, ip_any, &epp.remote, NULL,
     152            &errmsg);
     153        if (rc != EOK) {
     154                printf("Error: %s (host:port %s).\n", errmsg, hostport);
     155                goto error;
    155156        }
     157
     158        printf("Talking to %s\n", hostport);
    156159
    157160        rc = udp_create(&udp);
  • uspace/app/netecho/comm.h

    r5147ff1 ra62ceaf  
    3939#include <sys/types.h>
    4040
    41 extern int comm_open(const char *, const char *);
     41extern int comm_open_listen(const char *);
     42extern int comm_open_talkto(const char *);
    4243extern void comm_close(void);
    4344extern int comm_send(void *, size_t);
  • uspace/app/netecho/netecho.c

    r5147ff1 ra62ceaf  
    115115        printf("syntax:\n");
    116116        printf("\t%s -l <port>\n", NAME);
    117         printf("\t%s -d <host> <port> [<message> [<message...>]]\n", NAME);
     117        printf("\t%s -d <host>:<port> [<message> [<message...>]]\n", NAME);
    118118}
    119119
     
    151151int main(int argc, char *argv[])
    152152{
    153         char *host;
     153        char *hostport;
    154154        char *port;
    155155        char **msgs;
     
    167167                }
    168168
    169                 host = NULL;
    170169                port = argv[2];
    171170                msgs = NULL;
     171
     172                rc = comm_open_listen(port);
     173                if (rc != EOK) {
     174                        printf("Error setting up communication.\n");
     175                        return 1;
     176                }
    172177        } else if (str_cmp(argv[1], "-d") == 0) {
    173                 if (argc < 4) {
     178                if (argc < 3) {
    174179                        print_syntax();
    175180                        return 1;
    176181                }
    177182
    178                 host = argv[2];
    179                 port = argv[3];
    180                 msgs = argv + 4;
     183                hostport = argv[2];
     184                port = NULL;
     185                msgs = argv + 3;
     186
     187                rc = comm_open_talkto(hostport);
     188                if (rc != EOK) {
     189                        printf("Error setting up communication.\n");
     190                        return 1;
     191                }
    181192        } else {
    182193                print_syntax();
    183                 return 1;
    184         }
    185 
    186         rc = comm_open(host, port);
    187         if (rc != EOK) {
    188                 printf("Error setting up communication.\n");
    189194                return 1;
    190195        }
  • uspace/app/nterm/conn.c

    r5147ff1 ra62ceaf  
    3737#include <errno.h>
    3838#include <fibril.h>
    39 #include <inet/dnsr.h>
    4039#include <inet/endpoint.h>
     40#include <inet/hostport.h>
    4141#include <inet/tcp.h>
    4242#include <stdio.h>
     
    8686}
    8787
    88 int conn_open(const char *host, const char *port_s)
     88int conn_open(const char *hostport)
    8989{
    9090        inet_ep2_t epp;
     91        const char *errmsg;
     92        int rc;
    9193
    92         /* Interpret as address */
    93         inet_addr_t iaddr;
    94         int rc = inet_addr_parse(host, &iaddr);
    95 
     94        inet_ep2_init(&epp);
     95        rc = inet_hostport_plookup_one(hostport, ip_any, &epp.remote, NULL,
     96            &errmsg);
    9697        if (rc != EOK) {
    97                 /* Interpret as a host name */
    98                 dnsr_hostinfo_t *hinfo = NULL;
    99                 rc = dnsr_name2host(host, &hinfo, ip_any);
    100 
    101                 if (rc != EOK) {
    102                         printf("Error resolving host '%s'.\n", host);
    103                         goto error;
    104                 }
    105 
    106                 iaddr = hinfo->addr;
    107         }
    108 
    109         char *endptr;
    110         uint16_t port = strtol(port_s, &endptr, 10);
    111         if (*endptr != '\0') {
    112                 printf("Invalid port number %s\n", port_s);
     98                printf("Error: %s (host:port %s).\n", errmsg, hostport);
    11399                goto error;
    114100        }
    115101
    116         inet_ep2_init(&epp);
    117         epp.remote.addr = iaddr;
    118         epp.remote.port = port;
    119 
    120         printf("Connecting to host %s port %u\n", host, port);
     102        printf("Connecting to %s\n", hostport);
     103        char *s;
     104        rc = inet_addr_format(&epp.remote.addr, &s);
     105        if (rc != EOK)
     106                goto error;
    121107
    122108        rc = tcp_create(&tcp);
  • uspace/app/nterm/conn.h

    r5147ff1 ra62ceaf  
    3939#include <sys/types.h>
    4040
    41 extern int conn_open(const char *, const char *);
     41extern int conn_open(const char *);
    4242extern int conn_send(void *, size_t);
    4343
  • uspace/app/nterm/nterm.c

    r5147ff1 ra62ceaf  
    104104static void print_syntax(void)
    105105{
    106         printf("syntax: nterm <host> <port>\n");
     106        printf("syntax: nterm <host>:<port>\n");
    107107}
    108108
     
    112112        int rc;
    113113
    114         if (argc != 3) {
     114        if (argc != 2) {
    115115                print_syntax();
    116116                return 1;
    117117        }
    118118
    119         rc = conn_open(argv[1], argv[2]);
     119        rc = conn_open(argv[1]);
    120120        if (rc != EOK) {
    121121                printf("Error connecting.\n");
  • uspace/app/ping/ping.c

    r5147ff1 ra62ceaf  
    3737#include <errno.h>
    3838#include <fibril_synch.h>
    39 #include <inet/dnsr.h>
    4039#include <inet/addr.h>
     40#include <inet/host.h>
    4141#include <inet/inetping.h>
    4242#include <io/console.h>
     
    214214int main(int argc, char *argv[])
    215215{
    216         dnsr_hostinfo_t *hinfo = NULL;
    217216        char *asrc = NULL;
    218217        char *adest = NULL;
    219218        char *sdest = NULL;
     219        char *host;
     220        const char *errmsg;
    220221        ip_ver_t ip_ver = ip_any;
    221222       
     
    260261        }
    261262       
    262         /* Parse destination address */
    263         rc = inet_addr_parse(argv[optind], &dest_addr);
    264         if (rc != EOK) {
    265                 /* Try interpreting as a host name */
    266                 rc = dnsr_name2host(argv[optind], &hinfo, ip_ver);
    267                 if (rc != EOK) {
    268                         printf("Error resolving host '%s'.\n", argv[optind]);
    269                         goto error;
    270                 }
    271                
    272                 dest_addr = hinfo->addr;
     263        host = argv[optind];
     264       
     265        /* Look up host */
     266        rc = inet_host_plookup_one(host, ip_ver, &dest_addr, NULL, &errmsg);
     267        if (rc != EOK) {
     268                printf("Error resolving host '%s' (%s).\n", host, errmsg);
     269                goto error;
    273270        }
    274271       
     
    292289        }
    293290       
    294         if (hinfo != NULL) {
    295                 rc = asprintf(&sdest, "%s (%s)", hinfo->cname, adest);
    296                 if (rc < 0) {
    297                         printf("Out of memory.\n");
    298                         goto error;
    299                 }
    300         } else {
    301                 sdest = adest;
    302                 adest = NULL;
     291        rc = asprintf(&sdest, "%s (%s)", host, adest);
     292        if (rc < 0) {
     293                printf("Out of memory.\n");
     294                goto error;
    303295        }
    304296       
     
    330322        free(adest);
    331323        free(sdest);
    332         dnsr_hostinfo_destroy(hinfo);
    333324        return 0;
    334325       
     
    337328        free(adest);
    338329        free(sdest);
    339         dnsr_hostinfo_destroy(hinfo);
    340330        return 1;
    341331}
  • uspace/lib/c/Makefile

    r5147ff1 ra62ceaf  
    9898        generic/inet/addr.c \
    9999        generic/inet/endpoint.c \
     100        generic/inet/host.c \
     101        generic/inet/hostname.c \
     102        generic/inet/hostport.c \
    100103        generic/inet/tcp.c \
    101104        generic/inet/udp.c \
  • uspace/lib/c/generic/inet/addr.c

    r5147ff1 ra62ceaf  
    290290
    291291static int inet_addr_parse_v4(const char *str, inet_addr_t *raddr,
    292     int *prefix)
     292    int *prefix, char **endptr)
    293293{
    294294        uint32_t a = 0;
     
    306306                i++;
    307307
    308                 if (*cur == '\0' || *cur == '/')
     308                if (*cur != '.')
    309309                        break;
    310 
    311                 if (*cur != '.')
    312                         return EINVAL;
    313310
    314311                if (i < 4)
     
    326323        }
    327324
    328         if (i != 4 || (*cur != '\0'))
     325        if (i != 4)
     326                return EINVAL;
     327
     328        if (endptr == NULL && *cur != '\0')
    329329                return EINVAL;
    330330
     
    332332        raddr->addr = a;
    333333
     334        if (endptr != NULL)
     335                *endptr = cur;
     336
    334337        return EOK;
    335338}
    336339
    337 static int inet_addr_parse_v6(const char *str, inet_addr_t *raddr, int *prefix)
     340static int inet_addr_parse_v6(const char *str, inet_addr_t *raddr, int *prefix,
     341    char **endptr)
    338342{
    339343        uint8_t data[16];
     344        int explicit_groups;
    340345
    341346        memset(data, 0, 16);
     
    351356                wildcard_pos = 0;
    352357                wildcard_size = 16;
    353 
    354                 /* Handle the unspecified address */
    355                 if (*cur == '\0')
    356                         goto success;
    357358        }
    358359
    359360        while (i < 16) {
    360361                uint16_t bioctet;
    361                 int rc = str_uint16_t(cur, &cur, 16, false, &bioctet);
     362                const char *gend;
     363                int rc = str_uint16_t(cur, &gend, 16, false, &bioctet);
    362364                if (rc != EOK)
    363                         return rc;
     365                        break;
    364366
    365367                data[i] = (bioctet >> 8) & 0xff;
     
    375377                i += 2;
    376378
    377                 if (*cur != ':')
     379                if (*gend != ':') {
     380                        cur = gend;
    378381                        break;
     382                }
    379383
    380384                if (i < 16) {
    381                         cur++;
    382 
    383385                        /* Handle wildcard */
    384                         if (*cur == ':') {
     386                        if (gend[1] == ':') {
    385387                                if (wildcard_pos != (size_t) -1)
    386388                                        return EINVAL;
     
    388390                                wildcard_pos = i;
    389391                                wildcard_size = 16 - i;
    390                                 cur++;
    391 
    392                                 if (*cur == '\0' || *cur == '/')
    393                                         break;
     392                                cur = gend + 2;
    394393                        }
    395394                }
    396395        }
     396
     397        /* Number of explicitly specified groups */
     398        explicit_groups = i;
    397399
    398400        if (prefix != NULL) {
     
    406408        }
    407409
    408         if (*cur != '\0')
     410        if (endptr == NULL && *cur != '\0')
    409411                return EINVAL;
    410412
     
    418420                        data[j] = 0;
    419421                }
    420         }
    421 
    422 success:
     422        } else {
     423                /* Verify that all groups have been specified */
     424                if (explicit_groups != 16)
     425                        return EINVAL;
     426        }
     427
    423428        raddr->version = ip_v6;
    424429        memcpy(raddr->addr6, data, 16);
     430        if (endptr != NULL)
     431                *endptr = (char *)cur;
    425432        return EOK;
    426433}
    427434
    428435/** Parse node address.
     436 *
     437 * Will fail if @a text contains extra characters at the and and @a endptr
     438 * is @c NULL.
    429439 *
    430440 * @param text Network address in common notation.
    431441 * @param addr Place to store node address.
     442 * @param endptr Place to store pointer to next character oc @c NULL
    432443 *
    433444 * @return EOK on success, EINVAL if input is not in valid format.
    434445 *
    435446 */
    436 int inet_addr_parse(const char *text, inet_addr_t *addr)
     447int inet_addr_parse(const char *text, inet_addr_t *addr, char **endptr)
    437448{
    438449        int rc;
    439450
    440         rc = inet_addr_parse_v4(text, addr, NULL);
     451        rc = inet_addr_parse_v4(text, addr, NULL, endptr);
    441452        if (rc == EOK)
    442453                return EOK;
    443454
    444         rc = inet_addr_parse_v6(text, addr, NULL);
     455        rc = inet_addr_parse_v6(text, addr, NULL, endptr);
    445456        if (rc == EOK)
    446457                return EOK;
     
    451462/** Parse network address.
    452463 *
     464 * Will fail if @a text contains extra characters at the and and @a endptr
     465 * is @c NULL.
     466 *
    453467 * @param text  Network address in common notation.
    454468 * @param naddr Place to store network address.
     469 * @param endptr Place to store pointer to next character oc @c NULL
    455470 *
    456471 * @return EOK on success, EINVAL if input is not in valid format.
    457472 *
    458473 */
    459 int inet_naddr_parse(const char *text, inet_naddr_t *naddr)
     474int inet_naddr_parse(const char *text, inet_naddr_t *naddr, char **endptr)
    460475{
    461476        int rc;
     
    463478        int prefix;
    464479
    465         rc = inet_addr_parse_v4(text, &addr, &prefix);
     480        rc = inet_addr_parse_v4(text, &addr, &prefix, endptr);
    466481        if (rc == EOK) {
    467482                inet_addr_naddr(&addr, prefix, naddr);
     
    469484        }
    470485
    471         rc = inet_addr_parse_v6(text, &addr, &prefix);
     486        rc = inet_addr_parse_v6(text, &addr, &prefix, endptr);
    472487        if (rc == EOK) {
    473488                inet_addr_naddr(&addr, prefix, naddr);
  • uspace/lib/c/include/inet/addr.h

    r5147ff1 ra62ceaf  
    111111extern int inet_naddr_compare_mask(const inet_naddr_t *, const inet_addr_t *);
    112112
    113 extern int inet_addr_parse(const char *, inet_addr_t *);
    114 extern int inet_naddr_parse(const char *, inet_naddr_t *);
     113extern int inet_addr_parse(const char *, inet_addr_t *, char **);
     114extern int inet_naddr_parse(const char *, inet_naddr_t *, char **);
    115115
    116116extern int inet_addr_format(const inet_addr_t *, char **);
  • uspace/lib/c/include/inet/dnsr.h

    r5147ff1 ra62ceaf  
    3333 */
    3434
    35 #ifndef LIBC_INET_DNSRES_H_
    36 #define LIBC_INET_DNSRES_H_
     35#ifndef LIBC_INET_DNSR_H_
     36#define LIBC_INET_DNSR_H_
    3737
    3838#include <inet/inet.h>
  • uspace/lib/c/include/inet/endpoint.h

    r5147ff1 ra62ceaf  
    3333 */
    3434
    35 #ifndef LIBC_INET_ASSOC_H_
    36 #define LIBC_INET_ASSOC_H_
     35#ifndef LIBC_INET_ENDPOINT_H_
     36#define LIBC_INET_ENDPOINT_H_
    3737
    3838#include <stdint.h>
  • uspace/lib/http/src/http.c

    r5147ff1 ra62ceaf  
    4040#include <macros.h>
    4141
    42 #include <inet/dnsr.h>
     42#include <inet/endpoint.h>
     43#include <inet/host.h>
    4344#include <inet/tcp.h>
    4445
     
    8889                return EBUSY;
    8990       
    90         /* Interpret as address */
    91         int rc = inet_addr_parse(http->host, &http->addr);
    92        
    93         if (rc != EOK) {
    94                 /* Interpret as a host name */
    95                 dnsr_hostinfo_t *hinfo = NULL;
    96                 rc = dnsr_name2host(http->host, &hinfo, ip_any);
    97                
    98                 if (rc != EOK)
    99                         return rc;
    100                
    101                 http->addr = hinfo->addr;
    102                 dnsr_hostinfo_destroy(hinfo);
    103         }
     91        int rc = inet_host_plookup_one(http->host, ip_any, &http->addr, NULL,
     92            NULL);
     93        if (rc != EOK)
     94                return rc;
    10495       
    10596        inet_ep2_t epp;
Note: See TracChangeset for help on using the changeset viewer.