| version 1.3, 2002/09/20 16:22:25 |
version 1.4, 2002/09/29 03:55:01 |
|
|
| #include<stdio.h> |
#include <stdio.h> |
| #include<stdlib.h> |
#include <stdlib.h> |
| #include<string.h> |
#include <string.h> |
| #include<sys/types.h> |
#include <sys/types.h> |
| |
|
| #ifdef __MSDOS__ |
#ifdef __MSDOS__ |
| #include<io.h> |
# include <io.h> |
| #include<fcntl.h> |
# include <fcntl.h> |
| #include<dos.h> |
# include <dos.h> |
| #define sleep(__x) delay(__x*100) |
# define sleep(__x) delay(__x*100) |
| #else |
#else |
| #include<sys/time.h> |
# include <sys/time.h> |
| #include<unistd.h> |
# include <unistd.h> |
| #include<sys/socket.h> |
# include <sys/socket.h> |
| #include<netinet/in.h> |
# include <netinet/in.h> |
| #include<netdb.h> |
# include <netdb.h> |
| #include<arpa/inet.h> |
# include <arpa/inet.h> |
| #endif |
#endif |
| |
|
| #include "telnet.h" |
#include "Connection.h" |
| |
|
| #ifdef __cplusplus |
#ifdef __cplusplus |
| |
|
| telnet_cnx::telnet_cnx(const char*host,const int port) |
netConnection::netConnection(const char*host,const int port) |
| { |
{ |
| handle=-1; |
handle=-1; |
| __rbuf=NULL; |
__rbuf=NULL; |
| setHost(host,port); |
setHost(host,port); |
| #if DEBUG |
#if DEBUG |
| fprintf(stderr,"telnet_cnx::telnet_cnx() STATUS: Object created.\n"); |
fprintf(stderr,"netConnection::netConnection() 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) |
netConnection::~netConnection(void) |
| { |
{ |
| disconnect(); |
disconnect(); |
| #if DEBUG |
#if DEBUG |
| fprintf(stderr,"telnet_cnx::~telnet_cnx() STATUS: Object destroyed.\n"); |
fprintf(stderr,"netConnection::~netConnection() STATUS: Object destroyed.\n"); |
| #endif |
#endif |
| } |
} |
| |
|
| inline int telnet_cnx::isConnected(void) |
inline int netConnection::isConnected(void) |
| { |
{ |
| return(handle<0?0:1); |
return(handle<0?0:1); |
| } |
} |
| |
|
| inline void telnet_cnx::setPort(const int newport) |
inline void netConnection::setPort(const int newport) |
| { |
{ |
| port=newport; |
port=newport; |
| } |
} |
| |
|
| void telnet_cnx::setHost(const char*newhost,const int newport) |
void netConnection::setHost(const char*newhost,const int newport) |
| { |
{ |
| if(newhost!=NULL){ |
if(newhost!=NULL){ |
| strncpy(host,newhost,TELNET_MAX_HOST_SIZE); |
strncpy(host,newhost,NET_CONNECTION_MAX_HOST_SIZE); |
| host[TELNET_MAX_HOST_SIZE]='\0'; |
host[NET_CONNECTION_MAX_HOST_SIZE]='\0'; |
| } |
} |
| if(newport)setPort(newport); |
if(newport)setPort(newport); |
| } |
} |
| |
|
| int telnet_cnx::connect(void) |
int netConnection::connect(void) |
| { |
{ |
| |
|
| #ifdef __MSDOS__ |
#ifdef __MSDOS__ |
| Line 70 int telnet_cnx::connect(void) |
|
| Line 70 int telnet_cnx::connect(void) |
|
| |
|
| he=gethostbyname(host); |
he=gethostbyname(host); |
| #if DEBUG |
#if DEBUG |
| fprintf(stderr,"telnet_cnx::Connect()\n"); |
fprintf(stderr,"netConnection::Connect()\n"); |
| if(he==NULL) |
if(he==NULL) |
| fprintf(stderr," ERROR: Unknown host. [%s]\n",host); |
fprintf(stderr," ERROR: Unknown host. [%s]\n",host); |
| else |
else |
| Line 80 int telnet_cnx::connect(void) |
|
| Line 80 int telnet_cnx::connect(void) |
|
| |
|
| if(port<=0){ |
if(port<=0){ |
| #if DEBUG |
#if DEBUG |
| fprintf(stderr," WARNING: Bad port (%d) changed to default (%d).\n",port,TELNET_PORT); |
fprintf(stderr," WARNING: Bad port (%d) changed to default (%d).\n",port,NET_CONNECTION_PORT); |
| #endif |
#endif |
| port=TELNET_PORT; |
port=NET_CONNECTION_PORT; |
| } |
} |
| |
|
| sin.sin_family=AF_INET; |
sin.sin_family=AF_INET; |
| Line 99 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 |
| /* We want connect() syscall, not telnet_cnx method */ |
/* We want connect() syscall, not netConnection method */ |
| if(::connect(handle,(struct sockaddr*)&sin,sizeof(struct sockaddr_in))==-1){ |
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"); |
| Line 114 int telnet_cnx::connect(void) |
|
| Line 114 int telnet_cnx::connect(void) |
|
| return(isConnected()); |
return(isConnected()); |
| } |
} |
| |
|
| void telnet_cnx::disconnect(void){ |
void netConnection::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,"netConnection::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,"netConnection::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,"netConnection::disconnect() OK: Handle closed. [%d]\n",handle); |
| #endif |
#endif |
| } |
} |
| handle=-1; |
handle=-1; |
| Line 140 void telnet_cnx::disconnect(void){ |
|
| Line 140 void telnet_cnx::disconnect(void){ |
|
| } |
} |
| } |
} |
| |
|
| inline int telnet_cnx::writeLine(void){ |
inline int netConnection::writeLine(void){ |
| return(write(handle,"\n",1)); |
return(write(handle,"\n",1)); |
| } |
} |
| |
|
| inline int telnet_cnx::writeLine(const char*s){ |
inline int netConnection::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 netConnection::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 netConnection::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 netConnection::readData(char*buffer,const int size){ |
| #ifdef __MSDOS__ |
#ifdef __MSDOS__ |
| buffer[0]='\0'; |
buffer[0]='\0'; |
| buffer[size]='\0'; |
buffer[size]='\0'; |
| Line 199 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 netConnection::readLine(char*buffer,const int size){ |
| register int ret; |
register int ret; |
| |
|
| if(__rbuf!=NULL){ |
if(__rbuf!=NULL){ |
| Line 214 int telnet_cnx::readLine(char*buffer,con |
|
| Line 214 int telnet_cnx::readLine(char*buffer,con |
|
| else ret=1; |
else ret=1; |
| |
|
| if(ret>=0){ |
if(ret>=0){ |
| if(strstr(buffer,TELNET_EOL)!=NULL){ |
if(strstr(buffer,NET_CONNECTION_EOL)!=NULL){ |
| __rbuf=(char*)realloc(__rbuf,(strlen(strstr(buffer,TELNET_EOL)+strlen(TELNET_EOL))+1)*sizeof(char)); |
__rbuf=(char*)realloc(__rbuf,(strlen(strstr(buffer,NET_CONNECTION_EOL)+strlen(NET_CONNECTION_EOL))+1)*sizeof(char)); |
| strcpy(__rbuf,strstr(buffer,TELNET_EOL)+strlen(TELNET_EOL)); |
strcpy(__rbuf,strstr(buffer,NET_CONNECTION_EOL)+strlen(NET_CONNECTION_EOL)); |
| *(strstr(buffer,TELNET_EOL)+strlen(TELNET_EOL))='\0'; |
*(strstr(buffer,NET_CONNECTION_EOL)+strlen(NET_CONNECTION_EOL))='\0'; |
| } |
} |
| else{ |
else{ |
| if(__rbuf!=NULL){ |
if(__rbuf!=NULL){ |