Ignore:
File:
1 edited

Legend:

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

    r02a09ed rfff7ef4  
    7575int conn_open(const char *addr_s, const char *port_s)
    7676{
    77         int conn_fd = -1;
    78        
    79         /* Interpret as address */
    80         inet_addr_t addr_addr;
    81         int rc = inet_addr_parse(addr_s, &addr_addr);
    82        
     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);
    8385        if (rc != EOK) {
    84                 /* Interpret as a host name */
    85                 dnsr_hostinfo_t *hinfo = NULL;
     86                /* Try interpreting as a host name */
    8687                rc = dnsr_name2host(addr_s, &hinfo);
    87                
    8888                if (rc != EOK) {
    8989                        printf("Error resolving host '%s'.\n", addr_s);
    9090                        goto error;
    9191                }
    92                
    93                 addr_addr = hinfo->addr;
     92
     93                addr.sin_addr.s_addr = host2uint32_t_be(hinfo->addr.ipv4);
    9494        }
    95        
    96         struct sockaddr_in addr;
    97         struct sockaddr_in6 addr6;
    98         uint16_t af = inet_addr_sockaddr_in(&addr_addr, &addr, &addr6);
    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         printf("Connecting to host %s port %u\n", addr_s, port);
    108        
     101
    109102        conn_fd = socket(PF_INET, SOCK_STREAM, 0);
    110103        if (conn_fd < 0)
    111104                goto error;
    112        
    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        
     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));
    127109        if (rc != EOK)
    128110                goto error;
    129        
     111
    130112        rcv_fid = fibril_create(rcv_fibril, NULL);
    131113        if (rcv_fid == 0)
    132114                goto error;
    133        
     115
    134116        fibril_add_ready(rcv_fid);
    135        
     117
    136118        return EOK;
    137        
     119
    138120error:
    139121        if (conn_fd >= 0) {
     
    141123                conn_fd = -1;
    142124        }
    143        
     125
    144126        return EIO;
    145127}
     
    147129int conn_send(void *data, size_t size)
    148130{
    149         int rc = send(conn_fd, data, size, 0);
     131        int rc;
     132
     133        rc = send(conn_fd, data, size, 0);
    150134        if (rc != EOK)
    151135                return EIO;
    152        
     136
    153137        return EOK;
    154138}
Note: See TracChangeset for help on using the changeset viewer.