source: mainline/uspace/app/nterm/conn.c@ 2b5d966

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2b5d966 was 26de91a, checked in by Jiri Svoboda <jiri@…>, 12 years ago

IPv4 and v6 should not need separate handling by a simple client that is just connecting to a host/address. Add IPv6/DNS support in applications where missing.

  • Property mode set to 100644
File size: 3.5 KB
RevLine 
[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>
[3e6a98c5]36#include <stdbool.h>
[01a7aa1]37#include <errno.h>
38#include <fibril.h>
[287d729]39#include <inet/dnsr.h>
[26de91a]40#include <net/inet.h>
[01a7aa1]41#include <net/socket.h>
42#include <stdio.h>
[26de91a]43#include <stdlib.h>
[7c912b6]44#include <str_error.h>
[01a7aa1]45#include <sys/types.h>
46
47#include "conn.h"
48#include "nterm.h"
49
50static int conn_fd;
51static fid_t rcv_fid;
52
[7c912b6]53#define RECV_BUF_SIZE 1024
[01a7aa1]54static uint8_t recv_buf[RECV_BUF_SIZE];
55
56static 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
[7c912b6]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));
[01a7aa1]72
[0d67e16]73 exit(0);
[01a7aa1]74 return 0;
75}
76
[26de91a]77int conn_open(const char *host, const char *port_s)
[01a7aa1]78{
[02a09ed]79 int conn_fd = -1;
[26de91a]80 struct sockaddr *saddr = NULL;
81 socklen_t saddrlen;
[02a09ed]82
83 /* Interpret as address */
[26de91a]84 inet_addr_t iaddr;
85 int rc = inet_addr_parse(host, &iaddr);
[02a09ed]86
[01a7aa1]87 if (rc != EOK) {
[02a09ed]88 /* Interpret as a host name */
89 dnsr_hostinfo_t *hinfo = NULL;
[26de91a]90 rc = dnsr_name2host(host, &hinfo, ip_any);
[02a09ed]91
[287d729]92 if (rc != EOK) {
[26de91a]93 printf("Error resolving host '%s'.\n", host);
[287d729]94 goto error;
95 }
[3e66428]96
[26de91a]97 iaddr = hinfo->addr;
[01a7aa1]98 }
[02a09ed]99
100 char *endptr;
101 uint16_t port = strtol(port_s, &endptr, 10);
[01a7aa1]102 if (*endptr != '\0') {
103 printf("Invalid port number %s\n", port_s);
[287d729]104 goto error;
[01a7aa1]105 }
[b5cf742a]106
[26de91a]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 }
[02a09ed]113
[26de91a]114 printf("Connecting to host %s port %u\n", host, port);
[b5cf742a]115
[26de91a]116 conn_fd = socket(saddr->sa_family, SOCK_STREAM, 0);
117 if (conn_fd < 0)
[02a09ed]118 goto error;
[b5cf742a]119
[26de91a]120 rc = connect(conn_fd, saddr, saddrlen);
[01a7aa1]121 if (rc != EOK)
122 goto error;
[b5cf742a]123
[01a7aa1]124 rcv_fid = fibril_create(rcv_fibril, NULL);
125 if (rcv_fid == 0)
126 goto error;
[b5cf742a]127
[01a7aa1]128 fibril_add_ready(rcv_fid);
[b5cf742a]129
[26de91a]130 free(saddr);
[01a7aa1]131 return EOK;
132error:
133 if (conn_fd >= 0) {
134 closesocket(conn_fd);
135 conn_fd = -1;
136 }
[26de91a]137 free(saddr);
[b5cf742a]138
[01a7aa1]139 return EIO;
140}
141
142int conn_send(void *data, size_t size)
143{
[b5cf742a]144 int rc = send(conn_fd, data, size, 0);
[01a7aa1]145 if (rc != EOK)
146 return EIO;
[b5cf742a]147
[01a7aa1]148 return EOK;
149}
150
151/** @}
152 */
Note: See TracBrowser for help on using the repository browser.