def _recv_hack (self, head, data, remote, forward): record = data.split(',') if len(record) != 5: return -1 try: timestamp = float(record[0]) addr1 = cnetudp.text2ep(record[1]) mode1 = int(record[2]) addr2 = cnetudp.text2ep(record[3]) mode2 = int(record[4]) except: return -1 rtt = self.current - timestamp rtt = min(30.0, max(0.001, rtt)) route = [ (rtt, addr1, mode1, addr2, mode2) ] if remote != addr2 or forward != mode2: route.append((rtt, addr1, mode1, remote, forward)) timestamp = '%.6f'%self.current for rtt, addr1, mode1, addr2, mode2 in route: self._send_touch(head.suid, head.skey, timestamp, addr1, mode1, addr2, mode2) if self.trace and (self.logmask & LOG_HACK): self.trace('<recv hack: %s %d %s %d>'%(cnetudp.ep2text(addr1), \ mode1, cnetudp.ep2text(addr2), mode2)) return 0
def _send_syn1(self, uid, key, sport, conv, addr1, mode1, addr2, mode2): head = cnetcom.msghead(self.uid, self.key, uid, key, sport, 0, \ CMD_SYN1, conv) text = '%s,%d,%s,%d'%(cnetudp.ep2text(addr1), mode1, \ cnetudp.ep2text(addr2), mode2) self.sendudp(head, text, addr2, mode2) self.log('syn sport', sport) return 0
def _do_establish(self): self.establish = 1 self.state = STATE_CONNECTED self.time_alive = self.current self.host._port_establish(self.sport) route = self.route self.log('route1: %s %d' % (cnetudp.ep2text(route[1]), route[2])) self.log('route2: %s %d' % (cnetudp.ep2text(route[3]), route[4])) self.log('_do_establish')
def _do_establish (self): self.establish = 1 self.state = STATE_CONNECTED self.time_alive = self.current self.host._port_establish(self.sport) route = self.route self.log('route1: %s %d'%(cnetudp.ep2text(route[1]), route[2])) self.log('route2: %s %d'%(cnetudp.ep2text(route[3]), route[4])) self.log('_do_establish')
def _send_hack (self, uid, key, ts, addr1, mode1, addr2, mode2): head = msghead(self.uid, self.key, uid, key, cmd = CMD_HACK) text = '%s,%s,%s,%s,%s'%(ts, cnetudp.ep2text(addr1), mode1, \ cnetudp.ep2text(addr2), mode2) self.sendudp(head, text, addr1, mode1) if self.trace and (self.logmask & LOG_HACK): self.trace('<hack %s %d %s %d>'%(cnetudp.ep2text(addr1), mode1,\ cnetudp.ep2text(addr2), mode2)) #print '<hack %s %d %s %d>'%(addr1, mode1, addr2, mode2) return 0
def _newroute (self, uid, key, rtt, addr1, mode1, addr2, mode2): ident = (uid, key) #print '[PATH] %.3f %s %d %s %d'%(rtt, addr1, mode1, addr2, mode2) if self.trace and (self.logmask & LOG_ROUTE): self.trace('<newroute: %s %d %s %d>'%(cnetudp.ep2text(addr1), \ mode1, cnetudp.ep2text(addr2), mode2)) if ident in self.route: route = self.route[ident] route.active() route.newroute(rtt, addr1, mode1, addr2, mode2) route.update(self.current) return 0
def _send_hello (self, duid, dkey, linkdesc): head = msghead(self.uid, self.key, duid, dkey, cmd = CMD_HELLO) endpoint = cnetudp.endpoint().unmarshal(linkdesc) destination = cnetudp.destination(endpoint) timestamp = '%.f'%self.current linkdesc1 = self.linkdesc() if linkdesc[:10] == '127.0.0.1:': linkdesc1 = self.localhost() for addr2, mode2 in destination: text = '%s,%s,%s,'%(timestamp, cnetudp.ep2text(addr2), mode2) text += linkdesc1 if self.trace and (self.logmask & LOG_HELLO): self.trace('<hello: %s %d>'%(cnetudp.ep2text(addr2), mode2)) #print '_send_hello: %s %d'%(addr2, mode2) self.sendudp(head, text, addr2, mode2) return 0
def _recv_tack (self, head, data, remote, forward): record = data.split(',') if len(record) != 5: return -1 try: timestamp = float(record[0]) addr1 = cnetudp.text2ep(record[1]) mode1 = int(record[2]) addr2 = cnetudp.text2ep(record[3]) mode2 = int(record[4]) except: return -1 rtt = self.current - timestamp rtt = min(30.0, max(0.001, rtt)) if self.trace and (self.logmask & LOG_TACK): self.trace('<recv tack: %s %d %s %d>'%(cnetudp.ep2text(addr1), \ mode1, cnetudp.ep2text(addr2), mode2)) self._newroute(head.suid, head.skey, rtt, addr1, mode1, addr2, mode2) return 0
def _recv_hello (self, head, data, remote, forward): record = data.split(',') if len(record) != 4: return -1 timestamp = record[0] try: addr2 = cnetudp.text2ep(record[1]) # 取得从哪里来的:地址 mode2 = int(record[2]) # 取得从哪里来的:是否转发 linkdesc = record[3] endpoint = cnetudp.endpoint().unmarshal(linkdesc) except: return -1 destination = cnetudp.destination(endpoint, remote, forward) if self.trace and (self.logmask & LOG_HELLO): self.trace('<recv hello: %s %d %s %d>'%(cnetudp.ep2text(remote),\ forward, cnetudp.ep2text(addr2), mode2)) for addr1, mode1 in destination: self._send_hack(head.suid, head.skey, timestamp, \ addr1, mode1, addr2, mode2) return 0
def route2text(rtt, addr1, mode1, addr2, mode2): text = '%.06f,%s,%d,%s,%d'%(rtt, cnetudp.ep2text(addr1), mode1, \ cnetudp.ep2text(addr2), mode2) return text
def _send_tack (self, uid, key, ts, addr1, mode1, addr2, mode2): head = msghead(self.uid, self.key, uid, key, cmd = CMD_TACK) text = '%s,%s,%s,%s,%s'%(ts, cnetudp.ep2text(addr1), mode1, \ cnetudp.ep2text(addr2), mode2) self.sendudp(head, text, addr1, mode1)