2005-07-27 13:57:59 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2005-07-29 12:13:52 +02:00
|
|
|
#include <signal.h>
|
2005-07-27 13:57:59 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include <net/netlib.h>
|
2005-09-01 17:24:29 +02:00
|
|
|
#include <netinet/in.h>
|
2005-07-27 13:57:59 +02:00
|
|
|
|
|
|
|
#define DEBUG 0
|
|
|
|
|
|
|
|
static int _tcp_socket(int protocol);
|
|
|
|
static int _udp_socket(int protocol);
|
|
|
|
|
|
|
|
int socket(int domain, int type, int protocol)
|
|
|
|
{
|
2005-07-29 12:13:52 +02:00
|
|
|
#if DEBUG
|
|
|
|
fprintf(stderr, "socket: domain %d, type %d, protocol %d\n",
|
|
|
|
domain, type, protocol);
|
|
|
|
#endif
|
2005-07-27 13:57:59 +02:00
|
|
|
if (domain != AF_INET)
|
|
|
|
{
|
|
|
|
#if DEBUG
|
|
|
|
fprintf(stderr, "socket: bad domain %d\n", domain);
|
|
|
|
#endif
|
|
|
|
errno= EAFNOSUPPORT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (type == SOCK_STREAM)
|
|
|
|
return _tcp_socket(protocol);
|
|
|
|
|
|
|
|
if (type == SOCK_DGRAM)
|
|
|
|
return _udp_socket(protocol);
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
fprintf(stderr, "socket: nothing for domain %d, type %d, protocol %d\n",
|
|
|
|
domain, type, protocol);
|
|
|
|
#endif
|
|
|
|
errno= EPROTOTYPE;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _tcp_socket(int protocol)
|
|
|
|
{
|
|
|
|
int fd;
|
2005-09-01 17:24:29 +02:00
|
|
|
if (protocol != 0 && protocol != IPPROTO_TCP)
|
2005-07-27 13:57:59 +02:00
|
|
|
{
|
|
|
|
#if DEBUG
|
|
|
|
fprintf(stderr, "socket(tcp): bad protocol %d\n", protocol);
|
|
|
|
#endif
|
|
|
|
errno= EPROTONOSUPPORT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fd= open(TCP_DEVICE, O_RDWR);
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _udp_socket(int protocol)
|
|
|
|
{
|
|
|
|
int fd;
|
2005-07-29 12:13:52 +02:00
|
|
|
|
2005-09-01 17:24:29 +02:00
|
|
|
if (protocol != 0 && protocol != IPPROTO_UDP)
|
2005-07-27 13:57:59 +02:00
|
|
|
{
|
|
|
|
#if DEBUG
|
|
|
|
fprintf(stderr, "socket(udp): bad protocol %d\n", protocol);
|
|
|
|
#endif
|
|
|
|
errno= EPROTONOSUPPORT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fd= open(UDP_DEVICE, O_RDWR);
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|