Changes in uspace/app/nterm/conn.c [204ba47:26de91a] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/nterm/conn.c
r204ba47 r26de91a 38 38 #include <fibril.h> 39 39 #include <inet/dnsr.h> 40 #include < inet/endpoint.h>41 #include < inet/tcp.h>40 #include <net/inet.h> 41 #include <net/socket.h> 42 42 #include <stdio.h> 43 43 #include <stdlib.h> … … 48 48 #include "nterm.h" 49 49 50 static tcp_t *tcp;51 static tcp_conn_t *conn;50 static int conn_fd; 51 static fid_t rcv_fid; 52 52 53 53 #define RECV_BUF_SIZE 1024 54 54 static uint8_t recv_buf[RECV_BUF_SIZE]; 55 55 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) 56 static int rcv_fibril(void *arg) 65 57 { 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; 73 59 74 60 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) 78 63 break; 79 }80 64 81 nterm_received(recv_buf, nrecv); 65 nterm_received(recv_buf, nr); 66 } 82 67 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; 86 75 } 87 76 88 77 int conn_open(const char *host, const char *port_s) 89 78 { 90 inet_ep2_t epp; 91 79 int conn_fd = -1; 80 struct sockaddr *saddr = NULL; 81 socklen_t saddrlen; 82 92 83 /* Interpret as address */ 93 84 inet_addr_t iaddr; 94 85 int rc = inet_addr_parse(host, &iaddr); 95 86 96 87 if (rc != EOK) { 97 88 /* Interpret as a host name */ 98 89 dnsr_hostinfo_t *hinfo = NULL; 99 90 rc = dnsr_name2host(host, &hinfo, ip_any); 100 91 101 92 if (rc != EOK) { 102 93 printf("Error resolving host '%s'.\n", host); 103 94 goto error; 104 95 } 105 96 106 97 iaddr = hinfo->addr; 107 98 } 108 99 109 100 char *endptr; 110 101 uint16_t port = strtol(port_s, &endptr, 10); … … 113 104 goto error; 114 105 } 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 120 114 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); 123 121 if (rc != EOK) 124 122 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) 128 126 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); 134 131 return EOK; 135 132 error: 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 139 139 return EIO; 140 140 } … … 142 142 int conn_send(void *data, size_t size) 143 143 { 144 int rc = tcp_conn_send(conn, data, size);144 int rc = send(conn_fd, data, size, 0); 145 145 if (rc != EOK) 146 146 return EIO; 147 147 148 148 return EOK; 149 149 }
Note:
See TracChangeset
for help on using the changeset viewer.