[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 Network UDP echo diagnostic utility.
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #include <stdbool.h>
|
---|
| 36 | #include <errno.h>
|
---|
| 37 | #include <io/console.h>
|
---|
| 38 | #include <stdio.h>
|
---|
| 39 | #include <str.h>
|
---|
| 40 |
|
---|
| 41 | #include "comm.h"
|
---|
| 42 | #include "netecho.h"
|
---|
| 43 |
|
---|
| 44 | #define NAME "netecho"
|
---|
| 45 |
|
---|
| 46 | static console_ctrl_t *con;
|
---|
| 47 | static bool done;
|
---|
| 48 |
|
---|
| 49 | void netecho_received(void *data, size_t size)
|
---|
| 50 | {
|
---|
| 51 | char *p;
|
---|
| 52 | size_t i;
|
---|
| 53 |
|
---|
| 54 | printf("Received message '");
|
---|
| 55 | p = data;
|
---|
| 56 |
|
---|
| 57 | for (i = 0; i < size; i++)
|
---|
| 58 | putchar(p[i]);
|
---|
| 59 | printf("'.\n");
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | static void key_handle_ctrl(kbd_event_t *ev)
|
---|
| 63 | {
|
---|
| 64 | switch (ev->key) {
|
---|
| 65 | case KC_Q:
|
---|
| 66 | done = true;
|
---|
| 67 | break;
|
---|
| 68 | default:
|
---|
| 69 | break;
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | static void send_char(wchar_t c)
|
---|
| 74 | {
|
---|
| 75 | char cbuf[STR_BOUNDS(1)];
|
---|
| 76 | size_t offs;
|
---|
[b7fd2a0] | 77 | errno_t rc;
|
---|
[5147ff1] | 78 |
|
---|
| 79 | offs = 0;
|
---|
| 80 | chr_encode(c, cbuf, &offs, STR_BOUNDS(1));
|
---|
| 81 |
|
---|
| 82 | rc = comm_send(cbuf, offs);
|
---|
| 83 | if (rc != EOK) {
|
---|
| 84 | printf("[Failed sending data]\n");
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | static void key_handle_unmod(kbd_event_t *ev)
|
---|
| 89 | {
|
---|
| 90 | switch (ev->key) {
|
---|
| 91 | case KC_ENTER:
|
---|
| 92 | send_char('\n');
|
---|
| 93 | break;
|
---|
| 94 | default:
|
---|
| 95 | if (ev->c >= 32 || ev->c == '\t' || ev->c == '\b') {
|
---|
| 96 | send_char(ev->c);
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | static void key_handle(kbd_event_t *ev)
|
---|
| 102 | {
|
---|
| 103 | if ((ev->mods & KM_ALT) == 0 &&
|
---|
| 104 | (ev->mods & KM_SHIFT) == 0 &&
|
---|
| 105 | (ev->mods & KM_CTRL) != 0) {
|
---|
| 106 | key_handle_ctrl(ev);
|
---|
| 107 | } else if ((ev->mods & (KM_CTRL | KM_ALT)) == 0) {
|
---|
| 108 | key_handle_unmod(ev);
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 |
|
---|
| 113 | static void print_syntax(void)
|
---|
| 114 | {
|
---|
| 115 | printf("syntax:\n");
|
---|
| 116 | printf("\t%s -l <port>\n", NAME);
|
---|
[a62ceaf] | 117 | printf("\t%s -d <host>:<port> [<message> [<message...>]]\n", NAME);
|
---|
[5147ff1] | 118 | }
|
---|
| 119 |
|
---|
| 120 | /* Interactive mode */
|
---|
| 121 | static void netecho_interact(void)
|
---|
| 122 | {
|
---|
| 123 | cons_event_t ev;
|
---|
| 124 |
|
---|
| 125 | printf("Communication started. Press Ctrl-Q to quit.\n");
|
---|
| 126 |
|
---|
| 127 | con = console_init(stdin, stdout);
|
---|
| 128 |
|
---|
| 129 | done = false;
|
---|
| 130 | while (!done) {
|
---|
| 131 | console_get_event(con, &ev);
|
---|
| 132 | if (ev.type == CEV_KEY && ev.ev.key.type == KEY_PRESS)
|
---|
| 133 | key_handle(&ev.ev.key);
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | static void netecho_send_messages(char **msgs)
|
---|
| 138 | {
|
---|
[b7fd2a0] | 139 | errno_t rc;
|
---|
[5147ff1] | 140 |
|
---|
| 141 | while (*msgs != NULL) {
|
---|
| 142 | rc = comm_send(*msgs, str_size(*msgs));
|
---|
| 143 | if (rc != EOK) {
|
---|
| 144 | printf("[Failed sending data]\n");
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | ++msgs;
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | int main(int argc, char *argv[])
|
---|
| 152 | {
|
---|
[a62ceaf] | 153 | char *hostport;
|
---|
[5147ff1] | 154 | char *port;
|
---|
| 155 | char **msgs;
|
---|
[b7fd2a0] | 156 | errno_t rc;
|
---|
[5147ff1] | 157 |
|
---|
| 158 | if (argc < 2) {
|
---|
| 159 | print_syntax();
|
---|
| 160 | return 1;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | if (str_cmp(argv[1], "-l") == 0) {
|
---|
| 164 | if (argc != 3) {
|
---|
| 165 | print_syntax();
|
---|
| 166 | return 1;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | port = argv[2];
|
---|
| 170 | msgs = NULL;
|
---|
[a62ceaf] | 171 |
|
---|
| 172 | rc = comm_open_listen(port);
|
---|
| 173 | if (rc != EOK) {
|
---|
| 174 | printf("Error setting up communication.\n");
|
---|
| 175 | return 1;
|
---|
| 176 | }
|
---|
[5147ff1] | 177 | } else if (str_cmp(argv[1], "-d") == 0) {
|
---|
[a62ceaf] | 178 | if (argc < 3) {
|
---|
[5147ff1] | 179 | print_syntax();
|
---|
| 180 | return 1;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[a62ceaf] | 183 | hostport = argv[2];
|
---|
| 184 | port = NULL;
|
---|
| 185 | msgs = argv + 3;
|
---|
| 186 |
|
---|
| 187 | rc = comm_open_talkto(hostport);
|
---|
| 188 | if (rc != EOK) {
|
---|
| 189 | printf("Error setting up communication.\n");
|
---|
| 190 | return 1;
|
---|
| 191 | }
|
---|
[5147ff1] | 192 | } else {
|
---|
| 193 | print_syntax();
|
---|
| 194 | return 1;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | if (msgs != NULL && *msgs != NULL) {
|
---|
| 198 | /* Just send messages and quit */
|
---|
| 199 | netecho_send_messages(msgs);
|
---|
| 200 | } else {
|
---|
| 201 | /* Interactive mode */
|
---|
| 202 | netecho_interact();
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | comm_close();
|
---|
| 206 | return 0;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | /** @}
|
---|
| 210 | */
|
---|