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

Last change on this file was b7fd2a0, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Use errno_t in all uspace and kernel code.

Change type of every variable, parameter and return value that holds an
<errno.h> constant to either errno_t (the usual case), or sys_errno_t
(some places in kernel). This is for the purpose of self-documentation,
as well as for type-checking with a bit of type definition hackery.

Although this is a massive commit, it is a simple text replacement, and thus
is very easy to verify. Simply do the following:

`
git checkout <this commit's hash>
git reset HEAD
git add .
tools/srepl '\berrno_t\b' int
git add .
tools/srepl '\bsys_errno_t\b' sysarg_t
git reset
git diff
`

While this doesn't ensure that the replacements are correct, it does ensure
that the commit doesn't do anything except those replacements. Since errno_t
is typedef'd to int in the usual case (and sys_errno_t to sysarg_t), even if
incorrect, this commit cannot change behavior.

  • Property mode set to 100644
File size: 3.3 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 <errno.h>
37#include <fibril.h>
38#include <inet/endpoint.h>
39#include <inet/hostport.h>
40#include <inet/tcp.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <stdbool.h>
44#include <stddef.h>
45#include <str_error.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 errno_t 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: %s]\n", str_error_name(rc));
78 break;
79 }
80
81 nterm_received(recv_buf, nrecv);
82
83 if (nrecv != RECV_BUF_SIZE)
84 break;
85 }
86}
87
88errno_t conn_open(const char *hostport)
89{
90 inet_ep2_t epp;
91 const char *errmsg;
92 errno_t 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);
99 goto error;
100 }
101
102 printf("Connecting to %s\n", hostport);
103 char *s;
104 rc = inet_addr_format(&epp.remote.addr, &s);
105 if (rc != EOK)
106 goto error;
107
108 rc = tcp_create(&tcp);
109 if (rc != EOK)
110 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
120 return EOK;
121error:
122 tcp_conn_destroy(conn);
123 tcp_destroy(tcp);
124
125 return EIO;
126}
127
128errno_t conn_send(void *data, size_t size)
129{
130 errno_t rc = tcp_conn_send(conn, data, size);
131 if (rc != EOK)
132 return EIO;
133
134 return EOK;
135}
136
137/** @}
138 */
Note: See TracBrowser for help on using the repository browser.