packet.php
来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 626 行 · 第 1/2 页
PHP
626 行
*/ function data() { $data = $this->header->data(); for ($ctr = 0; $ctr < $this->header->qdcount; $ctr++) { $data .= $this->question[$ctr]->data($this, strlen($data)); } for ($ctr = 0; $ctr < $this->header->ancount; $ctr++) { $data .= $this->answer[$ctr]->data($this, strlen($data)); } for ($ctr = 0; $ctr < $this->header->nscount; $ctr++) { $data .= $this->authority[$ctr]->data($this, strlen($data)); } for ($ctr = 0; $ctr < $this->header->arcount; $ctr++) { $data .= $this->additional[$ctr]->data($this, strlen($data)); } return($data); } /*}}}*/ /* Net_DNS_Packet::dn_comp($name, $offset) {{{*/ /** * DNS packet compression method * * Returns a domain name compressed for a particular packet object, to * be stored beginning at the given offset within the packet data. The * name will be added to a running list of compressed domain names for * future use. * * @param string $name The name of the label to compress * @param integer $offset The location offset in the packet to where * the label will be stored. * @return string $compname A binary string containing the compressed * label. * @see Net_DNS_Packet::dn_expand() */ function dn_comp($name, $offset) { $names = explode('.', $name); $compname = ''; while (count($names)) { $dname = join('.', $names); if (isset($this->compnames[$dname])) { $compname .= pack('n', 0xc000 | $this->compnames[$dname]); break; } $this->compnames[$dname] = $offset; $first = array_shift($names); $length = strlen($first); $compname .= pack('Ca*', $length, $first); $offset += $length + 1; } if (! count($names)) { $compname .= pack('C', 0); } return($compname); } /*}}}*/ /* Net_DNS_Packet::dn_expand($packet, $offset) {{{ */ /** * DNS packet decompression method * * Expands the domain name stored at a particular location in a DNS * packet. The first argument is a variable containing the packet * data. The second argument is the offset within the packet where * the (possibly) compressed domain name is stored. * * @param string $packet The packet data * @param integer $offset The location offset in the packet of the * label to decompress. * @return array Returns a list of type array($name, $offset) where * $name is the name of the label which was decompressed * and $offset is the offset of the next field in the * packet. Returns array(NULL, NULL) on error */ function dn_expand($packet, $offset) { $packetlen = strlen($packet); $int16sz = 2; $name = ''; while (1) { if ($packetlen < ($offset + 1)) { return(array(NULL, NULL)); } $a = unpack("@$offset/Cchar", $packet); $len = $a['char']; if ($len == 0) { $offset++; break; } else if (($len & 0xc0) == 0xc0) { if ($packetlen < ($offset + $int16sz)) { return(array(NULL, NULL)); } $ptr = unpack("@$offset/ni", $packet); $ptr = $ptr['i']; $ptr = $ptr & 0x3fff; $name2 = Net_DNS_Packet::dn_expand($packet, $ptr); if (is_null($name2[0])) { return(array(NULL, NULL)); } $name .= $name2[0]; $offset += $int16sz; break; } else { $offset++; if ($packetlen < ($offset + $len)) { return(array(NULL, NULL)); } $elem = substr($packet, $offset, $len); $name .= $elem . '.'; $offset += $len; } } $name = ereg_replace('\.$', '', $name); return(array($name, $offset)); } /*}}}*/ /* Net_DNS_Packet::parse_question($data, $offset) {{{ */ /** * Parses the question section of a packet * * Examines a DNS packet at the specified offset and parses the data * of the QUESTION section. * * @param string $data The packet data returned from the server * @param integer $offset The location offset of the start of the * question section. * @return array An array of type array($q, $offset) where $q * is a Net_DNS_Question object and $offset is the * location of the next section of the packet which * needs to be parsed. */ function parse_question($data, $offset) { list($qname, $offset) = $this->dn_expand($data, $offset); if (is_null($qname)) { return(array(NULL, NULL)); } if (strlen($data) < ($offset + 2 * 2)) { return(array(NULL, NULL)); } $q = unpack("@$offset/n2int", $data); $qtype = $q['int1']; $qclass = $q['int2']; $offset += 2 * 2; $qtype = Net_DNS::typesbyval($qtype); $qclass = Net_DNS::classesbyval($qclass); $q = new Net_DNS_Question($qname, $qtype, $qclass); return(array($q, $offset)); } /*}}}*/ /* Net_DNS_Packet::parse_rr($data, $offset) {{{ */ /** * Parses a resource record section of a packet * * Examines a DNS packet at the specified offset and parses the data * of a section which contains RRs (ANSWER, AUTHORITY, ADDITIONAL). * * @param string $data The packet data returned from the server * @param integer $offset The location offset of the start of the resource * record section. * @return array An array of type array($rr, $offset) where $rr * is a Net_DNS_RR object and $offset is the * location of the next section of the packet which * needs to be parsed. */ function parse_rr($data, $offset) { list($name, $offset) = $this->dn_expand($data, $offset); if (! strlen($name)) { return(array(NULL, NULL)); } if (strlen($data) < ($offset + 10)) { return(array(NULL, NULL)); } $a = unpack("@$offset/n2tc/Nttl/nrdlength", $data); $type = $a['tc1']; $class = $a['tc2']; $ttl = $a['ttl']; $rdlength = $a['rdlength']; $type = Net_DNS::typesbyval($type); $class = Net_DNS::classesbyval($class); $offset += 10; if (strlen($data) < ($offset + $rdlength)) { return(array(NULL, NULL)); } $rrobj = new Net_DNS_RR(array($name, $type, $class, $ttl, $rdlength, $data, $offset)); if (is_null($rrobj)) { return(array(NULL, NULL)); } $offset += $rdlength; return(array($rrobj, $offset)); } /* }}} */ /* Net_DNS_Packet::display() {{{ */ /** * Prints out the packet in a human readable formatted string */ function display() { echo $this->string(); } /*}}}*/ /* Net_DNS_Packet::string() {{{ */ /** * Builds a human readable formatted string representing a packet */ function string() { $retval = ''; if ($this->answerfrom) { $retval .= ';; Answer received from ' . $this->answerfrom . '(' . $this->answersize . " bytes)\n;;\n"; } $retval .= ";; HEADER SECTION\n"; $retval .= $this->header->string(); $retval .= "\n"; $section = ($this->header->opcode == 'UPDATE') ? 'ZONE' : 'QUESTION'; $retval .= ";; $section SECTION (" . $this->header->qdcount . ' record' . ($this->header->qdcount == 1 ? '' : 's') . ")\n"; foreach ($this->question as $qr) { $retval .= ';; ' . $qr->string() . "\n"; } $section = ($this->header->opcode == 'UPDATE') ? 'PREREQUISITE' : 'ANSWER'; $retval .= "\n;; $section SECTION (" . $this->header->ancount . ' record' . ($this->header->ancount == 1 ? '' : 's') . ")\n"; if (is_array($this->answer)) { foreach ($this->answer as $ans) { $retval .= ';; ' . $ans->string() . "\n"; } } $section = ($this->header->opcode == 'UPDATE') ? 'UPDATE' : 'AUTHORITY'; $retval .= "\n;; $section SECTION (" . $this->header->nscount . ' record' . ($this->header->nscount == 1 ? '' : 's') . ")\n"; if (is_array($this->authority)) { foreach ($this->authority as $auth) { $retval .= ';; ' . $auth->string() . "\n"; } } $retval .= "\n;; ADDITIONAL SECTION (" . $this->header->arcount . ' record' . ($this->header->arcount == 1 ? '' : 's') . ")\n"; if (is_array($this->additional)) { foreach ($this->additional as $addl) { $retval .= ';; ' . $addl->string() . "\n"; } } $retval .= "\n\n"; return($retval); } /*}}}*/}/* }}} *//* VIM settings {{{ * Local variables: * tab-width: 4 * c-basic-offset: 4 * soft-stop-width: 4 * c indent on * End: * vim600: sw=4 ts=4 sts=4 cindent fdm=marker et * vim<600: sw=4 ts=4 * }}} */?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?