Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/nterm/conn.c

    r26de91a rfff7ef4  
    3838#include <fibril.h>
    3939#include <inet/dnsr.h>
    40 #include <net/inet.h>
    4140#include <net/socket.h>
    4241#include <stdio.h>
    43 #include <stdlib.h>
    4442#include <str_error.h>
    4543#include <sys/types.h>
     
    7573}
    7674
    77 int conn_open(const char *host, const char *port_s)
     75int conn_open(const char *addr_s, const char *port_s)
    7876{
    79         int conn_fd = -1;
    80         struct sockaddr *saddr = NULL;
    81         socklen_t saddrlen;
    82        
    83         /* Interpret as address */
    84         inet_addr_t iaddr;
    85         int rc = inet_addr_parse(host, &iaddr);
    86        
     77        struct sockaddr_in addr;
     78        dnsr_hostinfo_t *hinfo = NULL;
     79        int rc;
     80        char *endptr;
     81
     82        addr.sin_family = AF_INET;
     83
     84        rc = inet_pton(addr.sin_family, addr_s, (uint8_t *)&addr.sin_addr);
    8785        if (rc != EOK) {
    88                 /* Interpret as a host name */
    89                 dnsr_hostinfo_t *hinfo = NULL;
    90                 rc = dnsr_name2host(host, &hinfo, ip_any);
    91                
     86                /* Try interpreting as a host name */
     87                rc = dnsr_name2host(addr_s, &hinfo);
    9288                if (rc != EOK) {
    93                         printf("Error resolving host '%s'.\n", host);
     89                        printf("Error resolving host '%s'.\n", addr_s);
    9490                        goto error;
    9591                }
    96                
    97                 iaddr = hinfo->addr;
     92
     93                addr.sin_addr.s_addr = host2uint32_t_be(hinfo->addr.ipv4);
    9894        }
    99        
    100         char *endptr;
    101         uint16_t port = strtol(port_s, &endptr, 10);
     95
     96        addr.sin_port = htons(strtol(port_s, &endptr, 10));
    10297        if (*endptr != '\0') {
    10398                printf("Invalid port number %s\n", port_s);
    10499                goto error;
    105100        }
    106        
    107         rc = inet_addr_sockaddr(&iaddr, port, &saddr, &saddrlen);
    108         if (rc != EOK) {
    109                 assert(rc == ENOMEM);
    110                 printf("Out of memory.\n");
    111                 return ENOMEM;
    112         }
    113        
    114         printf("Connecting to host %s port %u\n", host, port);
    115        
    116         conn_fd = socket(saddr->sa_family, SOCK_STREAM, 0);
     101
     102        conn_fd = socket(PF_INET, SOCK_STREAM, 0);
    117103        if (conn_fd < 0)
    118104                goto error;
    119        
    120         rc = connect(conn_fd, saddr, saddrlen);
     105
     106        printf("Connecting to host %s port %u\n", addr_s, ntohs(addr.sin_port));
     107
     108        rc = connect(conn_fd, (struct sockaddr *)&addr, sizeof(addr));
    121109        if (rc != EOK)
    122110                goto error;
    123        
     111
    124112        rcv_fid = fibril_create(rcv_fibril, NULL);
    125113        if (rcv_fid == 0)
    126114                goto error;
    127        
     115
    128116        fibril_add_ready(rcv_fid);
    129        
    130         free(saddr);
     117
    131118        return EOK;
     119
    132120error:
    133121        if (conn_fd >= 0) {
     
    135123                conn_fd = -1;
    136124        }
    137         free(saddr);
    138        
     125
    139126        return EIO;
    140127}
     
    142129int conn_send(void *data, size_t size)
    143130{
    144         int rc = send(conn_fd, data, size, 0);
     131        int rc;
     132
     133        rc = send(conn_fd, data, size, 0);
    145134        if (rc != EOK)
    146135                return EIO;
    147        
     136
    148137        return EOK;
    149138}
Note: See TracChangeset for help on using the changeset viewer.