1 /* @(#)socket.h	1.3 12/11/14 Copyright 2009-2012 J. Schilling */
2 /*
3  *	Socket abstraction
4  *
5  *	Copyright (c) 2009-2012 J. Schilling
6  */
7 /*
8  * The contents of this file are subject to the terms of the
9  * Common Development and Distribution License, Version 1.0 only
10  * (the "License").  You may not use this file except in compliance
11  * with the License.
12  *
13  * See the file CDDL.Schily.txt in this distribution for details.
14  * A copy of the CDDL is also available via the Internet at
15  * http://www.opensource.org/licenses/cddl1.txt
16  *
17  * When distributing Covered Code, include this CDDL HEADER in each
18  * file and include the License file CDDL.Schily.txt from this distribution.
19  */
20 
21 #ifndef	_SCHILY_SOCKET_H
22 #define	_SCHILY_SOCKET_H
23 
24 #ifndef _SCHILY_MCONFIG_H
25 #include <schily/mconfig.h>
26 #endif
27 
28 #ifdef	HAVE_SYS_SOCKET_H
29 
30 #ifndef	_INCL_SYS_SOCKET_H
31 #include <sys/socket.h>
32 #define	_INCL_SYS_SOCKET_H
33 #endif
34 
35 /*
36  * Compatibility defines for UNIX/POSIX:
37  *
38  * Win32 defines a socket layer in winsock.h that is not POSIX compliant.
39  * The functions socket() and accept() return an "unsigned int" instead of just
40  * an "int". As a result, an error return from socket() and accept() cannot be
41  * -1 but is INVALID_SOCKET. All functions from the Win32 socket layer except
42  * socket() and accept() return -1 on error.
43  * Since a socket is not a file descriptor on Win32, we cannot call close()
44  * but need to call closesocket().
45  * If we like to write software that compiles on a Win32 system without a
46  * POSIX layer, we need use the following definitions as a workaround even
47  * for UNIX/POSIX systems.
48  */
49 
50 #define	SOCKET		int	/* The socket type on UNIX/POSIX */
51 #define	INVALID_SOCKET	(-1)	/* Error return code for socket()/accept() */
52 #define	closesocket	close	/* Use instead of close(s) for Win32 compat */
53 
54 #else	/* On a non-POSIX system: */
55 /*
56  * If we are on a Win32 system without a POSIX layer, we would need to include
57  * winsock.h but this includes definitions that cause compatibility problems.
58  * For this reason, we instead include our windows.h that contains the needed
59  * workaround.
60  */
61 #ifdef	HAVE_WINDOWS_H
62 #include <schily/windows.h>
63 #endif	/* HAVE_WINDOWS_H */
64 #endif
65 
66 #endif	/* _SCHILY_SOCKET_H */
67