source: mainline/uspace/app/nterm/conn.c@ 08bb73b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 08bb73b was a2e3ee6, checked in by Martin Decky <martin@…>, 13 years ago

use new network address infrastructure (towards IPv6 support)

  • Property mode set to 100644
File size: 3.4 KB
Line 
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/socket.h>
41#include <stdio.h>
42#include <str_error.h>
43#include <sys/types.h>
44
45#include "conn.h"
46#include "nterm.h"
47
48static int conn_fd;
49static fid_t rcv_fid;
50
51#define RECV_BUF_SIZE 1024
52static uint8_t recv_buf[RECV_BUF_SIZE];
53
54static int rcv_fibril(void *arg)
55{
56 ssize_t nr;
57
58 while (true) {
59 nr = recv(conn_fd, recv_buf, RECV_BUF_SIZE, 0);
60 if (nr < 0)
61 break;
62
63 nterm_received(recv_buf, nr);
64 }
65
66 if (nr == ENOTCONN)
67 printf("\n[Other side has closed the connection]\n");
68 else
69 printf("'\n[Receive errror (%s)]\n", str_error(nr));
70
71 exit(0);
72 return 0;
73}
74
75int conn_open(const char *addr_s, const char *port_s)
76{
77 struct sockaddr_in addr;
78 dnsr_hostinfo_t *hinfo = NULL;
79 int rc;
80 char *endptr;
81
82 addr.sin_family = AF_INET;
83
84 rc = inet_pton(addr.sin_family, addr_s, (uint8_t *)&addr.sin_addr);
85 if (rc != EOK) {
86 /* Try interpreting as a host name */
87 rc = dnsr_name2host(addr_s, &hinfo);
88 if (rc != EOK) {
89 printf("Error resolving host '%s'.\n", addr_s);
90 goto error;
91 }
92
93 rc = inet_addr_sockaddr_in(&hinfo->addr, &addr);
94 if (rc != EOK) {
95 printf("Host '%s' not resolved as IPv4 address.\n", addr_s);
96 return rc;
97 }
98 }
99
100 addr.sin_port = htons(strtol(port_s, &endptr, 10));
101 if (*endptr != '\0') {
102 printf("Invalid port number %s\n", port_s);
103 goto error;
104 }
105
106 conn_fd = socket(PF_INET, SOCK_STREAM, 0);
107 if (conn_fd < 0)
108 goto error;
109
110 printf("Connecting to host %s port %u\n", addr_s, ntohs(addr.sin_port));
111
112 rc = connect(conn_fd, (struct sockaddr *)&addr, sizeof(addr));
113 if (rc != EOK)
114 goto error;
115
116 rcv_fid = fibril_create(rcv_fibril, NULL);
117 if (rcv_fid == 0)
118 goto error;
119
120 fibril_add_ready(rcv_fid);
121
122 return EOK;
123
124error:
125 if (conn_fd >= 0) {
126 closesocket(conn_fd);
127 conn_fd = -1;
128 }
129
130 return EIO;
131}
132
133int conn_send(void *data, size_t size)
134{
135 int rc;
136
137 rc = send(conn_fd, data, size, 0);
138 if (rc != EOK)
139 return EIO;
140
141 return EOK;
142}
143
144/** @}
145 */
Note: See TracBrowser for help on using the repository browser.