xpDateTime_t DLLCALL gmtime_to_xpDateTime(time_t ti) { xpDateTime_t never; struct tm tm; ZERO_VAR(never); ZERO_VAR(tm); if(gmtime_r(&ti,&tm)==NULL) return(never); return xpDateTime_create(1900+tm.tm_year,1+tm.tm_mon,tm.tm_mday ,tm.tm_hour,tm.tm_min,(float)tm.tm_sec ,xpTimeZone_UTC); }
xpDateTime_t DLLCALL time_to_xpDateTime(time_t ti, xpTimeZone_t zone) { xpDateTime_t never; struct tm tm; ZERO_VAR(never); ZERO_VAR(tm); if(localtime_r(&ti,&tm)==NULL) return(never); return xpDateTime_create(1900+tm.tm_year,1+tm.tm_mon,tm.tm_mday ,tm.tm_hour,tm.tm_min,(float)tm.tm_sec ,zone==xpTimeZone_LOCAL ? xpTimeZone_local() : zone); }
int DLLCALL getmail(scfg_t* cfg, int usernumber, BOOL sent) { char str[128]; int i=0; long l; idxrec_t idx; smb_t smb; ZERO_VAR(smb); sprintf(smb.file,"%smail",cfg->data_dir); smb.retry_time=cfg->smb_retry_time; sprintf(str,"%s.sid",smb.file); l=flength(str); if(l<(long)sizeof(idxrec_t)) return(0); if(!usernumber) return(l/sizeof(idxrec_t)); /* Total system e-mail */ smb.subnum=INVALID_SUB; if(smb_open(&smb)!=0) return(0); while(!smb_feof(smb.sid_fp)) { if(smb_fread(&smb,&idx,sizeof(idx),smb.sid_fp) != sizeof(idx)) break; if(idx.number==0) /* invalid message number, ignore */ continue; if(idx.attr&MSG_DELETE) continue; if((!sent && idx.to==usernumber) || (sent && idx.from==usernumber)) i++; } smb_close(&smb); return(i); }
SOCKET connect_socket(const char* host, ushort port) { SOCKET sock; SOCKADDR_IN addr; if((sock=socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) == INVALID_SOCKET) { lprintf(LOG_ERR,"ERROR %u creating socket", ERROR_VALUE); return INVALID_SOCKET; } lprintf(LOG_DEBUG,"Setting TCP_NODELAY to %d",tcp_nodelay); setsockopt(sock,IPPROTO_TCP,TCP_NODELAY,(char*)&tcp_nodelay,sizeof(tcp_nodelay)); ZERO_VAR(addr); addr.sin_addr.s_addr = resolve_ip(host); addr.sin_family = AF_INET; addr.sin_port = htons(port); lprintf(LOG_INFO,"Connecting to %s port %u", host, port); if(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0) { lprintf(LOG_INFO,"Connected from COM Port (handle %u) to %s TCP port %u using socket descriptor %u" ,com_handle, host, port, sock); return sock; } lprintf(LOG_ERR,"SOCKET ERROR %u connecting to %s port %u", ERROR_VALUE, host, port); closesocket(sock); return INVALID_SOCKET; }
isoDate_t DLLCALL gmtime_to_isoDateTime(time_t ti, isoTime_t* isoTime) { struct tm tm; if(isoTime!=NULL) *isoTime=0; ZERO_VAR(tm); if(gmtime_r(&ti,&tm)==NULL) return(0); if(isoTime!=NULL) *isoTime=isoTime_create(tm.tm_hour,tm.tm_min,tm.tm_sec); return isoDate_create(1900+tm.tm_year,1+tm.tm_mon,tm.tm_mday); }
xpDateTime_t time_to_xpDateTime(time_t ti) { xpDateTime_t never; struct tm tm; memset(&never,0,sizeof(never)); if(ti==0) return(never); ZERO_VAR(tm); if(gmtime_r(&ti,&tm)==NULL) return(never); return xpDateTime_create(1900+tm.tm_year,1+tm.tm_mon,tm.tm_mday ,tm.tm_hour,tm.tm_min,(float)tm.tm_sec,xpTimeZone_local()); }
time_t DLLCALL isoDateTime_to_time(isoDate_t date, isoTime_t ti) { struct tm tm; ZERO_VAR(tm); if(date==0) return(INVALID_TIME); tm.tm_year = isoDate_year(date); tm.tm_mon = isoDate_month(date); tm.tm_mday = isoDate_day(date); tm.tm_hour = isoTime_hour(ti); tm.tm_min = isoTime_minute(ti); tm.tm_sec = isoTime_second(ti); return sane_mktime(&tm); }
time_t DLLCALL xpDateTime_to_time(xpDateTime_t xpDateTime) { struct tm tm; ZERO_VAR(tm); if(xpDateTime.date.year==0) return(INVALID_TIME); tm.tm_year = xpDateTime.date.year; tm.tm_mon = xpDateTime.date.month; tm.tm_mday = xpDateTime.date.day; tm.tm_hour = xpDateTime.time.hour; tm.tm_min = xpDateTime.time.minute; tm.tm_sec = (int)xpDateTime.time.second; return sane_mktime(&tm); }
/* TODO: adjust times in 24:xx:xx format */ xpDateTime_t DLLCALL isoDateTimeStr_parse(const char* str) { char zone[16]; xpDateTime_t xpDateTime; zone[0]=0; ZERO_VAR(xpDateTime); if((sscanf(str,"%4u-%2u-%2uT%2u:%2u:%f%6s" /* CCYY-MM-DDThh:MM:ss±hhmm */ ,&xpDateTime.date.year ,&xpDateTime.date.month ,&xpDateTime.date.day ,&xpDateTime.time.hour ,&xpDateTime.time.minute ,&xpDateTime.time.second ,zone)>=2 || sscanf(str,"%4u%2u%2uT%2u%2u%f%6s" /* CCYYMMDDThhmmss±hhmm */ ,&xpDateTime.date.year ,&xpDateTime.date.month ,&xpDateTime.date.day ,&xpDateTime.time.hour ,&xpDateTime.time.minute ,&xpDateTime.time.second ,zone)>=4 || sscanf(str,"%4u%2u%2u%2u%2u%f%6s" /* CCYYMMDDhhmmss±hhmm */ ,&xpDateTime.date.year ,&xpDateTime.date.month ,&xpDateTime.date.day ,&xpDateTime.time.hour ,&xpDateTime.time.minute ,&xpDateTime.time.second ,zone)>=1 ) && isoTimeZoneStr_parse(zone,&xpDateTime.zone)) return xpDateTime; return xpDateTime; }
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; }
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; }