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