2 * Test name: test05_srv.c
4 * Objective: Test an impatient UDP server with timeouts
6 * Description: Implements an echo server using the UDP protocol. It is
7 * based on test04_srv, but it has a timeout value.
10 #include <sys/types.h>
11 #include <sys/ioctl.h>
12 #include <sys/select.h>
13 #include <sys/asynchio.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>
31 /* type for received data */
38 int udp_conf(long port
) {
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");
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");
68 /* Get conf options */
69 if ((ioctl(netfd
, NWIOGUDPOPT
, &udpopt
))<0)
71 fprintf(stderr
,"Error getting the conf\n");
79 int main(int argc
,char *argv
[]) {
87 struct timeval timeout
;
88 ipaddr_t client_addr
, this_addr
;
89 udpport_t client_port
;
91 if ((fd
= udp_conf(PORT
)) < 0) {
92 fprintf(stderr
, "Error configuring UDP connection\n");
95 printf("Waiting for messages on port: %ld\n", PORT
);
98 /* get a first message so we know who is the client and we can harass it
101 /* Initialize fd_set */
103 FD_SET(fd
, &fds_read
);
104 /* Set timeout structure */
107 ret
= select(4, &fds_read
, NULL
, NULL
, NULL
);
110 fprintf(stderr
, "Error in select\n");
113 if (!FD_ISSET(fd
, &fds_read
)) {
114 fprintf(stderr
, "Error: Should be receiving some data from network(?)\n");
117 printf("Ready to receive...\n");
118 /* Read received data */
119 data_read
= read(fd
, &buffer
, sizeof(udp_buffer_t
));
120 printf("Received data: %s\n", buffer
.data
);
122 /* Can exit if the received string == exit */
123 if (!strcmp(buffer
.data
,"exit"))
126 /* Send data back, swap addresses */
127 tmp_addr
= buffer
.header
.uih_src_addr
;
128 buffer
.header
.uih_src_addr
= buffer
.header
.uih_dst_addr
;
129 buffer
.header
.uih_dst_addr
= tmp_addr
;
130 /* save address of both ends */
131 client_addr
= tmp_addr
;
132 this_addr
= buffer
.header
.uih_src_addr
;
135 tmp_port
= buffer
.header
.uih_src_port
;
136 buffer
.header
.uih_src_port
= buffer
.header
.uih_dst_port
;
137 buffer
.header
.uih_dst_port
= tmp_port
;
138 /* save client port */
139 client_port
= tmp_port
;
141 /* Write the same back */
142 write(fd
, &buffer
, data_read
);
147 /* Initialize fd_set */
149 FD_SET(fd
, &fds_read
);
150 /* Set timeout structure */
153 /* Wait for data available to be read (timeout) */
154 ret
= select(4, &fds_read
, NULL
, NULL
, &timeout
);
156 fprintf(stderr
, "Error on select: %d", errno
);
161 /* Send angry msg to client asking for more */
162 printf("Tired of waiting, send client an angry message\n");
163 buffer
.header
.uih_src_addr
= this_addr
;
164 buffer
.header
.uih_dst_addr
= client_addr
;
165 buffer
.header
.uih_src_port
= PORT
;
166 buffer
.header
.uih_dst_port
= client_port
;
167 strcpy(buffer
.data
, "Hey! I want to receive some data!\n");
168 write(fd
, &buffer
, sizeof(udp_buffer_t
));
170 /* If receive data from network */
171 if (FD_ISSET(fd
, &fds_read
)) {
172 printf("Ready to receive...\n");
173 /* Read received data */
174 data_read
= read(fd
, &buffer
, sizeof(udp_buffer_t
));
175 printf("Received data: %s\n", buffer
.data
);
177 /* Can exit if the received string == exit */
178 if (!strcmp(buffer
.data
,"exit"))
181 /* Send data back, swap addresses */
182 tmp_addr
= buffer
.header
.uih_src_addr
;
183 buffer
.header
.uih_src_addr
= buffer
.header
.uih_dst_addr
;
184 buffer
.header
.uih_dst_addr
= tmp_addr
;
187 tmp_port
= buffer
.header
.uih_src_port
;
188 buffer
.header
.uih_src_port
= buffer
.header
.uih_dst_port
;
189 buffer
.header
.uih_dst_port
= tmp_port
;
191 /* Write the same back */
192 write(fd
, &buffer
, data_read
);