udp.c
5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
/*
* Copyright (C) 2015 Martine Lenders <mlenders@inf.fu-berlin.de>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup examples
* @{
*
* @file
* @brief Demonstrating the sending and receiving of UDP data over POSIX sockets.
*
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
*
* @}
*/
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#include "thread.h"
#define SERVER_MSG_QUEUE_SIZE (8)
#define SERVER_BUFFER_SIZE (64)
static int server_socket = -1;
static char server_buffer[SERVER_BUFFER_SIZE];
static char server_stack[THREAD_STACKSIZE_DEFAULT];
static msg_t server_msg_queue[SERVER_MSG_QUEUE_SIZE];
static void *_server_thread(void *args)
{
struct sockaddr_in6 server_addr;
uint16_t port;
msg_init_queue(server_msg_queue, SERVER_MSG_QUEUE_SIZE);
server_socket = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
/* parse port */
port = atoi((char *)args);
if (port == 0) {
puts("Error: invalid port specified");
return NULL;
}
server_addr.sin6_family = AF_INET6;
memset(&server_addr.sin6_addr, 0, sizeof(server_addr.sin6_addr));
server_addr.sin6_port = htons(port);
if (server_socket < 0) {
puts("error initializing socket");
server_socket = 0;
return NULL;
}
if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
server_socket = -1;
puts("error binding socket");
return NULL;
}
printf("Success: started UDP server on port %" PRIu16 "\n", port);
while (1) {
int res;
struct sockaddr_in6 src;
socklen_t src_len = sizeof(struct sockaddr_in6);
if ((res = recvfrom(server_socket, server_buffer, sizeof(server_buffer), 0,
(struct sockaddr *)&src, &src_len)) < 0) {
puts("Error on receive");
}
else if (res == 0) {
puts("Peer did shut down");
}
else {
printf("Received data: ");
puts(server_buffer);
}
}
return NULL;
}
static int udp_send(char *addr_str, char *port_str, char *data, unsigned int num,
unsigned int delay)
{
struct sockaddr_in6 src, dst;
size_t data_len = strlen(data);
uint16_t port;
int s;
src.sin6_family = AF_INET6;
dst.sin6_family = AF_INET6;
memset(&src.sin6_addr, 0, sizeof(src.sin6_addr));
/* parse destination address */
if (inet_pton(AF_INET6, addr_str, &dst.sin6_addr) != 1) {
puts("Error: unable to parse destination address");
return 1;
}
/* parse port */
port = atoi(port_str);
dst.sin6_port = htons(port);
src.sin6_port = htons(port);
s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
if (s < 0) {
puts("error initializing socket");
return 1;
}
for (unsigned int i = 0; i < num; i++) {
if (sendto(s, data, data_len, 0, (struct sockaddr *)&dst, sizeof(dst)) < 0) {
puts("could not send");
}
else {
printf("Success: send %u byte to %s:%u\n", (unsigned)data_len, addr_str, port);
}
usleep(delay);
}
close(s);
return 0;
}
static int udp_start_server(char *port_str)
{
/* check if server is already running */
if (server_socket >= 0) {
puts("Error: server already running");
return 1;
}
/* start server (which means registering pktdump for the chosen port) */
if (thread_create(server_stack, sizeof(server_stack), THREAD_PRIORITY_MAIN - 1,
THREAD_CREATE_STACKTEST,
_server_thread, port_str, "UDP server") <= KERNEL_PID_UNDEF) {
server_socket = -1;
puts("error initializing thread");
return 1;
}
return 0;
}
int udp_cmd(int argc, char **argv)
{
if (argc < 2) {
printf("usage: %s [send|server]\n", argv[0]);
return 1;
}
if (strcmp(argv[1], "send") == 0) {
uint32_t num = 1;
uint32_t delay = 1000000;
if (argc < 5) {
printf("usage: %s send <addr> <port> <data> [<num> [<delay in us>]]\n",
argv[0]);
return 1;
}
if (argc > 5) {
num = atoi(argv[5]);
}
if (argc > 6) {
delay = atoi(argv[6]);
}
return udp_send(argv[2], argv[3], argv[4], num, delay);
}
else if (strcmp(argv[1], "server") == 0) {
if (argc < 3) {
printf("usage: %s server [start|stop]\n", argv[0]);
return 1;
}
if (strcmp(argv[2], "start") == 0) {
if (argc < 4) {
printf("usage %s server start <port>\n", argv[0]);
return 1;
}
return udp_start_server(argv[3]);
}
else {
puts("error: invalid command");
return 1;
}
}
else {
puts("error: invalid command");
return 1;
}
}
/** @} */