source: mainline/uspace/app/nterm/conn.c@ 01a7aa1

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

Add a trivial network terminal application.

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