static int selectmy(int n,fd_set *rfs,fd_set *efs,struct timeval *to) { fd_set r,e; int sec=to->tv_sec,rc; do { if(rfs)r=*rfs; else FD_ZERO(&r); if(efs)e=*efs; else FD_ZERO(&e); if(calling) { getevt(); if(tty_hangedup)return -1; } if(sec)to->tv_sec=1,sec--; else to->tv_sec=0; rc=select(n,&r,NULL,&e,to); } while(sec&&!rc); to->tv_sec=sec; *rfs=r;*efs=e; #if DEBUG_SLEEP==1 if(is_ip)usleep(1000); #endif return rc; }
/* * Open connection via proxy. Returns TRUE if OK, FALSE if fail. */ static int tcp_connect_proxy(char *name) { int rc, i; time_t t1; char buf[H_BUF]; char *n, *p = NULL, *dp = ""; if ( cfgs( CFG_PROXY ) && ( p = strchr( ccs, ' ' ))) *p++ = 0; if ( !strchr( name, ':' )) dp = bink ? ":24554" : ":60179"; i = snprintf( buf, H_BUF, "CONNECT %s%s HTTP/1.0\r\n", name, dp ); if ( p ) { if (( n = strchr( p,' ' ))) *n = ':'; i += snprintf( buf + i, H_BUF - i, "Proxy-Authorization: basic " ); i += base64( p, strlen( p ), buf + i ); buf[i++] = '\r'; buf[i++] = '\n'; } buf[i++] = '\r'; buf[i++] = '\n'; PUTBLK( (unsigned char*) buf, i ); t1 = timer_set( cfgi( CFG_HSTIMEOUT )); for( i = 0; i < H_BUF; i++ ) { while(( rc = GETCHAR(1)) == TIMEOUT && !timer_expired( t1 )) getevt(); if ( rc == RCDO || tty_gothup ) { write_log( "got hangup" ); return (FALSE); } if ( rc == TIMEOUT ) { write_log( "proxy timeout" ); return (FALSE); } else if ( rc < 0 ) { write_log("connection is closed by proxy"); return (FALSE); } buf[i] = rc; buf[i + 1] = '\0'; if (( i >= H_BUF ) || (( p = strstr( buf, "\r\n\r\n" )))) { if (( p = strchr( buf, '\n' ))) { *p-- = '\0'; if ( *p == '\r' ) *p = '\0'; } p = buf; if ( strstr( buf, " 200 " )) break; if ( !strncasecmp( buf, "HTTP", 4 )) p = strchr( buf, ' ' ) + 1; write_log( "connect rejected by proxy (%s)", p ); return (FALSE); } } return (TRUE); }
int modem_chat(char *cmd, slist_t *oks, slist_t *nds, slist_t *ers, slist_t *bys, char *ringing, int maxr, int timeout, char *rest, size_t restlen) { char buf[MAX_STRING]; int rc,nrng=0; slist_t *cs; time_t t1; calling=1; DEBUG(('M',4,"modem_chat: cmd=\"%s\" timeout=%d",cmd,timeout)); rc=modem_sendstr(cmd); if(rc!=1) { if(rest) xstrcpy(rest, "FAILURE", restlen); DEBUG(('M',3,"modem_chat: modem_sendstr failed, rc=%d",rc)); return MC_FAIL; } if(!oks && !ers && !bys) return MC_OK; rc=OK; t1=t_set(timeout); while(ISTO(rc) && !t_exp(t1) && (!maxr || nrng<maxr)) { getevt(); rc=tty_gets(buf, MAX_STRING-1, t_rest(t1)); if(rc==RCDO) { if(rest)xstrcpy(rest,"HANGUP",restlen); return MC_BUSY; } if(rc!=OK) { if(rest) xstrcpy(rest, "FAILURE", restlen); DEBUG(('M',3,"modem_chat: tty_gets failed, rc=%d",rc)); return MC_FAIL; } if(!*buf)continue; for(cs=oks;cs;cs=cs->next) if(!strncmp(buf,cs->str,strlen(cs->str))) { if(rest)xstrcpy(rest,buf,restlen); return MC_OK; } for(cs=ers;cs;cs=cs->next) if(!strncmp(buf,cs->str,strlen(cs->str))) { if(rest)xstrcpy(rest,buf,restlen); return MC_ERROR; } if(ringing&&!strncmp(buf,ringing,strlen(ringing))) { if(!nrng&&strlen(ringing)==4) { if(rest)xstrcpy(rest,buf,restlen); return MC_RING; } nrng++; continue; } for(cs=nds;cs;cs=cs->next) if(!strncmp(buf,cs->str,strlen(cs->str))) { if(rest)xstrcpy(rest,buf,restlen); return MC_NODIAL; } for(cs=bys;cs;cs=cs->next) if(!strncmp(buf,cs->str,strlen(cs->str))) { if(rest)xstrcpy(rest,buf,restlen); return MC_BUSY; } } if(rest) { if(nrng && maxr && nrng>=maxr) snprintf(rest, restlen, "%d RINGINGs", nrng); else if(ISTO(rc)) xstrcpy(rest, "TIMEOUT", restlen); else xstrcpy(rest, "FAILURE", restlen); } return MC_FAIL; }