source: mainline/uspace/app/netecho/comm.c@ 87822ce

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 87822ce was 1d6dd2a, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Remove unnecessary includes from <stdio.h>.

  • Property mode set to 100644
File size: 4.4 KB
Line 
1/*
2 * Copyright (c) 2016 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 netecho
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/endpoint.h>
40#include <inet/hostport.h>
41#include <inet/udp.h>
42#include <macros.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <stdint.h>
46#include <str.h>
47#include <str_error.h>
48
49#include "comm.h"
50#include "netecho.h"
51
52static udp_t *udp;
53static udp_assoc_t *assoc;
54static inet_ep_t remote;
55
56#define RECV_BUF_SIZE 1024
57static uint8_t recv_buf[RECV_BUF_SIZE];
58
59static void comm_udp_recv_msg(udp_assoc_t *, udp_rmsg_t *);
60static void comm_udp_recv_err(udp_assoc_t *, udp_rerr_t *);
61static void comm_udp_link_state(udp_assoc_t *, udp_link_state_t);
62
63static udp_cb_t comm_udp_cb = {
64 .recv_msg = comm_udp_recv_msg,
65 .recv_err = comm_udp_recv_err,
66 .link_state = comm_udp_link_state
67};
68
69static void comm_udp_recv_msg(udp_assoc_t *assoc, udp_rmsg_t *rmsg)
70{
71 size_t size;
72 size_t pos;
73 size_t now;
74 errno_t rc;
75
76 size = udp_rmsg_size(rmsg);
77 pos = 0;
78 while (pos < size) {
79 now = min(size - pos, RECV_BUF_SIZE);
80 rc = udp_rmsg_read(rmsg, pos, recv_buf, now);
81 if (rc != EOK) {
82 printf("Error reading message.\n");
83 return;
84 }
85
86 netecho_received(recv_buf, now);
87 pos += now;
88 }
89}
90
91static void comm_udp_recv_err(udp_assoc_t *assoc, udp_rerr_t *rerr)
92{
93 printf("Got ICMP error message.\n");
94}
95
96static void comm_udp_link_state(udp_assoc_t *assoc, udp_link_state_t lstate)
97{
98 const char *sstate = NULL;
99
100 switch (lstate) {
101 case udp_ls_down:
102 sstate = "Down";
103 break;
104 case udp_ls_up:
105 sstate = "Up";
106 break;
107 }
108
109 printf("Link state change: %s.\n", sstate);
110}
111
112errno_t comm_open_listen(const char *port_s)
113{
114 inet_ep2_t epp;
115 errno_t rc;
116
117 char *endptr;
118 uint16_t port = strtol(port_s, &endptr, 10);
119 if (*endptr != '\0') {
120 printf("Invalid port number %s\n", port_s);
121 goto error;
122 }
123
124 inet_ep2_init(&epp);
125 epp.local.port = port;
126
127 printf("Listening on port %u\n", port);
128
129 rc = udp_create(&udp);
130 if (rc != EOK)
131 goto error;
132
133 rc = udp_assoc_create(udp, &epp, &comm_udp_cb, NULL, &assoc);
134 if (rc != EOK)
135 goto error;
136
137 return EOK;
138error:
139 udp_assoc_destroy(assoc);
140 udp_destroy(udp);
141
142 return EIO;
143}
144
145errno_t comm_open_talkto(const char *hostport)
146{
147 inet_ep2_t epp;
148 const char *errmsg;
149 errno_t rc;
150
151 inet_ep2_init(&epp);
152 rc = inet_hostport_plookup_one(hostport, ip_any, &epp.remote, NULL,
153 &errmsg);
154 if (rc != EOK) {
155 printf("Error: %s (host:port %s).\n", errmsg, hostport);
156 goto error;
157 }
158
159 printf("Talking to %s\n", hostport);
160
161 rc = udp_create(&udp);
162 if (rc != EOK)
163 goto error;
164
165 rc = udp_assoc_create(udp, &epp, &comm_udp_cb, NULL, &assoc);
166 if (rc != EOK)
167 goto error;
168
169 return EOK;
170error:
171 udp_assoc_destroy(assoc);
172 udp_destroy(udp);
173
174 return EIO;
175}
176
177void comm_close(void)
178{
179 if (assoc != NULL)
180 udp_assoc_destroy(assoc);
181 if (udp != NULL)
182 udp_destroy(udp);
183}
184
185errno_t comm_send(void *data, size_t size)
186{
187 errno_t rc = udp_assoc_send_msg(assoc, &remote, data, size);
188 if (rc != EOK)
189 return EIO;
190
191 return EOK;
192}
193
194/** @}
195 */
Note: See TracBrowser for help on using the repository browser.