ZG2100 Network interface source
core/ipaddr.cpp@1:3a7c15057192, 2010-08-06 (annotated)
- Committer:
- donatien
- Date:
- Fri Aug 06 10:23:41 2010 +0000
- Revision:
- 1:3a7c15057192
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 1:3a7c15057192 | 1 | |
donatien | 1:3a7c15057192 | 2 | /* |
donatien | 1:3a7c15057192 | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
donatien | 1:3a7c15057192 | 4 | |
donatien | 1:3a7c15057192 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
donatien | 1:3a7c15057192 | 6 | of this software and associated documentation files (the "Software"), to deal |
donatien | 1:3a7c15057192 | 7 | in the Software without restriction, including without limitation the rights |
donatien | 1:3a7c15057192 | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
donatien | 1:3a7c15057192 | 9 | copies of the Software, and to permit persons to whom the Software is |
donatien | 1:3a7c15057192 | 10 | furnished to do so, subject to the following conditions: |
donatien | 1:3a7c15057192 | 11 | |
donatien | 1:3a7c15057192 | 12 | The above copyright notice and this permission notice shall be included in |
donatien | 1:3a7c15057192 | 13 | all copies or substantial portions of the Software. |
donatien | 1:3a7c15057192 | 14 | |
donatien | 1:3a7c15057192 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
donatien | 1:3a7c15057192 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
donatien | 1:3a7c15057192 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
donatien | 1:3a7c15057192 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
donatien | 1:3a7c15057192 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
donatien | 1:3a7c15057192 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
donatien | 1:3a7c15057192 | 21 | THE SOFTWARE. |
donatien | 1:3a7c15057192 | 22 | */ |
donatien | 1:3a7c15057192 | 23 | |
donatien | 1:3a7c15057192 | 24 | #include "ipaddr.h" |
donatien | 1:3a7c15057192 | 25 | |
donatien | 1:3a7c15057192 | 26 | #include "netCfg.h" |
donatien | 1:3a7c15057192 | 27 | #if NET_LWIP_STACK |
donatien | 1:3a7c15057192 | 28 | #include "lwip/ip_addr.h" |
donatien | 1:3a7c15057192 | 29 | #endif |
donatien | 1:3a7c15057192 | 30 | |
donatien | 1:3a7c15057192 | 31 | |
donatien | 1:3a7c15057192 | 32 | #if NET_LWIP_STACK |
donatien | 1:3a7c15057192 | 33 | IpAddr::IpAddr(ip_addr_t* pIp) |
donatien | 1:3a7c15057192 | 34 | { |
donatien | 1:3a7c15057192 | 35 | *((uint32_t*)m_ip) = pIp->addr; |
donatien | 1:3a7c15057192 | 36 | } |
donatien | 1:3a7c15057192 | 37 | #endif |
donatien | 1:3a7c15057192 | 38 | |
donatien | 1:3a7c15057192 | 39 | ///Initializes IP address with provided values |
donatien | 1:3a7c15057192 | 40 | IpAddr::IpAddr(uint8_t ip0, uint8_t ip1, uint8_t ip2, uint8_t ip3) |
donatien | 1:3a7c15057192 | 41 | { |
donatien | 1:3a7c15057192 | 42 | //We are in LE |
donatien | 1:3a7c15057192 | 43 | m_ip[0] = ip0; |
donatien | 1:3a7c15057192 | 44 | m_ip[1] = ip1; |
donatien | 1:3a7c15057192 | 45 | m_ip[2] = ip2; |
donatien | 1:3a7c15057192 | 46 | m_ip[3] = ip3; |
donatien | 1:3a7c15057192 | 47 | } |
donatien | 1:3a7c15057192 | 48 | |
donatien | 1:3a7c15057192 | 49 | ///Initializes IP address with null values |
donatien | 1:3a7c15057192 | 50 | IpAddr::IpAddr() |
donatien | 1:3a7c15057192 | 51 | { |
donatien | 1:3a7c15057192 | 52 | m_ip[0] = 0; |
donatien | 1:3a7c15057192 | 53 | m_ip[1] = 0; |
donatien | 1:3a7c15057192 | 54 | m_ip[2] = 0; |
donatien | 1:3a7c15057192 | 55 | m_ip[3] = 0; |
donatien | 1:3a7c15057192 | 56 | } |
donatien | 1:3a7c15057192 | 57 | |
donatien | 1:3a7c15057192 | 58 | |
donatien | 1:3a7c15057192 | 59 | #if NET_LWIP_STACK |
donatien | 1:3a7c15057192 | 60 | ip_addr_t IpAddr::getStruct() const |
donatien | 1:3a7c15057192 | 61 | { |
donatien | 1:3a7c15057192 | 62 | ip_addr_t ip_struct; |
donatien | 1:3a7c15057192 | 63 | ip_struct.addr = *((uint32_t*)m_ip); |
donatien | 1:3a7c15057192 | 64 | return ip_struct; |
donatien | 1:3a7c15057192 | 65 | } |
donatien | 1:3a7c15057192 | 66 | #endif |
donatien | 1:3a7c15057192 | 67 | |
donatien | 1:3a7c15057192 | 68 | uint8_t IpAddr::operator[](unsigned int i) const |
donatien | 1:3a7c15057192 | 69 | { |
donatien | 1:3a7c15057192 | 70 | uint8_t null = 0; |
donatien | 1:3a7c15057192 | 71 | if( i > 3 ) |
donatien | 1:3a7c15057192 | 72 | return null; |
donatien | 1:3a7c15057192 | 73 | return m_ip[i]; |
donatien | 1:3a7c15057192 | 74 | } |
donatien | 1:3a7c15057192 | 75 | |
donatien | 1:3a7c15057192 | 76 | bool IpAddr::isEq(const IpAddr& b) const |
donatien | 1:3a7c15057192 | 77 | { |
donatien | 1:3a7c15057192 | 78 | return (*((uint32_t*)m_ip) == *((uint32_t*)(b.m_ip))); |
donatien | 1:3a7c15057192 | 79 | } |
donatien | 1:3a7c15057192 | 80 | |
donatien | 1:3a7c15057192 | 81 | bool IpAddr::operator==(const IpAddr& b) const |
donatien | 1:3a7c15057192 | 82 | { |
donatien | 1:3a7c15057192 | 83 | return isEq(b); |
donatien | 1:3a7c15057192 | 84 | } |
donatien | 1:3a7c15057192 | 85 | |
donatien | 1:3a7c15057192 | 86 | bool IpAddr::operator!=(const IpAddr& b) const |
donatien | 1:3a7c15057192 | 87 | { |
donatien | 1:3a7c15057192 | 88 | return !(operator==(b)); |
donatien | 1:3a7c15057192 | 89 | } |
donatien | 1:3a7c15057192 | 90 | |
donatien | 1:3a7c15057192 | 91 | bool IpAddr::isNull() const |
donatien | 1:3a7c15057192 | 92 | { |
donatien | 1:3a7c15057192 | 93 | return (*((uint32_t*)m_ip) == 0); |
donatien | 1:3a7c15057192 | 94 | } |
donatien | 1:3a7c15057192 | 95 | |
donatien | 1:3a7c15057192 | 96 | bool IpAddr::isBroadcast() const |
donatien | 1:3a7c15057192 | 97 | { |
donatien | 1:3a7c15057192 | 98 | return (*((uint32_t*)m_ip) == 0xFFFFFFFF); |
donatien | 1:3a7c15057192 | 99 | } |
donatien | 1:3a7c15057192 | 100 | |
donatien | 1:3a7c15057192 | 101 | bool IpAddr::isMulticast() const |
donatien | 1:3a7c15057192 | 102 | { |
donatien | 1:3a7c15057192 | 103 | return ((m_ip[0] & 0xF0) == 0xE0); |
donatien | 1:3a7c15057192 | 104 | } |