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