1 /* Modified version for the Win32 example.
2 stephane.piskorski.ext@parrot.com
6 #include <VP_Os/vp_os_malloc.h>
7 #include <VP_Os/vp_os_print.h>
8 #include <VP_Com/win32/vp_com_config_itf.h>
10 //#include <winsock2.h>
13 /********************************************************************
14 * Static function declarations
15 *******************************************************************/
16 static int vp_com_set_interface_flags( int skfd, const char *ifname, int isUp );
18 /********************************************************************
20 *******************************************************************/
21 // taken from ifconfig
22 static int vp_com_set_interface_flags( int skfd, const char *ifname, int isUp)
28 vp_os_memset( &ifr, 0, sizeof(struct ifreq) );
29 strncpy( ifr.ifr_name, ifname, IFNAMSIZ );
31 if( ioctl( skfd, SIOCGIFFLAGS, &ifr ) < 0 )
34 strncpy( ifr.ifr_name, ifname, IFNAMSIZ );
37 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
39 ifr.ifr_flags &= ~IFF_UP;
41 if( ioctl(skfd, SIOCSIFFLAGS, &ifr ) < 0 )
48 static int vp_com_set_ip( int skfd, const char* ifname, int cmd, int ip )
52 struct sockaddr_in* addr;
53 struct sockaddr_in local_sin;
55 vp_os_memset( &ifr, 0, sizeof(struct ifreq) );
56 strncpy( ifr.ifr_name, ifname, IFNAMSIZ );
58 // Check if an ip is already set
59 if( ioctl( skfd, SIOCGIFADDR, &ifr ) != -1 )
61 addr = (struct sockaddr_in*) &ifr.ifr_addr;
63 // Is this the same ip?
64 if( addr->sin_family == AF_INET && ip == addr->sin_addr.s_addr )
67 // No so we try to delete it
68 if( ioctl( skfd, SIOCDIFADDR, &ifr ) < 0 )
70 DEBUG_PRINT_SDK("Unable to delete old ip address - You should remove your interface %s\n", ifname);
76 vp_os_memset( &ifr, 0, sizeof(struct ifreq) );
77 vp_os_memset( &local_sin, 0, sizeof(local_sin) );
78 strncpy( ifr.ifr_name, ifname, IFNAMSIZ );
80 addr = (struct sockaddr_in*) &ifr.ifr_addr;
82 local_sin.sin_family = AF_INET;
83 local_sin.sin_addr.s_addr = ip;
84 vp_os_memcpy(addr,&local_sin,sizeof(local_sin));
86 strncpy( ifr.ifr_name, ifname, IFNAMSIZ );
88 if( ioctl( skfd, cmd, &ifr ) < 0 )
95 int vp_com_config_itf( const char* _interface, const char * _ip, const char* _broadcast, const char* _netmask )
101 int sck = socket( PF_INET, SOCK_DGRAM, 0 );
106 if( vp_com_set_interface_flags( sck, interface, 0 ) < 0 )
107 goto vp_com_config_itf_end;
109 if( vp_com_set_ip( sck, interface, SIOCSIFADDR, inet_addr(ip) ) < 0 )
110 goto vp_com_config_itf_end;
112 if( vp_com_set_ip( sck, interface, SIOCSIFBRDADDR, inet_addr(broadcast) ) < 0 )
113 goto vp_com_config_itf_end;
115 if( vp_com_set_ip( sck, interface, SIOCSIFNETMASK, inet_addr(netmask) ) < 0 )
116 goto vp_com_config_itf_end;
118 if( vp_com_set_interface_flags( sck, interface, 1 ) < 0 )
119 goto vp_com_config_itf_end;
123 vp_com_config_itf_end:
127 /* Dont change configuration under Windows. The user must do it manually. */