I have been trying to create a program in C++ that can take down an interface, change an IP of an interface, or up a downed interface. I have searched around here and found some examples that helped.
However, I am now running into the problem of when I change the IP address of an existing interface lets say eth0, the change occurs correctly, however, I no longer can access the internet until i restart the interface, in which, after the restart, the IP address reverts back to the original IP
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <sys/socket.h>
#include <strings.h>
#include <unistd.h>
#include <string.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h> /* offsetof */
#include <net/if.h>
#include <linux/sockios.h>
#include <netinet/in.h>
#if __GLIBC__ >=2 && __GLIBC_MINOR >= 1
#include <netpacket/packet.h>
#include <net/ethernet.h>
#else
#include <asm/types.h>
#include <linux/if_ether.h>
#endif
#define IFNAME "eth0"
#define HOST "192.168.1.204"
#define ifreq_offsetof(x) offsetof(struct ifreq, x)
int shared_sock,n, received, receivelen;
struct sockaddr_in receivesocket;
socklen_t len;
char mesg[100];
unsigned char assist_id[] = {0x18, 0xDE, 0x1B};
void test_down(int sockfd){
//int sockfd;
struct ifreq ifr;
//sockfd = socket(AF_INET, SOCK_DGRAM, 0);
//if (sockfd < 0)
// return;
memset(&ifr, 0, sizeof ifr);
strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
ifr.ifr_flags &= ~IFF_UP;
ioctl(sockfd, SIOCSIFFLAGS, &ifr);
//close(sockfd);
}
void test_up(int sockfd){
//int sockfd;
struct ifreq ifr;
//sockfd = socket(AF_INET, SOCK_DGRAM, 0);
// if (sockfd < 0)
// return;
memset(&ifr, 0, sizeof ifr);
strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
ifr.ifr_flags |= IFF_UP;
ioctl(sockfd, SIOCSIFFLAGS, &ifr);
//close(sockfd);
}
void test_function(){
struct ifreq ifr;
struct sockaddr_in sai;
int sockfd; /* socket fd we use to manipulate stuff with */
int selector;
unsigned char mask;
char *p;
/* Create a channel to the NET kernel. */
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
/* get interface name */
strncpy(ifr.ifr_name, IFNAME, IFNAMSIZ);
memset(&sai, 0, sizeof(struct sockaddr));
sai.sin_family = AF_INET;
sai.sin_port = 0;
sai.sin_addr.s_addr = inet_addr(HOST);
p = (char *) &sai;
memcpy( (((char *)&ifr + ifreq_offsetof(ifr_addr) )), p, sizeof(struct sockaddr));
test_down(sockfd);
ioctl(sockfd, SIOCSIFADDR, &ifr);
ioctl(sockfd, SIOCGIFFLAGS, &ifr);
ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
// ifr.ifr_flags &= ~selector; // unset something
ioctl(sockfd, SIOCSIFFLAGS, &ifr);
test_up(sockfd);
close(sockfd);
return;
}
int main()
{
shared_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
/* my address */
memset(&receivesocket, 0, sizeof(receivesocket));
receivesocket.sin_family = AF_INET;
receivesocket.sin_addr.s_addr = htonl(INADDR_ANY);
receivesocket.sin_port = htons(11000);
receivelen = sizeof(receivesocket);
if (bind(shared_sock, (struct sockaddr *) &receivesocket, receivelen) < 0) {
perror("bind");
return 1;
}
memset(mesg, 0, 100);
while(true){
if ((received = recvfrom(shared_sock, mesg, 100, 0, NULL, NULL)) < 0){
perror("recvfrom");
return 1;
}
printf("Received the following %d:\n", received);
printf("%s\n",mesg);
fflush(stdout);
//test_up();
test_function();
//test_up();
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire