void NodeVisitor::visitComputedProperty(const ComputedPropertyPtr& node) { ACCEPT(node->getInitializer()); ACCEPT(node->getGetter()); ACCEPT(node->getSetter()); ACCEPT(node->getWillSet()); ACCEPT(node->getDidSet()); }
void NodeVisitor::visitFunction(const FunctionDefPtr& node) { for(ParametersNodePtr params : node->getParametersList()) { ACCEPT(params); } ACCEPT(node->getBody()); }
void NodeVisitor::visitDictionaryLiteral(const DictionaryLiteralPtr& node) { for(auto entry : *node) { ACCEPT(entry.first); ACCEPT(entry.second); } }
void NodeVisitor::visitCase(const CaseStatementPtr& node) { for(auto condition : node->getConditions()) { ACCEPT(condition.condition); ACCEPT(condition.guard); } ACCEPT(node->getCodeBlock()); }
void NodeVisitor::visitSwitchCase(const SwitchCasePtr& node) { ACCEPT(node->getControlExpression()); for(CaseStatementPtr c : *node) { ACCEPT(c); } ACCEPT(node->getDefaultCase()); }
void NodeVisitor::visitClosure(const ClosurePtr& node) { ACCEPT(node->getCapture()); ACCEPT(node->getParameters()); for(const StatementPtr& st : *node) { ACCEPT(st); } }
void NodeVisitor::visitForLoop(const ForLoopPtr& node) { for(ExpressionPtr init : node->inits) { ACCEPT(init); } ACCEPT(node->getInitializer()); ACCEPT(node->condition); ACCEPT(node->step); ACCEPT(node->codeBlock); }
void cmj_incoming_call(struct NTD *tsk) { int rval, rc; struct CLB *lnk; struct sockaddr_in in; GTM_SOCKLEN_TYPE sz = SIZEOF(in); cmi_status_t status; while ((-1 == (rval = ACCEPT(tsk->listen_fd, (struct sockaddr *)&in, (GTM_SOCKLEN_TYPE *)&sz))) && EINTR == errno) ; while (rval >= 0) { status = cmj_setupfd(rval); if (CMI_ERROR(status)) { CLOSEFILE_RESET(rval, rc); /* resets "rval" to FD_INVALID */ return; } status = cmj_set_async(rval); if (CMI_ERROR(status)) { CLOSEFILE_RESET(rval, rc); /* resets "rval" to FD_INVALID */ return; } /* grab a clb off of the free list */ lnk = cmi_alloc_clb(); if (!lnk || !tsk->acc || !tsk->acc(lnk) || !tsk->crq) { /* no point if the callbacks are not in place */ cmi_free_clb(lnk); CLOSEFILE_RESET(rval, rc); /* resets "rval" to FD_INVALID */ return; } if (rval > tsk->max_fd) tsk->max_fd = rval; lnk->mun = rval; lnk->sta = CM_CLB_IDLE; lnk->peer = in; insqh(&lnk->cqe, &tsk->cqh); lnk->ntd = tsk; FD_SET(rval, &tsk->es); /* setup for callback processing */ lnk->deferred_event = TRUE; lnk->deferred_reason = CMI_REASON_CONNECT; while ((-1 == (rval = ACCEPT(tsk->listen_fd, (struct sockaddr *)&in, (GTM_SOCKLEN_TYPE *)&sz))) && EINTR == errno) ; } }
void NodeVisitor::visitTuple(const TuplePtr& node) { for(const PatternPtr & p : *node) { ACCEPT(p); } }
void NodeVisitor::visitValueBindings(const ValueBindingsPtr &node) { for(ValueBindingPtr var : *node) { ACCEPT(var); } }
void NodeVisitor::visitProgram(const ProgramPtr& node) { for(StatementPtr st : *node) { ACCEPT(st); } }
void NodeVisitor::visitStringInterpolation(const StringInterpolationPtr &node) { for(auto expr : * node) { ACCEPT(expr); } }
void NodeVisitor::visitCodeBlock(const CodeBlockPtr& node) { for(auto st: *node) { ACCEPT(st); } }
void NodeVisitor::visitArrayLiteral(const ArrayLiteralPtr& node) { for(ExpressionPtr expr : node->elements) { ACCEPT(expr); } }
static int XFP(XFlash *Xf,int server){ IStr(svhost,MaxHostNameLen); int svport; int svsock; int timeout = 0; Connection *Conn = Xf->xf_Conn; if( !server ){ XFlashClient(Xf); return 0; } /* observers */ strcpy(svhost,"0.0.0.0"); svport = 6003; svsock = server_open("XFlash",AVStr(svhost),svport,1); for(;;){ /* broad casting ? */ Xf->xf_xfsockfd = ACCEPT(svsock,0,-1,timeout); Xf->xf_xfsockfp = fdopen(Xf->xf_xfsockfd,"w+"); fprintf(stderr,"---- xfsock[%d]\n",Xf->xf_xfsockfd); toFlash(Xf,"INIT"); } return 0; }
void NodeVisitor::visitParenthesizedExpression(const ParenthesizedExpressionPtr& node) { for(auto term : *node) { ACCEPT(term.expression); } }
void NodeVisitor::visitParameters(const ParametersNodePtr& node) { for(ParameterNodePtr p : *node) { ACCEPT(p); } }
void echo_accept_handler(schedule* s, void* args) { echoserver* echo = (echoserver*)args; int listenfd = echo->listenfd(); for (;;) { struct sockaddr addr; socklen_t addrlen; int connfd = ACCEPT(listenfd, &addr, &addrlen); if (connfd > 0) { schedule::ref().new_coroutine(echo_connection_handler, (void*)(intptr_t)connfd); } else if (connfd == 0) { printf("connfd: %d\n", connfd); schedule::ref().wait(200); continue; } else { printf("connfd: %d\n", connfd); } } }
int Parser::parse() try { push__(0); // initial state clearin(); // clear the tokens. while (true) { try { if (s_state[d_state__]->d_type & REQ_TOKEN) nextToken(); // obtain next token int action = lookup(false); // lookup d_token__ in d_state__ if (action > 0) // SHIFT: push a new state { push__(action); popToken__(); // token processed } else if (action < 0) // REDUCE: execute and pop. { executeAction(-action); // next token is the rule's LHS reduce__(s_productionInfo[-action]); } else ACCEPT(); } catch (ErrorRecovery__) { errorRecovery(); } } } catch (Return__ retValue) { return retValue; }
int main(int argc, char *argv[]) { int sockfd; struct addrinfo hints, *servinfo; int status; if (argc != 2) { printf("Usage: %s <port>\n", argv[0]); return 1; } printf("ftps: Starting server on port %s...\n", argv[1]); // Create output folder if (mkdir(OUTPUT_DIR, S_IRWXU) == -1) { int err = errno; if (err != EEXIST) { fprintf(stderr, "ftps: Error creating output directory: %s\n", strerror(err)); exit(1); } } // Setup structures memset(&hints, 0, sizeof hints); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; if ((status = getaddrinfo(NULL, argv[1], &hints, &servinfo)) != 0) { fprintf(stderr, "ftps: getaddrinfo: %s\n", gai_strerror(status)); return 1; } // Get socket if ((sockfd = SOCKET(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol)) == -1) { perror("ftps: socket"); exit(1); } // Bind if (BIND(sockfd, servinfo->ai_addr, servinfo->ai_addrlen) == -1) { perror("ftps: bind"); CLOSE(sockfd); exit(1); } // Accept - Block until connection while (ACCEPT(sockfd, servinfo->ai_addr, &(servinfo->ai_addrlen)) != 0); // Clean up freeaddrinfo(servinfo); // Wait for file then exit receiveFile(sockfd); CLOSE(sockfd); exit(0); return 0; }
void clipline(dcpt winmin,dcpt winmax,wcpt2 p1,wcpt2 p2) { unsigned char encode(wcpt2,dcpt,dcpt); unsigned char code1,code2; int done = 0 , draw = 0; float m; void swapcode(unsigned char *c1,unsigned char *c2); void swappts(wcpt2 *p1,wcpt2 *p2); while(!done) { code1 = encode(p1,winmin,winmax); code2 = encode(p2,winmin,winmax); if(ACCEPT(code1,code2)) { draw = 1; done = 1; } else if(REJECT(code1,code2)) done = 1; else if(INSIDE(code1)) { swappts(&p1,&p2); swapcode(&code1,&code2); } if(code1 & LEFT_EDGE) { p1.y += (winmin.x - p1.x) * (p2.y - p1.y) / (p2.x - p1.x); p1.x = winmin.x; } else if(code1 & RIGHT_EDGE) { p1.y += (winmax.x - p1.x) * (p2.y - p1.y) / (p2.x - p1.x); p1.x = winmax.x; } else if(code1 & TOP_EDGE) { if(p2.x != p1.x) p1.x += (winmin.y - p1.y) * (p2.x - p1.x) / (p2.y - p1.y); p1.y = winmin.y; } else if(code1 & BOTTOM_EDGE) { if(p2.x != p1.x) p1.x += (winmax.y - p1.y) * (p2.x - p1.x) / (p2.y - p1.y); p1.y = winmax.y; } } if(draw) { setcolor(5); line(p1.x,p1.y,p2.x,p2.y); } }
USE_SWALLOW_NS #define ACCEPT(node) do { \ Node* n = node.get(); \ if(n) { \ n->accept(this); \ } \ } while(0); void NodeVisitor::visitDeinit(const DeinitializerDefPtr& node) { ACCEPT(node->getBody()); }
bool TcpServer::makeConnect() { bool rtn = false; int rc = this->SOCKET_FAIL; //int socket = this->SOCKET_FAIL; int disableNodeDelay = 1; int err = 0; if (!this->isConnected()) { this->setConnected(false); if (this->SOCKET_FAIL != this->getSockHandle()) { CLOSE(this->getSockHandle()); this->setSockHandle(this->SOCKET_FAIL); } rc = ACCEPT(this->getSrvrHandle(), NULL, NULL); if (this->SOCKET_FAIL != rc) { this->setSockHandle(rc); LOG_INFO("Client socket accepted"); // The set no delay disables the NAGEL algorithm rc = SET_NO_DELAY(this->getSockHandle(), disableNodeDelay); err = errno; if (this->SOCKET_FAIL == rc) { LOG_WARN("Failed to set no socket delay, errno: %d, sending data can be delayed by up to 250ms", err); } this->setConnected(true); rtn = true; } else { LOG_ERROR("Failed to accept for client connection"); rtn = false; } } else { LOG_WARN("Tried to connect when socket already in connected state"); } return rtn; }
static int Xnew(XFlash *Xf,int svsock){ Connection *Conn = Xf->xf_Conn; int client; int server; for(;;){ client = ACCEPT(svsock,0,-1,0); server = client_open("XFlash","XCFlash","127.0.0.1",6001); if( server < 0 ){ continue; } FromC = ToC = client; FromS = ToS = server; Xf->xf_cstid = thread_fork(0,0,"XFlash-CS",(IFUNCP)XCS,Xf); Xf->xf_sctid = thread_fork(0,0,"XFlash-SC",(IFUNCP)XSC,Xf,1024); } return 0; }
int main(int argc, char *argv[]) { int call = 0; unsigned int server_s; // Server socket descriptor unsigned int connect_s; // Connection socket descriptor char in_buf[BUFFER_SIZE]; // BUFFER_SIZE-byte input buffer for data unsigned int bytes_recv=0; // Gauge number of bytes received int remaining; struct sockaddr_in server_addr; socklen_t addr_len; struct sockaddr_in client_addr; FILE *fp; struct { unsigned int size; char name[20]; } in_meta; if(argc!=2){ perror("Format ftps <Port-num>\n"); return 1; } server_s = SOCKET(AF_INET, SOCK_STREAM, 0); /*START:Fill-in my socket's address information and bind to the socket*/ server_addr.sin_family = AF_INET; // Address family to use server_addr.sin_port = htons(atoi(argv[1])); // Port number to use server_addr.sin_addr.s_addr = htonl(INADDR_ANY); // Listen on any IP address if(BIND(server_s, (struct sockaddr *)&server_addr, sizeof(server_addr))!=0){ perror("Binding failure \n"); return 1; } /*END:Fill-in my socket's address information and bind to the socket*/ /*START:Establish TCP connection with the client*/ if((connect_s = ACCEPT(server_s, (struct sockaddr *)&client_addr, &addr_len))==-1){ perror("Accept failure \n"); return 1; } /*END:Establish TCP connection with the client*/ /*START:Start Receiving meta data using RECV*/ bytes_recv = RECV (connect_s, in_buf, sizeof(in_meta), 0); //receive the metadata of the file if(bytes_recv==-1){ perror("Error receiving"); return 1; } printf("count:%u size %u\n ",call, bytes_recv); call++; memcpy(&in_meta,in_buf,sizeof(in_meta)); printf("size is %d\n",in_meta.size); remaining = in_meta.size; printf("Name is %s\n",in_meta.name); /*END:Start Receiving meta data using RECV*/ /*START:Create a file with received name*/ fp=fopen(in_meta.name,"w"); if(fp==NULL){ perror("Cannot Create file"); return 1; } /*END:Create a file with received name*/ /*START:Receive file data using UDP RECV*/ while(remaining>0) { bytes_recv = RECV (connect_s, in_buf, BUFFER_SIZE , 0); //You might not receive what you ask for. Hence check what bytes_recv is.. if(bytes_recv==-1){ perror("Error receiving"); return 1; } printf("count:%u size %u\n",call, bytes_recv); call++; remaining = remaining - fwrite(in_buf, 1,(remaining<BUFFER_SIZE?remaining:BUFFER_SIZE) , fp); } printf("File Received %s\n",in_meta.name); /*START:Receive file data using UDP RECV*/ fclose(fp); CLOSE(connect_s); CLOSE(server_s); return 0; }
//Some thoughts: we don't need all these character classes //Some other thoughts: Understand how scan works in the //context of the entire program. void scan(location_t * loc, token_t * tok) { //an enumeration of the different possible states //also initializes "state" to "start," which is 0 enum { start, got_space, got_nl_space, got_other, done, /* numbers: */ got_dot, got_dec, got_fp_dot, /* operators: */ got_plus, got_minus, got_star, got_slash, got_pct, got_lparen, got_incr, got_decr, got_incr2, got_decr2, got_caret, got_bang } state = start; /* Standard way to recognize a token: put back lookahead character that isn't part of current token: */ #define ACCEPT_REUSE(t) \ *loc = loc_save; \ tok->length--; \ tok->tc = t; \ state = done; #define ACCEPT_SHIFT(t, i) \ move_location_back(loc, i); \ tok->length = tok->length - i; \ tok->tc = t; \ state = done; /* Shortcut to eliminate final states with no out transitions: go ahead and accept token in previous state, but don't put back the lookahead: it's actually part of the token: */ #define ACCEPT(t) \ tok->tc = t; \ state = done; //NOTE: the following code is NOT part of the ACCEPT(t) tok->location = *loc; tok->length = 0; while (state != done) { //Points to loc -- but if we update loc, we update loc_save location_t loc_save = *loc; //Gets the character, but also updates loc by incrementing it int c = get_character(loc); //I guess... we might be adding this character to the token? tok->length++; //WAIT A SECOND -- THIS WHOLE SWITCH-CASE IS A DFA. :O switch (state) { //If we're in the start state... case start: //... find out which character class the character is in switch (char_classes[c]) { case WHITE: state = got_space; break; case EOLN: state = got_nl_space; break; case DOT: state = got_dot; break; case DIG: state = got_dec; break; case PLUS: state = got_plus; break; case MINUS: state = got_minus; break; case STAR: state = got_star; break; case PCT: state = got_pct; break; case SLASH: state = got_slash; break; case CARET: state = got_caret; break; case BANG: state = got_bang; break; case LPAREN: state = got_lparen; break; case RPAREN: tok->terminal = t_RPAREN; ACCEPT(T_RPAREN); break; case SEMIC: tok->terminal = t_SEMIC; ACCEPT(T_SEMIC); break; case END: ACCEPT_REUSE(T_EOF); break; case OTHER: /* This will be an error. Eat as many bogus characters as possible. */ state = got_other; break; default: state = got_other; break; } break; case got_space: switch (char_classes[c]) { case WHITE: break; /* stay put */ case EOLN: state = got_nl_space; break; default: ACCEPT_REUSE(T_SPACE); break; } break; case got_nl_space: switch (char_classes[c]) { case WHITE: case EOLN: break; /* stay put */ default: ACCEPT_REUSE(T_NL_SPACE); break; } break; case got_other: switch (char_classes[c]) { case OTHER: case WHITE: case EOLN: break; /* stay put */ default: fprintf(stderr, "Invalid token"); print_location(tok); ACCEPT_REUSE(T_SPACE); /* most likely recovery? */ break; } break; //This, theoretically, should be unreachable -- state would have to change //outside of a switch statement, which, by current design is, dare I say, //impossible. But, this being a computer program prone to bugs, let's just //say "improbable." case done: fprintf(stderr, "scan: unexpected done in switch\n"); exit(-1); break; /* operators: */ //What about positive and negative? We want the scanner //to parse the positive and negative signs for me case got_plus: tok->terminal = t_PLUS; ACCEPT_REUSE(T_OPERATOR); // + break; case got_minus: tok->terminal = t_MINUS; ACCEPT_REUSE(T_OPERATOR); // - break; case got_star: tok->terminal = t_STAR; ACCEPT_REUSE(T_OPERATOR); // * break; case got_slash: tok->terminal = t_SLASH; ACCEPT_REUSE(T_OPERATOR); // / break; case got_pct: tok->terminal = t_PCT; ACCEPT_REUSE(T_OPERATOR); // % break; case got_caret: tok->terminal = t_CARET; ACCEPT_REUSE(T_OPERATOR); break; case got_bang: tok->terminal = t_BANG; ACCEPT_REUSE(T_OPERATOR); break; case got_lparen: switch(char_classes[c]){ case PLUS: state = got_incr; break; case MINUS: state = got_decr; break; default: tok->terminal = t_LPAREN; ACCEPT_REUSE(T_LPAREN); break; } break; case got_incr: switch(char_classes[c]){ case PLUS: state = got_incr2; break; case RPAREN: tok->terminal = t_PLUS_UNARY; ACCEPT(T_UNARY); break; default: tok->terminal = t_LPAREN; ACCEPT_SHIFT(T_LPAREN, 2); break; } break; case got_decr: switch(char_classes[c]){ case MINUS: state = got_decr2; break; case RPAREN: tok->terminal = t_MINUS_UNARY; ACCEPT(T_UNARY); break; default: tok->terminal = t_LPAREN; ACCEPT_SHIFT(T_LPAREN, 2); break; } break; case got_incr2: switch(char_classes[c]){ case RPAREN: tok->terminal = t_INCREMENT; ACCEPT(T_INCREMENT); break; default: tok->terminal = t_LPAREN; ACCEPT_SHIFT(T_LPAREN, 3); break; } break; case got_decr2: switch(char_classes[c]){ case RPAREN: tok->terminal = t_DECREMENT; ACCEPT(T_INCREMENT); break; default: tok->terminal = t_LPAREN; ACCEPT_SHIFT(T_LPAREN, 3); break; } break; /* numeric literals: */ //We assume that a dot means that it's a decimal case got_dot: state = got_fp_dot; break; case got_dec: switch (char_classes[c]) { case DIG: break; /* stay put */ case DOT: state = got_fp_dot; break; /*case LET_E: state = starting_exp; break;*/ default: tok->terminal = t_LITERAL; ACCEPT_REUSE(T_LITERAL); /* decimal integer */ break; } break; case got_fp_dot: switch (char_classes[c]) { case DIG: break; /* stay put */ default: tok->terminal = t_LITERAL; ACCEPT_REUSE(T_LITERAL); /* fp */ break; } break; } } }
static int do_accept(PCStr(lport),PCStr(opts),int shared,int priobase,int fromC,int toC) { int shlock,exlock; CStr(shlockpath,1024); CStr(exlockpath,1024); CStr(host,1024); int port; const char *op; CStr(opt1,128); int rcode; int clsock; int wcc; CStr(sockname,MaxHostNameLen); CStr(peername,MaxHostNameLen); int fi,fdv[32],rfv[32],nready; int svsock; int priority,timeout; int start; clsock = -1; if( SvSockN < 1 ){ SockPrintf(toC,"%s %d no socket to accept\r\n",VER,NO_ACCEPT); return -1; } /* repeat until accept succeed and authorization OK, * while the client's connection is alive ... */ shlock = -1; host[0] = 0; port = 0; if( strchr(host,':') ) Xsscanf(lport,"%[^:]:%d",AVStr(host),&port); else sscanf(lport,"%d",&port); priority = 0; timeout = 0; for( op = wordScan(opts,opt1); *opt1; op = wordScan(op,opt1) ){ if( strncmp(opt1,"-p=",3) == 0 ){ priority = atoi(opt1+3); }else if( strncmp(opt1,"-t=",3) == 0 ){ timeout = atoi(opt1+3); }else break; } if( priobase == 0 ) priority = 0; daemonlog("D","accept: priority=%d timeout=%d\n",priority,timeout); if( shared ){ const char *ltype; int rem; start = time(0L); shlock = PortLocks(lport,2,AVStr(shlockpath)); rcode = -1; for(;;){ if( priority ){ rcode = lock_exclusiveTO(shlock,1000,NULL); ltype = "exclusive"; }else{ rcode = lock_sharedTO(shlock,1000,NULL); ltype = "shared"; } if( !IsAlive(toC) ){ daemonlog("E","## disconnected during LOCKing\n"); goto EXIT; } if( rcode == 0 ) break; if( timeout <= time(0L) - start ) break; } rem = timeout - (time(0L) - start); if( rcode != 0 && rem <= 0 ){ daemonlog("E","## accept: timedout during %s LOCKing (%d)\n", ltype,timeout); goto EXIT; } daemonlog("D","## accept: %s LOCKed (%d)\n",ltype,timeout-rem); } daemonlog("D","## START accept at %d (%d ports)\n",port,SvSockN); for(;;){ RETRY: fdv[0] = fromC; for( fi = 1; fi <= SvSockN; fi++ ){ fdv[fi] = SvSocks[fi].a_sock; if( !shared && 0 < bind_nlisten ) Listen(fdv[fi],bind_nlisten); } nready = PollIns(timeout*1000,1+SvSockN,fdv,rfv); if( nready <= 0 ) break; if( rfv[0] != 0 && !IsAlive(fromC) ) { daemonlog("E","## disconnected during POLLing\n"); break; } svsock = -1; for( fi = 1; fi <= SvSockN; fi++ ){ if( rfv[fi] < 0 ) goto EXIT; if( 0 < rfv[fi] ){ svsock = fdv[fi]; break; } } if( svsock < 0 ) break; /* not lport but the real port-number of svsock ? */ exlock = PortLocks(lport,3,AVStr(exlockpath)); start = time(0L); rcode = -1; for(;;){ rcode = lock_exclusiveTO(exlock,1000,NULL); if( !IsAlive(toC) ){ daemonlog("E","## disconnected during ACCEPT LOCKing\n"); rcode = -1; break; } if( rcode == 0 ) break; if( timeout <= time(0L) - start ){ daemonlog("E","## timedout during ACCEPT LOCKing\n"); lock_unlock(exlock); goto RETRY; } } if( rcode == 0 ){ if( PollIn(svsock,1) <= 0 ){ daemonlog("E","## be snatched during ACCEPT LOCKing\n"); lock_unlock(exlock); goto RETRY; }else{ clsock = ACCEPT(svsock,1,-1,timeout); } } if( !shared ) closeSvSocks(); if( 0 <= exlock ) lock_unlock(exlock); if( 0 <= shlock ){ lock_unlock(shlock); shlock = -1; } break; } EXIT: if( !shared ) closeSvSocks(); if( 0 <= bind_exlock ){ lock_unlock(bind_exlock); bind_exlock = -1; } if( clsock < 0 ) daemonlog("D","## FAILED accept at %d\n",port); if( 0 <= shlock ) lock_unlock(shlock); return clsock; }
static void SftpGW(PCStr(host),int port,int gw){ int tofrom_sftp[2]; FILE *fc; FILE *tc; FILE *ts; FILE *fs; int rcc; CStr(req,1024); CStr(com,1024); const unsigned char *ucom = (const unsigned char*)com; CStr(arg,1024); CStr(scom,1024); CStr(user,128); CStr(passMD5,128); CStr(md5,1024); CStr(logindir,1024); CStr(rnfr,512); CStr(res,1024); CStr(xcom,1024); const char *dp; int vsock = -1; int dsock = -1; CStr(fifo,1024); CStr(sresp,0x10000); CStr(path,1024); CStr(resp1,1024); int Ok; int Bad; int xpid; int nready; int nfc; FILE *fpv[2]; int rdv[2]; int ncc = 0; int leng; FileSize datamax = DATA_MAX; IStr(opt1,128); if( getMountOpt1(MainConn(),"datamax",AVStr(opt1),sizeof(opt1)) ){ datamax = kmxatoi(opt1); sv1log("## datamax=%s 0x%llX\n",opt1,datamax); } DEBUG("--SFTPGW start\n"); fc = fdopen(gw,"r"); tc = fdopen(gw,"w"); putresp(tc,220,"SFTP/FTP gateway ready."); fflush(tc); ts = NULL; fs = NULL; logindir[0] = 0; fpv[0] = fc; nfc = 1; for(;;){ nready = fPollIns(0,nfc,fpv,rdv); if( nready == 0 && errno == EINTR ){ DEBUG("--SFTPGW ignored EINTR\n"); sleep(1); continue; } if( nready <= 0 ){ break; } if( 1 < nfc && 0 < rdv[1] ){ DEBUG("--SFTPGW EOF from server?\n"); relay_resp(fs,-1,0,AVStr(sresp),"",1); break; } fflush(tc); if( fgets(req,sizeof(req),fc) == 0 ) break; dp = wordScan(req,com); if( *dp == ' ' ){ textScan(dp+1,arg); }else lineScan(dp,arg); if( strcaseeq(com,"PASS") ) DEBUG("--SFTPGW << [%s][****]\n",com); else DEBUG("--SFTPGW << [%s][%s]\n",com,arg); if( strcaseeq(com,"QUIT") ){ putresp(tc,221,"Ok Bye."); fclose(fc); fclose(tc); fc = 0; tc = 0; if( fs == NULL ){ DEBUG("--SFTPGW DONE (not started)\n"); break; } gw = CC_accept("sftp",host,port,user,fileno(fs)); if( 0 <= gw ){ putsftp(ts,"cd .",""); relay_resp(fs,-1,0,AVStr(sresp),"restart",1); if( sresp[0] == 0 ){ sv1log("##sftp_CC NotAlive\n"); break; } fc = fdopen(gw,"r"); fpv[0] = fc; tc = fdopen(gw,"w"); ncc++; DEBUG("--SFTPGW SFTPCC restart #%d\n",ncc); putresp(tc,220,"Ok (reusing)"); continue; } DEBUG("--SFTPGW DONE\n"); break; }else if( strcaseeq(com,"NOOP") ){ putresp(tc,200,"Ok"); }else if( strcaseeq(com,"USER") ){ strcpy(user,arg); putresp(tc,331,"Send password or passphrase for '%s'", arg); }else if( strcaseeq(com,"PASS") ){ if( ts != NULL ){ toMD5(arg,md5); DEBUG("--SFTP reusing user[%s]pass[%s][%s]\n", user,passMD5,md5); if( !streq(md5,passMD5) ){ putresp(tc,530,"No"); continue; }else putresp(tc,230,"Ok"); putsftp(ts,"cd %s",logindir); relay_resp(fs,-1,0,AVStr(sresp),"restart",1); continue; } /* if( forkSftp(host,port,user,arg,tofrom_sftp) != 0 ){ */ if( forkSftp(host,port,user,arg,tofrom_sftp,AVStr(sresp)) != 0 ){ IStr(prompt,1024); lineScan(sresp,prompt); DEBUG("--SFTP login failed\n"); /* putresp(tc,530,"No (Login failed)"); */ putresp(tc,530,"No (Login failed:'%s')",prompt); if( lGATEWAY() ) sv1log("####@@@@ LOGIN FAILURE\n"); continue; /* to return normal resp. for following commands... */ break; } toMD5(arg,passMD5); fs = fdopen(tofrom_sftp[0],"r"); ts = fdopen(tofrom_sftp[1],"w"); fpv[1] = fs; nfc = 2; putresp(tc,230,"Ok"); putsftp(ts,"pwd"); relay_resp(fs,-1,0,AVStr(sresp),com,1); /* if( strneq(sresp,"Remote working directory: ",26) ){ CStr(path,1024); lineScan(sresp+26,path); */ if( scanresp(sresp,"Remote working directory:", AVStr(path),sizeof(path)) ){ strcpy(logindir,path); } }else if( strcaseeq(com,"TYPE") ){ putresp(tc,200,"Ok"); }else if( strcaseeq(com,"PASV") ){ PASV(tc,&vsock); #if 0 VSAddr ba; int balen; CStr(mport,128); vsock = server_open("SftpGW",VStrNULL,0,1); if( 0 <= vsock ){ balen = sizeof(ba); getsockname(vsock,(SAP)&ba,&balen); #if defined(__CYGWIN__) if( VSA_addrisANY(&ba) ){ /*connection to 0.0.0.0 fails on CYGWIN?*/ VSA_atosa(&ba,VSA_port(&ba),"127.0.0.1"); } #endif VSA_prftp(&ba,AVStr(mport)); putresp(tc,227,"Enterning Passive Mode (%s)",mport); }else{ putresp(tc,500,"No"); } #endif }else if( ts == NULL ){ sv1log("Not Logged In: %s",req); putresp(tc,530,"Please login with USER and PASS."); }else if( strcaseeq(com,"PWD") ){ putsftp(ts,"pwd"); relay_resp(fs,-1,0,AVStr(sresp),com,1); /* if( strneq(sresp,"Remote working directory: ",26) ){ CStr(path,1024); lineScan(sresp+26,path); */ if( scanresp(sresp,"Remote working directory:", AVStr(path),sizeof(path)) ){ putresp(tc,257,"\"%s\"",path); }else{ putresp(tc,257,"\"/\""); } }else if( strcaseeq(com,"CWD") ){ putsftp(ts,"cd %s",arg); relay_resp(fs,-1,0,AVStr(sresp),com,1); /* if( strneq(sresp,"Couldn't",8) || strneq(sresp,"Can't",4) ){ */ if( linehead(sresp,"Couldn't",0) || linehead(sresp,"Can't",0) ){ if( strstr(sresp,BadPath) ) putresp(tc,550,BadPath); else putresp(tc,550,"No"); }else putresp(tc,250,"Ok"); }else /* if( strcaseeq(com,"SIZE") ){ putsftp(ts,"ls -ld %s",arg); relay_resp(fs,-1,0,AVStr(sresp),com,1); if( *sresp == '-' ){ int sz = 0; sscanf(sresp,"%*s %*s %*s %*s %d",&sz); putresp(tc,213,"%d",sz); }else{ putresp(tc,550,"Not a plain file"); } }else */ if( strcaseeq(com,"STOR") ){ if( vsock < 0 ){ putresp(tc,500,"No"); continue; } dsock = ACCEPT(vsock,1,-1,10); if( 0 <= dsock ){ CStr(tmpf,1024); FILE *dfp; FILE *tmp; /* int bytes; */ FileSize Bytes; int isbin; double St; dfp = fdopen(dsock,"r"); if( dfp == NULL ){ putresp(tc,500,"No-1"); continue; } tmp = getfile(AVStr(tmpf),"w",0); if( tmp == NULL ){ putresp(tc,500,"No-2"); fclose(dfp); continue; } putresp(tc,150,"Ok"); St = Time(); /* bytes = file_copy(dfp,tmp,NULL,DATA_MAX,&isbin); DEBUG("--SFTPGW bin=%d,%d bytes upload\n", isbin,bytes); */ Bytes = file_copy(dfp,tmp,NULL,datamax,&isbin); DEBUG("--SFTPGW bin=%d,%lld bytes upload\n", isbin,Bytes); fclose(tmp); fclose(dfp); DEBUG("--SFTPGW put %s (%lld %lld)\n",tmpf, Bytes,File_sizeX(tmpf)); putsftp(ts,"put %s %s",tmpf,arg); /* relay_resp(fs,-1,15*1000,AVStr(sresp),com,1); */ relay_resp(fs,-1,5*1000,AVStr(sresp),com,1); if( strneq(sresp,"Uploading ",10) && strstr(sresp,"sftp>") == 0 ){ waitUploaded(com,AVStr(sresp),fs,fc,tc, Time()-St,&vsock); }else if( strncmp(sresp,"Could",5) == 0 ) putresp(tc,550,"No"); else putresp(tc,226,"Ok"); unlink(tmpf); dsock = -1; }else{ putresp(tc,500,"accept error"); } vsock = -1; }else if( strcaseeq(com,"RETR") ){ if( vsock < 0 ){ putresp(tc,500,"No"); continue; } dsock = ACCEPT(vsock,1,-1,10); if( arg[0]==0 ||streq(arg,".") ||strtailstr(arg,"/") ){ putresp(tc,550,"RETR for dir [%s]",arg); if( 0 <= dsock ){ close(dsock); dsock = -1; } }else if( 0 <= dsock ){ CStr(rpath,1024); FILE *rfp; int pid; truncVStr(rpath); getfile(AVStr(fifo),NULL,1); if( SFTP_WITH_SEEK ){ /* newer sftp with output seeking */ sprintf(rpath,"%sr",fifo); rfp = fopen(rpath,"w+"); if( rfp ){ pid = Fork("SFTP-DATA"); if( pid == 0 ){ /* relayFile2Fifo(rfp,rpath,fifo); */ relayFile2Fifo(fs,rfp,rpath,fifo); _exit(0); } else{ fclose(rfp); /* 9.9.8 */ } } putsftp(ts,"get %s %s",arg,rpath); }else{ putsftp(ts,"get %s %s",arg,fifo); } /* if( relay_fifo(fs,tc,fifo,dsock,com)==0 ) */ leng = relay_fifo(fs,tc,fifo,dsock,com,AVStr(sresp)); if( leng <= 0 ){ sv1log("RETR ERROR: %s\n",sresp); } if( leng == 0 && strstr(sresp,BadPath) ) putresp(tc,550,BadPath); else if( 0 < leng ) putresp(tc,226,"Ok"); else putresp(tc,550,"No"); relay_resp(fs,-1,1,AVStr(sresp),com,1); if( rpath[0] ){ unlink(rpath); xpid = NoHangWait(); DEBUG("--SFTP-DATA finished, pid=%d\n", xpid); } close(dsock); dsock = -1; unlink(fifo); }else{ putresp(tc,500,"accept error"); } if( 0 <= vsock ){ close(vsock); /* 9.9.8 */ } vsock = -1; }else if( strcaseeq(com,"NLST") || strcaseeq(com,"LIST") ){ if( vsock < 0 ){ putresp(tc,500,"No"); continue; } dsock = ACCEPT(vsock,1,-1,10); if( 0 <= dsock ){ putresp(tc,150,"Ok"); if( arg[0] == '-' ){ char *op; for( op = arg+1; *op; ){ if( strchr(" \t\r\n",*op) ) break; if( strchr("L",*op) ){ ovstrcpy(op,op+1); }else op++; } } /* if( streq(com,"NLST") || strstr(arg,"-l") ) putsftp(ts,"ls %s",arg); else putsftp(ts,"ls -l %s",arg); relay_resp(fs,dsock,0,VStrNULL,com,0); */ if( streq(com,"NLST") || strstr(arg,"-l") ) sprintf(scom,"ls %s",arg); else sprintf(scom,"ls -l %s",arg); putsftp(ts,"%s",scom); relay_list(fs,dsock,scom); close(dsock); dsock = -1; putresp(tc,226,"Ok"); }else{ putresp(tc,500,"accept error"); } vsock = -1; }else if( ucom[0]==0xFF && ucom[1]==0xF4 /* IAC+IP */ && ucom[2]==0xF2 /* SYNC */ && strcaseeq(com+3,"ABOR") ){ sv1log("--SFTPGW ABOR\n"); }else if( strcaseeq(com,"RNFR") ){ strcpy(rnfr,arg); putresp(tc,350,"Ok"); }else if( strcaseeq(com,"RNTO") ){ putsftp(ts,"rename %s %s",rnfr,arg); relay_resp(fs,-1,0,AVStr(sresp),com,1); if( dp = strstr(sresp,"Couldn't") ){ lineScan(dp,resp1); putresp(tc,550,"No %s",resp1); }else putresp(tc,250,"Ok"); }else if( strcaseeq(com,"MKD") || strcaseeq(com,"RMD") || strcaseeq(com,"DELE") ){ Bad = 550; if( strcaseeq(com,"MKD") ){ Ok = 257; putsftp(ts,"mkdir %s",arg); }else if( strcaseeq(com,"RMD") ){ Ok = 250; putsftp(ts,"rmdir %s",arg); }else{ Ok = 250; putsftp(ts,"rm %s",arg); } relay_resp(fs,-1,0,AVStr(sresp),com,1); if( dp = strstr(sresp,"Couldn't") ){ lineScan(dp,resp1); putresp(tc,Bad,"No %s",resp1); }else putresp(tc,Ok,"Ok"); }else { putresp(tc,502,"No not supported"); } } if( fs != NULL && !feof(fs) ){ putsftp(ts,"quit"); relay_resp(fs,-1,0,AVStr(sresp),com,1); } xpid = NoHangWait(); DEBUG("--SFTPGW finished, pid=%d\n",xpid); }
void NodeVisitor::visitLabeledStatement(const LabeledStatementPtr& node) { ACCEPT(node->getStatement()); }
void NodeVisitor::visitDoLoop(const DoLoopPtr& node) { ACCEPT(node->getCodeBlock()); ACCEPT(node->getCondition()); }