ArDrone SDK 1.8 added
[mardrone] / mardrone / ARDrone_SDK_Version_1_8_20110726 / ARDroneLib / VP_SDK / VP_Com / win32 / vp_com_config_itf.c
1 /* Modified version for the Win32 example.
2 stephane.piskorski.ext@parrot.com
3 July 2010.
4 */
5
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>
9 #include <string.h>
10 //#include <winsock2.h>
11
12
13 /********************************************************************
14  * Static function declarations
15  *******************************************************************/
16 static int vp_com_set_interface_flags( int skfd, const char *ifname, int isUp );
17
18 /********************************************************************
19  * Static functions
20  *******************************************************************/
21 // taken from ifconfig
22 static int vp_com_set_interface_flags( int skfd, const char *ifname, int isUp)
23 {
24 #ifndef _WIN32
25
26         struct ifreq ifr;
27
28         vp_os_memset( &ifr, 0, sizeof(struct ifreq) );
29         strncpy( ifr.ifr_name, ifname, IFNAMSIZ );
30
31         if( ioctl( skfd, SIOCGIFFLAGS, &ifr ) < 0 )
32                 return -1;
33
34         strncpy( ifr.ifr_name, ifname, IFNAMSIZ );
35
36         if( isUp )
37                 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
38         else
39                 ifr.ifr_flags &= ~IFF_UP;
40
41         if( ioctl(skfd, SIOCSIFFLAGS, &ifr ) < 0 )
42                 return -1;
43                 */
44 #endif
45         return 0;
46 }
47
48 static int vp_com_set_ip( int skfd, const char* ifname, int cmd, int ip )
49 {
50         #ifndef _WIN32
51   struct ifreq ifr;
52   struct sockaddr_in* addr;
53   struct sockaddr_in local_sin;
54
55   vp_os_memset( &ifr, 0, sizeof(struct ifreq) );
56   strncpy( ifr.ifr_name, ifname, IFNAMSIZ );
57
58   // Check if an ip is already set
59   if( ioctl( skfd, SIOCGIFADDR, &ifr ) != -1 )
60   {
61     addr = (struct sockaddr_in*) &ifr.ifr_addr;
62
63     // Is this the same ip?
64     if( addr->sin_family == AF_INET && ip == addr->sin_addr.s_addr )
65       return 0;
66 /*
67     // No so we try to delete it
68     if( ioctl( skfd, SIOCDIFADDR, &ifr ) < 0 )
69     {
70       DEBUG_PRINT_SDK("Unable to delete old ip address - You should remove your interface %s\n", ifname);
71       return -1;
72     }
73 */
74   }
75
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 );
79
80   addr = (struct sockaddr_in*) &ifr.ifr_addr;
81
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));
85
86   strncpy( ifr.ifr_name, ifname, IFNAMSIZ );
87
88   if( ioctl( skfd, cmd, &ifr ) < 0 )
89     return -1;
90 #endif
91   return 0;
92 }
93
94
95 int vp_com_config_itf( const char* _interface, const char * _ip, const char* _broadcast, const char* _netmask )
96 {
97         int ret = -1;
98         #ifndef _WIN32
99   
100
101   int sck = socket( PF_INET, SOCK_DGRAM, 0 );
102
103   if( sck < 0 )
104     return -1;
105
106   if( vp_com_set_interface_flags( sck, interface, 0 ) < 0 )
107     goto vp_com_config_itf_end;
108
109   if( vp_com_set_ip( sck, interface, SIOCSIFADDR, inet_addr(ip) ) < 0 )
110     goto vp_com_config_itf_end;
111
112   if( vp_com_set_ip( sck, interface, SIOCSIFBRDADDR, inet_addr(broadcast) ) < 0 )
113     goto vp_com_config_itf_end;
114
115   if( vp_com_set_ip( sck, interface, SIOCSIFNETMASK, inet_addr(netmask) ) < 0 )
116     goto vp_com_config_itf_end;
117
118   if( vp_com_set_interface_flags( sck, interface, 1 ) < 0 )
119     goto vp_com_config_itf_end;
120
121   ret = 0;
122
123 vp_com_config_itf_end:
124
125   close(sck);
126 #else
127         /* Dont change configuration under Windows. The user must do it manually. */ 
128         ret = 0;
129 #endif
130   return ret;
131 }