Changeset a62ceaf in mainline for uspace/app/netecho


Ignore:
Timestamp:
2016-02-29T19:19:19Z (9 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/app/netecho
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • 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        }
Note: See TracChangeset for help on using the changeset viewer.