Linux下C语言获取和修改本机IP地址
2015-02-09 10:35
387 查看
<pre name="code" class="cpp"><pre name="code" class="cpp">/* * 头文件 */ #ifndef _GET_IP_H #define _GET_IP_H extern char *get_ip(char *ip); extern int set_hand_ip(const char *ip); extern int check_right_ip(const char *ip); #endif //============================================================== <pre name="code" class="cpp">/* * 源代码 */
#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<string.h> #include<sys/types.h> #include<arpa/inet.h> #include<net/if.h> #include<string.h> #include<signal.h> #include<sys/wait.h> #include<sys/ioctl.h> #include<regex.h> #define ETH_NAME "eth0" // /************************************************************* * 函数功能: 通过正则表达式检测是否为IP地址 * 参数类型: 需要检测的IP地址 * 返回类型: 成功返回0,失败返回-1 **************************************************************/ int check_right_ip(const char *ip) { int status = 0; int cflags = REG_EXTENDED; regmatch_t pmatch[1]; const size_t nmatch = 1; regex_t reg; char str_ip[30] = ""; const char *pattern = "[0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}";//存在局限 strcpy(str_ip,ip); regcomp(®, pattern, cflags); status = regexec(®,str_ip,nmatch,pmatch,0); if(status==REG_NOMATCH) { printf("No match\n"); return -1; } else if(status==0) { return 0; } regfree(®); return 0; } /************************************************************* * 函数功能: 获取IP地址 * 参数类型: IP地址存放位置 * 返回类型: **************************************************************/ char *get_ip(char *ip) { int sock; struct sockaddr_in sin; struct ifreq ifr; char *temp_ip = NULL; sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock == -1) { perror("socket"); return NULL; } strncpy(ifr.ifr_name, ETH_NAME, IFNAMSIZ-1); ifr.ifr_name[IFNAMSIZ - 1] = 0; if (ioctl(sock, SIOCGIFADDR, &ifr) < 0) { perror("ioctl"); return NULL; } memcpy(&sin, &ifr.ifr_addr, sizeof(sin)); temp_ip = inet_ntoa(sin.sin_addr); strcpy(ip,temp_ip); fprintf(stdout, "eth0: %s\n", temp_ip); return ip; } /************************************************************* * 函数功能: 手动设置IP地址 * 参数类型: 要设置的IP地址 * 返回类型: 成功返回0,失败返回-1 **************************************************************/ int set_hand_ip(const char *ipaddr) { int sock_set_ip; struct sockaddr_in sin_set_ip; struct ifreq ifr_set_ip; memset( &ifr_set_ip,0,sizeof(ifr_set_ip)); if( ipaddr == NULL ) { return -1; } sock_set_ip = socket( AF_INET, SOCK_STREAM, 0 ); //printf("sock_set_ip=====%d\n",sock_set_ip); if(sock_set_ip<0) { perror("socket create failse...SetLocalIp!"); return -1; } memset( &sin_set_ip, 0, sizeof(sin_set_ip)); strncpy(ifr_set_ip.ifr_name, ETH_NAME, sizeof(ifr_set_ip.ifr_name)-1); sin_set_ip.sin_family = AF_INET; sin_set_ip.sin_addr.s_addr = inet_addr(ipaddr); memcpy( &ifr_set_ip.ifr_addr, &sin_set_ip, sizeof(sin_set_ip)); printf("ipaddr===%s\n",ipaddr); if( ioctl( sock_set_ip, SIOCSIFADDR, &ifr_set_ip) < 0 ) { perror( "Not setup interface"); return -1; } //设置激活标志 ifr_set_ip.ifr_flags |= IFF_UP |IFF_RUNNING; //get the status of the device if( ioctl( sock_set_ip, SIOCSIFFLAGS, &ifr_set_ip ) < 0 ) { perror("SIOCSIFFLAGS"); return -1; } close( sock_set_ip ); return 0; }
相关文章推荐
- linux C 获取与修改IP地址
- linux 利用ioctl函数获取IP地址、子网掩码、MAC地址和修改本机IP地址、子网掩码、网关
- linux C 获取与修改IP地址
- Linux下C语言获取本机IP地址
- linux C 获取与修改IP地址
- linux下 获取本机ip地址和掩码地址
- LINUX修改IP地址_ifcfg-eth0详解
- 常用的Linux 命令 -- 修改主机名和ip地址
- LINUX--修改IP地址
- linux服务修改ip地址
- 在linux下 获取,修改网关GateWay的两个函数
- LINUX--修改IP地址
- 在Linux下获取多个ip地址(转载)
- Linux 修改 IP地址 和 网关
- linux下修改本机Ip地址的方法
- 在linux下 获取,修改本机IP地址的两个函数
- linux获取IP地址
- linux系统下获取IP地址
- 在Linux下获取IP地址
- Linux系统修改IP地址的方法