1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
|
/*
* Dibbler - a portable DHCPv6
*
* authors: Tomasz Mrugalski <thomson@klub.com.pl>
* Marek Senderski <msend@o2.pl>
*
* released under GNU GPL v2 only licence
*
* $Id: IPv6Addr.cpp,v 1.11 2008-08-29 00:07:30 thomson Exp $
*
* $Log: IPv6Addr.cpp,v $
* Revision 1.11 2008-08-29 00:07:30 thomson
* Temporary license change(GPLv2 or later -> GPLv2 only)
*
* Revision 1.10 2008-03-02 21:59:53 thomson
* non %8 prefix lengths fixed.
*
* Revision 1.9 2007-05-03 23:16:29 thomson
* Fix for assigning the same prefix several times.
*
* Revision 1.8 2006-12-04 23:33:11 thomson
* Prefix delegation related fixes
*
* Revision 1.7 2006-12-02 14:54:45 thomson
* IPv6Addr::truncate implemented, inet_pton6 does not use IPv4-eccapsulated form
*
* Revision 1.6 2006-10-06 00:25:53 thomson
* Initial PD support.
*
* Revision 1.5 2006-08-21 22:52:40 thomson
* Various fixes.
*
* Revision 1.4 2004/12/07 20:53:14 thomson
* Link local safety checks added (bug #39)
*
*/
#include <stdlib.h>
#include <string.h>
#include "IPv6Addr.h"
#include "Portable.h"
#include "Logger.h"
static char truncLeft[] = { 0xff, 0x7f, 0x3f, 0x1f, 0xf, 0x7, 0x3, 0x1, 0 };
static char truncRight[]= { 0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
TIPv6Addr::TIPv6Addr() {
memset(Addr,0,16);
inet_ntop6(Addr,Plain);
}
TIPv6Addr::TIPv6Addr(const char* addr, bool plain) {
if (plain) {
strcpy(Plain,addr);
inet_pton6(Plain,Addr);
} else {
memcpy(Addr,addr,16);
inet_ntop6(Addr,Plain);
}
}
TIPv6Addr::TIPv6Addr(const char* prefix, const char* host, int prefixLength) {
int offset = prefixLength/8;
if (prefixLength%8==0) {
memmove(Addr, host, 16);
memmove(Addr, prefix, offset);
inet_ntop6(Addr, Plain);
return;
}
memmove(Addr, host, 16); // copy whole host address, but...
memmove(Addr, prefix, offset); // overwrite first bits with prefix...
Addr[offset] = (prefix[offset] & truncRight[prefixLength%8]) | (host[offset] & truncLeft[prefixLength%8]);
inet_ntop6(Addr, Plain);
}
bool TIPv6Addr::linkLocal() {
if (this->Addr[0]==0xfe &&
this->Addr[1]==0x80)
return true;
return false;
}
char* TIPv6Addr::getAddr() {
return Addr;
}
char* TIPv6Addr::getPlain() {
inet_ntop6( Addr, Plain);
return Plain;
}
void TIPv6Addr::setAddr(char* addr) {
memcpy(Addr,addr,16);
inet_ntop6(Addr,Plain);
}
char* TIPv6Addr::storeSelf(char *buf) {
memcpy(buf,this->Addr,16);
return buf+16;
}
bool TIPv6Addr::operator==(const TIPv6Addr &other) {
return !memcmp(this->Addr,other.Addr,16);
}
void TIPv6Addr::truncate(int minPrefix, int maxPrefix) {
if (minPrefix>128 || minPrefix<0 || maxPrefix>128 || maxPrefix<0) {
Log(Error) << "Unable to truncate address: invalid prefix lengths: minPrefix=" << minPrefix << ", maxPrefix=" << maxPrefix << LogEnd;
return;
}
// truncating from the left
int x = minPrefix/8;
memset(this->Addr, 0, x);
if (minPrefix%8) {
this->Addr[x] = this->Addr[x] & truncLeft[minPrefix%8];
}
// truncating from the right
x = maxPrefix/8;
if (maxPrefix%8)
x++;
memset(this->Addr+x, 0, 16-x);
if (maxPrefix%8) {
x = maxPrefix/8;
this->Addr[x] = this->Addr[x] & truncRight[maxPrefix%8];
}
// update plain form
inet_ntop6(Addr,Plain);
}
void TIPv6Addr::shiftL(int bits)
{
}
void TIPv6Addr::shiftR(int bits)
{
}
ostream& operator<<(ostream& out,TIPv6Addr& addr)
{
char buf[48];
if (addr.Addr) {
inet_ntop6(addr.Addr, buf);
out << buf;
} else {
out << "::";
}
return out;
}
bool TIPv6Addr::operator<=(const TIPv6Addr &other)
{
for (int i=0;i<16;i++)
{
if (Addr[i]<other.Addr[i])
return true;
if(Addr[i]>other.Addr[i])
return false;
}
return true; //hmm: are they equal
}
TIPv6Addr TIPv6Addr::operator-(const TIPv6Addr &other)
{
char result[16];
memset(result,0,16);
char carry=0;
for (int i=15;i>=0;i--)
{
unsigned int left=Addr[i];
unsigned int right=other.Addr[i];
if(left>=(right+carry))
{
result[i]=left-other.Addr[i]-carry;
carry=0;
}
else
{
result[i]=Addr[i]+256-other.Addr[i]-carry;
carry=1;
}
}
return TIPv6Addr(result);
}
TIPv6Addr TIPv6Addr::operator+(const TIPv6Addr &other)
{
char result[16];
memset(result,0,16);
unsigned int carry=0;
for (int i=15;i>=0;i--)
{
unsigned int left=Addr[i];
unsigned int right=other.Addr[i];
if(left+right+carry>255)
{
result[i]=char (left+right+carry-256);
carry=1;
}
else
{
result[i]=left+right+carry;
carry=0;
}
}
return TIPv6Addr(result);
}
/**
* Decreases randomly an address
*
*
* @return
*/
TIPv6Addr& TIPv6Addr::operator--()
{
for (int i=15;i>=0;i--)
{
int j=i-1;
while((j>=0)&&(!Addr[j])) j--;
int r;
if (j>0) r=rand()%256;
else r=rand()%(Addr[i]+1);
if (r>Addr[i])
{
Addr[j]--;
for(j++;j<i;j++) Addr[j]=255;
Addr[i]=char(int(256)+int(Addr[i])-r);
}
else Addr[i]=r;
}
return *this;
}
|