Loading...
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 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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | /*
* Copyright (c) 2017 Linaro Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdio.h>
#include <stdbool.h>
#include <errno.h>
#include <stdlib.h>
#if !defined(__ZEPHYR__) || defined(CONFIG_POSIX_API)
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>
#define USE_IPV6
#else
#include <fcntl.h>
#include <net/socket.h>
#include <kernel.h>
#ifdef CONFIG_NET_IPV6
#define USE_IPV6
#endif
#endif
/* For Zephyr, keep max number of fd's in sync with max poll() capacity */
#ifdef CONFIG_NET_SOCKETS_POLL_MAX
#define NUM_FDS CONFIG_NET_SOCKETS_POLL_MAX
#else
#define NUM_FDS 5
#endif
#define BIND_PORT 4242
/* Number of simultaneous client connections will be NUM_FDS be minus 2 */
struct pollfd pollfds[NUM_FDS];
int pollnum;
#define fatal(msg, ...) { \
printf("Error: " msg "\n", ##__VA_ARGS__); \
exit(1); \
}
static void setblocking(int fd, bool val)
{
int fl, res;
fl = fcntl(fd, F_GETFL, 0);
if (fl == -1) {
fatal("fcntl(F_GETFL): %d", errno);
}
if (val) {
fl &= ~O_NONBLOCK;
} else {
fl |= O_NONBLOCK;
}
res = fcntl(fd, F_SETFL, fl);
if (fl == -1) {
fatal("fcntl(F_SETFL): %d", errno);
}
}
int pollfds_add(int fd)
{
int i;
if (pollnum < NUM_FDS) {
i = pollnum++;
} else {
for (i = 0; i < NUM_FDS; i++) {
if (pollfds[i].fd < 0) {
goto found;
}
}
return -1;
}
found:
pollfds[i].fd = fd;
pollfds[i].events = POLLIN;
return 0;
}
void pollfds_del(int fd)
{
for (int i = 0; i < pollnum; i++) {
if (pollfds[i].fd == fd) {
pollfds[i].fd = -1;
break;
}
}
}
void main(void)
{
int res;
static int counter;
int num_servs = 0;
#if !defined(USE_IPV6) || !(CONFIG_SOC_SERIES_CC32XX)
int serv4;
struct sockaddr_in bind_addr4 = {
.sin_family = AF_INET,
.sin_port = htons(BIND_PORT),
.sin_addr = {
.s_addr = htonl(INADDR_ANY),
},
};
#endif
#ifdef USE_IPV6
int serv6;
struct sockaddr_in6 bind_addr6 = {
.sin6_family = AF_INET6,
.sin6_port = htons(BIND_PORT),
.sin6_addr = IN6ADDR_ANY_INIT,
};
#endif
#if !defined(USE_IPV6) || !(CONFIG_SOC_SERIES_CC32XX)
serv4 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (serv4 < 0) {
printf("error: socket: %d\n", errno);
exit(1);
}
res = bind(serv4, (struct sockaddr *)&bind_addr4, sizeof(bind_addr4));
if (res == -1) {
printf("Cannot bind IPv4, errno: %d\n", errno);
}
num_servs++;
setblocking(serv4, false);
listen(serv4, 5);
pollfds_add(serv4);
#endif
#ifdef USE_IPV6
serv6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
if (serv6 < 0) {
printf("error: socket(AF_INET6): %d\n", errno);
exit(1);
}
#ifdef IPV6_V6ONLY
/* For Linux, we need to make socket IPv6-only to bind it to the
* same port as IPv4 socket above.
*/
int TRUE = 1;
res = setsockopt(serv6, IPPROTO_IPV6, IPV6_V6ONLY, &TRUE, sizeof(TRUE));
if (res < 0) {
printf("error: setsockopt: %d\n", errno);
exit(1);
}
#endif
res = bind(serv6, (struct sockaddr *)&bind_addr6, sizeof(bind_addr6));
if (res == -1) {
printf("Cannot bind IPv6, errno: %d\n", errno);
}
num_servs++;
setblocking(serv6, false);
listen(serv6, 5);
pollfds_add(serv6);
#endif
printf("Asynchronous TCP echo server waits for connections on "
"port %d...\n", BIND_PORT);
while (1) {
struct sockaddr_storage client_addr;
socklen_t client_addr_len = sizeof(client_addr);
char addr_str[32];
res = poll(pollfds, pollnum, -1);
if (res == -1) {
printf("poll error: %d\n", errno);
continue;
}
for (int i = 0; i < pollnum; i++) {
if (!(pollfds[i].revents & POLLIN)) {
continue;
}
int fd = pollfds[i].fd;
if (i < num_servs) {
/* If server socket */
int client = accept(fd, (struct sockaddr *)&client_addr,
&client_addr_len);
void *addr = &((struct sockaddr_in *)&client_addr)->sin_addr;
if (client < 0) {
printf("error: accept: %d\n", errno);
continue;
}
inet_ntop(client_addr.ss_family, addr,
addr_str, sizeof(addr_str));
printf("Connection #%d from %s fd=%d\n", counter++,
addr_str, client);
if (pollfds_add(client) < 0) {
static char msg[] = "Too many connections\n";
res = send(client, msg, sizeof(msg) - 1, 0);
if (res < 0) {
printf("error: send: %d\n", errno);
}
close(client);
} else {
setblocking(client, false);
}
} else {
char buf[128];
int len = recv(fd, buf, sizeof(buf), 0);
if (len <= 0) {
if (len < 0) {
printf("error: recv: %d\n", errno);
}
error:
pollfds_del(fd);
close(fd);
printf("Connection fd=%d closed\n", fd);
} else {
int out_len;
const char *p;
/* We implement semi-async server,
* where reads are async, but writes
* *can* be sync (blocking). Note that
* in majority of cases they expected
* to not block, but to be robust, we
* handle all possibilities.
*/
setblocking(fd, true);
for (p = buf; len; len -= out_len) {
out_len = send(fd, p, len, 0);
if (out_len < 0) {
printf("error: "
"send: %d\n",
errno);
goto error;
}
p += out_len;
}
setblocking(fd, false);
}
}
}
}
}
|