source: mainline/uspace/app/nterm/conn.c@ 7c912b6

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

Fix delivery of end-of-connection condition.

  • Property mode set to 100644
File size: 3.1 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 <bool.h>
36#include <errno.h>
37#include <fibril.h>
38#include <net/socket.h>
39#include <stdio.h>
40#include <str_error.h>
41#include <sys/types.h>
42
43#include "conn.h"
44#include "nterm.h"
45
46static int conn_fd;
47static fid_t rcv_fid;
48
49#define RECV_BUF_SIZE 1024
50static uint8_t recv_buf[RECV_BUF_SIZE];
51
52static 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
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));
68
69 return 0;
70}
71
72int conn_open(const char *addr_s, const char *port_s)
73{
74 struct sockaddr_in addr;
75 int rc;
76 char *endptr;
77
78 addr.sin_family = AF_INET;
79
80 rc = inet_pton(addr.sin_family, addr_s, (uint8_t *)&addr.sin_addr);
81 if (rc != EOK) {
82 printf("Invalid addres %s\n", addr_s);
83 return EINVAL;
84 }
85
86 addr.sin_port = htons(strtol(port_s, &endptr, 10));
87 if (*endptr != '\0') {
88 printf("Invalid port number %s\n", port_s);
89 return EINVAL;
90 }
91
92 conn_fd = socket(PF_INET, SOCK_STREAM, 0);
93 if (conn_fd < 0)
94 goto error;
95
96 printf("Connecting to address %s port %u\n", addr_s, ntohs(addr.sin_port));
97
98 rc = connect(conn_fd, (struct sockaddr *)&addr, sizeof(addr));
99 if (rc != EOK)
100 goto error;
101
102 rcv_fid = fibril_create(rcv_fibril, NULL);
103 if (rcv_fid == 0)
104 goto error;
105
106 fibril_add_ready(rcv_fid);
107
108 return EOK;
109
110error:
111 if (conn_fd >= 0) {
112 closesocket(conn_fd);
113 conn_fd = -1;
114 }
115
116 return EIO;
117}
118
119int conn_send(void *data, size_t size)
120{
121 int rc;
122
123 rc = send(conn_fd, data, size, 0);
124 if (rc != EOK)
125 return EIO;
126
127 return EOK;
128}
129
130/** @}
131 */
Note: See TracBrowser for help on using the repository browser.