source: mainline/uspace/app/nterm/conn.c@ fc22069

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since fc22069 was 204ba47, checked in by Jiri Svoboda <jiri@…>, 10 years ago

Fix data_avail callback. Fix tcp_conn_recv(). Do not generate spurious data_avail callbacks.

  • Property mode set to 100644
File size: 3.5 KB
Line 
1/*
2 * Copyright (c) 2012 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup nterm
30 * @{
31 */
32/** @file
33 */
34
35#include <byteorder.h>
36#include <stdbool.h>
37#include <errno.h>
38#include <fibril.h>
39#include <inet/dnsr.h>
40#include <inet/endpoint.h>
41#include <inet/tcp.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <str_error.h>
45#include <sys/types.h>
46
47#include "conn.h"
48#include "nterm.h"
49
50static tcp_t *tcp;
51static tcp_conn_t *conn;
52
53#define RECV_BUF_SIZE 1024
54static uint8_t recv_buf[RECV_BUF_SIZE];
55
56static void conn_conn_reset(tcp_conn_t *);
57static void conn_data_avail(tcp_conn_t *);
58
59static tcp_cb_t conn_cb = {
60 .conn_reset = conn_conn_reset,
61 .data_avail = conn_data_avail
62};
63
64static void conn_conn_reset(tcp_conn_t *conn)
65{
66 printf("\n[Connection reset]\n");
67}
68
69static void conn_data_avail(tcp_conn_t *conn)
70{
71 int rc;
72 size_t nrecv;
73
74 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);
78 break;
79 }
80
81 nterm_received(recv_buf, nrecv);
82
83 if (nrecv != RECV_BUF_SIZE)
84 break;
85 }
86}
87
88int conn_open(const char *host, const char *port_s)
89{
90 inet_ep2_t epp;
91
92 /* Interpret as address */
93 inet_addr_t iaddr;
94 int rc = inet_addr_parse(host, &iaddr);
95
96 if (rc != EOK) {
97 /* Interpret as a host name */
98 dnsr_hostinfo_t *hinfo = NULL;
99 rc = dnsr_name2host(host, &hinfo, ip_any);
100
101 if (rc != EOK) {
102 printf("Error resolving host '%s'.\n", host);
103 goto error;
104 }
105
106 iaddr = hinfo->addr;
107 }
108
109 char *endptr;
110 uint16_t port = strtol(port_s, &endptr, 10);
111 if (*endptr != '\0') {
112 printf("Invalid port number %s\n", port_s);
113 goto error;
114 }
115
116 inet_ep2_init(&epp);
117 epp.remote.addr = iaddr;
118 epp.remote.port = port;
119
120 printf("Connecting to host %s port %u\n", host, port);
121
122 rc = tcp_create(&tcp);
123 if (rc != EOK)
124 goto error;
125
126 rc = tcp_conn_create(tcp, &epp, &conn_cb, NULL, &conn);
127 if (rc != EOK)
128 goto error;
129
130 rc = tcp_conn_wait_connected(conn);
131 if (rc != EOK)
132 goto error;
133
134 return EOK;
135error:
136 tcp_conn_destroy(conn);
137 tcp_destroy(tcp);
138
139 return EIO;
140}
141
142int conn_send(void *data, size_t size)
143{
144 int rc = tcp_conn_send(conn, data, size);
145 if (rc != EOK)
146 return EIO;
147
148 return EOK;
149}
150
151/** @}
152 */
Note: See TracBrowser for help on using the repository browser.