/** *@brief 发送数据 *@param WData:发送数组 *@param Len: 发送数据长度,Len<=8K *@return 无 */ void RJ45_2_Write(u8 *WData,u16 Len) { u16 Start_Address; u32 Address; RJ45_2_Read_Buf(Sn_TX_WR0(0),ReadTem,2); Start_Address=ReadTem[0]*256+ReadTem[1]; //获取写入地址指针 Address=(u32)(Start_Address<<8)+(0<<5)+0x10; //计算绝对地址 RJ45_2_Write_Buf(Address,WData,Len); //写入缓存 Start_Address+=Len; WriteTem[0]=Start_Address/256; WriteTem[1]=Start_Address%256; RJ45_2_Write_Buf(Sn_TX_WR0(0),WriteTem,2); //更新指针 RJ45_2_Write_Register(Sn_CR(0),Sn_CR_SEND); //发送 while(RJ45_2_Read_Register(Sn_CR(0))); /*Wait to process the command*/ }
// return value: // A DNSError_t (DNSSuccess on success, something else otherwise) // in "int" mode: positive on success, negative on error DNSError_t EthernetDNSClass::_sendDNSQueryPacket(const char* hostName) { DNSError_t statusCode = DNSSuccess; uint16_t ptr = 0; #if defined(_USE_MALLOC_) DNSHeader_t* dnsHeader = NULL; #else DNSHeader_t dnsHeaderBuf; DNSHeader_t* dnsHeader = &dnsHeaderBuf; #endif const char* p1, *p2; char* p3; uint8_t* buf; int c, i, bsize, len; if (NULL == hostName || 0 == *hostName) { statusCode = DNSInvalidArgument; goto errorReturn; } ptr = IINCHIP_READ(Sn_TX_WR0(this->_socket)); ptr = ((ptr & 0x00ff) << 8) + IINCHIP_READ(Sn_TX_WR0(this->_socket) + 1); #if defined(_USE_MALLOC_) dnsHeader = (DNSHeader_t*)malloc(sizeof(DNSHeader_t)); if (NULL == dnsHeader) { statusCode = DNSOutOfMemory; goto errorReturn; } #endif memset(dnsHeader, 0, sizeof(DNSHeader_t)); dnsHeader->xid = dns_htons(++this->_dnsData.xid); dnsHeader->recursionDesired = 1; dnsHeader->queryCount = dns_htons(1); dnsHeader->opCode = DNSOpQuery; write_data(this->_socket, (vuint8*)dnsHeader, (vuint8*)ptr, sizeof(DNSHeader_t)); ptr += sizeof(DNSHeader_t); p1 = hostName; bsize = sizeof(DNSHeader_t); buf = (uint8_t*)dnsHeader; while(*p1) { c = 1; p2 = p1; while (0 != *p2 && '.' != *p2) { p2++; c++; }; p3 = (char*)buf; i = c; len = bsize-1; *p3++ = (uint8_t)--i; while (i-- > 0) { *p3++ = *p1++; if (--len <= 0) { write_data(this->_socket, (vuint8*)buf, (vuint8*)ptr, bsize); ptr += bsize; len = bsize; p3 = (char*)buf; } } while ('.' == *p1) ++p1; if (len != bsize) { write_data(this->_socket, (vuint8*)buf, (vuint8*)ptr, bsize-len); ptr += bsize-len; } } // first byte is the query string's zero termination, then qtype and class follow. buf[0] = buf[1] = buf[3] = 0; buf[2] = buf[4] = 1; write_data(this->_socket, (vuint8*)buf, (vuint8*)ptr, 5); ptr += 5; IINCHIP_WRITE(Sn_TX_WR0(this->_socket), (vuint8)((ptr & 0xff00) >> 8)); IINCHIP_WRITE((Sn_TX_WR0(this->_socket) + 1), (vuint8)(ptr & 0x00ff)); IINCHIP_WRITE(Sn_CR(this->_socket), Sn_CR_SEND); while(IINCHIP_READ(Sn_CR(this->_socket))); if (_state == DNSStateIdle) { this->_dnsData.lastQueryFirstXid = this->_dnsData.xid; } this->_state = DNSStateQuerySent; statusCode = DNSSuccess; errorReturn: this->_lastSendMillis = millis(); #if defined(_USE_MALLOC_) if (NULL != dnsHeader) free(dnsHeader); #endif return statusCode; }