udp_client.c 7.15 KB
Newer Older
Daniele Lacamera's avatar
Daniele Lacamera committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include "utils.h"
#include <pico_socket.h>

/*** START UDP CLIENT ***/
/*
 * udpclient expects the following format: udpclient:dest_addr:sendto_port[:listen_port:datasize:loops:subloops]
 * dest_addr: IP address to send datagrams to
 * sendto_port: port number to send datagrams to
 * listen_port [OPTIONAL]: port number on which the udpclient listens
 * datasize [OPTIONAL]: size of the data given to the socket in one go
 * loops [OPTIONAL]: number of intervals in which data is send
 * subloops [OPTIONAL]: number of sends in one interval
 *
 * REMARK: once an optional parameter is given, all optional parameters need a value!
 *
 * f.e.: ./build/test/picoapp.elf --vde pic0:/tmp/pic0.ctl:10.40.0.2:255.255.255.0: -a udpclient:10.40.0.3:6667:6667:1400:100:10
 */

struct udpclient_pas *udpclient_pas;

21 22 23 24 25 26 27 28 29 30
static int exit_retry = 0;

static void request_exit_echo(pico_time now, void *arg)
{
    struct pico_socket *s = (struct pico_socket *)arg;
    char end[4] = "end";
    pico_socket_send(s, end, 4);
    if (exit_retry++ > 3) {
        pico_timer_add(1000, deferred_exit, udpclient_pas);
    } else {
31
        pico_timer_add(1000, request_exit_echo, s);
32 33 34 35
        printf("%s: requested exit of echo\n", __FUNCTION__);
    }
}

Daniele Lacamera's avatar
Daniele Lacamera committed
36 37 38 39 40 41 42 43
void udpclient_send(pico_time __attribute__((unused)) now, void __attribute__((unused))  *arg)
{
    struct pico_socket *s = udpclient_pas->s;
    char *buf = NULL;
    int i = 0, w = 0;
    static uint16_t loop = 0;

    if (++loop > udpclient_pas->loops) {
44
        pico_timer_add(1000, request_exit_echo, s);
Daniele Lacamera's avatar
Daniele Lacamera committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
        return;
    } else {
        buf = calloc(1, udpclient_pas->datasize);
        if (!buf) {
            printf("%s: no memory available\n", __FUNCTION__);
            return;
        }

        memset(buf, '1', udpclient_pas->datasize);
        picoapp_dbg("%s: performing loop %u\n", __FUNCTION__, loop);
        for (i = 0; i < udpclient_pas->subloops; i++) {
            w =  pico_socket_send(s, buf, udpclient_pas->datasize);
            if (w <= 0)
                break;
        }
        picoapp_dbg("%s: written %u byte(s) in each of %u subloops\n", __FUNCTION__, udpclient_pas->datasize, i);
        free(buf);
    }

    pico_timer_add(100, udpclient_send, NULL);
}

void cb_udpclient(uint16_t ev, struct pico_socket *s)
{
    char *recvbuf = NULL;
    int r = 0;

    if (ev & PICO_SOCK_EV_RD) {
        recvbuf = calloc(1, udpclient_pas->datasize);
        if (!recvbuf) {
            printf("%s: no memory available\n", __FUNCTION__);
            return;
        }

        do {
            r = pico_socket_recv(s, recvbuf, udpclient_pas->datasize);
        } while ( r > 0);
        free(recvbuf);
    }

    if (ev == PICO_SOCK_EV_ERR) {
        printf("Socket Error received. Bailing out.\n");
        free(udpclient_pas);
        exit(7);
    }
}

