//------------------------------------------------------------------------ void TcpServer::onReceive(Header hdr, Cbor& cbor) { PT_BEGIN() ; WIFI_DISCONNECT: { while (true) { PT_YIELD(); if (hdr.is(self(), left(), REPLY(CONNECT), 0)) { LOGF("TcpServer started. %x", this); listen(); goto CONNECTING; } } } CONNECTING: { while (true) { setReceiveTimeout(2000); PT_YIELD(); if (hdr.is(self(), left(), REPLY(CONNECT), 0)) { listTcp(); goto WIFI_DISCONNECT; } } } PT_END() }
void TcpServer::onReceive(Header hdr, Cbor& cbor) { if (hdr.is(INIT)) { init(); return; } else if (hdr.is(_wifi, REPLY(CONNECT))) { // LOGF("TcpServer started. %x", this); listen(); state(TCP_SERVER_LISTEN); } else if (hdr.is(_wifi, REPLY(DISCONNECT))) { // LOGF("TcpServer stopped. %x", this); state(TCP_SERVER_DISCONNECTED); } }
void TcpClient::onReceive(Header hdr, Cbor& cbor) { switch(state()) { case S_INIT: { if ( hdr.is(INIT) ) { state(S_WIFI_DISCONNECTED); } break; } case S_WIFI_DISCONNECTED :{ if (hdr.is(self(), left(), REPLY(CONNECT), 0)) { connect(); state(S_TCP_DISCONNECTED); } break; } case S_TCP_DISCONNECTED :{ if (hdr.is(self(), self(), REPLY(CONNECT),0)) { // TCP connect established right().tell(self(), REPLY(CONNECT), 0); state(S_TCP_CONNECTED); } else if (hdr.is(self(), left(), REPLY(DISCONNECT), 0)) { // WIFI disconnected state(S_WIFI_DISCONNECTED); }; break; } case S_TCP_CONNECTED :{ if (hdr.is(right(),TXD)) { Bytes data(256); cbor.get(data); write(data); } else if (hdr.is(right(),DISCONNECT)) { // listener demands disconnect disconnect(); state(S_RECONNECT_WAIT); setReceiveTimeout(2000); } else if (hdr.is(left(),REPLY(DISCONNECT))) { // WIFI disconnected state(S_WIFI_DISCONNECTED); } break; } case S_RECONNECT_WAIT:{ if ( hdr.is(TIMEOUT)) { connect(); state(S_TCP_DISCONNECTED); } break; } } return; }
static void forkcmd(char *cmd, char *p, int reqfd, int datafd, int want_reply) { char *q; switch (fork()) { case 0: if (restdir && chdir(restdir) < 0) { syslog(0, "ssh", "can't chdir(%s): %r", restdir); exits("can't chdir"); } if (!prevent || (q = getenv("sshsession")) && strcmp(q, "allow") == 0) get_string(p+1, cmd); else confine(p+1, cmd); syslog(0, "ssh", "server running `%s' for %s", cmd, uname); /* runcmd doesn't return */ runcmd(reqfd, datafd, "rx", "/bin/rc", "-lc", cmd); case -1: REPLY("failure"); syslog(0, "ssh", "server can't fork: %r"); exits("fork"); } }
//____________________________________________________________ // // //____________________________________________________________ // void Tcp::onReceive(Header hdr, Cbor& cbor) { PT_BEGIN() ; WIFI_DISCONNECTED: { while (true) { PT_YIELD_UNTIL(hdr.is(left(), self(), REPLY(CONNECT), ANY)); // LOGF("Tcp started. %x", this); goto CONNECTING; } } CONNECTING: { LOGF("CONNECTING"); while (true) { PT_YIELD(); if (hdr.is(self(), self(), REPLY(CONNECT), 0)) { right().tell(self(), REPLY(CONNECT), 0); _state=READY; goto CONNECTED; } else if (hdr.is(self(), self(), REPLY(DISCONNECT), 0)) { right().tell(self(), REPLY(DISCONNECT), 0); goto WIFI_DISCONNECTED; } } } CONNECTED: { LOGF("CONNECTED"); while (true) { setReceiveTimeout(5000); PT_YIELD(); if (hdr.is(self(), REPLY(DISCONNECT))) { // tcp link gone, callback was called right().tell(self(), REPLY(DISCONNECT), 0); goto CONNECTING; } else if (hdr.is(left(), REPLY(DISCONNECT))) { // wifi link gone right().tell(self(), REPLY(DISCONNECT), 0); goto WIFI_DISCONNECTED; } else if (hdr.is(right(), DISCONNECT)) { // wifi link gone disconnect(); goto CONNECTING; } else if (timeout()) { LOGF("%d:%d %d", _lastRxd + 5000, Sys::millis()); if ((_lastRxd + 5000) < Sys::millis()) { LOGF(" timeout - disconnect %X:%X", this, _conn); disconnect(); } } } } ; PT_END() }
void Tcp::sendCb(void *arg) { sends_cb++; struct espconn* pconn = (struct espconn*) arg; Tcp *pTcp = findTcp(pconn); if (pTcp) { pTcp->_state = READY; pTcp->send(); pTcp->listener().tell(pTcp->self(), REPLY(TXD), 0); } else { LOGF("Tcp not found"); } return; }
void Tcp::disconnectCb(void* arg) { struct espconn* pconn = (struct espconn*) arg; Tcp *pTcp = findTcp(pconn); if (pTcp) { pTcp->_connected = false; if (pTcp->getType() == LIVE) { pTcp->loadEspconn(0); } pTcp->self().tell(pTcp->self(), REPLY(DISCONNECT), 0); } else LOGF("connection not found"); return; }
void Tcp::reconnectCb(void* arg, int8_t err) { struct espconn* pconn = (struct espconn*) arg; Tcp *pTcp = findTcp(pconn); if ( pTcp) { pTcp->_state = READY; pTcp->_buffer.clear(); pTcp->_txd.clear(); LOGF("TCP: Reconnect %s:%d err : %d", pTcp->_host, pTcp->_remote_port, err); pTcp->right().tell(pTcp->self(), REPLY(DISCONNECT), 0); pTcp->_connected = false; }else LOGF("connection not found"); }
void LineFramer::onReceive(Header hdr, Cbor& cbor) { if (hdr.is(INIT)) { init(); } else if (hdr.is(_link, REPLY(CONNECT))) { _upStream.tell(hdr); } else if (hdr.is(_link, REPLY(DISCONNECT))) { _upStream.tell(hdr); } else if (hdr.is(_link, RXD)) { LOGF("RXD %d",cbor.length()); Bytes bytes(0); cbor.getMapped(bytes); bytes.offset(0); while (bytes.hasData()) { LOGF("%s",_input->c_str()); char ch = bytes.read(); if (ch == '\n') { _cborOut.putf("B", (Bytes*) _input); _upStream.tell(hdr, _cborOut); _input->clear(); } else { _input->append(ch); } } } else if (hdr.is(_upStream, TXD)) { Bytes bytes(0); cbor.getMapped(bytes); bytes.offset(0); if (bytes.hasData()) { _output->append((Str&) bytes); _output->append('\n'); _cborOut.putf("B", (Bytes*) _output); _link.tell(hdr, _cborOut); } _output->clear(); } }
void Tcp::connectCb(void* arg) { // LOGF(" tcp connection received"); struct espconn* pconn = (struct espconn*) arg; Tcp* pTcp = findTcp(pconn); // if found it's TcpClient if (pTcp == 0) { pTcp = findFreeTcp(pconn); } if (pTcp) { pTcp->loadEspconn(pconn); pTcp->registerCb(pconn); pTcp->_state = READY; /* uint32_t keeplive; espconn_set_opt(pesp_conn, ESPCONN_KEEPALIVE); // enable TCP keep alive //set keepalive: 75s = 60 + 5*3 keeplive = 60; espconn_set_keepalive(pesp_conn, ESPCONN_KEEPIDLE, &keeplive); keeplive = 5; espconn_set_keepalive(pesp_conn, ESPCONN_KEEPINTVL, &keeplive); keeplive = 3; //try times espconn_set_keepalive(pesp_conn, ESPCONN_KEEPCNT, &keeplive); */ // espconn_set_opt(pconn, ESPCONN_NODELAY); // espconn_set_opt(pconn, ESPCONN_COPY); // espconn_set_keepalive(pconn, ESPCONN_KEEPALIVE, (void*) 1); // espconn_set_keepalive(pconn, ESPCONN_KEEPIDLE, (void*) 1); // espconn_regist_time(pconn, 100, 0); pTcp->listener().tell(pTcp->self(), REPLY(CONNECT), 0); pTcp->_buffer.clear(); pTcp->_txd.clear(); pTcp->_connected = true; pTcp->_lastRxd = Sys::millis(); pTcp->_state = READY; } else { LOGF(" no free TCP : disconnecting %X ", pconn); LOGF(" tcp : %x , ip : %d.%d.%d.%d:%d ", pconn->reverse, pconn->proto.tcp->remote_ip[0], pconn->proto.tcp->remote_ip[1], pconn->proto.tcp->remote_ip[2], pconn->proto.tcp->remote_ip[3], pconn->proto.tcp->remote_port); espconn_disconnect(pconn); } return; }
static void forkshell(char *cmd, int reqfd, int datafd, int want_reply) { switch (fork()) { case 0: if (sflag) snprint(cmd, sizeof cmd, "-s%s", shell); else cmd[0] = '\0'; USED(cmd); syslog(0, "ssh", "server starting ssh shell for %s", uname); /* runcmd doesn't return */ runcmd(reqfd, datafd, "con", "/bin/ip/telnetd", "-nt", nil); case -1: REPLY("failure"); syslog(0, "ssh", "server can't fork: %r"); exits("fork"); } }
PUBLIC int SpExit() { BUFFER_DECLARATIONS; long Status; DEBUG(( "SP.EXIT" )); INIT_BUFFERS; GET_INT32( Status ); DEBUG(( "%ld", Status )); if( Status == 999999999 ) Status = TERMINATE_OK_EXIT; else if( Status == -999999999 ) Status = TERMINATE_FAIL_EXIT; else Status = TERMINATE_OTHER_STATUS; DEBUG(( "exit with %d", (int)Status )); PUT_BYTE( SP_SUCCESS ); REPLY ( (int)Status ); }
void TF20::write_signal(int id, uint32 data, uint32 mask) { switch(phase) { case PHASE_IDLE: if(data == EOT) { break; } if(data == ENQ) { REPLY(ACK); break; } if(data != DID_FIRST) { REPLY(NAK); break; } phase = PHASE_SELECT; buflen = 0; break; case PHASE_SELECT: bufr[buflen++] = data; if(buflen < 3) { break; } if(bufr[0] != DID_FIRST && bufr[0] != DID_SECOND) { phase = PHASE_IDLE; break; } if(bufr[2] != DS_SEL) { REPLY(NAK); phase = PHASE_IDLE; break; } REPLY(ACK); phase = PHASE_FUNC; break; case PHASE_FUNC: if(data == EOT) { phase = PHASE_IDLE; break; } if(data == ENQ) { REPLY(ACK); break; } if(data != SOH) { REPLY(NAK); phase = PHASE_IDLE; break; } phase = PHASE_HEAD; buflen = 0; break; case PHASE_HEAD: bufr[buflen++] = data; if(buflen < 6) { break; } REPLY(ACK); phase = PHASE_DATA; break; case PHASE_DATA: bufr[buflen++] = data; if(buflen < (6 + bufr[4] + 4)) { break; } if(bufr[6] != STX) { REPLY(NAK); phase = PHASE_IDLE; break; } REPLY(ACK); phase = PHASE_EXEC; break; case PHASE_EXEC: if(data != EOT) { REPLY(NAK); phase = PHASE_IDLE; break; } if(!process_cmd()) { REPLY(NAK); phase = PHASE_IDLE; break; } for(int i = 0; i < 7; i++) { REPLY(bufs[i]); } phase = PHASE_RESULT; break; case PHASE_RESULT: if(data == ENQ) { REPLY(ACK); phase = PHASE_FUNC; break; } if(data != ACK) { phase = PHASE_FUNC; break; } for(int i = 7; i < buflen; i++) { REPLY(bufs[i]); } phase = PHASE_END; break; case PHASE_END: if(data == ENQ) { REPLY(ACK); } else if(data == ACK) { REPLY(EOT); } phase = PHASE_FUNC; break; } }
void newchannel(int fd, char *conndir, int channum) { char *p, *reqfile, *datafile; int n, reqfd, datafd, want_reply, already_done; char buf[Maxpayload], cmd[Bigbufsz]; close(fd); already_done = 0; reqfile = smprint("%s/%d/request", conndir, channum); if (reqfile == nil) sysfatal("out of memory"); reqfd = open(reqfile, ORDWR); if (reqfd < 0) { syslog(0, "ssh", "can't open request file %s: %r", reqfile); exits("net"); } datafile = smprint("%s/%d/data", conndir, channum); if (datafile == nil) sysfatal("out of memory"); datafd = open(datafile, ORDWR); if (datafd < 0) { syslog(0, "ssh", "can't open data file %s: %r", datafile); exits("net"); } while ((n = read(reqfd, buf, sizeof buf - 1)) > 0) { fprint(errfd, "read from request file returned %d\n", n); for (p = buf; p < buf + n && *p != ' '; ++p) ; *p++ = '\0'; want_reply = (*p == 't'); /* shell, exec, and various flavours of failure */ if (strcmp(buf, "shell") == 0) { if (already_done) { REPLY("failure"); continue; } forkshell(cmd, reqfd, datafd, want_reply); already_done = 1; REPLY("success"); } else if (strcmp(buf, "exec") == 0) { if (already_done) { REPLY("failure"); continue; } forkcmd(cmd, p, reqfd, datafd, want_reply); already_done = 1; REPLY("success"); } else if (strcmp(buf, "pty-req") == 0 || strcmp(buf, "window-change") == 0) { REPLY("success"); } else if (strcmp(buf, "x11-req") == 0 || strcmp(buf, "env") == 0 || strcmp(buf, "subsystem") == 0) { REPLY("failure"); } else if (strcmp(buf, "xon-xoff") == 0 || strcmp(buf, "signal") == 0 || strcmp(buf, "exit-status") == 0 || strcmp(buf, "exit-signal") == 0) { ; } else syslog(0, "ssh", "server unknown channel request: %s", buf); } if (n < 0) syslog(0, "ssh", "server read failed: %r"); exits(nil); }