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
;
54 static inet_ep_t remote
;
56 #define RECV_BUF_SIZE 1024
57 static uint8_t recv_buf
[RECV_BUF_SIZE
];
59 static void comm_udp_recv_msg(udp_assoc_t
*, udp_rmsg_t
*);
60 static void comm_udp_recv_err(udp_assoc_t
*, udp_rerr_t
*);
61 static void comm_udp_link_state(udp_assoc_t
*, udp_link_state_t
);
63 static 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
69 static void comm_udp_recv_msg(udp_assoc_t
*assoc
, udp_rmsg_t
*rmsg
)
76 size
= udp_rmsg_size(rmsg
);
79 now
= min(size
- pos
, RECV_BUF_SIZE
);
80 rc
= udp_rmsg_read(rmsg
, pos
, recv_buf
, now
);
82 printf("Error reading message.\n");
86 netecho_received(recv_buf
, now
);
91 static void comm_udp_recv_err(udp_assoc_t
*assoc
, udp_rerr_t
*rerr
)
93 printf("Got ICMP error message.\n");
96 static void comm_udp_link_state(udp_assoc_t
*assoc
, udp_link_state_t lstate
)
98 const char *sstate
= NULL
;
109 printf("Link state change: %s.\n", sstate
);
112 errno_t
comm_open_listen(const char *port_s
)
118 uint16_t port
= strtol(port_s
, &endptr
, 10);
119 if (*endptr
!= '\0') {
120 printf("Invalid port number %s\n", port_s
);
125 epp
.local
.port
= port
;
127 printf("Listening on port %u\n", port
);
129 rc
= udp_create(&udp
);
133 rc
= udp_assoc_create(udp
, &epp
, &comm_udp_cb
, NULL
, &assoc
);
139 udp_assoc_destroy(assoc
);
145 errno_t
comm_open_talkto(const char *hostport
)
152 rc
= inet_hostport_plookup_one(hostport
, ip_any
, &epp
.remote
, NULL
,
155 printf("Error: %s (host:port %s).\n", errmsg
, hostport
);
159 printf("Talking to %s\n", hostport
);
161 rc
= udp_create(&udp
);
165 rc
= udp_assoc_create(udp
, &epp
, &comm_udp_cb
, NULL
, &assoc
);
171 udp_assoc_destroy(assoc
);
177 void comm_close(void)
180 udp_assoc_destroy(assoc
);
185 errno_t
comm_send(void *data
, size_t size
)
187 errno_t rc
= udp_assoc_send_msg(assoc
, &remote
, data
, size
);