| 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/endpoint.h>
|
|---|
| 40 | #include <inet/hostport.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 |
|
|---|
| 50 | static tcp_t *tcp;
|
|---|
| 51 | static tcp_conn_t *conn;
|
|---|
| 52 |
|
|---|
| 53 | #define RECV_BUF_SIZE 1024
|
|---|
| 54 | static uint8_t recv_buf[RECV_BUF_SIZE];
|
|---|
| 55 |
|
|---|
| 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)
|
|---|
| 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 |
|
|---|
| 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);
|
|---|
| 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;
|
|---|
| 121 | error:
|
|---|
| 122 | tcp_conn_destroy(conn);
|
|---|
| 123 | tcp_destroy(tcp);
|
|---|
| 124 |
|
|---|
| 125 | return EIO;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | int conn_send(void *data, size_t size)
|
|---|
| 129 | {
|
|---|
| 130 | int rc = tcp_conn_send(conn, data, size);
|
|---|
| 131 | if (rc != EOK)
|
|---|
| 132 | return EIO;
|
|---|
| 133 |
|
|---|
| 134 | return EOK;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | /** @}
|
|---|
| 138 | */
|
|---|