minix/test/select/test06_srv.c

128 lines
3.1 KiB
C
Raw Normal View History

/*
* Test name: test06_srv.c
*
* Objective: Test a simple TCP server
*
* Description: Implements a simple echo server using the TCP protocol. Instead
* of blocking on read(), it performs a select call first blocking there
* until there is data to be read
*/
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/wait.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <net/netlib.h>
#include <net/gen/netdb.h>
#include <net/gen/in.h>
#include <net/gen/tcp.h>
#include <net/gen/tcp_io.h>
#include <net/hton.h>
#include <net/gen/inet.h>
#define PORT 6060L
This patch switches the MINIX3 ethernet driver stack from a port-based model to an instance-based model. Each ethernet driver instance is now responsible for exactly one network interface card. The port field in /etc/inet.conf now acts as an instance field instead. This patch also updates the data link protocol. This update: - eliminates the concept of ports entirely; - eliminates DL_GETNAME entirely; - standardizes on using m_source for IPC and DL_ENDPT for safecopies; - removes error codes from TASK/STAT replies, as they were unused; - removes a number of other old or unused fields; - names and renames a few other fields. All ethernet drivers have been changed to: - conform to the new protocol, and exactly that; - take on an instance number based on a given "instance" argument; - skip that number of PCI devices in probe iterations; - use config tables and environment variables based on that number; - no longer be limited to a predefined maximum of cards in any way; - get rid of any leftover non-safecopy support and other ancient junk; - have a correct banner protocol figure, or none at all. Other changes: * Inet.conf is now taken to be line-based, and supports #-comments. No existing installations are expected to be affected by this. * A new, select-based asynchio library replaces the old one. Kindly contributed by Kees J. Bot. * Inet now supports use of select() on IP devices. Combined, the last two changes together speed up dhcpd considerably in the presence of multiple interfaces. * A small bug has been fixed in nonamed.
2010-05-18 00:22:53 +02:00
int my_listen(long port) {
char *tcp_device;
int netfd;
nwio_tcpconf_t tcpconf;
nwio_tcpcl_t tcplistenopt;
/* Get default UDP device */
if ((tcp_device = getenv("TCP_DEVICE")) == NULL)
tcp_device = TCP_DEVICE;
/* Open TCP connection */
if ((netfd = open(tcp_device, O_RDWR)) < 0)
{
fprintf(stderr,"Error opening TCP connection\n");
return -1;
}
/* Configure TCP connection */
tcpconf.nwtc_flags = NWTC_LP_SET | NWTC_UNSET_RA | NWTC_UNSET_RP;
tcpconf.nwtc_locport = (tcpport_t) htons(port);
if ((ioctl(netfd, NWIOSTCPCONF, &tcpconf))<0)
{
fprintf(stderr,"Error configuring the connection\n");
close(netfd);
return -1;
}
/* Get communication options */
if ((ioctl(netfd, NWIOGTCPCONF, &tcpconf)) < 0) {
fprintf(stderr, "Error getting configuration\n");
close(netfd);
return -1;
}
/* Set conf options */
tcplistenopt.nwtcl_flags = 0;
printf("Waiting for connections...\n");
while ((ioctl(netfd, NWIOTCPLISTEN, &tcplistenopt)) == -1)
{
if (errno != EAGAIN)
{
fprintf(stderr,"Unable to listen for connections\n");
close(netfd);
}
sleep(-1);
}
return netfd;
}
int main(int argc,char *argv[]) {
int fd;
ssize_t data_read;
char buffer[1024];
int ret;
fd_set fds_read;
This patch switches the MINIX3 ethernet driver stack from a port-based model to an instance-based model. Each ethernet driver instance is now responsible for exactly one network interface card. The port field in /etc/inet.conf now acts as an instance field instead. This patch also updates the data link protocol. This update: - eliminates the concept of ports entirely; - eliminates DL_GETNAME entirely; - standardizes on using m_source for IPC and DL_ENDPT for safecopies; - removes error codes from TASK/STAT replies, as they were unused; - removes a number of other old or unused fields; - names and renames a few other fields. All ethernet drivers have been changed to: - conform to the new protocol, and exactly that; - take on an instance number based on a given "instance" argument; - skip that number of PCI devices in probe iterations; - use config tables and environment variables based on that number; - no longer be limited to a predefined maximum of cards in any way; - get rid of any leftover non-safecopy support and other ancient junk; - have a correct banner protocol figure, or none at all. Other changes: * Inet.conf is now taken to be line-based, and supports #-comments. No existing installations are expected to be affected by this. * A new, select-based asynchio library replaces the old one. Kindly contributed by Kees J. Bot. * Inet now supports use of select() on IP devices. Combined, the last two changes together speed up dhcpd considerably in the presence of multiple interfaces. * A small bug has been fixed in nonamed.
2010-05-18 00:22:53 +02:00
if ((fd = my_listen(PORT)) < 0) {
exit(-1);
}
printf("Waiting for messages on port: %ld\n", PORT);
fflush(stdout);
/* Initialize fd_set */
FD_ZERO(&fds_read);
FD_SET(fd, &fds_read);
while (1)
{
/* Wait for data available to be read (no timeout) */
ret = select(4, &fds_read, NULL, NULL, NULL);
if (ret < 0) {
fprintf(stderr, "Error on select: %d\n", errno);
exit(-1);
}
if (!FD_ISSET(fd, &fds_read)) {
printf("Error: network fd is not ready (?)\n");
exit(-1);
}
printf("Ready to receive...\n");
/* Read received data */
data_read = read(fd, &buffer, 1024);
printf("Received data: %s\n", buffer);
/* Can exit if the received string == exit */
if (!strcmp(buffer,"exit"))
break;
/* Write the same back */
write(fd, &buffer, data_read);
}
printf("Connection finished\n");
close(fd);
}