/* ***********************IMPORTANT *********************************************
 *  Copyright (C) 2004-2007 Thierry Lagarde <tlagarde@users.sourceforge.net>
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License.
 *
 *
 *                      ********************
 *
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *
 *                      ********************
 *
 *
 * You are highly encouraged to send your changes to <tlagarde@users.sourceforge.net>
 * for possible incorporation into the main distribution.
 * By sending these changes to <tlagarde@users.sourceforge.net>,
 * it is assumed that you are offering <tlagarde@users.sourceforge.net>
 * the unlimited, non-exclusive right to reuse, modify, and relicense the code.
 *
 *
 *                      ********************
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 **************************************************************************************/
#include <semaphore.h>
#include <sys/types.h>
#ifndef         __WINDOWS_ENABLE
#include <sys/socket.h>
#else
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
#include <errno.h>
#include <unistd.h>
#include "Socket.h"
#include "Semaphore.h"
#include "Debug.h"

int socket_control (int domain, int type, int protocol)
{
	int       Socket;

	if ((Socket = socket (domain, type, protocol)) != -1) {
		PROTECT_CODE (Semaphore_Control_Usage_Socket);
		Nb_Socket_Ouverte++;
		DEPROTECT_CODE(Semaphore_Control_Usage_Socket);
	}
	return Socket;
}

int close_control (int fd)
{
	int       close_s;

	if (fd<=0){
		debug_printf (D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "The descriptor is not a socket");
		return -1;
	}
#ifndef         __WINDOWS_ENABLE
	if ((close_s = close (fd)) != -1) {
		PROTECT_CODE (Semaphore_Control_Usage_Socket);
		Nb_Socket_Ouverte--;
		DEPROTECT_CODE (Semaphore_Control_Usage_Socket);	
	}
#else
	if ((close_s=close(fd)) == -1 && errno == EBADF) {
	        if ((close_s=closesocket(fd))==-1){
	   		int error = WSAGetLastError();
			switch (error){
				case WSANOTINITIALISED:
					debug_printf (D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__,"A successful WSAStartup call must occur before using this function.");
				break;
			case WSAENETDOWN:
					debug_printf (D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__,"The network subsystem has failed.");
				break;
			case WSAENOTSOCK:
					debug_printf (D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__,"The descriptor is not a socket.");
					break;
			case WSAEINPROGRESS:
					debug_printf (D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__,"A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.");
					break;
			case WSAEINTR:
					debug_printf (D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__,"The (blocking) Windows Socket 1.1 call was canceled through WSACancelBlockingCall.");
					break;
			case WSAEWOULDBLOCK:
					debug_printf (D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__,"The socket is marked as nonblocking, but the l_onoff member of the linger structure is set to non-zero and the l_linger member of the linger structure is set to a nonzero timeout value.");
					break;
			default:
					debug_printf (D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__,"Erreur Inconnu = %d",error);
			}
		}
	}
	PROTECT_CODE (Semaphore_Control_Usage_Socket);
	Nb_Socket_Ouverte--;
	DEPROTECT_CODE (Semaphore_Control_Usage_Socket);
#endif
	return close_s;
}

int close_Shutdown_control (FILE *In)
{
	int       close_s;

	if ((close_s = shutdown(fileno(In),SHUT_RDWR)) != -1) {
		PROTECT_CODE (Semaphore_Control_Usage_Socket);
		Nb_Socket_Ouverte--;
		DEPROTECT_CODE (Semaphore_Control_Usage_Socket);
	}
	else
		debug_printf (D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "%d", close_s);
	return close_s;
}
