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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c70703a was 204ba47, checked in by Jiri Svoboda <jiri@…>, 11 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
RevLine 
[01a7aa1]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
[287d729]35#include <byteorder.h>
[3e6a98c5]36#include <stdbool.h>
[01a7aa1]37#include <errno.h>
38#include <fibril.h>
[287d729]39#include <inet/dnsr.h>
[fab2746]40#include <inet/endpoint.h>
41#include <inet/tcp.h>
[01a7aa1]42#include <stdio.h>
[26de91a]43#include <stdlib.h>
[7c912b6]44#include <str_error.h>
[01a7aa1]45#include <sys/types.h>
46
47#include "conn.h"
48#include "nterm.h"
49
[fab2746]50static tcp_t *tcp;
51static tcp_conn_t *conn;
[01a7aa1]52
[7c912b6]53#define RECV_BUF_SIZE 1024
[01a7aa1]54static uint8_t recv_buf[RECV_BUF_SIZE];
55
[fab2746]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)
[01a7aa1]70{
[fab2746]71 int rc;
72 size_t nrecv;
[01a7aa1]73
74 while (true) {
[fab2746]75 rc = tcp_conn_recv(conn, recv_buf, RECV_BUF_SIZE, &nrecv);
76 if (rc != EOK) {
[204ba47]77 printf("\n[Receive error %d]\n", rc);
[01a7aa1]78 break;
[fab2746]79 }
[01a7aa1]80
[fab2746]81 nterm_received(recv_buf, nrecv);
[01a7aa1]82
[fab2746]83 if (nrecv != RECV_BUF_SIZE)
84 break;
85 }
[01a7aa1]86}
87
[26de91a]88int conn_open(const char *host, const char *port_s)
[01a7aa1]89{
[fab2746]90 inet_ep2_t epp;
91
[02a09ed]92 /* Interpret as address */
[26de91a]93 inet_addr_t iaddr;
94 int rc = inet_addr_parse(host, &iaddr);
[fab2746]95
[01a7aa1]96 if (rc != EOK) {
[02a09ed]97 /* Interpret as a host name */
98 dnsr_hostinfo_t *hinfo = NULL;
[26de91a]99 rc = dnsr_name2host(host, &hinfo, ip_any);
[fab2746]100
[287d729]101 if (rc != EOK) {
[26de91a]102 printf("Error resolving host '%s'.\n", host);
[287d729]103 goto error;
104 }
[fab2746]105
[26de91a]106 iaddr = hinfo->addr;
[01a7aa1]107 }
[fab2746]108
[02a09ed]109 char *endptr;
110 uint16_t port = strtol(port_s, &endptr, 10);
[01a7aa1]111 if (*endptr != '\0') {
112 printf("Invalid port number %s\n", port_s);
[287d729]113 goto error;
[01a7aa1]114 }
[fab2746]115
116 inet_ep2_init(&epp);
117 epp.remote.addr = iaddr;
118 epp.remote.port = port;
119
[26de91a]120 printf("Connecting to host %s port %u\n", host, port);
[fab2746]121
122 rc = tcp_create(&tcp);
123 if (rc != EOK)
[02a09ed]124 goto error;
[fab2746]125
126 rc = tcp_conn_create(tcp, &epp, &conn_cb, NULL, &conn);
[01a7aa1]127 if (rc != EOK)
128 goto error;
[fab2746]129
130 rc = tcp_conn_wait_connected(conn);
131 if (rc != EOK)
[01a7aa1]132 goto error;
[fab2746]133
[01a7aa1]134 return EOK;
135error:
[fab2746]136 tcp_conn_destroy(conn);
137 tcp_destroy(tcp);
138
[01a7aa1]139 return EIO;
140}
141
142int conn_send(void *data, size_t size)
143{
[fab2746]144 int rc = tcp_conn_send(conn, data, size);
[01a7aa1]145 if (rc != EOK)
146 return EIO;
[fab2746]147
[01a7aa1]148 return EOK;
149}
150
151/** @}
152 */
Note: See TracBrowser for help on using the repository browser.