1 #include <VP_Os/vp_os_malloc.h>
2 #include <VP_Os/vp_os_print.h>
4 #include <VP_Com/linux/vp_com_config_itf.h>
7 #include <sys/socket.h>
10 #include <netinet/in.h>
12 #include <arpa/inet.h>
15 /********************************************************************
16 * Static function declarations
17 *******************************************************************/
18 static int vp_com_set_interface_flags( int skfd, const char *ifname, int isUp );
20 /********************************************************************
22 *******************************************************************/
23 // taken from ifconfig
24 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 )
47 static int vp_com_set_ip( int skfd, const char* ifname, int cmd, int ip )
50 struct sockaddr_in* addr;
51 struct sockaddr_in local_sin;
53 vp_os_memset( &ifr, 0, sizeof(struct ifreq) );
54 strncpy( ifr.ifr_name, ifname, IFNAMSIZ );
56 // Check if an ip is already set
57 if( ioctl( skfd, SIOCGIFADDR, &ifr ) != -1 )
59 addr = (struct sockaddr_in*) &ifr.ifr_addr;
61 // Is this the same ip?
62 if( addr->sin_family == AF_INET && ip == addr->sin_addr.s_addr )
65 // No so we try to delete it
66 if( ioctl( skfd, SIOCDIFADDR, &ifr ) < 0 )
68 DEBUG_PRINT_SDK("Unable to delete old ip address - You should remove your interface %s\n", ifname);
74 vp_os_memset( &ifr, 0, sizeof(struct ifreq) );
75 vp_os_memset( &local_sin, 0, sizeof(local_sin) );
76 strncpy( ifr.ifr_name, ifname, IFNAMSIZ );
78 addr = (struct sockaddr_in*) &ifr.ifr_addr;
80 local_sin.sin_family = AF_INET;
81 local_sin.sin_addr.s_addr = ip;
82 vp_os_memcpy(addr,&local_sin,sizeof(local_sin));
84 strncpy( ifr.ifr_name, ifname, IFNAMSIZ );
86 if( ioctl( skfd, cmd, &ifr ) < 0 )
93 int vp_com_config_itf( const char* interface, const char * ip, const char* broadcast, const char* netmask )
97 int sck = socket( PF_INET, SOCK_DGRAM, 0 );
102 if( vp_com_set_interface_flags( sck, interface, 0 ) < 0 )
103 goto vp_com_config_itf_end;
105 if( vp_com_set_ip( sck, interface, SIOCSIFADDR, inet_addr(ip) ) < 0 )
106 goto vp_com_config_itf_end;
108 if( vp_com_set_ip( sck, interface, SIOCSIFBRDADDR, inet_addr(broadcast) ) < 0 )
109 goto vp_com_config_itf_end;
111 if( vp_com_set_ip( sck, interface, SIOCSIFNETMASK, inet_addr(netmask) ) < 0 )
112 goto vp_com_config_itf_end;
114 if( vp_com_set_interface_flags( sck, interface, 1 ) < 0 )
115 goto vp_com_config_itf_end;
119 vp_com_config_itf_end: