1#include "utils.h"
2#include <pico_ipv6.h>
3#include <pico_socket.h>
4
5/**** START UDP ECHO ****/
6/*
7 * udpecho expects the following format: udpecho:bind_addr:listen_port[:sendto_port:datasize]
8 * bind_addr: IP address to bind to
9 * listen_port: port number on which the udpecho listens
10 * sendto_port [OPTIONAL]: port number to echo datagrams to (echo to originating IP address)
11 * datasize [OPTIONAL]: max size of the data red from the socket in one go
12 *
13 * REMARK: once an optional parameter is given, all optional parameters need a value!
14 *
15 * f.e.: ./build/test/picoapp.elf --vde pic0:/tmp/pic0.ctl:10.40.0.3:255.255.255.0: -a udpecho:10.40.0.3:6667:6667:1400
16 */
17
18void dummy_cb(uint16_t __attribute__((unused)) ev, struct pico_socket __attribute__((unused)) *s)
19{
20
21}
22
23void app_sendto_test(char *arg)
24{
25    char *nxt = arg;
26    char *dstaddr = NULL;
27    char *dstport = NULL;
28    struct pico_ip4 inaddr_dst = {};
29    struct pico_ip6 inaddr_dst6 = {};
30    uint16_t dport;
31    struct pico_socket *sock;
32    int ret;
33
34    /* start of argument parsing */
35    if (nxt) {
36        nxt = cpy_arg(&dstaddr, nxt);
37        if (dstaddr) {
38            if (!IPV6_MODE)
39                pico_string_to_ipv4(dstaddr, &inaddr_dst.addr);
40
41      #ifdef PICO_SUPPORT_IPV6
42            else
43                pico_string_to_ipv6(dstaddr, inaddr_dst6.addr);
44      #endif
45        } else {
46            goto out;
47        }
48    } else {
49        /* missing bind_addr */
50        goto out;
51    }
52
53    if (nxt) {
54        nxt = cpy_arg(&dstport, nxt);
55        if (dstport && atoi(dstport)) {
56            dport = short_be(atoi(dstport));
57        } else {
58            dport = short_be(5555);
59        }
60    } else {
61        /* missing listen_port */
62        goto out;
63    }
64
65    if (!IPV6_MODE)
66        sock = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_UDP, &dummy_cb);
67    else
68        sock = pico_socket_open(PICO_PROTO_IPV6, PICO_PROTO_UDP, &dummy_cb);
69
70    ret = pico_socket_sendto(sock, "Testing", 7u, ((IPV6_MODE) ? (void *)(&inaddr_dst6) : (void *)(&inaddr_dst)), dport);
71    if (ret < 0)
72        printf("Failure in first pico_socket_send\n");
73
74    ret = pico_socket_sendto(sock, "Testing", 7u, ((IPV6_MODE) ? (void *)(&inaddr_dst6) : (void *)(&inaddr_dst)), dport);
75    if (ret < 0)
76        printf("Failure in second pico_socket_send\n");
77
78    ret = pico_socket_close(sock);
79    if (ret)
80        printf("Failure in pico_socket_close\n");
81
82    printf("\n%s: UDP sendto test launched. Sending packets to ip %s port %u\n\n", __FUNCTION__, dstaddr, short_be(dport));
83    return;
84
85out:
86    fprintf(stderr, "udp_sendto_test expects the following format: udp_sendto_test:dest_addr:[dest_por]t\n");
87    exit(255);
88}
89