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/dnsr.h>
|
---|
40 | #include <net/inet.h>
|
---|
41 | #include <net/socket.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 int conn_fd;
|
---|
51 | static fid_t rcv_fid;
|
---|
52 |
|
---|
53 | #define RECV_BUF_SIZE 1024
|
---|
54 | static uint8_t recv_buf[RECV_BUF_SIZE];
|
---|
55 |
|
---|
56 | static int rcv_fibril(void *arg)
|
---|
57 | {
|
---|
58 | ssize_t nr;
|
---|
59 |
|
---|
60 | while (true) {
|
---|
61 | nr = recv(conn_fd, recv_buf, RECV_BUF_SIZE, 0);
|
---|
62 | if (nr < 0)
|
---|
63 | break;
|
---|
64 |
|
---|
65 | nterm_received(recv_buf, nr);
|
---|
66 | }
|
---|
67 |
|
---|
68 | if (nr == ENOTCONN)
|
---|
69 | printf("\n[Other side has closed the connection]\n");
|
---|
70 | else
|
---|
71 | printf("'\n[Receive errror (%s)]\n", str_error(nr));
|
---|
72 |
|
---|
73 | exit(0);
|
---|
74 | return 0;
|
---|
75 | }
|
---|
76 |
|
---|
77 | int conn_open(const char *host, const char *port_s)
|
---|
78 | {
|
---|
79 | int conn_fd = -1;
|
---|
80 | struct sockaddr *saddr = NULL;
|
---|
81 | socklen_t saddrlen;
|
---|
82 |
|
---|
83 | /* Interpret as address */
|
---|
84 | inet_addr_t iaddr;
|
---|
85 | int rc = inet_addr_parse(host, &iaddr);
|
---|
86 |
|
---|
87 | if (rc != EOK) {
|
---|
88 | /* Interpret as a host name */
|
---|
89 | dnsr_hostinfo_t *hinfo = NULL;
|
---|
90 | rc = dnsr_name2host(host, &hinfo, ip_any);
|
---|
91 |
|
---|
92 | if (rc != EOK) {
|
---|
93 | printf("Error resolving host '%s'.\n", host);
|
---|
94 | goto error;
|
---|
95 | }
|
---|
96 |
|
---|
97 | iaddr = hinfo->addr;
|
---|
98 | }
|
---|
99 |
|
---|
100 | char *endptr;
|
---|
101 | uint16_t port = strtol(port_s, &endptr, 10);
|
---|
102 | if (*endptr != '\0') {
|
---|
103 | printf("Invalid port number %s\n", port_s);
|
---|
104 | goto error;
|
---|
105 | }
|
---|
106 |
|
---|
107 | rc = inet_addr_sockaddr(&iaddr, port, &saddr, &saddrlen);
|
---|
108 | if (rc != EOK) {
|
---|
109 | assert(rc == ENOMEM);
|
---|
110 | printf("Out of memory.\n");
|
---|
111 | return ENOMEM;
|
---|
112 | }
|
---|
113 |
|
---|
114 | printf("Connecting to host %s port %u\n", host, port);
|
---|
115 |
|
---|
116 | conn_fd = socket(saddr->sa_family, SOCK_STREAM, 0);
|
---|
117 | if (conn_fd < 0)
|
---|
118 | goto error;
|
---|
119 |
|
---|
120 | rc = connect(conn_fd, saddr, saddrlen);
|
---|
121 | if (rc != EOK)
|
---|
122 | goto error;
|
---|
123 |
|
---|
124 | rcv_fid = fibril_create(rcv_fibril, NULL);
|
---|
125 | if (rcv_fid == 0)
|
---|
126 | goto error;
|
---|
127 |
|
---|
128 | fibril_add_ready(rcv_fid);
|
---|
129 |
|
---|
130 | free(saddr);
|
---|
131 | return EOK;
|
---|
132 | error:
|
---|
133 | if (conn_fd >= 0) {
|
---|
134 | closesocket(conn_fd);
|
---|
135 | conn_fd = -1;
|
---|
136 | }
|
---|
137 | free(saddr);
|
---|
138 |
|
---|
139 | return EIO;
|
---|
140 | }
|
---|
141 |
|
---|
142 | int conn_send(void *data, size_t size)
|
---|
143 | {
|
---|
144 | int rc = send(conn_fd, data, size, 0);
|
---|
145 | if (rc != EOK)
|
---|
146 | return EIO;
|
---|
147 |
|
---|
148 | return EOK;
|
---|
149 | }
|
---|
150 |
|
---|
151 | /** @}
|
---|
152 | */
|
---|