[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>
|
---|
[01a7aa1] | 36 | #include <errno.h>
|
---|
| 37 | #include <fibril.h>
|
---|
[fab2746] | 38 | #include <inet/endpoint.h>
|
---|
[a62ceaf] | 39 | #include <inet/hostport.h>
|
---|
[fab2746] | 40 | #include <inet/tcp.h>
|
---|
[01a7aa1] | 41 | #include <stdio.h>
|
---|
[26de91a] | 42 | #include <stdlib.h>
|
---|
[8d2dd7f2] | 43 | #include <stdbool.h>
|
---|
| 44 | #include <stddef.h>
|
---|
[7c912b6] | 45 | #include <str_error.h>
|
---|
[01a7aa1] | 46 |
|
---|
| 47 | #include "conn.h"
|
---|
| 48 | #include "nterm.h"
|
---|
| 49 |
|
---|
[fab2746] | 50 | static tcp_t *tcp;
|
---|
| 51 | static tcp_conn_t *conn;
|
---|
[01a7aa1] | 52 |
|
---|
[7c912b6] | 53 | #define RECV_BUF_SIZE 1024
|
---|
[01a7aa1] | 54 | static uint8_t recv_buf[RECV_BUF_SIZE];
|
---|
| 55 |
|
---|
[fab2746] | 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)
|
---|
| 65 | {
|
---|
| 66 | printf("\n[Connection reset]\n");
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | static void conn_data_avail(tcp_conn_t *conn)
|
---|
[01a7aa1] | 70 | {
|
---|
[b7fd2a0] | 71 | errno_t rc;
|
---|
[fab2746] | 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) {
|
---|
[c1694b6b] | 77 | printf("\n[Receive error: %s]\n", str_error_name(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 |
|
---|
[b7fd2a0] | 88 | errno_t conn_open(const char *hostport)
|
---|
[01a7aa1] | 89 | {
|
---|
[fab2746] | 90 | inet_ep2_t epp;
|
---|
[a62ceaf] | 91 | const char *errmsg;
|
---|
[b7fd2a0] | 92 | errno_t rc;
|
---|
[fab2746] | 93 |
|
---|
[a62ceaf] | 94 | inet_ep2_init(&epp);
|
---|
| 95 | rc = inet_hostport_plookup_one(hostport, ip_any, &epp.remote, NULL,
|
---|
| 96 | &errmsg);
|
---|
[01a7aa1] | 97 | if (rc != EOK) {
|
---|
[a62ceaf] | 98 | printf("Error: %s (host:port %s).\n", errmsg, hostport);
|
---|
[287d729] | 99 | goto error;
|
---|
[01a7aa1] | 100 | }
|
---|
[fab2746] | 101 |
|
---|
[a62ceaf] | 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;
|
---|
[fab2746] | 107 |
|
---|
| 108 | rc = tcp_create(&tcp);
|
---|
| 109 | if (rc != EOK)
|
---|
[02a09ed] | 110 | goto error;
|
---|
[fab2746] | 111 |
|
---|
| 112 | rc = tcp_conn_create(tcp, &epp, &conn_cb, NULL, &conn);
|
---|
[01a7aa1] | 113 | if (rc != EOK)
|
---|
| 114 | goto error;
|
---|
[fab2746] | 115 |
|
---|
| 116 | rc = tcp_conn_wait_connected(conn);
|
---|
| 117 | if (rc != EOK)
|
---|
[01a7aa1] | 118 | goto error;
|
---|
[fab2746] | 119 |
|
---|
[01a7aa1] | 120 | return EOK;
|
---|
| 121 | error:
|
---|
[fab2746] | 122 | tcp_conn_destroy(conn);
|
---|
| 123 | tcp_destroy(tcp);
|
---|
| 124 |
|
---|
[01a7aa1] | 125 | return EIO;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[b7fd2a0] | 128 | errno_t conn_send(void *data, size_t size)
|
---|
[01a7aa1] | 129 | {
|
---|
[b7fd2a0] | 130 | errno_t rc = tcp_conn_send(conn, data, size);
|
---|
[01a7aa1] | 131 | if (rc != EOK)
|
---|
| 132 | return EIO;
|
---|
[fab2746] | 133 |
|
---|
[01a7aa1] | 134 | return EOK;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | /** @}
|
---|
| 138 | */
|
---|