BOOL hangup_call(COM_HANDLE com_handle) { time_t start; int attempt; int mdm_status; if(!carrier_detect(com_handle))/* DCD already low */ return TRUE; lprintf(LOG_DEBUG,"Waiting for transmit buffer to empty"); SLEEP(dtr_delay); for(attempt=0; attempt<hangup_attempts; attempt++) { lprintf(LOG_INFO,"Dropping DTR (attempt #%d)", attempt+1); if(!comLowerDTR(com_handle)) { lprintf(LOG_ERR,"ERROR %u lowering DTR", COM_ERROR); continue; } lprintf(LOG_DEBUG,"Waiting for loss of Carrier Detect (DCD)"); start=time(NULL); while(time(NULL)-start <= dcd_timeout) { if(((mdm_status=modem_status(com_handle))&COM_DCD)==0) return TRUE; SLEEP(1000); } lprintf(LOG_ERR,"TIMEOUT waiting for DCD to drop (attempt #%d of %d)" ,attempt+1, hangup_attempts); lprintf(LOG_DEBUG,"Modem status: 0x%lX", mdm_status); } return FALSE; }
/* connect to the newsserver */ int news_connect( char *news_server ) { int rc; if (carrier_detect()>=0) { ncn=open_connection( news_server, STANDARD_NEWS_PORT, 0); } if (ncn<0) { fprintf( log, "open_connection returns %s \n", get_err_text(ncn)); browser->msg_status(0, ncn); } else { /* rc = news_send_command( ncn, "mode reader" ); */ rc = news_receive( ncn, "dummy", FALSE ); /* get response from server */ } return(ncn); }
int ftp_connect(char *ftp_server, int port) { int rc; if (carrier_detect()>=0) { fpi = open_connection( ftp_server, port, 0 ); } if (fpi<0) { fprintf( log, "open_connection returns %s \n", get_err_text(fpi)); browser->msg_status(0, fpi); } else { rc = ftp_receive_continuations( fpi, "dummy", FALSE ); /* get response from server */ /* rc = news_receive( fpi, "dummy", FALSE ); /* get response from server */ /* fputs( header, log ); */ } pasv_failed=0; return(fpi); }
int imap_connect(char *imap_server, int port) { int rc; char server[80]; if (port==0) { port = IMAP_PORT; } strcpy( server, imap_server ); if (server[0]=='\0') { strcpy( server, DEFAULT_IMAP_SERVER ); } if (carrier_detect()>=0) { imapi = open_connection( server, port, 0 ); } if (imapi<0) { fprintf( log, "open_connection returns %s \n", get_err_text(imapi)); browser->msg_status(0, imapi); } else { /* rc = imap_receive_continuations( imapi, "dummy", FALSE, "*" ); /* get response from server */ rc = news_receive( imapi, "dummy", FALSE ); /* get response from server */ if (header[0]=='*') { imap_queue_unsolicited( header ); } /* fputs( header, log ); */ } return(imapi); }
BOOL wait_for_call(COM_HANDLE com_handle) { char str[128]; char* p; BOOL result=TRUE; DWORD events=0; time_t start=time(NULL); ZERO_VAR(cid_name); ZERO_VAR(cid_number); if(!comRaiseDTR(com_handle)) lprintf(LOG_ERR,"ERROR %u raising DTR", COM_ERROR_VALUE); if(com_alreadyconnected) return TRUE; if(!mdm_null) { if(mdm_init[0]) { lprintf(LOG_INFO,"Initializing modem:"); if(!modem_command(com_handle, mdm_init)) return FALSE; } if(!mdm_manswer && mdm_autoans[0]) { lprintf(LOG_INFO,"Setting modem to auto-answer:"); if(!modem_command(com_handle, mdm_autoans)) return FALSE; } if(mdm_cid[0]) { lprintf(LOG_INFO,"Enabling modem Caller-ID:"); if(!modem_command(com_handle, mdm_cid)) return FALSE; } } lprintf(LOG_INFO,"Waiting for incoming call (%s) ...", mdm_manswer ? "Ring Indication" : "Carrier Detect"); while(1) { if(terminated) return FALSE; if(comReadLine(com_handle, str, sizeof(str), /* timeout (ms): */250) > 0) { truncsp(str); if(str[0]==0) continue; lprintf(LOG_DEBUG,"Received from modem: '%s'", str); p=str; SKIP_WHITESPACE(p); if(*p) { lprintf(LOG_INFO, "Modem Message: %s", p); if(strncmp(p,"CONNECT ",8)==0) { long rate=atoi(p+8); if(rate) SAFEPRINTF2(termspeed,"%u,%u", rate, rate); } else if(strncmp(p,"NMBR",4)==0 || strncmp(p,"MESG",4)==0) { p+=4; FIND_CHAR(p,'='); SKIP_CHAR(p,'='); SKIP_WHITESPACE(p); if(cid_number[0]==0) /* Don't overwrite, if multiple messages received */ SAFECOPY(cid_number, p); } else if(strncmp(p,"NAME",4)==0) { p+=4; FIND_CHAR(p,'='); SKIP_CHAR(p,'='); SKIP_WHITESPACE(p); SAFECOPY(cid_name, p); } else if(strcmp(p,"NO CARRIER")==0) { ZERO_VAR(cid_name); ZERO_VAR(cid_number); } else if(mdm_ring[0] && strcmp(p,mdm_ring)==0 && mdm_manswer && mdm_answer[0]) { if(!modem_send(com_handle, mdm_answer)) { lprintf(LOG_ERR,"ERROR %u sending modem command (%s) on %s" ,COM_ERROR_VALUE, mdm_answer, com_dev); } } } continue; /* don't check DCD until we've received all the modem msgs */ } if(carrier_detect(com_handle)) break; if(mdm_reinit && (time(NULL)-start)/60 >= mdm_reinit) { lprintf(LOG_INFO,"Re-initialization timer elapsed: %u minutes", mdm_reinit); return TRUE; } } if(strcmp(cid_name,"P")==0) SAFECOPY(cid_name,"Private"); else if(strcmp(cid_name,"O")==0) SAFECOPY(cid_name,"Out-of-area"); if(strcmp(cid_number,"P")==0) SAFECOPY(cid_number,"Private"); else if(strcmp(cid_number,"O")==0) SAFECOPY(cid_number,"Out-of-area"); lprintf(LOG_INFO,"Carrier detected"); return TRUE; }
static void #if defined(_WIN32) WINAPI #endif service_loop(int argc, char** argv) { int argn; char* arg; char str[128]; char compiler[128]; for(argn=1; argn<(int)argc; argn++) { arg=argv[argn]; if(*arg!='-') { /* .ini file specified */ if(!fexist(arg)) { lprintf(LOG_ERR,"Initialization file does not exist: %s", arg); exit(usage(argv[0])); } parse_ini_file(arg); continue; } while(*arg=='-') arg++; if(stricmp(arg,"null")==0) mdm_null=TRUE; else if(stricmp(arg,"com")==0 && argc > argn+1) SAFECOPY(com_dev, argv[++argn]); else if(stricmp(arg,"baud")==0 && argc > argn+1) com_baudrate = (ulong)strtol(argv[++argn],NULL,0); else if(stricmp(arg,"host")==0 && argc > argn+1) SAFECOPY(host, argv[++argn]); else if(stricmp(arg,"port")==0 && argc > argn+1) port = (ushort)strtol(argv[++argn], NULL, 0); else if(stricmp(arg,"live")==0) { if(argc > argn+1 && (com_handle = (COM_HANDLE)strtol(argv[argn+1], NULL, 0)) != 0) { argn++; com_handle_passed=TRUE; } com_alreadyconnected=TRUE; terminate_after_one_call=TRUE; mdm_null=TRUE; } else if(stricmp(arg,"nohangup")==0) { com_hangup=FALSE; } else if(stricmp(arg,"debug")==0) { log_level=LOG_DEBUG; } else if(stricmp(arg,"help")==0 || *arg=='?') exit(usage(argv[0])); else { fprintf(stderr,"Invalid option: %s\n", arg); exit(usage(argv[0])); } } #if defined(_WIN32) /* Convert "1" to "COM1" for Windows */ { int i; if((i=atoi(com_dev)) != 0) SAFEPRINTF(com_dev, "COM%d", i); } if(daemonize) { if((svc_status_handle = RegisterServiceCtrlHandler(NAME, ServiceControlHandler))==0) { lprintf(LOG_ERR,"!ERROR %d registering service control handler",GetLastError()); return; } svc_status.dwServiceType=SERVICE_WIN32_OWN_PROCESS; svc_status.dwControlsAccepted=SERVICE_ACCEPT_SHUTDOWN; svc_status.dwWaitHint=NTSVC_TIMEOUT_STARTUP; svc_status.dwCurrentState=SERVICE_START_PENDING; SetServiceStatus(svc_status_handle, &svc_status); } #endif lprintf(LOG_INFO,"%s", comVersion(str,sizeof(str))); DESCRIBE_COMPILER(compiler); lprintf(LOG_INFO,"Build %s %s %s", __DATE__, __TIME__, compiler); /************************************/ /* Inititalize WinSock and COM Port */ /************************************/ if(!winsock_startup()) exit(1); /* Install clean-up callback */ atexit(cleanup); lprintf(LOG_INFO,"TCP Host: %s", host); lprintf(LOG_INFO,"TCP Port: %u", port); if(!com_handle_passed) { lprintf(LOG_INFO,"Opening Communications Device (COM Port): %s", com_dev); if((com_handle=comOpen(com_dev)) == COM_HANDLE_INVALID) { lprintf(LOG_ERR,"ERROR %u opening communications device/port: '%s'", COM_ERROR_VALUE, com_dev); exit(1); } } lprintf(LOG_INFO,"COM Port device handle: %u", com_handle); if(com_baudrate!=0) { if(!comSetBaudRate(com_handle,com_baudrate)) lprintf(LOG_ERR,"ERROR %u setting DTE rate to %lu bps" ,COM_ERROR_VALUE, com_baudrate); } lprintf(LOG_INFO,"COM Port DTE rate: %ld bps", comGetBaudRate(com_handle)); if(ident) _beginthread(ident_server_thread, 0, NULL); #if defined(_WIN32) if(daemonize) { svc_status.dwCurrentState=SERVICE_RUNNING; svc_status.dwControlsAccepted|=SERVICE_ACCEPT_STOP; SetServiceStatus(svc_status_handle, &svc_status); } #endif /***************************/ /* Initialization Complete */ /***************************/ /* Main service loop: */ while(!terminated && wait_for_call(com_handle)) { if(!carrier_detect(com_handle)) /* re-initialization timer time-out? */ continue; comWriteByte(com_handle,'\r'); comWriteString(com_handle, banner); comWriteString(com_handle, "\r\n"); if((sock=connect_socket(host, port)) == INVALID_SOCKET) { comWriteString(com_handle,"\7\r\n!ERROR connecting to TCP port\r\n"); } else { handle_call(); close_socket(&sock); total_calls++; lprintf(LOG_INFO,"Call completed (%lu total)", total_calls); } if(com_hangup && !hangup_call(com_handle)) break; if(terminate_after_one_call) break; } exit(0); }
BOOL handle_call(void) { BYTE buf[4096]; BYTE telnet_buf[sizeof(buf)]; BYTE* p; int result; int rd; int wr; fd_set socket_set; struct timeval tv = {0, 0}; bytes_sent=0; bytes_received=0; call_terminated=FALSE; /* Reset Telnet state information */ telnet_cmdlen=0; ZERO_VAR(telnet_local_option); ZERO_VAR(telnet_remote_option); if(telnet && telnet_advertise_cid && (cid_number[0] || cid_name[0])) /* advertise the ability to send our location */ send_telnet_cmd(TELNET_WILL, TELNET_SEND_LOCATION); input_thread_terminated=FALSE; _beginthread(input_thread, 0, NULL); while(!terminated) { if(!dcd_ignore && !carrier_detect(com_handle)) { lprintf(LOG_WARNING,"Loss of Carrier Detect (DCD) detected"); break; } #if 0 /* single-threaded mode: */ if(comReadByte(com_handle, &ch)) { lprintf(LOG_DEBUG,"read byte: %c", ch); send(sock, &ch, sizeof(ch), 0); } #endif FD_ZERO(&socket_set); FD_SET(sock,&socket_set); if((result = select(sock+1,&socket_set,NULL,NULL,&tv)) == 0) { YIELD(); continue; } if(result == SOCKET_ERROR) { lprintf(LOG_ERR,"SOCKET ERROR %u on select", ERROR_VALUE); break; } rd=recv(sock,buf,sizeof(buf),0); if(rd < 1) { if(rd==0) { lprintf(LOG_WARNING,"Socket Disconnected"); break; } if(ERROR_VALUE == EAGAIN) continue; else if(ERROR_VALUE == ENOTSOCK) lprintf(LOG_WARNING,"Socket closed by peer on receive"); else if(ERROR_VALUE==ECONNRESET) lprintf(LOG_WARNING,"Connection reset by peer on receive"); else if(ERROR_VALUE==ESHUTDOWN) lprintf(LOG_WARNING,"Socket shutdown on receive"); else if(ERROR_VALUE==ECONNABORTED) lprintf(LOG_WARNING,"Connection aborted by peer on receive"); else lprintf(LOG_ERR,"SOCKET RECV ERROR %d",ERROR_VALUE); break; } if(telnet) p=telnet_interpret(buf,rd,telnet_buf,&rd); else p=buf; if((wr=comWriteBuf(com_handle, p, rd)) != COM_ERROR) bytes_sent += wr; } call_terminated=TRUE; /* terminate input_thread() */ while(!input_thread_terminated) { YIELD(); } lprintf(LOG_INFO,"Bytes sent-to, received-from COM Port: %lu, %lu" ,bytes_sent, bytes_received); return TRUE; }