void EthernetClass::begin(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet) { w5500.init(); w5500.setIPAddress(local_ip.raw_address()); w5500.setGatewayIp(gateway.raw_address()); w5500.setSubnetMask(subnet.raw_address()); _dnsServerAddress = dns_server; }
void EthernetClass::begin(uint8_t *mac, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet) { W5100.init(); W5100.setMACAddress(mac); W5100.setIPAddress(local_ip.raw_address()); W5100.setGatewayIp(gateway.raw_address()); W5100.setSubnetMask(subnet.raw_address()); _dnsServerAddress = dns_server; }
void EthernetClass::begin(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet) { W5100.init(); SPI.beginTransaction(SPI_ETHERNET_SETTINGS); W5100.setIPAddress(local_ip.raw_address()); W5100.setGatewayIp(gateway.raw_address()); W5100.setSubnetMask(subnet.raw_address()); SPI.endTransaction(); _dnsServerAddress = dns_server; }
int Client::connect(IPAddress ip, uint16_t port) { if (_sock != MAX_SOCK_NUM) return 0; for (int i = 0; i < MAX_SOCK_NUM; i++) { uint8_t s = W5100.readSnSR(i); if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT) { _sock = i; break; } } if (_sock == MAX_SOCK_NUM) return 0; _srcport++; if (_srcport == 0) _srcport = 1024; socket(_sock, SnMR::TCP, _srcport, 0); if (!::connect(_sock, ip.raw_address(), port)) { _sock = MAX_SOCK_NUM; return 0; } while (status() != SnSR::ESTABLISHED) { delay(1); if (status() == SnSR::CLOSED) { _sock = MAX_SOCK_NUM; return 0; } } return 1; }
IPAddress EthernetClass::gatewayIP() { IPAddress ret; SPI.beginTransaction(SPI_ETHERNET_SETTINGS); W5100.getGatewayIp(ret.raw_address()); SPI.endTransaction(); return ret; }
IPAddress EthernetClass::subnetMask() { IPAddress ret; SPI.beginTransaction(SPI_ETHERNET_SETTINGS); W5100.getSubnetMask(ret.raw_address()); SPI.endTransaction(); return ret; }
int UdpPort::recvfrom ( uint8_t* buf, uint16_t len, int flags, uint32_t* ipAddressPtr, uint16_t* portPtr ) { IPAddress remoteIp; uint8_t packLen; if(_castStat == STAT_UNICAST) { packLen = _udpUnicast.read(buf, len); *portPtr = _udpUnicast.remotePort(); remoteIp = _udpUnicast.remoteIP(); } else if(_castStat == STAT_MULTICAST) { packLen = _udpMulticast.read(buf, len); *portPtr = _udpMulticast.remotePort(); remoteIp = _udpMulticast.remoteIP(); } else { return 0; } memcpy(ipAddressPtr,remoteIp.raw_address(), 4); return packLen; }
int EthernetClient::connect(IPAddress ip, uint16_t port) { if (m_sock) return 0; struct sockaddr_in sin = {0}; sin.sin_family = AF_INET; sin.sin_port = htons(port); sin.sin_addr.s_addr = *(uint32_t*) (ip.raw_address()); m_sock = socket(AF_INET, SOCK_STREAM, 0); if (::connect(m_sock, (struct sockaddr *) &sin, sizeof(sin)) < 0) { trace("Error Connecting(%d)%s\n", errno, strerror(errno)); return false; } m_connected = true; return 1; }
IPAddress EthernetClass::gatewayIP() { IPAddress ret; W5100.getGatewayIp(ret.raw_address()); return ret; }
IPAddress EthernetClass::subnetMask() { IPAddress ret; W5100.getSubnetMask(ret.raw_address()); return ret; }
IPAddress EthernetClass::localIP() { IPAddress ret; W5100.getIPAddress(ret.raw_address()); return ret; }
uint16_t DNSClient::ProcessResponse(uint16_t aTimeout, IPAddress& aAddress) { uint32_t startTime = millis(); // Wait for a response packet while(iUdp.parsePacket() <= 0) { if((millis() - startTime) > aTimeout) return TIMED_OUT; delay(50); } // We've had a reply! // Read the UDP header uint8_t header[DNS_HEADER_SIZE]; // Enough space to reuse for the DNS header // Check that it's a response from the right server and the right port if ( (iDNSServer != iUdp.remoteIP()) || (iUdp.remotePort() != DNS_PORT) ) { // It's not from who we expected return INVALID_SERVER; } // Read through the rest of the response if (iUdp.available() < DNS_HEADER_SIZE) { return TRUNCATED; } iUdp.read(header, DNS_HEADER_SIZE); uint16_t header_flags = htons(*((uint16_t*)&header[2])); // Check that it's a response to this request if ( ( iRequestId != (*((uint16_t*)&header[0])) ) || ((header_flags & QUERY_RESPONSE_MASK) != (uint16_t)RESPONSE_FLAG) ) { // Mark the entire packet as read iUdp.flush(); return INVALID_RESPONSE; } // Check for any errors in the response (or in our request) // although we don't do anything to get round these if ( (header_flags & TRUNCATION_FLAG) || (header_flags & RESP_MASK) ) { // Mark the entire packet as read iUdp.flush(); return -5; //INVALID_RESPONSE; } // And make sure we've got (at least) one answer uint16_t answerCount = htons(*((uint16_t*)&header[6])); if (answerCount == 0 ) { // Mark the entire packet as read iUdp.flush(); return -6; //INVALID_RESPONSE; } // Skip over any questions for (uint16_t i =0; i < htons(*((uint16_t*)&header[4])); i++) { // Skip over the name uint8_t len; do { iUdp.read(&len, sizeof(len)); if (len > 0) { // Don't need to actually read the data out for the string, just // advance ptr to beyond it while(len--) { iUdp.read(); // we don't care about the returned byte } } } while (len != 0); // Now jump over the type and class for (int i =0; i < 4; i++) { iUdp.read(); // we don't care about the returned byte } } // Now we're up to the bit we're interested in, the answer // There might be more than one answer (although we'll just use the first // type A answer) and some authority and additional resource records but // we're going to ignore all of them. for (uint16_t i =0; i < answerCount; i++) { // Skip the name uint8_t len; do { iUdp.read(&len, sizeof(len)); if ((len & LABEL_COMPRESSION_MASK) == 0) { // It's just a normal label if (len > 0) { // And it's got a length // Don't need to actually read the data out for the string, // just advance ptr to beyond it while(len--) { iUdp.read(); // we don't care about the returned byte } } } else { // This is a pointer to a somewhere else in the message for the // rest of the name. We don't care about the name, and RFC1035 // says that a name is either a sequence of labels ended with a // 0 length octet or a pointer or a sequence of labels ending in // a pointer. Either way, when we get here we're at the end of // the name // Skip over the pointer iUdp.read(); // we don't care about the returned byte // And set len so that we drop out of the name loop len = 0; } } while (len != 0); // Check the type and class uint16_t answerType; uint16_t answerClass; iUdp.read((uint8_t*)&answerType, sizeof(answerType)); iUdp.read((uint8_t*)&answerClass, sizeof(answerClass)); // Ignore the Time-To-Live as we don't do any caching for (int i =0; i < TTL_SIZE; i++) { iUdp.read(); // we don't care about the returned byte } // And read out the length of this answer // Don't need header_flags anymore, so we can reuse it here iUdp.read((uint8_t*)&header_flags, sizeof(header_flags)); if ( (htons(answerType) == TYPE_A) && (htons(answerClass) == CLASS_IN) ) { if (htons(header_flags) != 4) { // It's a weird size // Mark the entire packet as read iUdp.flush(); return -9;//INVALID_RESPONSE; } iUdp.read(aAddress.raw_address(), 4); return SUCCESS; } else { // This isn't an answer type we're after, move onto the next one for (uint16_t i =0; i < htons(header_flags); i++) { iUdp.read(); // we don't care about the returned byte } } } // Mark the entire packet as read iUdp.flush(); // If we get here then we haven't found an answer return -10;//INVALID_RESPONSE; }
IPAddress EthernetClass::dnsServerIP() { IPAddress ret; SwsSock.getDnsServerIp(ret.raw_address()); return ret; }