2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
CSocket.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
(c) 2003-2004 Daniel Campos Fernández <dcamposf@gmail.com>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
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, or (at your option)
|
|
|
|
any later version.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
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.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __CSOCKET_C
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/poll.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
#include "tools.h"
|
|
|
|
|
|
|
|
#include "CSocket.h"
|
|
|
|
#include "CServerSocket.h"
|
|
|
|
#include "CDnsClient.h"
|
|
|
|
#define MAX_CLIENT_BUFFER_SIZE 65536
|
|
|
|
#define UNIXPATHMAX 108
|
|
|
|
|
2008-11-12 22:50:36 +01:00
|
|
|
DECLARE_EVENT (EVENT_Error);
|
|
|
|
DECLARE_EVENT (EVENT_Close);
|
|
|
|
DECLARE_EVENT (EVENT_Found);
|
|
|
|
DECLARE_EVENT (EVENT_Ready);
|
2009-06-07 01:31:48 +02:00
|
|
|
DECLARE_EVENT (EVENT_Read);
|
|
|
|
DECLARE_EVENT (EVENT_Write);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-06-07 01:31:48 +02:00
|
|
|
GB_STREAM_DESC SocketStream =
|
|
|
|
{
|
2007-12-30 17:41:49 +01:00
|
|
|
open: CSocket_stream_open,
|
|
|
|
close: CSocket_stream_close,
|
|
|
|
read: CSocket_stream_read,
|
|
|
|
write: CSocket_stream_write,
|
|
|
|
seek: CSocket_stream_seek,
|
|
|
|
tell: CSocket_stream_tell,
|
|
|
|
flush: CSocket_stream_flush,
|
|
|
|
eof: CSocket_stream_eof,
|
|
|
|
lof: CSocket_stream_lof,
|
|
|
|
handle: CSocket_stream_handle
|
|
|
|
};
|
2008-01-17 22:39:26 +01:00
|
|
|
|
|
|
|
|
2010-01-03 18:09:57 +01:00
|
|
|
bool SOCKET_update_timeout(CSOCKET_COMMON *socket)
|
2010-01-03 14:08:00 +01:00
|
|
|
{
|
|
|
|
struct timeval timeout;
|
|
|
|
|
2010-01-03 18:09:57 +01:00
|
|
|
if (socket->socket < 0)
|
2010-01-03 14:08:00 +01:00
|
|
|
return TRUE;
|
|
|
|
|
2010-01-03 18:09:57 +01:00
|
|
|
timeout.tv_sec = socket->timeout / 1000;
|
|
|
|
timeout.tv_usec = (socket->timeout % 1000) * 1000;
|
2010-01-03 14:08:00 +01:00
|
|
|
|
2010-01-03 18:09:57 +01:00
|
|
|
if (setsockopt(socket->socket, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) < 0)
|
2010-01-03 14:08:00 +01:00
|
|
|
{
|
|
|
|
GB.Error("Cannot set sending timeout");
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-01-03 18:09:57 +01:00
|
|
|
if (setsockopt(socket->socket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0)
|
2010-01-03 14:08:00 +01:00
|
|
|
{
|
|
|
|
GB.Error("Cannot set receiving timeout");
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
/**********************************
|
2010-12-28 22:58:42 +01:00
|
|
|
Routines to call events
|
|
|
|
**********************************/
|
2008-11-12 22:50:36 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
static void CSocket_post_error(void *_object)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-11-12 22:50:36 +01:00
|
|
|
GB.Raise(THIS,EVENT_Error,0);
|
2008-02-15 12:12:55 +01:00
|
|
|
GB.Unref(POINTER(&_object));
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
static void CSocket_post_closed(void *_object)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-12-28 22:58:42 +01:00
|
|
|
GB.Raise(THIS, EVENT_Close, 0);
|
2008-02-15 12:12:55 +01:00
|
|
|
GB.Unref(POINTER(&_object));
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
static void CSocket_post_hostfound(void *_object)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-11-12 22:50:36 +01:00
|
|
|
GB.Raise(THIS,EVENT_Found,0);
|
2008-02-15 12:12:55 +01:00
|
|
|
GB.Unref(POINTER(&_object));
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2008-02-15 12:12:55 +01:00
|
|
|
void CSocket_post_connected(void *_object)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-11-12 22:50:36 +01:00
|
|
|
GB.Raise(THIS,EVENT_Ready,0);
|
2008-02-15 12:12:55 +01:00
|
|
|
GB.Unref(POINTER(&_object));
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2009-06-07 03:04:58 +02:00
|
|
|
static void CSocket_close(CSOCKET *_object)
|
|
|
|
{
|
2010-12-28 22:58:42 +01:00
|
|
|
if (THIS->DnsTool)
|
|
|
|
{
|
2009-06-07 03:04:58 +02:00
|
|
|
dns_close_all(THIS->DnsTool);
|
|
|
|
GB.Unref(POINTER(&THIS->DnsTool));
|
|
|
|
THIS->DnsTool=NULL;
|
2010-12-28 22:58:42 +01:00
|
|
|
}
|
2009-06-07 03:04:58 +02:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
if (SOCKET->status > NET_INACTIVE) /* if it's not connected, does nothing */
|
|
|
|
{
|
|
|
|
GB.Watch(SOCKET->socket , GB_WATCH_NONE , (void *)CSocket_CallBack,0);
|
|
|
|
SOCKET->stream.desc = NULL;
|
|
|
|
close(SOCKET->socket);
|
2010-01-03 18:09:57 +01:00
|
|
|
SOCKET->socket = -1;
|
2010-12-28 22:58:42 +01:00
|
|
|
SOCKET->status = NET_INACTIVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (THIS->OnClose)
|
|
|
|
THIS->OnClose(_object);
|
2009-06-07 03:04:58 +02:00
|
|
|
}
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
/**************************************************
|
2010-12-28 22:58:42 +01:00
|
|
|
This function is called by DnsClient to inform
|
|
|
|
that it has finished its work
|
|
|
|
*************************************************/
|
2008-02-15 12:12:55 +01:00
|
|
|
void CSocket_CallBackFromDns(void *_object)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-10-30 01:54:18 +01:00
|
|
|
//int NoBlock=1;
|
2007-12-30 17:41:49 +01:00
|
|
|
int myval=0;
|
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
if ( SOCKET->status != NET_SEARCHING) return;
|
2008-02-15 12:12:55 +01:00
|
|
|
if ( !THIS->DnsTool->sHostIP)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-06-07 03:04:58 +02:00
|
|
|
/* error host not found */
|
2010-12-28 22:58:42 +01:00
|
|
|
CSocket_stream_internal_error(THIS, NET_HOST_NOT_FOUND, TRUE);
|
2007-12-30 17:41:49 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-02-15 12:12:55 +01:00
|
|
|
GB.FreeString (&THIS->sRemoteHostIP);
|
2010-06-05 01:48:53 +02:00
|
|
|
THIS->sRemoteHostIP = GB.NewZeroString (THIS->DnsTool->sHostIP);
|
2007-12-30 17:41:49 +01:00
|
|
|
/* Let's turn socket to async mode */
|
2010-01-03 18:09:57 +01:00
|
|
|
//ioctl(SOCKET->socket,FIONBIO,&NoBlock);
|
2007-12-30 17:41:49 +01:00
|
|
|
/* Third, we connect the socket */
|
2008-02-15 12:12:55 +01:00
|
|
|
THIS->Server.sin_family=AF_INET;
|
2010-12-28 22:58:42 +01:00
|
|
|
THIS->Server.sin_port=htons(THIS->iPort);
|
|
|
|
THIS->Server.sin_addr.s_addr =inet_addr(THIS->DnsTool->sHostIP);
|
|
|
|
bzero(&(THIS->Server.sin_zero),8);
|
2010-01-03 18:09:57 +01:00
|
|
|
myval=connect(SOCKET->socket,(struct sockaddr*)&(THIS->Server), sizeof(struct sockaddr));
|
2008-11-12 22:50:36 +01:00
|
|
|
if (!myval || errno==EINPROGRESS) /* this is the good answer : connect in progress */
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-12-28 22:58:42 +01:00
|
|
|
SOCKET->status = NET_CONNECTING;
|
2010-01-03 18:09:57 +01:00
|
|
|
GB.Watch (SOCKET->socket,GB_WATCH_WRITE,(void *)CSocket_CallBackConnecting,(intptr_t)THIS);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-01-03 18:09:57 +01:00
|
|
|
GB.Watch (SOCKET->socket , GB_WATCH_NONE , (void *)CSocket_CallBack,0);
|
|
|
|
SOCKET->stream.desc=NULL;
|
|
|
|
close(SOCKET->socket);
|
2010-12-28 22:58:42 +01:00
|
|
|
SOCKET->status = NET_INACTIVE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
2008-02-15 12:12:55 +01:00
|
|
|
if (THIS->DnsTool)
|
2010-12-28 22:58:42 +01:00
|
|
|
{
|
2008-02-15 12:12:55 +01:00
|
|
|
dns_close_all(THIS->DnsTool);
|
|
|
|
GB.Unref(POINTER(&THIS->DnsTool));
|
|
|
|
THIS->DnsTool=NULL;
|
2010-12-28 22:58:42 +01:00
|
|
|
}
|
|
|
|
if ( SOCKET->status <= NET_INACTIVE )
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-12-28 22:58:42 +01:00
|
|
|
CSocket_stream_internal_error(THIS, NET_CONNECTION_REFUSED, TRUE);
|
2007-12-30 17:41:49 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-02-15 12:12:55 +01:00
|
|
|
GB.Ref(THIS);
|
|
|
|
GB.Post(CSocket_post_hostfound,(intptr_t)THIS);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
2008-10-30 01:54:18 +01:00
|
|
|
|
2009-06-07 01:31:48 +02:00
|
|
|
|
|
|
|
void CSOCKET_init_connected(CSOCKET *_object)
|
|
|
|
{
|
2010-01-03 18:09:57 +01:00
|
|
|
GB.Watch(SOCKET->socket, GB_WATCH_READ, (void *)CSocket_CallBack, (intptr_t)THIS);
|
|
|
|
SOCKET->stream.desc = &SocketStream;
|
|
|
|
SOCKET_update_timeout(SOCKET);
|
2009-06-07 01:31:48 +02:00
|
|
|
}
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
/*******************************************************************
|
2010-12-28 22:58:42 +01:00
|
|
|
This CallBack is used while waiting to finish a connection process
|
|
|
|
******************************************************************/
|
2008-11-12 22:50:36 +01:00
|
|
|
void CSocket_CallBackConnecting(int t_sock,int type,intptr_t param)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
struct sockaddr_in myhost;
|
|
|
|
int mylen;
|
2008-11-12 22:50:36 +01:00
|
|
|
void *_object = (void *)param;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-03 18:09:57 +01:00
|
|
|
GB.Watch(SOCKET->socket, GB_WATCH_NONE, (void *)CSocket_CallBackConnecting, 0);
|
2008-11-12 22:50:36 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
if (SOCKET->status != NET_CONNECTING) return;
|
2008-11-12 22:50:36 +01:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
/****************************************************
|
|
|
|
Checks if Connection was Stablished or there was
|
|
|
|
an error trying to connect
|
|
|
|
****************************************************/
|
2008-11-12 22:50:36 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
SOCKET->status = CheckConnection(SOCKET->socket);
|
|
|
|
if (SOCKET->status == NET_INACTIVE)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-12-28 22:58:42 +01:00
|
|
|
CSocket_stream_internal_error(THIS, NET_CONNECTION_REFUSED, TRUE);
|
2007-12-30 17:41:49 +01:00
|
|
|
return;
|
|
|
|
}
|
2010-12-28 22:58:42 +01:00
|
|
|
if (SOCKET->status != NET_CONNECTED) return;
|
2007-12-30 17:41:49 +01:00
|
|
|
// we obtain local IP and host
|
2008-11-12 22:50:36 +01:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
mylen=sizeof(struct sockaddr);
|
2010-01-03 18:09:57 +01:00
|
|
|
getsockname (SOCKET->socket,(struct sockaddr*)&myhost,(socklen_t *)&mylen);
|
2008-02-15 12:12:55 +01:00
|
|
|
THIS->iLocalPort=ntohs(myhost.sin_port);
|
|
|
|
GB.FreeString( &THIS->sLocalHostIP);
|
2010-06-05 01:48:53 +02:00
|
|
|
THIS->sLocalHostIP = GB.NewZeroString(inet_ntoa(myhost.sin_addr));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-06-07 01:31:48 +02:00
|
|
|
CSOCKET_init_connected(THIS);
|
2010-01-03 18:09:57 +01:00
|
|
|
GB.Stream.SetSwapping(&SOCKET->stream, htons(1234) != 1234);
|
2008-11-12 22:50:36 +01:00
|
|
|
|
2008-02-15 12:12:55 +01:00
|
|
|
GB.Ref(THIS);
|
|
|
|
GB.Post(CSocket_post_connected,(intptr_t)THIS);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
2008-11-12 22:50:36 +01:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
/*******************************************************************
|
2010-12-28 22:58:42 +01:00
|
|
|
This CallBack is used while socket is connected to remote host
|
|
|
|
******************************************************************/
|
2009-06-07 01:31:48 +02:00
|
|
|
static void callback_write(int t_sock,int type, CSOCKET *_object)
|
|
|
|
{
|
|
|
|
//fprintf(stderr, "callback write %p\n", THIS);
|
|
|
|
THIS->watch_write = FALSE;
|
2010-01-03 18:09:57 +01:00
|
|
|
GB.Watch(SOCKET->socket, GB_WATCH_WRITE, NULL, 0);
|
2009-06-07 01:31:48 +02:00
|
|
|
GB.Raise(THIS, EVENT_Write, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSocket_CallBack(int t_sock,int type, CSOCKET *_object)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
char buf[1];
|
|
|
|
struct pollfd mypoll;
|
|
|
|
int numpoll;
|
|
|
|
struct timespec mywait;
|
|
|
|
|
2009-06-07 01:31:48 +02:00
|
|
|
//fprintf(stderr, "callback read %p\n", THIS);
|
2007-12-30 17:41:49 +01:00
|
|
|
/* Just sleeping a little to reduce CPU waste */
|
|
|
|
mywait.tv_sec=0;
|
|
|
|
mywait.tv_nsec=100000;
|
|
|
|
nanosleep(&mywait,NULL);
|
|
|
|
|
2009-06-07 01:31:48 +02:00
|
|
|
/* is there data available or an error? */
|
2010-12-28 22:58:42 +01:00
|
|
|
if (SOCKET->status != NET_CONNECTED) return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
mypoll.fd=t_sock;
|
|
|
|
mypoll.events=POLLIN | POLLNVAL;
|
|
|
|
mypoll.revents=0;
|
|
|
|
numpoll=poll(&mypoll,1,0);
|
|
|
|
if (numpoll<=0) return;
|
|
|
|
/* there's data available */
|
2008-11-12 22:50:36 +01:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
USE_MSG_NOSIGNAL(numpoll=recv(t_sock,(void*)buf,sizeof(char),MSG_PEEK | MSG_NOSIGNAL));
|
|
|
|
if (!numpoll)
|
|
|
|
{ /* socket error, no valid data received */
|
2008-11-12 22:50:36 +01:00
|
|
|
|
2009-06-07 03:04:58 +02:00
|
|
|
CSocket_close(THIS);
|
|
|
|
GB.Ref(THIS);
|
|
|
|
GB.Post(CSocket_post_closed, (intptr_t)THIS);
|
2007-12-30 17:41:49 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
/******************************************************
|
|
|
|
There's data available to read, so we'll raise event
|
2008-11-12 22:50:36 +01:00
|
|
|
EVENT_Read
|
2007-12-30 17:41:49 +01:00
|
|
|
*******************************************************/
|
|
|
|
|
2009-06-07 01:31:48 +02:00
|
|
|
GB.Raise(THIS, EVENT_Read, 0);
|
|
|
|
//GB.Ref(_object);
|
|
|
|
//GB.Post(CSocket_post_data_available,(intptr_t)THIS);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-07 03:04:58 +02:00
|
|
|
void CSocket_stream_internal_error(void *_object, int ncode, bool post)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-06-07 03:04:58 +02:00
|
|
|
CSocket_close(THIS);
|
2010-12-28 22:58:42 +01:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
/* fatal socket error handling */
|
2010-01-04 13:04:53 +01:00
|
|
|
SOCKET->status = ncode;
|
2009-06-07 03:04:58 +02:00
|
|
|
|
|
|
|
if (post)
|
|
|
|
{
|
|
|
|
GB.Ref(THIS);
|
|
|
|
GB.Post(CSocket_post_error, (intptr_t)THIS);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//################################################################################
|
|
|
|
/*********************************************************************************
|
|
|
|
"PUBLIC" C/C++ INTERFACE
|
|
|
|
**********************************************************************************/
|
|
|
|
//################################################################################
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/* not allowed methods */
|
|
|
|
int CSocket_stream_open(GB_STREAM *stream, const char *path, int mode, void *data){return -1;}
|
2008-01-17 22:39:26 +01:00
|
|
|
int CSocket_stream_seek(GB_STREAM *stream, int64_t pos, int whence){return -1;}
|
|
|
|
int CSocket_stream_tell(GB_STREAM *stream, int64_t *pos){return -1;}
|
2008-11-05 00:53:39 +01:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
int CSocket_stream_flush(GB_STREAM *stream)
|
|
|
|
{
|
|
|
|
return 0; /* OK */
|
|
|
|
}
|
2008-11-05 00:53:39 +01:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
int CSocket_stream_handle(GB_STREAM *stream)
|
|
|
|
{
|
2008-10-30 01:54:18 +01:00
|
|
|
void *_object = stream->tag;
|
2010-01-03 18:09:57 +01:00
|
|
|
return SOCKET->socket;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
2008-11-05 00:53:39 +01:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
int CSocket_stream_close(GB_STREAM *stream)
|
|
|
|
{
|
2010-12-28 22:58:42 +01:00
|
|
|
void *_object = stream->tag;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
if (!THIS) return -1;
|
2009-06-07 03:04:58 +02:00
|
|
|
CSocket_close(THIS);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
return 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
2008-10-30 01:54:18 +01:00
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
int CSocket_stream_lof(GB_STREAM *stream, int64_t *len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-12-28 22:58:42 +01:00
|
|
|
void *_object = stream->tag;
|
2007-12-30 17:41:49 +01:00
|
|
|
int bytes;
|
|
|
|
|
|
|
|
*len=0;
|
2008-02-15 12:12:55 +01:00
|
|
|
if (!THIS) return -1;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-03 18:09:57 +01:00
|
|
|
if (ioctl(SOCKET->socket,FIONREAD,&bytes))
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-12-28 22:58:42 +01:00
|
|
|
CSocket_stream_internal_error(THIS, NET_CANNOT_READ, FALSE);
|
2007-12-30 17:41:49 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*len=bytes;
|
|
|
|
return 0;
|
|
|
|
}
|
2008-10-30 01:54:18 +01:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
int CSocket_stream_eof(GB_STREAM *stream)
|
|
|
|
{
|
2010-12-28 22:58:42 +01:00
|
|
|
void *_object = stream->tag;
|
2007-12-30 17:41:49 +01:00
|
|
|
int bytes;
|
|
|
|
|
2008-02-15 12:12:55 +01:00
|
|
|
if (!THIS) return -1;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-03 18:09:57 +01:00
|
|
|
if (ioctl(SOCKET->socket,FIONREAD,&bytes))
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-12-28 22:58:42 +01:00
|
|
|
CSocket_stream_internal_error(THIS, NET_CANNOT_READ, FALSE);
|
2007-12-30 17:41:49 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!bytes) return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
int CSocket_stream_read(GB_STREAM *stream, char *buffer, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-12-28 22:58:42 +01:00
|
|
|
void *_object = stream->tag;
|
2008-10-04 14:31:49 +02:00
|
|
|
int npos=-1;
|
2008-10-30 01:54:18 +01:00
|
|
|
//int NoBlock=0;
|
2007-12-30 17:41:49 +01:00
|
|
|
int bytes;
|
|
|
|
|
2008-02-15 12:12:55 +01:00
|
|
|
if (!THIS) return -1;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-03 18:09:57 +01:00
|
|
|
if (ioctl(SOCKET->socket,FIONREAD,&bytes))
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-12-28 22:58:42 +01:00
|
|
|
CSocket_stream_internal_error(THIS, NET_CANNOT_READ, FALSE);
|
2007-12-30 17:41:49 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2008-10-30 01:54:18 +01:00
|
|
|
//if (bytes < len) return -1;
|
|
|
|
if (bytes < len)
|
|
|
|
len = bytes;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-03 18:09:57 +01:00
|
|
|
//ioctl(SOCKET->socket,FIONBIO,&NoBlock);
|
|
|
|
USE_MSG_NOSIGNAL(npos=recv(SOCKET->socket,(void*)buffer,len*sizeof(char),MSG_NOSIGNAL));
|
2008-10-30 01:54:18 +01:00
|
|
|
//NoBlock++;
|
2010-12-28 22:58:42 +01:00
|
|
|
//ioctl(SOCKET->socket,FIONBIO,&NoBlock);
|
2008-10-30 01:54:18 +01:00
|
|
|
|
|
|
|
GB.Stream.SetBytesRead(stream, npos);
|
|
|
|
|
|
|
|
if (npos==len) return 0;
|
|
|
|
|
2009-06-07 03:04:58 +02:00
|
|
|
if (npos < 0 && errno != EAGAIN)
|
2010-12-28 22:58:42 +01:00
|
|
|
CSocket_stream_internal_error(THIS, NET_CANNOT_READ, FALSE);
|
2008-10-30 01:54:18 +01:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
int CSocket_stream_write(GB_STREAM *stream, char *buffer, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-12-28 22:58:42 +01:00
|
|
|
void *_object = stream->tag;
|
2007-12-30 17:41:49 +01:00
|
|
|
int npos=-1;
|
2008-10-30 01:54:18 +01:00
|
|
|
//int NoBlock=0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-02-15 12:12:55 +01:00
|
|
|
if (!THIS) return -1;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-03 18:09:57 +01:00
|
|
|
//ioctl(SOCKET->socket,FIONBIO,&NoBlock);
|
2010-01-02 20:44:25 +01:00
|
|
|
while (len > 0)
|
|
|
|
{
|
2010-01-03 18:09:57 +01:00
|
|
|
USE_MSG_NOSIGNAL(npos=send(SOCKET->socket,(void*)buffer,len*sizeof(char),MSG_NOSIGNAL));
|
2010-01-02 20:44:25 +01:00
|
|
|
if (npos <= 0)
|
|
|
|
break;
|
|
|
|
len -= npos;
|
|
|
|
buffer += npos;
|
|
|
|
}
|
2008-10-30 01:54:18 +01:00
|
|
|
//NoBlock++;
|
2010-01-03 18:09:57 +01:00
|
|
|
//ioctl(SOCKET->socket,FIONBIO,&NoBlock);
|
2009-06-07 01:31:48 +02:00
|
|
|
|
2009-06-07 03:04:58 +02:00
|
|
|
if (npos >= 0 || errno == EAGAIN)
|
2009-06-07 01:31:48 +02:00
|
|
|
{
|
2009-06-07 02:01:59 +02:00
|
|
|
if (GB.CanRaise(THIS, EVENT_Write) && !THIS->watch_write)
|
|
|
|
{
|
|
|
|
//fprintf(stderr, "watch write %p\n", THIS);
|
|
|
|
THIS->watch_write = TRUE;
|
2010-01-03 18:09:57 +01:00
|
|
|
GB.Watch(SOCKET->socket, GB_WATCH_WRITE, (void *)callback_write, (intptr_t)THIS);
|
2009-06-07 02:01:59 +02:00
|
|
|
}
|
2009-06-07 03:04:58 +02:00
|
|
|
}
|
2009-06-07 02:01:59 +02:00
|
|
|
|
2009-06-07 03:04:58 +02:00
|
|
|
if (npos >= 0)
|
2009-06-07 02:01:59 +02:00
|
|
|
return 0;
|
2009-06-07 01:31:48 +02:00
|
|
|
|
2009-06-07 03:04:58 +02:00
|
|
|
if (errno != EAGAIN)
|
2010-12-28 22:58:42 +01:00
|
|
|
CSocket_stream_internal_error(THIS, NET_CANNOT_WRITE, FALSE);
|
2009-06-07 03:04:58 +02:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**************************************************************************
|
2010-12-28 22:58:42 +01:00
|
|
|
To start a UNIX connection
|
|
|
|
**************************************************************************/
|
2010-01-02 20:44:25 +01:00
|
|
|
int CSocket_connect_unix(void *_object,char *sPath, int lenpath)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-01-02 20:44:25 +01:00
|
|
|
int doNotBlock = 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
if ( SOCKET->status > NET_INACTIVE ) return 1;
|
2007-12-30 17:41:49 +01:00
|
|
|
if (!sPath) return 7;
|
|
|
|
if ( (lenpath<1) || (lenpath>UNIXPATHMAX) ) return 7;
|
|
|
|
|
2008-02-15 12:12:55 +01:00
|
|
|
GB.FreeString(&THIS->sRemoteHostIP);
|
2008-10-30 01:54:18 +01:00
|
|
|
GB.FreeString(&THIS->sLocalHostIP);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-02-15 12:12:55 +01:00
|
|
|
THIS->UServer.sun_family=AF_UNIX;
|
|
|
|
strcpy(THIS->UServer.sun_path,sPath);
|
2010-01-03 18:09:57 +01:00
|
|
|
if ( (SOCKET->socket=socket(AF_UNIX,SOCK_STREAM,0))==-1 )
|
2008-10-30 01:54:18 +01:00
|
|
|
{
|
2010-12-28 22:58:42 +01:00
|
|
|
SOCKET->status = NET_CANNOT_CREATE_SOCKET;
|
2008-02-15 12:12:55 +01:00
|
|
|
GB.Ref (THIS);
|
2008-10-30 01:54:18 +01:00
|
|
|
CSocket_post_error(_object); /* Unable to create socket */
|
2007-12-30 17:41:49 +01:00
|
|
|
return 2;
|
2008-10-30 01:54:18 +01:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
GB.FreeString(&THIS->sPath);
|
2010-06-05 01:48:53 +02:00
|
|
|
THIS->sPath = GB.NewZeroString(THIS->UServer.sun_path);
|
2010-12-28 22:58:42 +01:00
|
|
|
|
|
|
|
THIS->conn_type = NET_TYPE_INTERNET;
|
|
|
|
|
|
|
|
if (connect(SOCKET->socket,(struct sockaddr*)&THIS->UServer,sizeof(struct sockaddr_un))==0)
|
|
|
|
{
|
|
|
|
SOCKET->status = NET_CONNECTED;
|
2010-01-03 18:09:57 +01:00
|
|
|
//ioctl(SOCKET->socket,FIONBIO,&NoBlock);
|
2009-06-07 01:31:48 +02:00
|
|
|
CSOCKET_init_connected(THIS);
|
2010-01-03 18:09:57 +01:00
|
|
|
/*GB.Watch (SOCKET->socket,GB_WATCH_READ,(void *)CSocket_CallBack,(intptr_t)THIS);
|
2009-06-07 01:31:48 +02:00
|
|
|
if (GB.CanRaise(THIS, EVENT_Write))
|
2010-01-03 18:09:57 +01:00
|
|
|
GB.Watch(SOCKET->socket, GB_WATCH_WRITE, (void *)CSocket_CallBackWrite, (intptr_t)THIS);*/
|
2008-10-30 01:54:18 +01:00
|
|
|
// $BM
|
|
|
|
if (THIS->Host) GB.FreeString(&THIS->Host);
|
|
|
|
if (THIS->Path) GB.FreeString(&THIS->Path);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
THIS->Path = GB.NewZeroString(sPath);
|
2008-02-15 12:12:55 +01:00
|
|
|
GB.Ref (THIS);
|
|
|
|
CSocket_post_connected(_object);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
return 0;
|
2010-12-28 22:58:42 +01:00
|
|
|
}
|
2008-10-30 01:54:18 +01:00
|
|
|
|
2010-01-02 20:44:25 +01:00
|
|
|
// Set socket to blocking mode, after the connect() call!
|
2010-01-03 18:09:57 +01:00
|
|
|
ioctl(SOCKET->socket, FIONBIO, &doNotBlock);
|
2010-12-28 22:58:42 +01:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
/* Error */
|
2010-01-03 18:09:57 +01:00
|
|
|
SOCKET->stream.desc=NULL;
|
|
|
|
close(SOCKET->socket);
|
2008-02-15 12:12:55 +01:00
|
|
|
GB.FreeString(&THIS->sPath);
|
2010-12-28 22:58:42 +01:00
|
|
|
SOCKET->status = NET_CONNECTION_REFUSED;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-02-15 12:12:55 +01:00
|
|
|
GB.Ref (THIS);
|
|
|
|
CSocket_post_error(_object); /* Unable to connect to remote host */
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
return 3;
|
|
|
|
}
|
2008-10-30 01:54:18 +01:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
/**************************************************************************
|
2010-12-28 22:58:42 +01:00
|
|
|
To start a TCP connection
|
|
|
|
**************************************************************************/
|
2008-02-15 12:12:55 +01:00
|
|
|
int CSocket_connect_socket(void *_object,char *sHost,int lenhost,int myport)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-01-02 20:44:25 +01:00
|
|
|
int doNotBlock = 0;
|
2008-10-30 01:54:18 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
if ( SOCKET->status > NET_INACTIVE ) return 1;
|
2007-12-30 17:41:49 +01:00
|
|
|
if (!lenhost) return 9;
|
|
|
|
if (!sHost) return 9;
|
|
|
|
if ( (myport<1) || (myport>65535) ) return 8;
|
|
|
|
|
2008-10-30 01:54:18 +01:00
|
|
|
GB.FreeString(&THIS->sRemoteHostIP);
|
|
|
|
GB.FreeString(&THIS->sLocalHostIP);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-03 18:09:57 +01:00
|
|
|
if ( (SOCKET->socket=socket(AF_INET,SOCK_STREAM,0))==-1 )
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-12-28 22:58:42 +01:00
|
|
|
SOCKET->status = NET_CANNOT_CREATE_SOCKET;
|
2008-02-15 12:12:55 +01:00
|
|
|
GB.Ref (THIS);
|
|
|
|
CSocket_post_error(_object);
|
2007-12-30 17:41:49 +01:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2008-10-30 01:54:18 +01:00
|
|
|
// Set socket to non-blocking mode
|
2010-01-03 18:09:57 +01:00
|
|
|
ioctl(SOCKET->socket, FIONBIO, &doNotBlock);
|
2008-10-30 01:54:18 +01:00
|
|
|
|
2008-02-15 12:12:55 +01:00
|
|
|
THIS->iPort=myport;
|
2010-12-28 22:58:42 +01:00
|
|
|
THIS->conn_type = NET_TYPE_INTERNET;
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
/******************************************
|
|
|
|
Let's turn hostname into host IP
|
|
|
|
*******************************************/
|
2008-02-15 12:12:55 +01:00
|
|
|
if (!THIS->DnsTool)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-02-15 12:12:55 +01:00
|
|
|
GB.New(POINTER(&THIS->DnsTool),GB.FindClass("DnsClient"),NULL,NULL);
|
|
|
|
THIS->DnsTool->CliParent=_object;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2008-10-30 01:54:18 +01:00
|
|
|
if (THIS->DnsTool->iStatus > 0 ) dns_close_all(THIS->DnsTool);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-30 01:54:18 +01:00
|
|
|
dns_set_async_mode(1,THIS->DnsTool);
|
|
|
|
GB.FreeString (&(THIS->DnsTool->sHostName));
|
2010-06-05 01:48:53 +02:00
|
|
|
THIS->DnsTool->sHostName = GB.NewString(sHost,lenhost);
|
2008-10-30 01:54:18 +01:00
|
|
|
THIS->DnsTool->finished_callback=CSocket_CallBackFromDns;
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
/********************************************
|
|
|
|
We start DNS lookup, when it is finished
|
|
|
|
it will call to CSocket_CallBack_fromDns,
|
|
|
|
and we'll continue there connection proccess
|
|
|
|
********************************************/
|
2010-12-28 22:58:42 +01:00
|
|
|
SOCKET->status = NET_SEARCHING; /* looking for IP */
|
2008-02-15 12:12:55 +01:00
|
|
|
dns_thread_getip(THIS->DnsTool);
|
2010-01-03 18:09:57 +01:00
|
|
|
SOCKET->stream.desc=&SocketStream;
|
2008-10-30 01:54:18 +01:00
|
|
|
THIS->iUsePort=THIS->iPort;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-30 01:54:18 +01:00
|
|
|
// $BM
|
|
|
|
if (THIS->Path) GB.FreeString(&THIS->Path);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-07-03 22:15:26 +02:00
|
|
|
if (sHost != THIS->Host)
|
|
|
|
{
|
|
|
|
if (THIS->Host) GB.FreeString(&THIS->Host);
|
2010-06-05 01:48:53 +02:00
|
|
|
THIS->Host = GB.NewZeroString(sHost);
|
2009-07-03 22:15:26 +02:00
|
|
|
}
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************
|
|
|
|
This function is used to peek data from socket,
|
|
|
|
you have to pass 3 parameters:
|
|
|
|
|
2008-02-15 12:12:55 +01:00
|
|
|
_object-> CSocket object
|
2007-12-30 17:41:49 +01:00
|
|
|
buf -> Data Buffer (you have to free it after using it!)
|
|
|
|
MaxLen -> 0 no limit, >0 max. data t read
|
|
|
|
***********************************************/
|
2008-02-15 12:12:55 +01:00
|
|
|
int CSocket_peek_data(void *_object,char **buf,int MaxLen)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-12-28 22:58:42 +01:00
|
|
|
int retval=0;
|
|
|
|
//int NoBlock=0;
|
|
|
|
int nread=0;
|
|
|
|
int bytes=0;
|
|
|
|
|
|
|
|
(*buf)=NULL;
|
|
|
|
nread=ioctl(SOCKET->socket,FIONREAD,&bytes); /* Is there anythig to read? */
|
|
|
|
if (nread)
|
|
|
|
retval=-1;
|
|
|
|
else
|
|
|
|
retval=0;
|
|
|
|
if ( (!retval) && (bytes) ) /* if there's anything to receive */
|
|
|
|
{
|
2008-10-30 01:54:18 +01:00
|
|
|
if (bytes > MAX_CLIENT_BUFFER_SIZE) bytes=MAX_CLIENT_BUFFER_SIZE;
|
|
|
|
if (MaxLen >0 ) bytes=MaxLen;
|
|
|
|
GB.Alloc((void**)buf,bytes);
|
|
|
|
(*buf)[0]='\0';
|
2010-01-03 18:09:57 +01:00
|
|
|
//ioctl(SOCKET->socket,FIONBIO,&NoBlock);
|
|
|
|
USE_MSG_NOSIGNAL(retval=recv(SOCKET->socket,(void*)(*buf),bytes*sizeof(char),MSG_PEEK|MSG_NOSIGNAL));
|
2008-10-30 01:54:18 +01:00
|
|
|
//NoBlock++;
|
2010-01-03 18:09:57 +01:00
|
|
|
//ioctl(SOCKET->socket,FIONBIO,&NoBlock);
|
2010-12-28 22:58:42 +01:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
if (retval==-1)
|
|
|
|
{
|
|
|
|
/* An error happened while trying to receive data : SOCKET ERROR */
|
2008-10-30 01:54:18 +01:00
|
|
|
if (*buf)
|
|
|
|
{
|
|
|
|
GB.Free(POINTER(buf));
|
|
|
|
buf=NULL;
|
|
|
|
}
|
2010-01-03 18:09:57 +01:00
|
|
|
GB.Watch (SOCKET->socket , GB_WATCH_NONE , (void *)CSocket_CallBack,0);
|
|
|
|
SOCKET->stream.desc=NULL;
|
|
|
|
close(SOCKET->socket);
|
2010-12-28 22:58:42 +01:00
|
|
|
SOCKET->status = NET_CANNOT_READ;
|
2008-10-30 01:54:18 +01:00
|
|
|
GB.Ref (THIS);
|
|
|
|
CSocket_post_error(_object);
|
|
|
|
return -1;
|
2010-12-28 22:58:42 +01:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
return retval;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
2008-10-30 01:54:18 +01:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
/*****************************************************************************************
|
|
|
|
##########################################################################################
|
|
|
|
-------------------------SOCKET GAMBAS INTERFACE IMPLEMENTATION---------------------
|
|
|
|
##########################################################################################
|
|
|
|
******************************************************************************************/
|
|
|
|
/********************************************************************
|
|
|
|
Returns current Status of the socket (connected,connecting,closed)
|
|
|
|
*********************************************************************/
|
2010-12-28 22:58:42 +01:00
|
|
|
BEGIN_PROPERTY(Socket_Status)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
GB.ReturnInteger(SOCKET->status);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
/********************************************************************
|
2010-12-28 22:58:42 +01:00
|
|
|
Port to connect to. Can be 'Net.Local' for Unix sockets, or
|
|
|
|
1-65535 for TCP sockets
|
|
|
|
********************************************************************/
|
|
|
|
BEGIN_PROPERTY(Socket_Port)
|
|
|
|
|
|
|
|
int port;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (READ_PROPERTY)
|
|
|
|
{
|
|
|
|
GB.ReturnInteger(THIS->iUsePort);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
if (SOCKET->status > NET_INACTIVE)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-12-28 22:58:42 +01:00
|
|
|
GB.Error("Port property cannot be changed while the socket is active");
|
2007-12-30 17:41:49 +01:00
|
|
|
return;
|
|
|
|
}
|
2010-12-28 22:58:42 +01:00
|
|
|
|
|
|
|
port = VPROP(GB_INTEGER);
|
|
|
|
|
|
|
|
if (port < 0 || port > 65535)
|
|
|
|
{
|
|
|
|
GB.Error("Invalid port number");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
THIS->iUsePort = port;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
2010-12-28 22:58:42 +01:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
/*********************************************************************
|
|
|
|
Host or Path to connect to. If 'Port' value is zero, this
|
|
|
|
will be a local path, else, a remote host name
|
|
|
|
**********************************************************************/
|
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
BEGIN_PROPERTY (Socket_Host)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (READ_PROPERTY)
|
2010-05-23 13:07:14 +02:00
|
|
|
GB.ReturnNewZeroString(THIS->Host);
|
2010-12-28 22:58:42 +01:00
|
|
|
else
|
|
|
|
GB.StoreString(PROP(GB_STRING), &THIS->Host);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
BEGIN_PROPERTY (Socket_Path)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (READ_PROPERTY)
|
2010-05-23 13:07:14 +02:00
|
|
|
GB.ReturnNewZeroString(THIS->Path);
|
2010-12-28 22:58:42 +01:00
|
|
|
else
|
|
|
|
GB.StoreString(PROP(GB_STRING), &THIS->Path);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
/********************************************************************
|
2010-12-28 22:58:42 +01:00
|
|
|
Returns current TCP remote port (only when connected via TCP)
|
|
|
|
*********************************************************************/
|
|
|
|
BEGIN_PROPERTY(Socket_RemotePort)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
if (SOCKET->status != NET_CONNECTED || THIS->conn_type != NET_TYPE_INTERNET)
|
|
|
|
GB.ReturnInteger(0);
|
|
|
|
else
|
|
|
|
GB.ReturnInteger(THIS->iPort);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
/********************************************************************
|
|
|
|
Returns current TCP local port (only when connected via TCP)
|
|
|
|
*********************************************************************/
|
|
|
|
BEGIN_PROPERTY(Socket_LocalPort)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
if (SOCKET->status != NET_CONNECTED || THIS->conn_type != NET_TYPE_INTERNET)
|
|
|
|
GB.ReturnInteger(0);
|
|
|
|
else
|
|
|
|
GB.ReturnInteger(THIS->iLocalPort);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
/***********************************************************************
|
2010-12-28 22:58:42 +01:00
|
|
|
Returns current foreing host IP (only when connected via TCP)
|
|
|
|
***********************************************************************/
|
|
|
|
BEGIN_PROPERTY(Socket_RemoteHost)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
if (SOCKET->status != NET_CONNECTED || THIS->conn_type != NET_TYPE_INTERNET)
|
|
|
|
GB.ReturnNull();
|
|
|
|
else
|
|
|
|
GB.ReturnString(THIS->sRemoteHostIP);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
/***********************************************************************
|
2010-12-28 22:58:42 +01:00
|
|
|
Returns current local host IP (only when connected via TCP)
|
|
|
|
***********************************************************************/
|
|
|
|
BEGIN_PROPERTY(Socket_LocalHost)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
if (SOCKET->status != NET_CONNECTED || THIS->conn_type != NET_TYPE_INTERNET)
|
|
|
|
GB.ReturnNull();
|
|
|
|
else
|
|
|
|
GB.ReturnString(THIS->sLocalHostIP);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
/****************************************************
|
2010-12-28 22:58:42 +01:00
|
|
|
Gambas object "Constructor"
|
|
|
|
****************************************************/
|
|
|
|
BEGIN_METHOD_VOID(Socket_new)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
SOCKET->stream.tag = THIS;
|
|
|
|
THIS->iUsePort = 80;
|
2010-01-03 18:09:57 +01:00
|
|
|
SOCKET->socket = -1;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
/**************************************************
|
2010-12-28 22:58:42 +01:00
|
|
|
Gambas object "Destructor"
|
|
|
|
**************************************************/
|
|
|
|
BEGIN_METHOD_VOID(Socket_free)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-06-07 03:04:58 +02:00
|
|
|
CSocket_close(THIS);
|
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
GB.FreeString(&THIS->sPath);
|
|
|
|
GB.FreeString(&THIS->sLocalHostIP);
|
|
|
|
GB.FreeString(&THIS->sRemoteHostIP);
|
|
|
|
GB.FreeString(&THIS->Host);
|
|
|
|
GB.FreeString(&THIS->Path);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
/*************************************************************
|
|
|
|
To Peek data arrived from the other side of the socket
|
|
|
|
**************************************************************/
|
|
|
|
BEGIN_METHOD_VOID(Socket_Peek)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
char *buf=NULL;
|
|
|
|
int retval=0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
if (SOCKET->status != NET_CONNECTED) /* if socket is not connected we can't receive anything */
|
|
|
|
{
|
|
|
|
GB.Error("Socket is not connected");
|
|
|
|
return;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
retval = CSocket_peek_data(_object, &buf, 0);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
if (retval == -1)
|
|
|
|
{
|
|
|
|
/* An error happened while trying to receive data : SOCKET ERROR */
|
|
|
|
if (buf) GB.Free(POINTER(&buf));
|
|
|
|
GB.ReturnNull();
|
|
|
|
return;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
if (retval > 0)
|
|
|
|
GB.ReturnNewString(buf, retval);
|
|
|
|
else
|
|
|
|
GB.ReturnNull();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (buf) GB.Free(POINTER(&buf));
|
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
END_METHOD
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
/**************************************************************************
|
|
|
|
To start a TCP or UNIX connection
|
|
|
|
**************************************************************************/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
BEGIN_METHOD(Socket_Connect, GB_STRING HostOrPath; GB_INTEGER Port)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
int port;
|
|
|
|
int err;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
port = VARGOPT(Port, THIS->iUsePort);
|
|
|
|
|
|
|
|
if (!port)
|
|
|
|
{
|
|
|
|
if (MISSING(HostOrPath))
|
|
|
|
err = CSocket_connect_unix(_object,THIS->Path,GB.StringLength(THIS->Path));
|
|
|
|
else
|
|
|
|
err = CSocket_connect_unix(_object,STRING(HostOrPath),LENGTH(HostOrPath));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (MISSING(HostOrPath))
|
|
|
|
err = CSocket_connect_socket(_object,THIS->Host,GB.StringLength(THIS->Host),port);
|
|
|
|
else
|
|
|
|
err = CSocket_connect_socket(_object,STRING(HostOrPath),LENGTH(HostOrPath),port);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (err)
|
|
|
|
{
|
|
|
|
case 1: GB.Error("Socket is already connected"); return;
|
|
|
|
case 2: GB.Error("Invalid path length"); return;
|
|
|
|
case 8: GB.Error("Port value out of range"); return;
|
|
|
|
case 9: GB.Error("Invalid host name"); return;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
BEGIN_PROPERTY(Socket_Timeout)
|
2010-01-03 14:08:00 +01:00
|
|
|
|
|
|
|
if (READ_PROPERTY)
|
2010-01-03 18:09:57 +01:00
|
|
|
GB.ReturnInteger(SOCKET->timeout);
|
2010-01-03 14:08:00 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
int val = VPROP(GB_INTEGER);
|
|
|
|
if (val < 0)
|
|
|
|
val = 0;
|
2010-01-03 18:09:57 +01:00
|
|
|
SOCKET->timeout = val;
|
|
|
|
SOCKET_update_timeout(SOCKET);
|
2010-01-03 14:08:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
END_PROPERTY
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-28 16:37:34 +01:00
|
|
|
BEGIN_PROPERTY(Socket_Server)
|
|
|
|
|
|
|
|
GB.ReturnObject(THIS->parent);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
/**********************************************************
|
2010-12-28 22:58:42 +01:00
|
|
|
Here we declare public structure of Socket Class
|
|
|
|
***********************************************************/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
GB_DESC CSocketDesc[] =
|
|
|
|
{
|
2010-12-28 22:58:42 +01:00
|
|
|
GB_DECLARE("Socket", sizeof(CSOCKET)), GB_INHERITS("Stream"),
|
|
|
|
|
|
|
|
GB_EVENT("Error", NULL, NULL, &EVENT_Error),
|
|
|
|
GB_EVENT("Ready", NULL, NULL, &EVENT_Ready),
|
|
|
|
GB_EVENT("Closed", NULL, NULL, &EVENT_Close),
|
|
|
|
GB_EVENT("Found", NULL, NULL, &EVENT_Found),
|
|
|
|
GB_EVENT("Read", NULL, NULL, &EVENT_Read),
|
|
|
|
GB_EVENT("Write", NULL, NULL, &EVENT_Write),
|
|
|
|
|
|
|
|
GB_METHOD("_new", NULL, Socket_new, NULL),
|
|
|
|
GB_METHOD("_free", NULL, Socket_free, NULL),
|
|
|
|
GB_METHOD("Peek", "s", Socket_Peek, NULL),
|
|
|
|
GB_METHOD("Connect",NULL, Socket_Connect,"[(HostOrPath)s(Port)i]"),
|
|
|
|
|
|
|
|
GB_PROPERTY_READ("Status", "i", Socket_Status),
|
|
|
|
GB_PROPERTY_READ("RemotePort", "i", Socket_RemotePort),
|
|
|
|
GB_PROPERTY_READ("LocalPort", "i", Socket_LocalPort),
|
|
|
|
GB_PROPERTY_READ("RemoteHost", "s", Socket_RemoteHost),
|
|
|
|
GB_PROPERTY_READ("LocalHost", "s", Socket_LocalHost),
|
2010-01-03 14:08:00 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
GB_PROPERTY("Timeout", "i", Socket_Timeout),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
GB_PROPERTY("Host", "s", Socket_Host),
|
|
|
|
GB_PROPERTY("Path", "s", Socket_Path),
|
|
|
|
GB_PROPERTY("Port", "i", Socket_Port),
|
|
|
|
GB_PROPERTY_READ("Server", "ServerSocket", Socket_Server),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
GB_CONSTANT("_IsControl", "b", TRUE),
|
|
|
|
GB_CONSTANT("_IsVirtual", "b", TRUE),
|
|
|
|
GB_CONSTANT("_Group", "s", "Network"),
|
|
|
|
GB_CONSTANT("_Properties", "s", "Host,Path,Port=80,Timeout{Range:0;3600000;10;ms}"),
|
|
|
|
GB_CONSTANT("_DefaultEvent", "s", "Read"),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
GB_END_DECLARE
|
2007-12-30 17:41:49 +01:00
|
|
|
};
|