 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Xes Guest
|
Posted: Mon Apr 25, 2005 3:31 pm Post subject: Récupérer depuis C++ IP et Masque |
|
|
Bonjour,
J'aimerais récupérer depuis un programme écrit en C++, l'IP ainsi que le
MASQUE d'une de mes cartes réseaux.
Voici ce que j'ai trouvé sur le net et remis à ma sauce :
------------------------------------------------------------------
#define IFNAME "wlan0"
int fd;
struct ifreq ifr;
u_char *addr;
u_char *mask;
fd = socket (AF_INET, SOCK_DGRAM,IPPROTO_IP);
memset (&ifr, 0, sizeof (struct ifreq));
strcpy (ifr.ifr_name, IFNAME);
ifr.ifr_addr.sa_family = AF_INET;
ioctl(fd, SIOCGIFADDR, &ifr);
addr=(u_char*)&(((struct sockaddr_in * )&ifr.ifr_addr)->sin_addr);
mask=(u_char*)&(((struct sockaddr_in * )&ifr.ifr_netmask)->sin_addr);
------------------------------------------------------------------
addr contient bien l'adresse IP mais mask ne contient pas le masque mais
également l'adresse IP
Comment pourais je faire pour récupérer ce masque svp ?
Merci pour votre aide
|
|
| Back to top |
|
 |
Samuel Krempp Guest
|
Posted: Tue Apr 26, 2005 6:53 am Post subject: Re: Récupérer depuis C++ IP et Masque |
|
|
[email]pinonpierre (AT) free (DOT) fr[/email] (25 April 2005 17:31,
news:<426d0d7a$0$20639$626a14ce (AT) news (DOT) free.fr>) a écrit :
| Quote: | addr=(u_char*)&(((struct sockaddr_in * )&ifr.ifr_addr)->sin_addr);
mask=(u_char*)&(((struct sockaddr_in * )&ifr.ifr_netmask)->sin_addr);
|
ce n'est pas une question de C++, d'autre groupes seraient certainement plus
adaptés.
--
Sam
|
|
| Back to top |
|
 |
Vincent Kergonna Guest
|
Posted: Tue Apr 26, 2005 3:20 pm Post subject: Re: Récupérer depuis C++ IP et Masque |
|
|
Xes <pinonpierre (AT) free (DOT) fr> wrote
| Quote: | Bonjour,
J'aimerais récupérer depuis un programme écrit en C++, l'IP ainsi que le
MASQUE d'une de mes cartes réseaux.
Voici ce que j'ai trouvé sur le net et remis à ma sauce :
------------------------------------------------------------------
#define IFNAME "wlan0"
int fd;
struct ifreq ifr;
u_char *addr;
u_char *mask;
fd = socket (AF_INET, SOCK_DGRAM,IPPROTO_IP);
memset (&ifr, 0, sizeof (struct ifreq));
strcpy (ifr.ifr_name, IFNAME);
ifr.ifr_addr.sa_family = AF_INET;
ioctl(fd, SIOCGIFADDR, &ifr);
addr=(u_char*)&(((struct sockaddr_in * )&ifr.ifr_addr)->sin_addr);
mask=(u_char*)&(((struct sockaddr_in * )&ifr.ifr_netmask)->sin_addr);
------------------------------------------------------------------
addr contient bien l'adresse IP mais mask ne contient pas le masque mais
également l'adresse IP
|
C'est normal, il faut réinitialiser la structure ifr pour récupérer le masque.
Comme ceci:
ioctl(fd, SIOCGIFADDR, &ifr);
addr=(u_char*)&(((struct sockaddr_in * )&ifr.ifr_addr)->sin_addr);
ioctl (fd, SIOCGIFNETMASK, &ifr);
mask=(u_char*)&(((struct sockaddr_in * )&ifr.ifr_netmask)->sin_addr);
Je n'ai pas testé mais ca doit fonctionner je pense.
| Quote: |
Comment pourais je faire pour récupérer ce masque svp ?
Merci pour votre aide
|
|
|
| Back to top |
|
 |
Xes Guest
|
Posted: Tue Apr 26, 2005 3:50 pm Post subject: Re: Récupérer depuis C++ IP et Masque |
|
|
Vincent Kergonna a écrit :
| Quote: | Xes <pinonpierre (AT) free (DOT) fr> wrote
Bonjour,
J'aimerais récupérer depuis un programme écrit en C++, l'IP ainsi que le
MASQUE d'une de mes cartes réseaux.
Voici ce que j'ai trouvé sur le net et remis à ma sauce :
------------------------------------------------------------------
#define IFNAME "wlan0"
int fd;
struct ifreq ifr;
u_char *addr;
u_char *mask;
fd = socket (AF_INET, SOCK_DGRAM,IPPROTO_IP);
memset (&ifr, 0, sizeof (struct ifreq));
strcpy (ifr.ifr_name, IFNAME);
ifr.ifr_addr.sa_family = AF_INET;
ioctl(fd, SIOCGIFADDR, &ifr);
addr=(u_char*)&(((struct sockaddr_in * )&ifr.ifr_addr)->sin_addr);
mask=(u_char*)&(((struct sockaddr_in * )&ifr.ifr_netmask)->sin_addr);
------------------------------------------------------------------
addr contient bien l'adresse IP mais mask ne contient pas le masque mais
également l'adresse IP
C'est normal, il faut réinitialiser la structure ifr pour récupérer le masque.
Comme ceci:
ioctl(fd, SIOCGIFADDR, &ifr);
addr=(u_char*)&(((struct sockaddr_in * )&ifr.ifr_addr)->sin_addr);
ioctl (fd, SIOCGIFNETMASK, &ifr);
mask=(u_char*)&(((struct sockaddr_in * )&ifr.ifr_netmask)->sin_addr);
Je n'ai pas testé mais ca doit fonctionner je pense.
|
C'était bien ca !
Merci pour ton aide ..
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|