Ignore:
File:
1 edited

Legend:

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

    r204ba47 r26de91a  
    3838#include <fibril.h>
    3939#include <inet/dnsr.h>
    40 #include <inet/endpoint.h>
    41 #include <inet/tcp.h>
     40#include <net/inet.h>
     41#include <net/socket.h>
    4242#include <stdio.h>
    4343#include <stdlib.h>
     
    4848#include "nterm.h"
    4949
    50 static tcp_t *tcp;
    51 static tcp_conn_t *conn;
     50static int conn_fd;
     51static fid_t rcv_fid;
    5252
    5353#define RECV_BUF_SIZE 1024
    5454static uint8_t recv_buf[RECV_BUF_SIZE];
    5555
    56 static void conn_conn_reset(tcp_conn_t *);
    57 static void conn_data_avail(tcp_conn_t *);
    58 
    59 static tcp_cb_t conn_cb = {
    60         .conn_reset = conn_conn_reset,
    61         .data_avail = conn_data_avail
    62 };
    63 
    64 static void conn_conn_reset(tcp_conn_t *conn)
     56static int rcv_fibril(void *arg)
    6557{
    66         printf("\n[Connection reset]\n");
    67 }
    68 
    69 static void conn_data_avail(tcp_conn_t *conn)
    70 {
    71         int rc;
    72         size_t nrecv;
     58        ssize_t nr;
    7359
    7460        while (true) {
    75                 rc = tcp_conn_recv(conn, recv_buf, RECV_BUF_SIZE, &nrecv);
    76                 if (rc != EOK) {
    77                         printf("\n[Receive error %d]\n", rc);
     61                nr = recv(conn_fd, recv_buf, RECV_BUF_SIZE, 0);
     62                if (nr < 0)
    7863                        break;
    79                 }
    8064
    81                 nterm_received(recv_buf, nrecv);
     65                nterm_received(recv_buf, nr);
     66        }
    8267
    83                 if (nrecv != RECV_BUF_SIZE)
    84                         break;
    85         }
     68        if (nr == ENOTCONN)
     69                printf("\n[Other side has closed the connection]\n");
     70        else
     71                printf("'\n[Receive errror (%s)]\n", str_error(nr));
     72
     73        exit(0);
     74        return 0;
    8675}
    8776
    8877int conn_open(const char *host, const char *port_s)
    8978{
    90         inet_ep2_t epp;
    91 
     79        int conn_fd = -1;
     80        struct sockaddr *saddr = NULL;
     81        socklen_t saddrlen;
     82       
    9283        /* Interpret as address */
    9384        inet_addr_t iaddr;
    9485        int rc = inet_addr_parse(host, &iaddr);
    95 
     86       
    9687        if (rc != EOK) {
    9788                /* Interpret as a host name */
    9889                dnsr_hostinfo_t *hinfo = NULL;
    9990                rc = dnsr_name2host(host, &hinfo, ip_any);
    100 
     91               
    10192                if (rc != EOK) {
    10293                        printf("Error resolving host '%s'.\n", host);
    10394                        goto error;
    10495                }
    105 
     96               
    10697                iaddr = hinfo->addr;
    10798        }
    108 
     99       
    109100        char *endptr;
    110101        uint16_t port = strtol(port_s, &endptr, 10);
     
    113104                goto error;
    114105        }
    115 
    116         inet_ep2_init(&epp);
    117         epp.remote.addr = iaddr;
    118         epp.remote.port = port;
    119 
     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       
    120114        printf("Connecting to host %s port %u\n", host, port);
    121 
    122         rc = tcp_create(&tcp);
     115       
     116        conn_fd = socket(saddr->sa_family, SOCK_STREAM, 0);
     117        if (conn_fd < 0)
     118                goto error;
     119       
     120        rc = connect(conn_fd, saddr, saddrlen);
    123121        if (rc != EOK)
    124122                goto error;
    125 
    126         rc = tcp_conn_create(tcp, &epp, &conn_cb, NULL, &conn);
    127         if (rc != EOK)
     123       
     124        rcv_fid = fibril_create(rcv_fibril, NULL);
     125        if (rcv_fid == 0)
    128126                goto error;
    129 
    130         rc = tcp_conn_wait_connected(conn);
    131         if (rc != EOK)
    132                 goto error;
    133 
     127       
     128        fibril_add_ready(rcv_fid);
     129       
     130        free(saddr);
    134131        return EOK;
    135132error:
    136         tcp_conn_destroy(conn);
    137         tcp_destroy(tcp);
    138 
     133        if (conn_fd >= 0) {
     134                closesocket(conn_fd);
     135                conn_fd = -1;
     136        }
     137        free(saddr);
     138       
    139139        return EIO;
    140140}
     
    142142int conn_send(void *data, size_t size)
    143143{
    144         int rc = tcp_conn_send(conn, data, size);
     144        int rc = send(conn_fd, data, size, 0);
    145145        if (rc != EOK)
    146146                return EIO;
    147 
     147       
    148148        return EOK;
    149149}
Note: See TracChangeset for help on using the changeset viewer.