LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-06-2011, 05:32 AM   #1
roxy99
LQ Newbie
 
Registered: Dec 2011
Posts: 3

Rep: Reputation: Disabled
Problem with inet_pton()


hello every body,
My name is roxy and i am from brazil.I want to expand IPv6 address using inet_pton().After that increment the address by 1 using C code.If my address is - 1234:12::ffff then after expand it will be 1234:0012:0000:0000:0000:0000:0000:ffff
When I increment the address it will display - 1234:0012:0000:0000:0000:0000:0001:0000
Whenever it will reaches ffff it just put 0x0000 and increment the next value by 1.
I already done some code part.Please help me in this matter.

#include "stdafx.h"
#include "string.h"
#include "stdlib.h"
#include "conio.h"
#include "arpa/inet.h"
#include "inet/ip6.h"



int _tmain(int argc, char argv[])
{

struct in6_addr {
unsigned char s6_addr[16]; // IPv6 address
};
struct in6_addr ipv6;
char str[]="abcd:1234::ffff";
if(strcnt(str,":")>1)
{
if(inet_pton(AF_INET6,str,&ipv6.s6_addr))
printf("%s is %0llu(IPv6)\n",str,ipv6.s6_addr);
else
printf("Error..");
}

getch();

return 0;

}
 
Old 12-06-2011, 11:56 AM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946
struct in6_addr has a single member, an array of 16 bytes named s6_addr. It is in network byte order, i.e. most significant byte first. I don't know why you redefined the structure -- I'd just use #include <arpa/inet.h>

Increase the last value in the array. If it turns zero, increase the previous value; if that turns zero, increase the one before that too, and so on. Finally, use inet_ntop() to convert the modified address back to a string.
Code:
#include <arpa/inet.h>
#include <stdio.h>

struct in6_addr  ipv6;
char             addrbuf[INET6_ADDRSTRLEN]; /* 46 */
const char      *addrstr;
int              i;

if (inet_pton(AF_INET6, address_string, &ipv6) == 1) {

    /* Increase address by one */
    ipv6.s6_addr[15]++;
    /* Carry over to more significant bytes */
    i = 15;
    while (i > 0 && !ipv6.s6_addr[i--])
        ipv6.s6_addr[i]++;

    /* Convert back to a string and print. */
    if ((addrstr = inet_ntop(AF_INET6, &ipv6, addrbuf, sizeof(addrbuf))))
        printf("%s\n", addrstr);
}
 
1 members found this post helpful.
Old 12-07-2011, 02:01 AM   #3
roxy99
LQ Newbie
 
Registered: Dec 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
Hi nominal,

Thank you very much for your reply.

Last edited by roxy99; 12-07-2011 at 04:47 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Do I have a path problem, an Apache2 problem or a Javascript problem or any other pro rblampain Linux - Networking 0 12-29-2010 03:50 AM
Sound Card problem(every time i install linux i have diffirent hardware problem) jacka1l Linux - Newbie 7 08-11-2005 06:10 AM
Lan configuration problem - NFS boot problem - RX&TX packets errors 242VDM242 Linux - Networking 4 11-25-2004 01:35 PM
perl problem? apache problem? cgi problem? WorldBuilder Linux - Software 1 09-17-2003 07:45 PM


All times are GMT -5. The time now is 03:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration