Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ipaddr.cpp Source File

ipaddr.cpp

00001 
00002 /*
00003 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
00004  
00005 Permission is hereby granted, free of charge, to any person obtaining a copy
00006 of this software and associated documentation files (the "Software"), to deal
00007 in the Software without restriction, including without limitation the rights
00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 copies of the Software, and to permit persons to whom the Software is
00010 furnished to do so, subject to the following conditions:
00011  
00012 The above copyright notice and this permission notice shall be included in
00013 all copies or substantial portions of the Software.
00014  
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021 THE SOFTWARE.
00022 */
00023 
00024 #include "ipaddr.h"
00025 
00026 #include "netCfg.h"
00027 #if NET_LWIP_STACK
00028 #include "lwip/ip_addr.h"
00029 #endif
00030 
00031 
00032 #if NET_LWIP_STACK
00033 IpAddr::IpAddr(ip_addr_t* pIp)
00034 {
00035   *((uint32_t*)m_ip) = pIp->addr;
00036 }
00037 #endif
00038 
00039 ///Initializes IP address with provided values
00040 IpAddr::IpAddr(uint8_t ip0, uint8_t ip1, uint8_t ip2, uint8_t ip3)
00041 {
00042   //We are in LE
00043   m_ip[0] = ip0;
00044   m_ip[1] = ip1;
00045   m_ip[2] = ip2;
00046   m_ip[3] = ip3;
00047 }
00048 
00049 ///Initializes IP address with null values 
00050 IpAddr::IpAddr()
00051 {
00052   m_ip[0] = 0;
00053   m_ip[1] = 0;
00054   m_ip[2] = 0;
00055   m_ip[3] = 0;
00056 }
00057 
00058 
00059 #if NET_LWIP_STACK
00060 ip_addr_t IpAddr::getStruct() const
00061 {
00062   ip_addr_t ip_struct;
00063   ip_struct.addr = *((uint32_t*)m_ip);
00064   return ip_struct;
00065 }
00066 #endif
00067 
00068 uint8_t IpAddr::operator[](unsigned int i) const
00069 {
00070   uint8_t null = 0;
00071   if( i > 3 )
00072     return null;
00073   return m_ip[i];
00074 }
00075 
00076 bool IpAddr::isEq(const IpAddr& b) const
00077 {
00078   return (*((uint32_t*)m_ip) == *((uint32_t*)(b.m_ip)));
00079 }
00080 
00081 bool IpAddr::operator==(const IpAddr& b) const
00082 {
00083   return isEq(b);
00084 }
00085  
00086 bool IpAddr::operator!=(const IpAddr& b) const
00087 {
00088   return !(operator==(b));
00089 }
00090 
00091 bool IpAddr::isNull() const
00092 {
00093   return (*((uint32_t*)m_ip) == 0);
00094 }
00095 
00096 bool IpAddr::isBroadcast() const
00097 {
00098   return (*((uint32_t*)m_ip) == 0xFFFFFFFF);
00099 }
00100 
00101 bool IpAddr::isMulticast() const
00102 {
00103   return ((m_ip[0] & 0xF0) == 0xE0);
00104 }