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 // taken from ifconfig
16 static int vp_com_set_flag( int skfd, const char *ifname, short flag )
20 vp_os_memset( &ifr, 0, sizeof(struct ifreq) );
21 strncpy( ifr.ifr_name, ifname, IFNAMSIZ );
23 if( ioctl( skfd, SIOCGIFFLAGS, &ifr ) < 0 )
26 strncpy( ifr.ifr_name, ifname, IFNAMSIZ );
27 ifr.ifr_flags |= flag;
29 if( ioctl(skfd, SIOCSIFFLAGS, &ifr ) < 0 )
35 static int vp_com_set_ip( int skfd, const char* ifname, int cmd, int ip )
38 struct sockaddr_in* addr;
40 vp_os_memset( &ifr, 0, sizeof(struct ifreq) );
41 strncpy( ifr.ifr_name, ifname, IFNAMSIZ );
43 // Check if an ip is already set
44 if( ioctl( skfd, SIOCGIFADDR, &ifr ) != -1 )
46 addr = (struct sockaddr_in*) &ifr.ifr_addr;
48 // Is this the same ip?
49 if( addr->sin_family == AF_INET && ip == addr->sin_addr.s_addr )
52 // No so we try to delete it
53 if( ioctl( skfd, SIOCDIFADDR, &ifr ) < 0 )
55 DEBUG_PRINT_SDK("Unable to delete old ip address - You should remove your interface %s\n", ifname);
61 vp_os_memset( &ifr, 0, sizeof(struct ifreq) );
62 strncpy( ifr.ifr_name, ifname, IFNAMSIZ );
64 addr = (struct sockaddr_in*) &ifr.ifr_addr;
65 addr->sin_family = AF_INET;
66 addr->sin_addr.s_addr = ip;
68 strncpy( ifr.ifr_name, ifname, IFNAMSIZ );
70 if( ioctl( skfd, cmd, &ifr ) < 0 )
77 int vp_com_config_itf( const char* interface, const char * ip, const char* broadcast, const char* netmask )
81 int sck = socket( PF_INET, SOCK_DGRAM, 0 );
86 if( vp_com_set_ip( sck, interface, SIOCSIFADDR, inet_addr(ip) ) < 0 )
87 goto vp_com_config_itf_end;
89 if( vp_com_set_ip( sck, interface, SIOCSIFBRDADDR, inet_addr(broadcast) ) < 0 )
90 goto vp_com_config_itf_end;
92 if( vp_com_set_ip( sck, interface, SIOCSIFNETMASK, inet_addr(netmask) ) < 0 )
93 goto vp_com_config_itf_end;
95 if( vp_com_set_flag( sck, interface, IFF_UP | IFF_RUNNING ) < 0 )
96 goto vp_com_config_itf_end;
100 vp_com_config_itf_end: