[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 |
|
---|
| 35 | #include <bool.h>
|
---|
| 36 | #include <errno.h>
|
---|
| 37 | #include <fibril.h>
|
---|
| 38 | #include <net/socket.h>
|
---|
| 39 | #include <stdio.h>
|
---|
[7c912b6] | 40 | #include <str_error.h>
|
---|
[01a7aa1] | 41 | #include <sys/types.h>
|
---|
| 42 |
|
---|
| 43 | #include "conn.h"
|
---|
| 44 | #include "nterm.h"
|
---|
| 45 |
|
---|
| 46 | static int conn_fd;
|
---|
| 47 | static fid_t rcv_fid;
|
---|
| 48 |
|
---|
[7c912b6] | 49 | #define RECV_BUF_SIZE 1024
|
---|
[01a7aa1] | 50 | static uint8_t recv_buf[RECV_BUF_SIZE];
|
---|
| 51 |
|
---|
| 52 | static int rcv_fibril(void *arg)
|
---|
| 53 | {
|
---|
| 54 | ssize_t nr;
|
---|
| 55 |
|
---|
| 56 | while (true) {
|
---|
| 57 | nr = recv(conn_fd, recv_buf, RECV_BUF_SIZE, 0);
|
---|
| 58 | if (nr < 0)
|
---|
| 59 | break;
|
---|
| 60 |
|
---|
| 61 | nterm_received(recv_buf, nr);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[7c912b6] | 64 | if (nr == ENOTCONN)
|
---|
| 65 | printf("\n[Other side has closed the connection]\n");
|
---|
| 66 | else
|
---|
| 67 | printf("'\n[Receive errror (%s)]\n", str_error(nr));
|
---|
[01a7aa1] | 68 |
|
---|
[0d67e16] | 69 | exit(0);
|
---|
[01a7aa1] | 70 | return 0;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | int conn_open(const char *addr_s, const char *port_s)
|
---|
| 74 | {
|
---|
| 75 | struct sockaddr_in addr;
|
---|
| 76 | int rc;
|
---|
| 77 | char *endptr;
|
---|
| 78 |
|
---|
| 79 | addr.sin_family = AF_INET;
|
---|
| 80 |
|
---|
| 81 | rc = inet_pton(addr.sin_family, addr_s, (uint8_t *)&addr.sin_addr);
|
---|
| 82 | if (rc != EOK) {
|
---|
| 83 | printf("Invalid addres %s\n", addr_s);
|
---|
| 84 | return EINVAL;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | addr.sin_port = htons(strtol(port_s, &endptr, 10));
|
---|
| 88 | if (*endptr != '\0') {
|
---|
| 89 | printf("Invalid port number %s\n", port_s);
|
---|
| 90 | return EINVAL;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | conn_fd = socket(PF_INET, SOCK_STREAM, 0);
|
---|
| 94 | if (conn_fd < 0)
|
---|
| 95 | goto error;
|
---|
| 96 |
|
---|
| 97 | printf("Connecting to address %s port %u\n", addr_s, ntohs(addr.sin_port));
|
---|
| 98 |
|
---|
| 99 | rc = connect(conn_fd, (struct sockaddr *)&addr, sizeof(addr));
|
---|
| 100 | if (rc != EOK)
|
---|
| 101 | goto error;
|
---|
| 102 |
|
---|
| 103 | rcv_fid = fibril_create(rcv_fibril, NULL);
|
---|
| 104 | if (rcv_fid == 0)
|
---|
| 105 | goto error;
|
---|
| 106 |
|
---|
| 107 | fibril_add_ready(rcv_fid);
|
---|
| 108 |
|
---|
| 109 | return EOK;
|
---|
| 110 |
|
---|
| 111 | error:
|
---|
| 112 | if (conn_fd >= 0) {
|
---|
| 113 | closesocket(conn_fd);
|
---|
| 114 | conn_fd = -1;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | return EIO;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | int conn_send(void *data, size_t size)
|
---|
| 121 | {
|
---|
| 122 | int rc;
|
---|
| 123 |
|
---|
| 124 | rc = send(conn_fd, data, size, 0);
|
---|
| 125 | if (rc != EOK)
|
---|
| 126 | return EIO;
|
---|
| 127 |
|
---|
| 128 | return EOK;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | /** @}
|
---|
| 132 | */
|
---|