Ignore:
File:
1 edited

Legend:

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

    ra62ceaf r0aa70f4  
    3737#include <errno.h>
    3838#include <fibril.h>
    39 #include <inet/endpoint.h>
    40 #include <inet/hostport.h>
    41 #include <inet/tcp.h>
     39#include <inet/dnsr.h>
     40#include <net/socket.h>
    4241#include <stdio.h>
    43 #include <stdlib.h>
    4442#include <str_error.h>
    4543#include <sys/types.h>
     
    4846#include "nterm.h"
    4947
    50 static tcp_t *tcp;
    51 static tcp_conn_t *conn;
     48static int conn_fd;
     49static fid_t rcv_fid;
    5250
    5351#define RECV_BUF_SIZE 1024
    5452static uint8_t recv_buf[RECV_BUF_SIZE];
    5553
    56 static void conn_conn_reset(tcp_conn_t *);
    57 static void conn_data_avail(tcp_conn_t *);
     54static int rcv_fibril(void *arg)
     55{
     56        ssize_t nr;
    5857
    59 static tcp_cb_t conn_cb = {
    60         .conn_reset = conn_conn_reset,
    61         .data_avail = conn_data_avail
    62 };
     58        while (true) {
     59                nr = recv(conn_fd, recv_buf, RECV_BUF_SIZE, 0);
     60                if (nr < 0)
     61                        break;
    6362
    64 static void conn_conn_reset(tcp_conn_t *conn)
    65 {
    66         printf("\n[Connection reset]\n");
     63                nterm_received(recv_buf, nr);
     64        }
     65
     66        if (nr == ENOTCONN)
     67                printf("\n[Other side has closed the connection]\n");
     68        else
     69                printf("'\n[Receive errror (%s)]\n", str_error(nr));
     70
     71        exit(0);
     72        return 0;
    6773}
    6874
    69 static void conn_data_avail(tcp_conn_t *conn)
     75int conn_open(const char *addr_s, const char *port_s)
    7076{
    71         int rc;
    72         size_t nrecv;
    73 
    74         while (true) {
    75                 rc = tcp_conn_recv(conn, recv_buf, RECV_BUF_SIZE, &nrecv);
     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       
     83        if (rc != EOK) {
     84                /* Interpret as a host name */
     85                dnsr_hostinfo_t *hinfo = NULL;
     86                rc = dnsr_name2host(addr_s, &hinfo, 0);
     87               
    7688                if (rc != EOK) {
    77                         printf("\n[Receive error %d]\n", rc);
    78                         break;
     89                        printf("Error resolving host '%s'.\n", addr_s);
     90                        goto error;
    7991                }
    80 
    81                 nterm_received(recv_buf, nrecv);
    82 
    83                 if (nrecv != RECV_BUF_SIZE)
    84                         break;
     92               
     93                addr_addr = hinfo->addr;
    8594        }
    86 }
    87 
    88 int conn_open(const char *hostport)
    89 {
    90         inet_ep2_t epp;
    91         const char *errmsg;
    92         int rc;
    93 
    94         inet_ep2_init(&epp);
    95         rc = inet_hostport_plookup_one(hostport, ip_any, &epp.remote, NULL,
    96             &errmsg);
    97         if (rc != EOK) {
    98                 printf("Error: %s (host:port %s).\n", errmsg, hostport);
     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);
     102        if (*endptr != '\0') {
     103                printf("Invalid port number %s\n", port_s);
    99104                goto error;
    100105        }
    101 
    102         printf("Connecting to %s\n", hostport);
    103         char *s;
    104         rc = inet_addr_format(&epp.remote.addr, &s);
     106       
     107        printf("Connecting to host %s port %u\n", addr_s, port);
     108       
     109        conn_fd = socket(PF_INET, SOCK_STREAM, 0);
     110        if (conn_fd < 0)
     111                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       
    105127        if (rc != EOK)
    106128                goto error;
    107 
    108         rc = tcp_create(&tcp);
    109         if (rc != EOK)
     129       
     130        rcv_fid = fibril_create(rcv_fibril, NULL);
     131        if (rcv_fid == 0)
    110132                goto error;
    111 
    112         rc = tcp_conn_create(tcp, &epp, &conn_cb, NULL, &conn);
    113         if (rc != EOK)
    114                 goto error;
    115 
    116         rc = tcp_conn_wait_connected(conn);
    117         if (rc != EOK)
    118                 goto error;
    119 
     133       
     134        fibril_add_ready(rcv_fid);
     135       
    120136        return EOK;
     137       
    121138error:
    122         tcp_conn_destroy(conn);
    123         tcp_destroy(tcp);
    124 
     139        if (conn_fd >= 0) {
     140                closesocket(conn_fd);
     141                conn_fd = -1;
     142        }
     143       
    125144        return EIO;
    126145}
     
    128147int conn_send(void *data, size_t size)
    129148{
    130         int rc = tcp_conn_send(conn, data, size);
     149        int rc = send(conn_fd, data, size, 0);
    131150        if (rc != EOK)
    132151                return EIO;
    133 
     152       
    134153        return EOK;
    135154}
Note: See TracChangeset for help on using the changeset viewer.