2
Original copyright header listed below. This comes via rsync.
3
Any additional changes are provided via the same license as the original.
5
Copyright (C) 2011 Muhammad Umair
9
* Copyright (C) 1996-2001 Internet Software Consortium.
11
* Permission to use, copy, modify, and distribute this software for any
12
* purpose with or without fee is hereby granted, provided that the above
13
* copyright notice and this permission notice appear in all copies.
15
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
16
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
17
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
18
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
19
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
20
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
21
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
22
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
40
unsigned short ip6[8];
43
//Function to store the IPv4 address in IPv6 Data Structure
44
int ipv4_inet_pton(const char *src)
49
char ipv4_map_ipv6[20],ipv4[16], octet_1[5],octet_2[5],concat_octets[20];
50
int octet[4],octet_index = 0;
52
memset(ipv4_map_ipv6, NULL, sizeof(ipv4_map_ipv6));
53
strcpy(ipv4_map_ipv6,src);
55
memset(octet, NULL, sizeof(octet));
56
memset(ipv4, NULL, sizeof(ipv4));
57
memset(octet_1, NULL, sizeof(octet_1));
58
memset(octet_2, NULL, sizeof(octet_2));
59
memset(concat_octets, NULL, sizeof(concat_octets));
61
ptr_src = strtok(ipv4_map_ipv6,"::");
65
ptr_src = strtok(ipv4,".");
67
while (ptr_src != '\0')
70
sscanf( ptr_src,"%d", &octet[octet_index]);
72
if(octet[octet_index++] > 255)
74
return 0; // Invalid IP Address
77
ptr_src = strtok (NULL, ".");
79
}// end of main while loop
82
if(octet_index < 4 || octet_index > 4)
84
return 0; // Invalid IP Address
90
str.ip6[0] = str.ip6[1] = str.ip6[2] = str.ip6[3] = str.ip6[4] = str.ip6[5] = 0;
92
for (int i=6 ; i <= 7; i++)
99
sprintf(octet_1, "%02x", octet[octet_index]);
101
sprintf(octet_2, "%02x", octet[++octet_index]);
103
strcpy(concat_octets,octet_1);
105
strcat(concat_octets,octet_2);
107
sscanf(concat_octets,"%x",(unsigned int *)&str.ip6[i]);
109
memset(octet_1, NULL, sizeof(octet_1));
111
memset(octet_2, NULL, sizeof(octet_2));
113
memset(concat_octets, NULL, sizeof(concat_octets));
117
}//end of ipv4_inet_pton() function
119
//Function to retain the IPv4 address from IPv6 Data Structure
120
char * ipv4_inet_ntop(char *destination)
123
memset(destination,NULL,sizeof(destination));
125
snprintf( destination ,IPV6_BUFFER_LENGTH, "%03x:%03x:%03x:%03x:%03x:%03x:%03d.%03d.%03d.%03d" ,
126
str.ip6[0],str.ip6[1],str.ip6[2],str.ip6[3],str.ip6[4],str.ip6[5],
127
(((unsigned int )str.ip6[6]>>8) & 0xFF),
128
((unsigned int )str.ip6[6] & 0xFF),
129
(((unsigned int )str.ip6[7]>>8) & 0xFF),
130
((unsigned int )str.ip6[7] & 0xFF));
134
}// end of ipv4_inet_ntop function
137
//Function to store the IPv6 address in IPv6 Data Structure
138
int ipv6_inet_pton(const char *src)
141
char ipv6[IPV6_BUFFER_LENGTH];
143
memset(ipv6,NULL,IPV6_BUFFER_LENGTH);
147
char ipv6_temp[IPV6_BUFFER_LENGTH], ipv6_temp1[IPV6_BUFFER_LENGTH], ipv6_temp2[IPV6_BUFFER_LENGTH];
149
memset(ipv6_temp,NULL,IPV6_BUFFER_LENGTH);
151
strcpy(ipv6_temp,ipv6);
153
memset(ipv6_temp1,NULL,IPV6_BUFFER_LENGTH);
155
strcpy(ipv6_temp1,ipv6);
157
memset(ipv6_temp2,NULL,IPV6_BUFFER_LENGTH);
159
strcpy(ipv6_temp2,ipv6);
162
static const char hex[] = "0123456789abcdef";
163
char temp[IPV6_BUFFER_LENGTH];
164
char *ptr_src ,*ptr_char, *ptr_src1;
165
const char *char_ptr_src; // get the src char
166
int char_int = NULL, index_ip6 = 0 ,octet_count=0, not_colon = 0, colon =0, count_colon = 0;
167
char temp_first[IPV6_BUFFER_LENGTH],temp_end[IPV6_BUFFER_LENGTH];
169
memset(temp_first, NULL,IPV6_BUFFER_LENGTH);
171
memset(temp_end, NULL,IPV6_BUFFER_LENGTH);
174
//while loop check three consective colons
175
while (*ptr_src != '\0')
178
if (*ptr_src == ':' && *++ptr_src == ':' && *++ptr_src == ':')
180
return 0; // Invalid IP Address
186
//while loop count the total number of octets
187
ptr_src = strtok (ipv6_temp2,":");
190
while (ptr_src != NULL)
194
ptr_src = strtok (NULL, ":");
197
//Retrun zero if total number of octets are greater than 8
200
return 0 ; // Invalid IP Address
203
int num_miss_octet =0, size =0;
205
num_miss_octet = 8 - octet_count;
206
size = 2*num_miss_octet +1;
208
char * zero_append = new char[size];
210
memset(zero_append,NULL,sizeof(zero_append));
215
//while loop locate the "::" position (start,middle or end)
216
while(*ptr_src != '\0')
218
if(*ptr_src == ':' && *++ptr_src == ':')
220
if (*++ptr_src=='\0')
224
else if (not_colon == 0)
237
return 0; // Invalid IP Address. Ther must be single time double colon '::'
243
}// end of while loop
245
// if colon = 0 means the IPv6 Address string is in presffered form otherwise first it covert it into prefeered form
248
//zero padding format according to the '::' position
249
strcpy(zero_append,"");
252
for (int i= 0; i < num_miss_octet; i++)
254
if(colon==1) // start
256
strcat(zero_append,"0:");
258
if(colon==2 || colon==3) //middle or end colon =2 shows at end
260
strcat(zero_append,":0");
265
strcat(zero_append,":");
270
if(colon==1 || colon==3)
271
{ //only for start and middle
273
ptr_src1 = strstr (ipv6_temp,"::");
275
ptr_src1 = ptr_src1+2;
277
while(*ptr_src1 != '\0')
279
*ptr_src++ = *ptr_src1++;
281
if(*ptr_src1 == '\0')
288
//copy the input IPv6 string before and after '::'
289
ptr_src1 = strstr (ipv6_temp1,"::");
294
strcpy(temp_first,ipv6_temp1);
298
strcat(temp_first,zero_append);
302
strcat(temp_first,zero_append);
304
strcat(temp_first,temp_end);
307
memset(ipv6,NULL,IPV6_BUFFER_LENGTH);
309
strcpy(ipv6,temp_first);
310
}// end of main if statement
314
//while loop store each octet on ipv6 struture in decimal value of hexadecimal digits
317
ptr_src = strtok (ipv6,":");
320
while (ptr_src != NULL)
322
strcpy(temp, ptr_src);
326
int octet_length = strlen(ptr_char);
328
*(ptr_char + octet_length) = '\0';
331
while(*ptr_char != '\0')
333
char_int = tolower(*ptr_char);
335
char_ptr_src = strchr (hex, char_int);
337
if(char_ptr_src == NULL)
339
return 0; // Invalid IP Address
343
*ptr_char = *char_ptr_src;
345
}//end of inner while loop
347
ptr_char-= octet_length;
350
unsigned int *ptr = (unsigned int *)&(str.ip6[index_ip6++]);
352
sscanf( ptr_char,"%x", ptr);
355
memset(temp,NULL,IPV6_BUFFER_LENGTH);
358
ptr_src = strtok (NULL, ":");
359
}// end of main while loop
361
delete [] zero_append;
364
}// end of Ipv6_Inet_pton function
366
//Function to retain the IPv6 address from IPv6 Data Structure
367
char* ipv6_inet_ntop(char *destination)
371
memset(temp,NULL,sizeof(temp));
373
memset(destination,NULL,IPV6_BUFFER_LENGTH);
376
for (int i= 0; i <= 7; i++)
380
sprintf(temp,"%04x",str.ip6[i]);
382
strcat(destination,temp);
386
sprintf(temp,"%04x:",str.ip6[i]);
388
strcat(destination,temp);
391
memset(temp,NULL,sizeof(temp));
396
}// end of Ipv6_Inet_ntop function
403
str.ip6[0] = str.ip6[1] = str.ip6[2] = str.ip6[3] = str.ip6[4] = str.ip6[5] = str.ip6[6] = str.ip6[7] = 0;
407
void store_object(unsigned char *out)
409
memcpy(out, (unsigned char *)&str, sizeof(str));
412
void restore_object(const unsigned char * in)
414
memcpy(&str, (struct ipv6_ds *)in, sizeof(str));
417
int inet_pton(const char *ip)
421
pch=strchr((char *)ip,'.');
425
return ipv6_inet_pton(ip);
429
return ipv4_inet_pton(ip);
433
char * inet_ntop(char *dest)
435
if (str.ip6[0]==0 && str.ip6[1]==0 && str.ip6[2]==0 && str.ip6[3]==0 && str.ip6[4]==0 && str.ip6[5]==0 && str.ip6[6]!=0)
437
return ipv4_inet_ntop(dest);
441
return ipv6_inet_ntop(dest);
445
static const size_t LENGTH= 16;
446
static const size_t IPV6_DISPLAY_LENGTH= 39;
447
static const size_t IPV6_BUFFER_LENGTH= IPV6_DISPLAY_LENGTH+1;
451
} /* namespace type */
452
} /* namespace drizzled */