
/* OpenWebSpider
*
 *  Authors:     Stefano Alimonti AND Stefano Fantin
 *  Version:     0.7
 *  E-Mails:     shen139 [at] openwebspider (dot) org AND stefanofantinguz@yahoo.it
*
*
* This file is part of OpenWebSpider
*
* 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, or
* (at your option) any later version.
*
* 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 General Public License for more details.
*
* 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
*
*/

#ifndef __SOCKET
#define __SOCKET

#define SEND(sock,buf)       send(sock,buf,strlen(buf),0)

SOCKET OWS_Server_fd_conn;
SOCKET OWS_Server_fd;


int setnonblock(SOCKET sock,int to)
{
#ifdef WIN32
	setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,(char*) &to, sizeof(int));
#else
	struct timeval timeout;
	
	timeout.tv_sec = to/1000;
	timeout.tv_usec=0;
	
	if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,(char*) &timeout, sizeof(timeout)))
		return 0;
#endif
	return 1;
}

int StartUpWinsock()
{
#ifdef WIN32
	WSADATA wsadata;
	
	if((WSAStartup(MAKEWORD(1,1),&wsadata))!=0 || (LOBYTE(wsadata.wVersion )!=1 || HIBYTE(wsadata.wVersion)!=1))
		return 0;
#endif
	return 1;
}

int LoadSocket(SOCKET *sock,struct sHost* shost,SOCKADDR_IN *saddr)
{
	struct hostent *gAddr;
	if(strlen(shost->Host)<3)
		return 0;
	
	*sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	
#ifdef WIN32
	if (*sock==INVALID_SOCKET)
#else
		if (*sock<0)
#endif
			return 0;
		
		if((gAddr=gethostbyname(shost->Host))==0)
			return 0;
		
		saddr->sin_addr.s_addr=((struct in_addr *)(gAddr->h_addr))->s_addr;
		saddr->sin_family = AF_INET;
		saddr->sin_port = htons((unsigned short int)shost->port);
		
		return 1;
}

int RecvPackets(SOCKET *sock,char* packet, int maxlen)
{
	
	int x=0;
	int recvdbytes=0;
	
	memset(packet,0,maxlen);
	recvdbytes=0;
	
	setnonblock(*sock, FIRSTTIMEOUT);
	
	do
	{
		recvdbytes=recv(*sock,packet+x,50000,0);
		
		if(recvdbytes>0)
			x+=recvdbytes;
		else
			return x;
		
		setnonblock(*sock, TIMEOUTs);
		
	} while(recvdbytes!=0 && x+50000<maxlen);
	
	/*testing mycstr*/
	/*
	MYCSTR test;
	char curPack[50000];
	test.myString=NULL;
	myCStrCpy(&test, "");
	
	 memset(packet,0,maxlen);
	 recvdbytes=0;
	 
	  setnonblock(*sock, FIRSTTIMEOUT);
	  
	   do
	   {
	   recvdbytes=recv(*sock,curPack,50000,0);
	   
		if(recvdbytes>0)
		{
		x+=recvdbytes;
		myCStrMemCat(&test, curPack,recvdbytes);
		
		 }
		 else
		 {
		 printf("\n\n\n\n_______%i______%i_____\n\n\n",x,x/1024);
		 strncpy(packet,test.myString,maxlen);
		 return x;
		 }
		 
		  setnonblock(*sock, TIMEOUTs);
		  
		   } while(recvdbytes!=0);
		   
			printf("\n\n\n\n_______%i______%i_____\n\n\n",x,x/1024);
			strncpy(packet,test.myString,maxlen);
	*/
	
	return x;
}


/*OWS Server*/
int ListenToPort(int port, SOCKET* fd)
{
int opt = 1;

	SOCKADDR_IN saddr;
	
	*fd=socket(AF_INET,SOCK_STREAM,0);
#ifdef WIN32
	if (*fd==INVALID_SOCKET)
#else
		if (*fd<0)
#endif
			return -1;

		setsockopt(*fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
		
		saddr.sin_family = AF_INET;
		saddr.sin_addr.s_addr= htonl(INADDR_ANY);
		saddr.sin_port = htons((unsigned short int)port);
		
		if (bind(*fd, (LPSOCKADDR) &saddr, sizeof(saddr)) == SOCKET_ERROR)
			return -2;
		
		if (listen(*fd,100) == SOCKET_ERROR)
			return -3;
		
		return 1;
}


#endif

/*EOF*/
