1 /*
2  *  Copyright 2006  Serge van den Boom <svdb@stack.nl>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18 
19 #ifndef LIBS_NETWORK_SOCKET_SOCKET_H_
20 #define LIBS_NETWORK_SOCKET_SOCKET_H_
21 
22 typedef struct Socket Socket;
23 #define Socket_noSocket ((Socket *) NULL)
24 
25 #include "port.h"
26 
27 #ifdef USE_WINSOCK
28 #	include "socket_win.h"
29 #else
30 #	include "socket_bsd.h"
31 #endif
32 
33 
34 ////////////////////////////////////////////////////////////////////////////
35 
36 
37 // Defining our own types for protocol families and protocols instead of
38 // using the system defines, so that the layer using this API does not have
39 // to have anything to do with the system layer.
40 
41 typedef enum {
42 	PF_unspec,
43 	PF_inet,
44 	PF_inet6,
45 } ProtocolFamily;
46 typedef ProtocolFamily AddressFamily;
47 
48 typedef enum {
49 	IPProto_tcp,
50 	IPProto_udp,
51 } Protocol;
52 
53 typedef enum {
54 	Sock_stream,
55 	Sock_dgram,
56 } SocketType;
57 
58 #ifdef SOCKET_INTERNAL
59 extern const int protocolFamilyTranslation[];
60 #define addressFamilyTranslation protocolFamilyTranslation;
61 extern const int protocolTranslation[];
62 extern const int socketTypeTranslation[];
63 #endif
64 
65 
66 ////////////////////////////////////////////////////////////////////////////
67 
68 
69 Socket *Socket_open(ProtocolFamily domain, SocketType type,
70 		Protocol protocol);
71 #ifdef SOCKET_INTERNAL
72 Socket *Socket_openNative(int domain, int type, int protocol);
73 #endif
74 int Socket_close(Socket *sock);
75 
76 int Socket_connect(Socket *sock, const struct sockaddr *addr,
77 		socklen_t addrLen);
78 int Socket_bind(Socket *sock, const struct sockaddr *addr,
79 		socklen_t addrLen);
80 int Socket_listen(Socket *sock, int backlog);
81 Socket *Socket_accept(Socket *sock, struct sockaddr *addr, socklen_t *addrLen);
82 ssize_t Socket_send(Socket *sock, const void *buf, size_t len, int flags);
83 ssize_t Socket_sendto(Socket *sock, const void *buf, size_t len, int flags,
84 		const struct sockaddr *addr, socklen_t addrLen);
85 ssize_t Socket_recv(Socket *sock, void *buf, size_t len, int flags);
86 ssize_t Socket_recvfrom(Socket *sock, void *buf, size_t len, int flags,
87 		struct sockaddr *from, socklen_t *fromLen);
88 
89 int Socket_setNonBlocking(Socket *sock);
90 int Socket_setReuseAddr(Socket *sock);
91 int Socket_setNodelay(Socket *sock);
92 int Socket_setTOS(Socket *sock, int tos);
93 int Socket_setInteractive(Socket *sock);
94 int Socket_setInlineOOB(Socket *sock);
95 int Socket_setKeepAlive(Socket *sock);
96 int Socket_getError(Socket *sock, int *err);
97 
98 #endif  /* LIBS_NETWORK_SOCKET_SOCKET_H_ */
99 
100