2 * Copyright (c) 2016 Jiri Svoboda
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
29 /** @addtogroup netecho
35 #include <byteorder.h>
39 #include <inet/endpoint.h>
40 #include <inet/hostport.h>
47 #include <str_error.h>
53 static udp_assoc_t
*assoc
;
55 #define RECV_BUF_SIZE 1024
56 static uint8_t recv_buf
[RECV_BUF_SIZE
];
58 static void comm_udp_recv_msg(udp_assoc_t
*, udp_rmsg_t
*);
59 static void comm_udp_recv_err(udp_assoc_t
*, udp_rerr_t
*);
60 static void comm_udp_link_state(udp_assoc_t
*, udp_link_state_t
);
62 static udp_cb_t comm_udp_cb
= {
63 .recv_msg
= comm_udp_recv_msg
,
64 .recv_err
= comm_udp_recv_err
,
65 .link_state
= comm_udp_link_state
68 static void comm_udp_recv_msg(udp_assoc_t
*assoc
, udp_rmsg_t
*rmsg
)
75 size
= udp_rmsg_size(rmsg
);
78 now
= min(size
- pos
, RECV_BUF_SIZE
);
79 rc
= udp_rmsg_read(rmsg
, pos
, recv_buf
, now
);
81 printf("Error reading message.\n");
85 netecho_received(recv_buf
, now
);
90 static void comm_udp_recv_err(udp_assoc_t
*assoc
, udp_rerr_t
*rerr
)
92 printf("Got ICMP error message.\n");
95 static void comm_udp_link_state(udp_assoc_t
*assoc
, udp_link_state_t lstate
)
97 const char *sstate
= NULL
;
108 printf("Link state change: %s.\n", sstate
);
111 errno_t
comm_open_listen(const char *port_s
)
117 uint16_t port
= strtol(port_s
, &endptr
, 10);
118 if (*endptr
!= '\0') {
119 printf("Invalid port number %s\n", port_s
);
124 epp
.local
.port
= port
;
126 printf("Listening on port %u\n", port
);
128 rc
= udp_create(&udp
);
132 rc
= udp_assoc_create(udp
, &epp
, &comm_udp_cb
, NULL
, &assoc
);
138 udp_assoc_destroy(assoc
);
144 errno_t
comm_open_talkto(const char *hostport
)
151 rc
= inet_hostport_plookup_one(hostport
, ip_any
, &epp
.remote
, NULL
,
154 printf("Error: %s (host:port %s).\n", errmsg
, hostport
);
158 printf("Talking to %s\n", hostport
);
160 rc
= udp_create(&udp
);
164 rc
= udp_assoc_create(udp
, &epp
, &comm_udp_cb
, NULL
, &assoc
);
170 udp_assoc_destroy(assoc
);
176 void comm_close(void)
179 udp_assoc_destroy(assoc
);
184 errno_t
comm_send(void *data
, size_t size
)
186 errno_t rc
= udp_assoc_send_msg(assoc
, NULL
, data
, size
);