mirror of
https://github.com/genodelabs/genode.git
synced 2024-12-19 05:37:54 +00:00
test: RFC862 UDP echo server
This commit is contained in:
parent
304f2eaf38
commit
9d7cda04fa
121
repos/libports/run/echo_udp.run
Normal file
121
repos/libports/run/echo_udp.run
Normal file
@ -0,0 +1,121 @@
|
||||
set build_components {
|
||||
core init
|
||||
drivers/timer drivers/nic lib/vfs/lxip
|
||||
server/vfs server/dynamic_rom
|
||||
test/echo_udp
|
||||
}
|
||||
|
||||
source ${genode_dir}/repos/base/run/platform_drv.inc
|
||||
append_platform_drv_build_components
|
||||
|
||||
build $build_components
|
||||
|
||||
create_boot_directory
|
||||
|
||||
set config {
|
||||
<config verbose="yes">
|
||||
<parent-provides>
|
||||
<service name="CPU"/>
|
||||
<service name="IO_MEM"/>
|
||||
<service name="IO_PORT"/>
|
||||
<service name="IRQ"/>
|
||||
<service name="LOG"/>
|
||||
<service name="PD"/>
|
||||
<service name="RAM"/>
|
||||
<service name="RM"/>
|
||||
<service name="ROM"/>
|
||||
</parent-provides>
|
||||
<default-route>
|
||||
<any-service> <parent/> <any-child/> </any-service>
|
||||
</default-route>
|
||||
<start name="timer">
|
||||
<resource name="RAM" quantum="1M"/>
|
||||
<provides> <service name="Timer"/> </provides>
|
||||
</start>
|
||||
<start name="nic_drv">
|
||||
<resource name="RAM" quantum="2M"/>
|
||||
<provides> <service name="Nic"/> </provides>
|
||||
</start>
|
||||
|
||||
<start name="dynamic_rom">
|
||||
<resource name="RAM" quantum="4M"/>
|
||||
<provides><service name="ROM"/> </provides>
|
||||
<config verbose="yes">
|
||||
<rom name="socket_fs.config">
|
||||
<inline description="static">
|
||||
<config ld_verbose="yes">
|
||||
<vfs>
|
||||
<lxip ip_addr="10.0.2.55" netmask="255.255.255.0"
|
||||
gateway="10.0.2.1" nameserver="8.8.8.8"/>
|
||||
</vfs>
|
||||
<default-policy writeable="yes" />
|
||||
</config>
|
||||
</inline>
|
||||
<sleep milliseconds="5000"/>
|
||||
<inline description="dynamic">
|
||||
<config ld_verbose="yes">
|
||||
<vfs>
|
||||
<lxip dhcp="yes"/>
|
||||
</vfs>
|
||||
<default-policy writeable="yes" />
|
||||
</config>
|
||||
</inline>
|
||||
<sleep milliseconds="5000"/>
|
||||
</rom>
|
||||
</config>
|
||||
</start>
|
||||
|
||||
<start name="socket_fs">
|
||||
<binary name="vfs"/>
|
||||
<resource name="RAM" quantum="32M"/>
|
||||
<provides> <service name="File_system"/> </provides>
|
||||
<configfile name="socket_fs.config"/>
|
||||
<route>
|
||||
<service name="ROM" label="socket_fs.config"> <child name="dynamic_rom"/> </service>
|
||||
<any-service> <parent/> <any-child/> </any-service>
|
||||
</route>
|
||||
</start>
|
||||
|
||||
<start name="echo">
|
||||
<binary name="test-echo_udp"/>
|
||||
<resource name="RAM" quantum="4M"/>
|
||||
<config ld_verbose="yes">
|
||||
<vfs>
|
||||
<dir name="dev"> <log/> <null/> </dir>
|
||||
<dir name="socket"> <fs/> </dir>
|
||||
</vfs>
|
||||
<libc stdin="/dev/null" stdout="/dev/log" stderr="/dev/log" socket="/socket"/>
|
||||
</config>
|
||||
</start>
|
||||
}
|
||||
|
||||
append_platform_drv_config
|
||||
|
||||
append config {
|
||||
</config>
|
||||
}
|
||||
|
||||
install_config $config
|
||||
|
||||
set boot_modules {
|
||||
core init timer nic_drv vfs dynamic_rom
|
||||
ld.lib.so libc.lib.so libm.lib.so
|
||||
libc_resolv.lib.so stdcxx.lib.so
|
||||
vfs_lxip.lib.so lxip.lib.so
|
||||
test-echo_udp
|
||||
}
|
||||
|
||||
append_platform_drv_boot_modules
|
||||
|
||||
build_boot_image $boot_modules
|
||||
|
||||
puts "####################################################################"
|
||||
puts "## run 'netcat --udp <target ip> 7' to connect to the echo server ##"
|
||||
puts "####################################################################"
|
||||
sleep 1
|
||||
|
||||
append qemu_args " -nographic -net nic,model=e1000 -net tap,ifname=tap0,downscript=no,script=no "
|
||||
|
||||
run_genode_until forever
|
||||
|
||||
# vi: set ft=tcl :
|
119
repos/libports/src/test/echo_udp/main.cc
Normal file
119
repos/libports/src/test/echo_udp/main.cc
Normal file
@ -0,0 +1,119 @@
|
||||
/*
|
||||
* \brief RFC862 echo server
|
||||
* \author Emery Hemingway
|
||||
* \date 2016-10-17
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2016-2017 Genode Labs GmbH
|
||||
*
|
||||
* This file is part of the Genode OS framework, which is distributed
|
||||
* under the terms of the GNU General Public License version 2.
|
||||
*/
|
||||
|
||||
/* Genode includes */
|
||||
#include <base/log.h>
|
||||
|
||||
/* libc includes */
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/fcntl.h>
|
||||
#include <sys/select.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
|
||||
#define ECHO_PORT 7
|
||||
#define MAXBUFLEN 0xFFFF
|
||||
#define RECV_FLAGS 0
|
||||
#define SEND_FLAGS 0
|
||||
|
||||
static void print(Genode::Output &output, sockaddr_in const &addr)
|
||||
{
|
||||
print(output, (ntohl(addr.sin_addr.s_addr) >> 24) & 0xff);
|
||||
output.out_string(".");
|
||||
print(output, (ntohl(addr.sin_addr.s_addr) >> 16) & 0xff);
|
||||
output.out_string(".");
|
||||
print(output, (ntohl(addr.sin_addr.s_addr) >> 8) & 0xff);
|
||||
output.out_string(".");
|
||||
print(output, (ntohl(addr.sin_addr.s_addr) >> 0) & 0xff);
|
||||
output.out_string(":");
|
||||
print(output, ntohs(addr.sin_port));
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int udp_sock;
|
||||
int rv = 0;
|
||||
ssize_t numbytes;
|
||||
struct sockaddr_in their_addr;
|
||||
char buf[MAXBUFLEN];
|
||||
socklen_t addr_len;
|
||||
|
||||
udp_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
if (udp_sock < 0) {
|
||||
Genode::log("socket failed");
|
||||
return errno;
|
||||
}
|
||||
|
||||
struct sockaddr_in const addr = { 0, AF_INET, htons(ECHO_PORT), { INADDR_ANY } };
|
||||
|
||||
if (bind(udp_sock, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
|
||||
close(udp_sock);
|
||||
Genode::log("bind failed");
|
||||
return errno;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
fd_set read_fds;
|
||||
|
||||
FD_ZERO(&read_fds);
|
||||
FD_SET(udp_sock, &read_fds);
|
||||
|
||||
timeval tv { 10, 0 };
|
||||
|
||||
int num_ready = select(udp_sock + 1, &read_fds, nullptr, nullptr, &tv);
|
||||
if (num_ready == -1) {
|
||||
perror("select failed");
|
||||
break;
|
||||
}
|
||||
if (!num_ready) {
|
||||
Genode::log("timeout");
|
||||
continue;
|
||||
}
|
||||
if (!FD_ISSET(udp_sock, &read_fds)) {
|
||||
Genode::log("spurious wakeup");
|
||||
continue;
|
||||
}
|
||||
Genode::log("num_ready=", num_ready);
|
||||
|
||||
addr_len = sizeof their_addr;
|
||||
numbytes = recvfrom(udp_sock, buf, sizeof(buf), RECV_FLAGS,
|
||||
(struct sockaddr *)&their_addr, &addr_len);
|
||||
if (numbytes == -1) {
|
||||
rv = errno;
|
||||
perror("recvfrom failed");
|
||||
break;
|
||||
}
|
||||
|
||||
Genode::log("received ", numbytes, " bytes from ", their_addr);
|
||||
|
||||
numbytes = sendto(udp_sock, buf, numbytes, SEND_FLAGS,
|
||||
(struct sockaddr *)&their_addr, addr_len);
|
||||
if (numbytes == -1) {
|
||||
rv = errno;
|
||||
perror("sendto failed");
|
||||
break;
|
||||
}
|
||||
|
||||
Genode::log("sent ", numbytes, " bytes to ", their_addr);
|
||||
}
|
||||
|
||||
close(udp_sock);
|
||||
return rv;
|
||||
}
|
3
repos/libports/src/test/echo_udp/target.mk
Normal file
3
repos/libports/src/test/echo_udp/target.mk
Normal file
@ -0,0 +1,3 @@
|
||||
TARGET = test-echo_udp
|
||||
LIBS = posix libc_resolv
|
||||
SRC_CC = main.cc
|
Loading…
Reference in New Issue
Block a user