| version 1.2, 2001/10/05 18:14:54 |
version 1.3, 2002/09/20 16:22:25 |
|
|
| |
|
| #ifdef __cplusplus |
#ifdef __cplusplus |
| |
|
| telnet_cnx::telnet_cnx(const char*newhost,const int newport){ |
telnet_cnx::telnet_cnx(const char*host,const int port) |
| |
{ |
| handle=-1; |
handle=-1; |
| __rbuf=NULL; |
__rbuf=NULL; |
| SetHost(newhost,newport); |
setHost(host,port); |
| #if DEBUG |
#if DEBUG |
| fprintf(stderr,"telnet_cnx::telnet_cnx() STATUS: Object created.\n"); |
fprintf(stderr,"telnet_cnx::telnet_cnx() STATUS: Object created.\n"); |
| fprintf(stderr," host: %s, port: %d, handle: %d, __rbuf: %p\n",host,port,handle,__rbuf); |
fprintf(stderr," host: %s, port: %d, handle: %d, __rbuf: %p\n",host,port,handle,__rbuf); |
| #endif |
#endif |
| } |
} |
| |
|
| telnet_cnx::~telnet_cnx(void){ |
telnet_cnx::~telnet_cnx(void) |
| Disconnect(); |
{ |
| |
disconnect(); |
| #if DEBUG |
#if DEBUG |
| fprintf(stderr,"telnet_cnx::~telnet_cnx() STATUS: Object destroyed.\n"); |
fprintf(stderr,"telnet_cnx::~telnet_cnx() STATUS: Object destroyed.\n"); |
| #endif |
#endif |
| } |
} |
| |
|
| inline int telnet_cnx::IsConnected(void){ |
inline int telnet_cnx::isConnected(void) |
| |
{ |
| return(handle<0?0:1); |
return(handle<0?0:1); |
| } |
} |
| |
|
| inline void telnet_cnx::SetPort(const int newport){ |
inline void telnet_cnx::setPort(const int newport) |
| |
{ |
| port=newport; |
port=newport; |
| } |
} |
| |
|
| void telnet_cnx::SetHost(const char*newhost,const int newport){ |
void telnet_cnx::setHost(const char*newhost,const int newport) |
| |
{ |
| if(newhost!=NULL){ |
if(newhost!=NULL){ |
| strncpy(host,newhost,MAX_HOST_SIZE); |
strncpy(host,newhost,TELNET_MAX_HOST_SIZE); |
| host[MAX_HOST_SIZE]='\0'; |
host[TELNET_MAX_HOST_SIZE]='\0'; |
| } |
} |
| if(newport)SetPort(newport); |
if(newport)setPort(newport); |
| } |
} |
| |
|
| int telnet_cnx::Connect(void){ |
int telnet_cnx::connect(void) |
| |
{ |
| |
|
| #ifdef __MSDOS__ |
#ifdef __MSDOS__ |
| handle=open("TEST.$$$",O_CREAT|O_TEXT); |
handle=open("TEST.$$$",O_CREAT|O_TEXT); |
| Line 93 int telnet_cnx::Connect(void){ |
|
| Line 99 int telnet_cnx::Connect(void){ |
|
| #if DEBUG |
#if DEBUG |
| fprintf(stderr," STATUS: Trying to connect...\n"); |
fprintf(stderr," STATUS: Trying to connect...\n"); |
| #endif |
#endif |
| if(connect(handle,(struct sockaddr*)&sin,sizeof(struct sockaddr_in))==-1){ |
/* We want connect() syscall, not telnet_cnx method */ |
| |
if(::connect(handle,(struct sockaddr*)&sin,sizeof(struct sockaddr_in))==-1){ |
| #if DEBUG |
#if DEBUG |
| fprintf(stderr," ERROR: Cannot connect.\n"); |
fprintf(stderr," ERROR: Cannot connect.\n"); |
| #endif |
#endif |
| Line 104 int telnet_cnx::Connect(void){ |
|
| Line 111 int telnet_cnx::Connect(void){ |
|
| #endif |
#endif |
| #endif |
#endif |
| |
|
| return(IsConnected()); |
return(isConnected()); |
| } |
} |
| |
|
| void telnet_cnx::Disconnect(void){ |
void telnet_cnx::disconnect(void){ |
| if(handle<0){ |
if(handle<0){ |
| #if DEBUG |
#if DEBUG |
| fprintf(stderr,"telnet_cnx::Disconnect() WARNING: Handle not opened, already closed. [%d]\n",handle); |
fprintf(stderr,"telnet_cnx::disconnect() WARNING: Handle not opened, already closed. [%d]\n",handle); |
| #endif |
#endif |
| } |
} |
| else{ |
else{ |
| if(close(handle)==-1){ |
if(close(handle)==-1){ |
| #if DEBUG |
#if DEBUG |
| fprintf(stderr,"telnet_cnx::Disconnect() WARNING: Cannot close handle. [%d]\n",handle); |
fprintf(stderr,"telnet_cnx::disconnect() WARNING: Cannot close handle. [%d]\n",handle); |
| #endif |
#endif |
| } |
} |
| else{ |
else{ |
| #if DEBUG |
#if DEBUG |
| fprintf(stderr,"telnet_cnx::Disconnect() OK: Handle closed. [%d]\n",handle); |
fprintf(stderr,"telnet_cnx::disconnect() OK: Handle closed. [%d]\n",handle); |
| #endif |
#endif |
| } |
} |
| handle=-1; |
handle=-1; |
| Line 133 void telnet_cnx::Disconnect(void){ |
|
| Line 140 void telnet_cnx::Disconnect(void){ |
|
| } |
} |
| } |
} |
| |
|
| inline int telnet_cnx::WriteLine(void){ |
inline int telnet_cnx::writeLine(void){ |
| return(write(handle,"\n",1)); |
return(write(handle,"\n",1)); |
| } |
} |
| |
|
| inline int telnet_cnx::WriteLine(const char*s){ |
inline int telnet_cnx::writeLine(const char*s){ |
| register int ret; |
register int ret; |
| ret=write(handle,s,strlen(s)); |
ret=write(handle,s,strlen(s)); |
| ret+=WriteLine(); |
ret+=writeLine(); |
| return(ret<0?-1:ret); |
return(ret<0?-1:ret); |
| } |
} |
| |
|
| inline int telnet_cnx::WriteStr(const char*s){ |
inline int telnet_cnx::writeStr(const char*s){ |
| return(write(handle,s,strlen(s))); |
return(write(handle,s,strlen(s))); |
| } |
} |
| |
|
| inline int telnet_cnx::WriteData(const void*p,int len){ |
inline int telnet_cnx::writeData(const void*p,int len){ |
| return(write(handle,p,len)); |
return(write(handle,p,len)); |
| } |
} |
| |
|
| int telnet_cnx::ReadData(char*buffer,const int size){ |
int telnet_cnx::readData(char*buffer,const int size){ |
| #ifdef __MSDOS__ |
#ifdef __MSDOS__ |
| buffer[0]='\0'; |
buffer[0]='\0'; |
| buffer[size]='\0'; |
buffer[size]='\0'; |
| Line 160 int telnet_cnx::ReadData(char*buffer,con |
|
| Line 167 int telnet_cnx::ReadData(char*buffer,con |
|
| struct timeval tv; |
struct timeval tv; |
| fd_set rd,act; |
fd_set rd,act; |
| |
|
| if(IsConnected()){ |
if(isConnected()){ |
| FD_ZERO(&rd); |
FD_ZERO(&rd); |
| FD_ZERO(&act); |
FD_ZERO(&act); |
| FD_SET(handle,&rd); |
FD_SET(handle,&rd); |
| Line 174 int telnet_cnx::ReadData(char*buffer,con |
|
| Line 181 int telnet_cnx::ReadData(char*buffer,con |
|
| int i; |
int i; |
| i=read(handle,buffer,size-1); |
i=read(handle,buffer,size-1); |
| if(i<=0){ |
if(i<=0){ |
| Disconnect(); |
disconnect(); |
| buffer[0]='\0'; |
buffer[0]='\0'; |
| } |
} |
| else buffer[i]='\0'; |
else buffer[i]='\0'; |
| } |
} |
| /* |
/* |
| if(FD_ISSET(handle,&act)){ |
if(FD_ISSET(handle,&act)){ |
| Disconnect(); |
disconnect(); |
| buffer[0]='\0'; |
buffer[0]='\0'; |
| } |
} |
| */ |
*/ |
| Line 192 int telnet_cnx::ReadData(char*buffer,con |
|
| Line 199 int telnet_cnx::ReadData(char*buffer,con |
|
| return(strlen(buffer)); |
return(strlen(buffer)); |
| } |
} |
| |
|
| int telnet_cnx::ReadLine(char*buffer,const int size){ |
int telnet_cnx::readLine(char*buffer,const int size){ |
| register int ret; |
register int ret; |
| |
|
| if(__rbuf!=NULL){ |
if(__rbuf!=NULL){ |
| Line 202 int telnet_cnx::ReadLine(char*buffer,con |
|
| Line 209 int telnet_cnx::ReadLine(char*buffer,con |
|
| else buffer[0]='\0'; |
else buffer[0]='\0'; |
| |
|
| if((int)strlen(buffer)<(size-1)){ |
if((int)strlen(buffer)<(size-1)){ |
| ret=ReadData(strchr(buffer,'\0'),size-strlen(buffer)-1); |
ret=readData(strchr(buffer,'\0'),size-strlen(buffer)-1); |
| } |
} |
| else ret=1; |
else ret=1; |
| |
|