Changeset 0c2d9bb in mainline for uspace/app/nterm/conn.c


Ignore:
Timestamp:
2013-12-25T22:54:29Z (10 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
b51cf2c
Parents:
f7a33de (diff), ac36aed (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

merge mainline changes

File:
1 edited

Legend:

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

    rf7a33de r0c2d9bb  
    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{
    7779        int conn_fd = -1;
     80        struct sockaddr *saddr = NULL;
     81        socklen_t saddrlen;
    7882       
    7983        /* Interpret as address */
    80         inet_addr_t addr_addr;
    81         int rc = inet_addr_parse(addr_s, &addr_addr);
     84        inet_addr_t iaddr;
     85        int rc = inet_addr_parse(host, &iaddr);
    8286       
    8387        if (rc != EOK) {
    8488                /* Interpret as a host name */
    8589                dnsr_hostinfo_t *hinfo = NULL;
    86                 rc = dnsr_name2host(addr_s, &hinfo, 0);
     90                rc = dnsr_name2host(host, &hinfo, ip_any);
    8791               
    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                }
    9296               
    93                 addr_addr = hinfo->addr;
     97                iaddr = hinfo->addr;
    9498        }
    95        
    96         struct sockaddr_in addr;
    97         struct sockaddr_in6 addr6;
    98         uint16_t af = inet_addr_sockaddr_in(&addr_addr, &addr, &addr6);
    9999       
    100100        char *endptr;
     
    105105        }
    106106       
    107         printf("Connecting to host %s port %u\n", addr_s, port);
     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        }
    108113       
    109         conn_fd = socket(PF_INET, SOCK_STREAM, 0);
     114        printf("Connecting to host %s port %u\n", host, port);
     115       
     116        conn_fd = socket(saddr->sa_family, SOCK_STREAM, 0);
    110117        if (conn_fd < 0)
    111118                goto error;
    112119       
    113         switch (af) {
    114         case AF_INET:
    115                 addr.sin_port = htons(port);
    116                 rc = connect(conn_fd, (struct sockaddr *) &addr, sizeof(addr));
    117                 break;
    118         case AF_INET6:
    119                 addr6.sin6_port = htons(port);
    120                 rc = connect(conn_fd, (struct sockaddr *) &addr6, sizeof(addr6));
    121                 break;
    122         default:
    123                 printf("Unknown address family.\n");
    124                 goto error;
    125         }
    126        
     120        rc = connect(conn_fd, saddr, saddrlen);
    127121        if (rc != EOK)
    128122                goto error;
     
    134128        fibril_add_ready(rcv_fid);
    135129       
     130        free(saddr);
    136131        return EOK;
    137        
    138132error:
    139133        if (conn_fd >= 0) {
     
    141135                conn_fd = -1;
    142136        }
     137        free(saddr);
    143138       
    144139        return EIO;
Note: See TracChangeset for help on using the changeset viewer.