473,378 Members | 1,580 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Get list of all IP addresses

I have been trying to get all ip addresses from all interfaces, with
the code listed below.
However, all I get is the 1st IP only. I have configured 2 IPs on a
single interface, I want to get both from my c++ code.
thanks
svirdi

#include <unistd.h>
#include <stropts.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <net/if.h>
#include <sys/ioctl.h>
//#include <sys/sockio.h>
int main(int argn,char** argv)
{
int sock,n;
struct ifreq *ifr;
struct ifconf ifc;
char buf[1024],addres[16];
unsigned char* adr;
sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
// sock = socket(2, 2, 0);
printf("sock=%d\n", sock);
if (sock == -1)
{
printf("There is trouble of sock!! Exiting\n\n");
return 0;
}
ifc.ifc_buf = buf;
//ifc.ifc_len = 1024;
int retVal = ioctl(sock, SIOCGIFCONF, &ifc);
printf("retval ioctl(sock, SIOCGIFCONF, &ifc)=%d\n\n", retVal);

if (retVal < 0)
{
printf("There is trouble with ioctl\n\n");
close (sock);
return 0;
}
n = ifc.ifc_len/sizeof(struct ifreq);
for (ifr = ifc.ifc_req; n 0; n--, ifr++)
{
int rval = ioctl( sock, SIOCGIFADDR, ifr);
printf(" ioctl( sock, SIOCGIFADDR, ifr) rval = %d\n",
rval);
if (ifr->ifr_addr.sa_family == AF_INET ||
ifr->ifr_addr.sa_family == AF_INET6)
{
adr = reinterpret_cast<unsigned
char*>(ifr->ifr_ifru.ifru_addr.sa_data+2);
snprintf(addres,16,"%i.%i.%i.%i",adr[0],adr[1],adr[2],adr[3]);
printf("%s = %s\n",ifr->ifr_name,addres);
}
}
close(sock);
return 0;
}

Aug 8 '06 #1
2 5512
svirdi wrote:
I have been trying to get all ip addresses from all interfaces, with
the code listed below.
However, all I get is the 1st IP only. I have configured 2 IPs on a
single interface, I want to get both from my c++ code.
[..]
We cannot help you with that. Whatever mechanism you use to get all
IP addresses, it has nothing to do with C++. Neither the language nor
the library has any means for Internet communication, it is all done
using the functionality provided by your OS. Ask in the OS newsgroup.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 8 '06 #2
svirdi wrote:
I have been trying to get all ip addresses from all interfaces, with
the code listed below.
However, all I get is the 1st IP only. I have configured 2 IPs on a
single interface, I want to get both from my c++ code.
thanks
svirdi

#include <unistd.h>
#include <stropts.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <net/if.h>
#include <sys/ioctl.h>
I recommend comp.unix.programmer.


Brian
Aug 8 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: deko | last post by:
I have an html drop-down list (they're called combo boxes in vb, not sure what they're called in html): <select name="ipSelect" size="1" style="width:140px;"></select> The user is supposed to...
5
by: Paul Cheevers | last post by:
Hi, This is driving me nuts to say the least!!!!! I am trying to send an email from some server side ASP code and the CC field is giving me some problems. The code works fine if I have one...
15
by: C White | last post by:
I've got another drop list problem I am using the following code where users select a name, but it should pass a name and email into the table <select name="user"> <option value="<%...
4
by: Christine Forber | last post by:
I wonder if anyone knows of some javascript code to check a comma-delimited list of email addresses for basic formating. What I'm looking for is the javascript code to check a form field on form...
3
by: Mark V. | last post by:
Here's what I have and I'm stumped. I have a table that has several thousand names and addresses. I only want to send to one address in a household. So what I would like to do is create a new...
7
by: Evangelista Sami | last post by:
Hi all i have implemented a list type as an array of pointer like this typedef struct { int nb_elements; void **elements; } list; to avoid having a pointer for each element as it is done...
4
by: Andy M | last post by:
ALERT There is a person by the name of Mike Cox who's trying to turn this mailing list into a Big-8 newsgroup. Many of you know that this and most of the other postresql mailing lists are...
4
by: valeberry | last post by:
//Index.php <html><head><title>Mailing List Administration</title></head><body> <br> <center><H1>Mailing List Administration</H1></center> Send an email to a mailing list: <form method=post...
4
by: chris | last post by:
I need to maintain a list of subscribers to an email list for a "newsletter" that will be sent via a web form probably once a month. I anticipate low numbers--tens to maybe one hundred subscribers...
3
Sandboxer
by: Sandboxer | last post by:
I am not a coder, so please bear with me while I attempt to explain in only the surface language of Access. I have created a form to input all information about each of my customers. The base form...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.