Ignore:
File:
1 edited

Legend:

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

    r0d67e16 r26de91a  
    3333 */
    3434
    35 #include <bool.h>
     35#include <byteorder.h>
     36#include <stdbool.h>
    3637#include <errno.h>
    3738#include <fibril.h>
     39#include <inet/dnsr.h>
     40#include <net/inet.h>
    3841#include <net/socket.h>
    3942#include <stdio.h>
     43#include <stdlib.h>
    4044#include <str_error.h>
    4145#include <sys/types.h>
     
    7175}
    7276
    73 int conn_open(const char *addr_s, const char *port_s)
     77int conn_open(const char *host, const char *port_s)
    7478{
    75         struct sockaddr_in addr;
    76         int rc;
     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       
     87        if (rc != EOK) {
     88                /* Interpret as a host name */
     89                dnsr_hostinfo_t *hinfo = NULL;
     90                rc = dnsr_name2host(host, &hinfo, ip_any);
     91               
     92                if (rc != EOK) {
     93                        printf("Error resolving host '%s'.\n", host);
     94                        goto error;
     95                }
     96               
     97                iaddr = hinfo->addr;
     98        }
     99       
    77100        char *endptr;
    78 
    79         addr.sin_family = AF_INET;
    80 
    81         rc = inet_pton(addr.sin_family, addr_s, (uint8_t *)&addr.sin_addr);
    82         if (rc != EOK) {
    83                 printf("Invalid addres %s\n", addr_s);
    84                 return EINVAL;
    85         }
    86 
    87         addr.sin_port = htons(strtol(port_s, &endptr, 10));
     101        uint16_t port = strtol(port_s, &endptr, 10);
    88102        if (*endptr != '\0') {
    89103                printf("Invalid port number %s\n", port_s);
    90                 return EINVAL;
     104                goto error;
    91105        }
    92 
    93         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);
    94117        if (conn_fd < 0)
    95118                goto error;
    96 
    97         printf("Connecting to address %s port %u\n", addr_s, ntohs(addr.sin_port));
    98 
    99         rc = connect(conn_fd, (struct sockaddr *)&addr, sizeof(addr));
     119       
     120        rc = connect(conn_fd, saddr, saddrlen);
    100121        if (rc != EOK)
    101122                goto error;
    102 
     123       
    103124        rcv_fid = fibril_create(rcv_fibril, NULL);
    104125        if (rcv_fid == 0)
    105126                goto error;
    106 
     127       
    107128        fibril_add_ready(rcv_fid);
    108 
     129       
     130        free(saddr);
    109131        return EOK;
    110 
    111132error:
    112133        if (conn_fd >= 0) {
     
    114135                conn_fd = -1;
    115136        }
    116 
     137        free(saddr);
     138       
    117139        return EIO;
    118140}
     
    120142int conn_send(void *data, size_t size)
    121143{
    122         int rc;
    123 
    124         rc = send(conn_fd, data, size, 0);
     144        int rc = send(conn_fd, data, size, 0);
    125145        if (rc != EOK)
    126146                return EIO;
    127 
     147       
    128148        return EOK;
    129149}
Note: See TracChangeset for help on using the changeset viewer.