Ignore:
File:
1 edited

Legend:

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

    rfff7ef4 r26de91a  
    3838#include <fibril.h>
    3939#include <inet/dnsr.h>
     40#include <net/inet.h>
    4041#include <net/socket.h>
    4142#include <stdio.h>
     43#include <stdlib.h>
    4244#include <str_error.h>
    4345#include <sys/types.h>
     
    7375}
    7476
    75 int conn_open(const char *addr_s, const char *port_s)
     77int conn_open(const char *host, const char *port_s)
    7678{
    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);
     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       
    8587        if (rc != EOK) {
    86                 /* Try interpreting as a host name */
    87                 rc = dnsr_name2host(addr_s, &hinfo);
     88                /* Interpret as a host name */
     89                dnsr_hostinfo_t *hinfo = NULL;
     90                rc = dnsr_name2host(host, &hinfo, ip_any);
     91               
    8892                if (rc != EOK) {
    89                         printf("Error resolving host '%s'.\n", addr_s);
     93                        printf("Error resolving host '%s'.\n", host);
    9094                        goto error;
    9195                }
    92 
    93                 addr.sin_addr.s_addr = host2uint32_t_be(hinfo->addr.ipv4);
     96               
     97                iaddr = hinfo->addr;
    9498        }
    95 
    96         addr.sin_port = htons(strtol(port_s, &endptr, 10));
     99       
     100        char *endptr;
     101        uint16_t port = strtol(port_s, &endptr, 10);
    97102        if (*endptr != '\0') {
    98103                printf("Invalid port number %s\n", port_s);
    99104                goto error;
    100105        }
    101 
    102         conn_fd = socket(PF_INET, SOCK_STREAM, 0);
     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);
    103117        if (conn_fd < 0)
    104118                goto error;
    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));
     119       
     120        rc = connect(conn_fd, saddr, saddrlen);
    109121        if (rc != EOK)
    110122                goto error;
    111 
     123       
    112124        rcv_fid = fibril_create(rcv_fibril, NULL);
    113125        if (rcv_fid == 0)
    114126                goto error;
    115 
     127       
    116128        fibril_add_ready(rcv_fid);
    117 
     129       
     130        free(saddr);
    118131        return EOK;
    119 
    120132error:
    121133        if (conn_fd >= 0) {
     
    123135                conn_fd = -1;
    124136        }
    125 
     137        free(saddr);
     138       
    126139        return EIO;
    127140}
     
    129142int conn_send(void *data, size_t size)
    130143{
    131         int rc;
    132 
    133         rc = send(conn_fd, data, size, 0);
     144        int rc = send(conn_fd, data, size, 0);
    134145        if (rc != EOK)
    135146                return EIO;
    136 
     147       
    137148        return EOK;
    138149}
Note: See TracChangeset for help on using the changeset viewer.