Changes in uspace/app/nterm/conn.c [a62ceaf:0aa70f4] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/nterm/conn.c
ra62ceaf r0aa70f4 37 37 #include <errno.h> 38 38 #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> 42 41 #include <stdio.h> 43 #include <stdlib.h>44 42 #include <str_error.h> 45 43 #include <sys/types.h> … … 48 46 #include "nterm.h" 49 47 50 static tcp_t *tcp;51 static tcp_conn_t *conn;48 static int conn_fd; 49 static fid_t rcv_fid; 52 50 53 51 #define RECV_BUF_SIZE 1024 54 52 static uint8_t recv_buf[RECV_BUF_SIZE]; 55 53 56 static void conn_conn_reset(tcp_conn_t *); 57 static void conn_data_avail(tcp_conn_t *); 54 static int rcv_fibril(void *arg) 55 { 56 ssize_t nr; 58 57 59 static tcp_cb_t conn_cb ={60 .conn_reset = conn_conn_reset,61 .data_avail = conn_data_avail62 };58 while (true) { 59 nr = recv(conn_fd, recv_buf, RECV_BUF_SIZE, 0); 60 if (nr < 0) 61 break; 63 62 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; 67 73 } 68 74 69 static void conn_data_avail(tcp_conn_t *conn)75 int conn_open(const char *addr_s, const char *port_s) 70 76 { 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 76 88 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; 79 91 } 80 81 nterm_received(recv_buf, nrecv); 82 83 if (nrecv != RECV_BUF_SIZE) 84 break; 92 93 addr_addr = hinfo->addr; 85 94 } 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); 99 104 goto error; 100 105 } 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 105 127 if (rc != EOK) 106 128 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) 110 132 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 120 136 return EOK; 137 121 138 error: 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 125 144 return EIO; 126 145 } … … 128 147 int conn_send(void *data, size_t size) 129 148 { 130 int rc = tcp_conn_send(conn, data, size);149 int rc = send(conn_fd, data, size, 0); 131 150 if (rc != EOK) 132 151 return EIO; 133 152 134 153 return EOK; 135 154 }
Note:
See TracChangeset
for help on using the changeset viewer.