void app_udpclient(char *arg)
{
    char *daddr = NULL, *lport = NULL, *sport = NULL, *s_datasize = NULL, *s_loops = NULL, *s_subloops = NULL;
    char *nxt = arg;
    char sinaddr_any[40] = {
        0
    };
    uint16_t listen_port = 0;
    int ret = 0;

    udpclient_pas = calloc(1, sizeof(struct udpclient_pas));
    if (!udpclient_pas) {
        printf("%s: no memory available\n", __FUNCTION__);
        exit(255);
    }

    udpclient_pas->s = NULL;
    udpclient_pas->loops = 100;
    udpclient_pas->subloops = 10;
    udpclient_pas->datasize = 1400;

    /* start of argument parsing */
    if (nxt) {
        nxt = cpy_arg(&daddr, arg);
        if (daddr) {
            if (!IPV6_MODE)
                pico_string_to_ipv4(daddr, &udpclient_pas->dst.ip4.addr);

      #ifdef PICO_SUPPORT_IPV6
            else
                pico_string_to_ipv6(daddr, udpclient_pas->dst.ip6.addr);
      #endif
        } else {
            goto out;
        }
    } else {
        /* missing dest_addr */
        goto out;
    }

    if (nxt) {
        nxt = cpy_arg(&sport, nxt);
        if (sport && atoi(sport)) {
            udpclient_pas->sport = short_be(atoi(sport));
        } else {
            goto out;
        }
    } else {
        /* missing send_port */
        goto out;
    }

    if (nxt) {
        nxt = cpy_arg(&lport, nxt);
        if (lport && atoi(lport)) {
            listen_port = short_be(atoi(lport));
        } else {
            goto out;
        }
    } else {
        /* missing listen_port, use default */
        listen_port = 0;
    }

    if (nxt) {
        nxt = cpy_arg(&s_datasize, nxt);
        if (s_datasize && atoi(s_datasize)) {
            udpclient_pas->datasize = atoi(s_datasize);
        } else {
            goto out;
        }
    } else {
        /* missing datasize, incomplete optional parameters? -> exit */
        if (lport)
            goto out;
    }

    if (nxt) {
        nxt = cpy_arg(&s_loops, nxt);
        if (s_loops && atoi(s_loops)) {
            udpclient_pas->loops = atoi(s_loops);
        } else {
            goto out;
        }
    } else {
        /* missing loops, incomplete optional parameters? -> exit */
        if (s_datasize)
            goto out;
    }

    if (nxt) {
        nxt = cpy_arg(&s_subloops, nxt);
        if (s_subloops && atoi(s_subloops)) {
            udpclient_pas->subloops = atoi(s_subloops);
        } else {
            goto out;
        }
    } else {
        /* missing subloops, incomplete optional parameters? -> exit */
        if (s_loops)
            goto out;
    }

    /* end of argument parsing */

    if (!IPV6_MODE)
        pico_ipv4_to_string(sinaddr_any, inaddr_any.addr);

  #ifdef PICO_SUPPORT_IPV6
    else
        pico_ipv6_to_string(sinaddr_any, inaddr6_any.addr);
  #endif

    if (!IPV6_MODE)
        udpclient_pas->s = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_UDP, &cb_udpclient);
    else
        udpclient_pas->s = pico_socket_open(PICO_PROTO_IPV6, PICO_PROTO_UDP, &cb_udpclient);

    if (!udpclient_pas->s) {
        printf("%s: error opening socket: %s\n", __FUNCTION__, strerror(pico_err));
        free(udpclient_pas);
        exit(1);
    }

    if (!IPV6_MODE)
        ret = pico_socket_bind(udpclient_pas->s, &inaddr_any, &listen_port);
    else
        ret = pico_socket_bind(udpclient_pas->s, &inaddr6_any, &listen_port);

    if (ret < 0) {
        free(udpclient_pas);
        printf("%s: error binding socket to %s:%u: %s\n", __FUNCTION__, sinaddr_any, short_be(listen_port), strerror(pico_err));
        exit(1);
    }

    if (!IPV6_MODE)
        ret = pico_socket_connect(udpclient_pas->s, &udpclient_pas->dst.ip4, udpclient_pas->sport);
    else
        ret = pico_socket_connect(udpclient_pas->s, &udpclient_pas->dst.ip6, udpclient_pas->sport);

    if (ret < 0) {
roel0's avatar
roel0 committed
233
        printf("%s: error connecting to [%s]:%u: %s\n", __FUNCTION__, daddr, short_be(udpclient_pas->sport), strerror(pico_err));
Daniele Lacamera's avatar
Daniele Lacamera committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
        free(udpclient_pas);
        exit(1);
    }

    printf("\n%s: UDP client launched. Sending packets of %u bytes in %u loops and %u subloops to %s:%u\n\n",
           __FUNCTION__, udpclient_pas->datasize, udpclient_pas->loops, udpclient_pas->subloops, daddr, short_be(udpclient_pas->sport));

    pico_timer_add(100, udpclient_send, NULL);
    return;

out:
    fprintf(stderr, "udpclient expects the following format: udpclient:dest_addr:dest_port[:listen_port:datasize:loops:subloops]\n");
    free(udpclient_pas);
    exit(255);
}
/*** END UDP CLIENT ***/