📄 ppp.java
字号:
if (reconnectRequest) {// System.out.print("3"); modemHangUp(); // start over modemInit(); } if (disconnectRequest) { modemHangUp(); // stop the connection } if (state==CONNECTED) { // send waiting ip packets if (scnt==0) { // transmit buffer is free globTimer = Timer.getTimeoutMs(IP_SEND); // use IP timeout // // get a ready to send packet with source from this driver. // Packet p = Packet.getPacket(single, Packet.SND, Packet.ALLOC); if (p!=null) { sendIp(p); // send one packet } } else { // check sendTimer; if (Timer.timeout(globTimer)) {// System.out.print("4"); modemHangUp(); // start over modemInit(); } } } else { // do the negotiation stuff dropIp(); if (Timer.timeout(globTimer)) {/*Dbg.intVal(state);if (lcpAck) Dbg.wr('t'); else Dbg.wr('f');*/ if (scnt==0) { // once every three seconds send a REQ if (state == MODEM_OK) { makeLCP(); state = LCP_SENT; } else if (state == LCP_OK && lcpAck) { makePAP(); state = PAP_SENT;// } else if (state == PAP_OK && ipcpAck) { // wait for remote ipcp and ACK first on Linux } else if (state>=PAP_OK && state<CONNECTED) { // ONE makeIPCP(); state = IPCP_SENT; ++rejCnt; // incremenet counter to start over when no respond } globTimer = Timer.getTimeoutMs(NEG_SEND); // use negotiation timeout } } } } /** * drop waiting packet to prevent packet buffer overrun */ void dropIp() { Packet p = Packet.getPacket(single, Packet.SND, Packet.ALLOC); if (p!=null) { p.setStatus(Packet.FREE); // mark packet free } }void dbgCon() {if (state!=CONNECTED) {Dbg.wr('>');for (int i=0; i<cnt; ++i) { Dbg.byteVal(rbuf[i]); if ((i&0x0f) ==0) rth.waitForNextPeriod();}Dbg.wr('\n');}}void dbgIp(int ip) {Dbg.wr('I');Dbg.wr(' ');Dbg.intVal(ip>>>24);Dbg.intVal((ip>>>16)&0xff);Dbg.intVal((ip>>>8)&0xff);Dbg.intVal(ip&0xff);Dbg.wr('\n');} /** * generate a PPP (negotiation) request. */ void makeLCP() { lcpId = 0x22;Dbg.wr('L');Dbg.intVal(lcpId);Dbg.wr('\n'); sbuf[0] = 0xff; sbuf[1] = 0x03; // REQ LCP options 2, 7, 8 sbuf[2] = LCP>>8; sbuf[3] = LCP&0xff; sbuf[4] = REQ; sbuf[5] = lcpId; sbuf[6] = 0; sbuf[7] = 18-4; // length including code, id and length field sbuf[8] = 0x02; // async-map sbuf[9] = 0x06; sbuf[10] = 0x00; sbuf[11] = 0x0a;// sbuf[11] = 0x00; // one does not like this sbuf[12] = 0x00; sbuf[13] = 0x00; sbuf[14] = 0x07; // protocol field compression sbuf[15] = 0x02; sbuf[16] = 0x08; // addr., contr. field compression sbuf[17] = 0x02; checksum(18); } void makePAP() { int i; lcpId = 0x33;Dbg.wr('P');Dbg.intVal(lcpId);Dbg.wr('\n');/* compression sbuf[0] = 0xff; sbuf[1] = 0x03;*/ sbuf[0] = PAP>>8; sbuf[1] = PAP&0xff; sbuf[2] = REQ; sbuf[3] = lcpId; sbuf[4] = 0; int ulen = uid.length(); int plen = pwd.length(); sbuf[5] = ulen + plen + 6; // length including code, id and length field sbuf[6] = ulen; // length of user id for (i=0; i<ulen; ++i) { sbuf[7+i] = uid.charAt(i); } sbuf[7+ulen] = plen; for (i=0; i<plen; ++i) { sbuf[8+ulen+i] = pwd.charAt(i); } checksum(ulen + plen + 8);/* A1.net sbuf[5] = 24-2; // length including code, id and length field sbuf[6] = 13; // length of user id sbuf[7] = 'p'; sbuf[8] = 'p'; sbuf[9] = 'p'; sbuf[10] = '@'; sbuf[11] = 'A'; sbuf[12] = '1'; sbuf[13] = 'p'; sbuf[14] = 'l'; sbuf[15] = 'u'; sbuf[16] = 's'; sbuf[17] = '.'; sbuf[18] = 'a'; sbuf[19] = 't'; sbuf[20] = 3; // length of password sbuf[21] = 'p'; sbuf[22] = 'p'; sbuf[23] = 'p'; checksum(24);*//* OEBB// Z252245// EN$Pf1dV sbuf[5] = 22-2; // length including code, id and length field sbuf[6] = 7; // length of user id sbuf[7] = 'z'; sbuf[8] = '2'; sbuf[9] = '5'; sbuf[10] = '2'; sbuf[11] = '2'; sbuf[12] = '4'; sbuf[13] = '5'; sbuf[14] = 7; // length of password sbuf[15] = 'S'; sbuf[16] = 't'; sbuf[17] = 'T'; sbuf[18] = 'f'; sbuf[19] = 'D'; sbuf[20] = 'g'; sbuf[21] = '$'; checksum(22);*//* ONE sbuf[7] = 30-4; // length including code, id and length field sbuf[8] = 14; // length of user id sbuf[9] = '+'; sbuf[10] = '4'; sbuf[11] = '3'; sbuf[12] = '6'; sbuf[13] = '9'; sbuf[14] = '9'; sbuf[15] = '1'; sbuf[16] = '9'; sbuf[17] = '5'; sbuf[18] = '2'; sbuf[19] = '0'; sbuf[20] = '2'; sbuf[21] = '2'; sbuf[22] = '0'; sbuf[23] = 6; // length of password sbuf[24] = 'N'; sbuf[25] = '6'; sbuf[26] = 'J'; sbuf[27] = '8'; sbuf[28] = 'N'; sbuf[29] = '4'; checksum(30);*/ } void makeIPCP() { lcpId = 0x44;Dbg.wr('I');Dbg.intVal(lcpId);Dbg.wr('\n');/* compression sbuf[0] = 0xff; sbuf[1] = 0x03;*/ sbuf[0] = IPCP>>8; sbuf[1] = IPCP&0xff; sbuf[2] = REQ; sbuf[3] = lcpId; sbuf[4] = 0; sbuf[5] = 14-4; // length including code, id and length field sbuf[6] = 0x03; // ip-address 0.0.0.0 sbuf[7] = 0x06; sbuf[8] = ip>>>24; sbuf[9] = (ip>>16)&0xff; sbuf[10] = (ip>>8)&0xff; sbuf[11] = ip&0xff; // checksum(14); checksum(12); } /** * process a LCP, IPCP request */ boolean checkOptions(int type) { int i; int len = (rbuf[6]<<8) + rbuf[7] - 4; // including code, id and lentgh int ptr = 8;Dbg.wr('R');Dbg.wr(' '); int resp = ACK; for (i=0; i<cnt; ++i) sbuf[i] = rbuf[i]; // assume ACK int slen = len + 4; while (len > 0) { int opt = rbuf[ptr];Dbg.intVal(opt); if (type==LCP && opt==3) { // auth. protocol if ((rbuf[ptr+2]<<8) + rbuf[ptr+3] != PAP) { resp = REJ;Dbg.wr('!');Dbg.wr('P');Dbg.wr(' '); } } else if (type==IPCP) { if (opt==2) { // IP-Compression resp = REJ; } else if (opt==3) { // IP-address ipRemote = (rbuf[ptr+2]<<24) + (rbuf[ptr+3]<<16) + (rbuf[ptr+4]<<8) + rbuf[ptr+5];Dbg.hexVal(ipRemote);dbgIp(ipRemote); } } // } else if (opt==xx} if (resp==REJ) { int optlen = rbuf[ptr+1]; slen = 4 + optlen; for (i=0; i<optlen; ++i) { sbuf[8+i] = rbuf[ptr+i]; } break; // end check options } i = rbuf[ptr+1]; // check next option ptr += i; len -= i; } sbuf[4] = resp; sbuf[6] = slen>>>8; sbuf[7] = slen&0xff;Dbg.wr('\n'); checksum(slen+4); return resp == ACK; }/*** get a Packet buffer and copy from receive buffer.*/ void readIp() { int i, j, k; Packet p = Packet.getPacket(Packet.FREE, Packet.ALLOC, single); if (p==null) {Dbg.wr('!'); return; // try again later } // buf blocks receive buffer :-< int[] pb = p.buf; cnt -= 6; // minus ppp header and checksum rbuf[cnt+4] = 0; rbuf[cnt+4+1] = 0; rbuf[cnt+4+2] = 0; // copy buffer k = 0; for (i=0; i<cnt; i+=4) { for (j=0; j<4; ++j) { k <<= 8; k += rbuf[i+j+4]; // after header } pb[i>>>2] = k; } p.len = cnt;Dbg.wr('r');Dbg.intVal(cnt);/*dbgIp(pb[3]);dbgIp(pb[4]);for (i=0; i<(cnt+4)>>2; ++i) Dbg.hexVal(pb[i]);Dbg.wr('\n');*/ cnt = 0; ready = false; p.setStatus(Packet.RCV); // inform upper layer }/*** copy packet to send buffer.*/ void sendIp(Packet p) { int i, k; int[] pb = p.buf;Dbg.wr('s');Dbg.intVal(p.len); sbuf[0] = 0xff; sbuf[1] = 0x03; sbuf[2] = IP>>8; sbuf[3] = IP&0xff; int slen = p.len; sent = 0; for (i=0; i<slen; i+=4) { k = pb[i>>>2]; sbuf[i+4] = k>>>24; sbuf[i+4+1] = (k>>>16)&0xff; sbuf[i+4+2] = (k>>>8)&0xff; sbuf[i+4+3] = k&0xff; } p.setStatus(Packet.FREE); // mark packet free checksum(slen+4); }/* warum geht das nicht !!!!! private void loop() {*//*** read from serial buffer and build a ppp packet.* send a packet if one is in our send buffer.*/ boolean pppLoop() { int i; boolean ret = false; i = ser.rxCnt(); if (i!=0 && !ready) { ret = true; rcv(i); } if (scnt!=0) { i = ser.txFreeCnt(); if (i>2) { snd(i); } } return ret; }/*** copy from send buffer to serial buffer with flags and escapes.*/ void snd(int free) { int i; if (sent==0) { ser.wr('~'); --free; } for (i=sent; free>1 && i<scnt; ++i) { int c = sbuf[i];/* no hard codeif (state >= LCP_OK) { // hard code async map if (c=='~' || c=='}' || c==17 || c==19) { // 0x000a0000 async map ser.wr('}'); ser.wr(c ^ ' '); free -= 2; } else { ser.wr(c); --free; }} else {*/ if (c=='~' || c=='}' || c<0x20) { // c<0x20 could be omitted after LCP async map ser.wr('}'); ser.wr(c ^ ' '); free -= 2; } else { ser.wr(c); --free; }/*}*/ } sent = i; if (sent==scnt && free!=0) { ser.wr('~'); scnt = 0; sent = 0; } } private static boolean escape; private static int fcs;/*** copy from serial buffer to receive buffer.* calc CRC on the fly.*/ void rcv(int len) { int i; if (cnt==0) fcs = 0xffff; // get all bytes from serial buffer for (i=0; i<len && cnt<MAX_BUF; ++i) { int val = ser.rd(); if (cnt==0 && !flag && val!='~') { // wait for a packet start escape = false; // first data byte is not an escapeDbg.wr('d'); continue; // so don't worry about '~' escapes on cnt==0 } if (!escape && val=='~') { flag = true; // remember flag, because end flag and if (cnt!=0) { // starting flag can be the same (see rfc1549) if (fcs==0xf0b8) { // checksum ok? ready = true; } else {Dbg.wr('d');Dbg.intVal(cnt);Dbg.wr('\n'); cnt = 0; // just drop it } break; } continue; } else { flag = false; } if (!escape && val=='}') { escape = true; continue; } if (escape) { val ^= ' '; escape = false; } // rfc1549 3.2 Address-and-Control-Field-Compression if (cnt==0 && val!=0xff) rbuf[cnt++] = 0xff; if (cnt==1 && val!=0x03) rbuf[cnt++] = 0x03; // rfc1548 6.6 Protocol-Field-Compression if (cnt==2 && (val&1)!=0) rbuf[cnt++] = 0x00; rbuf[cnt++] = val; fcs = check(val^fcs) ^ (fcs>>8); }/*Dbg.wr('r');Dbg.intVal(cnt);Dbg.wr('\n');*/ }/*** calculate CRC of byte c for checksum*/ int check(int c) { c &= 0xff; for (int i=0; i<8; ++i) { if ((c&1) != 0) { c >>= 1; c ^= 0x8408; } else { c >>= 1; } } return c; }/*** calculate CRC for send packet and mark it ready to send.*/ void checksum(int len) { int k, j, i; int fcs = 0xffff; for (i=0; i<len; ++i) { j = sbuf[i]; j ^= fcs & 0xff; // only low order byte for (k=0; k<8; ++k) { if ((j&1) != 0) { j >>= 1; j ^= 0x8408; } else { j >>= 1; } } fcs = j ^ (fcs>>8); } fcs = fcs ^ 0xffff; sbuf[len] = fcs & 0xff; // LSB first ! sbuf[len+1] = fcs >> 8; scnt = len+2;if (state!=CONNECTED) {Dbg.wr('<');for (i=0; i<scnt; ++i) { Dbg.byteVal(sbuf[i]); if ((i&0x0f) ==0) rth.waitForNextPeriod();}Dbg.wr('\n');} } /** * @return */ public int getConnCount() { return connCount; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -