|
| 1 | +/* |
| 2 | + IPv6Address.cpp - Base class that provides IPv6Address |
| 3 | + Copyright (c) 2011 Adrian McEwen. All right reserved. |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | + */ |
| 19 | + |
| 20 | +#include <Arduino.h> |
| 21 | +#include <IPv6Address.h> |
| 22 | +#include <Print.h> |
| 23 | + |
| 24 | +IPv6Address::IPv6Address() |
| 25 | +{ |
| 26 | + memset(_address.bytes, 0, sizeof(_address.bytes)); |
| 27 | +} |
| 28 | + |
| 29 | +IPv6Address::IPv6Address(const uint8_t *address) |
| 30 | +{ |
| 31 | + memcpy(_address.bytes, address, sizeof(_address.bytes)); |
| 32 | +} |
| 33 | + |
| 34 | +IPv6Address::IPv6Address(const uint32_t *address) |
| 35 | +{ |
| 36 | + memcpy(_address.bytes, (const uint8_t *)address, sizeof(_address.bytes)); |
| 37 | +} |
| 38 | + |
| 39 | +IPv6Address& IPv6Address::operator=(const uint8_t *address) |
| 40 | +{ |
| 41 | + memcpy(_address.bytes, address, sizeof(_address.bytes)); |
| 42 | + return *this; |
| 43 | +} |
| 44 | + |
| 45 | +bool IPv6Address::operator==(const uint8_t* addr) const |
| 46 | +{ |
| 47 | + return memcmp(addr, _address.bytes, sizeof(_address.bytes)) == 0; |
| 48 | +} |
| 49 | + |
| 50 | +size_t IPv6Address::printTo(Print& p) const |
| 51 | +{ |
| 52 | + size_t n = 0; |
| 53 | + for(int i = 0; i < 16; i+=2) { |
| 54 | + if(i){ |
| 55 | + n += p.print(':'); |
| 56 | + } |
| 57 | + n += p.printf("%02x", _address.bytes[i]); |
| 58 | + n += p.printf("%02x", _address.bytes[i+1]); |
| 59 | + |
| 60 | + } |
| 61 | + return n; |
| 62 | +} |
| 63 | + |
| 64 | +String IPv6Address::toString() const |
| 65 | +{ |
| 66 | + char szRet[40]; |
| 67 | + sprintf(szRet,"%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x", |
| 68 | + _address.bytes[0], _address.bytes[1], _address.bytes[2], _address.bytes[3], |
| 69 | + _address.bytes[4], _address.bytes[5], _address.bytes[6], _address.bytes[7], |
| 70 | + _address.bytes[8], _address.bytes[9], _address.bytes[10], _address.bytes[11], |
| 71 | + _address.bytes[12], _address.bytes[13], _address.bytes[14], _address.bytes[15]); |
| 72 | + return String(szRet); |
| 73 | +} |
| 74 | + |
| 75 | +bool IPv6Address::fromString(const char *address) |
| 76 | +{ |
| 77 | + //format 0011:2233:4455:6677:8899:aabb:ccdd:eeff |
| 78 | + if(strlen(address) != 39){ |
| 79 | + return false; |
| 80 | + } |
| 81 | + char * pos = (char *)address; |
| 82 | + size_t i = 0; |
| 83 | + for(i = 0; i < 16; i+=2) { |
| 84 | + if(!sscanf(pos, "%2hhx", &_address.bytes[i]) || !sscanf(pos+2, "%2hhx", &_address.bytes[i+1])){ |
| 85 | + return false; |
| 86 | + } |
| 87 | + pos += 5; |
| 88 | + } |
| 89 | + return true; |
| 90 | +} |
0 commit comments