mkfs: symlink support
[minix.git] / test / select / test04_srv.c
blobb6fc6a5c49ffc731822484638075a68bdb814899
1 /*
2 * Test name: test04_srv.c
4 * Objective: Test a simple UDP server
6 * Description: Implements a simple echo server using the UDP protocol. Instead
7 * of blocking on read(), it performs a select call first blocking there
8 * until there is data to be read
9 */
11 #include <sys/types.h>
12 #include <sys/ioctl.h>
13 #include <sys/select.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <stdlib.h>
17 #include <stdarg.h>
18 #include <fcntl.h>
19 #include <errno.h>
20 #include <unistd.h>
21 #include <net/netlib.h>
22 #include <net/gen/netdb.h>
23 #include <net/gen/in.h>
24 #include <net/gen/udp.h>
25 #include <net/gen/udp_hdr.h>
26 #include <net/gen/udp_io.h>
27 #include <net/hton.h>
29 #define PORT 6000L
31 /* type for received data */
32 typedef struct
34 udp_io_hdr_t header;
35 char data[1024];
36 } udp_buffer_t;
38 int udp_conf(long port) {
40 char *udp_device;
41 int netfd;
42 nwio_udpopt_t udpopt;
44 /* Get default UDP device */
45 if ((udp_device = getenv("UDP_DEVICE")) == NULL)
46 udp_device = UDP_DEVICE;
48 /* Open UDP connection */
49 if ((netfd = open(udp_device, O_RDWR)) < 0)
51 fprintf(stderr,"Error opening UDP connection\n");
52 return -1;
55 /* Configure UDP connection */
56 udpopt.nwuo_flags = NWUO_COPY | NWUO_LP_SET | NWUO_EN_LOC | NWUO_DI_BROAD
57 | NWUO_RP_ANY | NWUO_RA_ANY | NWUO_RWDATALL | NWUO_DI_IPOPT;
59 udpopt.nwuo_locport = (udpport_t) htons(port);
61 if ((ioctl(netfd, NWIOSUDPOPT, &udpopt))<0)
63 fprintf(stderr,"Error configuring the connection\n");
64 close(netfd);
65 return -1;
68 /* Get conf options */
69 if ((ioctl(netfd, NWIOGUDPOPT, &udpopt))<0)
71 fprintf(stderr,"Error getting the conf\n");
72 close(netfd);
73 return -1;
76 return netfd;
79 int main(int argc,char *argv[]) {
80 int fd;
81 ssize_t data_read;
82 udp_buffer_t buffer;
83 ipaddr_t tmp_addr;
84 udpport_t tmp_port;
85 int ret;
86 fd_set fds_read;
88 if ((fd = udp_conf(PORT)) < 0) {
89 fprintf(stderr, "Error configuring UDP connection\n");
90 exit(-1);
92 printf("Waiting for messages on port: %ld\n", PORT);
93 fflush(stdout);
94 /* Initialize fd_set */
95 FD_ZERO(&fds_read);
96 FD_SET(fd, &fds_read);
98 while (1)
100 /* Wait for data available to be read (no timeout) */
101 ret = select(4, &fds_read, NULL, NULL, NULL);
102 if (ret < 0) {
103 fprintf(stderr, "Error on select: %d", errno);
104 exit(-1);
106 if (!FD_ISSET(fd, &fds_read)) {
107 printf("Error: network fd is not ready (?)\n");
108 exit(-1);
110 printf("Ready to receive...\n");
111 /* Read received data */
112 data_read = read(fd, &buffer, sizeof(udp_buffer_t));
113 printf("Received data: %s\n", buffer.data);
115 /* Can exit if the received string == exit */
116 if (!strcmp(buffer.data,"exit"))
117 break;
119 /* Send data back, swap addresses */
120 tmp_addr = buffer.header.uih_src_addr;
121 buffer.header.uih_src_addr = buffer.header.uih_dst_addr;
122 buffer.header.uih_dst_addr = tmp_addr;
124 /* Swap ports */
125 tmp_port = buffer.header.uih_src_port;
126 buffer.header.uih_src_port = buffer.header.uih_dst_port;
127 buffer.header.uih_dst_port = tmp_port;
129 /* Write the same back */
130 write(fd, &buffer, data_read);
133 close(fd);