Linux下ifconfig的简易实现

最近看了一下net-tools的源码,然后简易实现了ifconfig命令。    目前可以支持修改IP,MAC地址,netmask 以及gateway。#include #include #include #include #include #include #include #include #include #include #include #inclu
摘要由CSDN通过智能技术生成

    最近看了一下net-tools的源码,然后简易实现了ifconfig命令。

    目前可以支持修改IP,MAC地址,netmask 以及gateway。


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <net/route.h>
#include <linux/if_vlan.h>
#include <linux/sockios.h>
#include <fcntl.h>
#include <errno.h>

static int is_valid_ip(const char *ip)
{
	if (ip == NULL || strlen(ip) < 7)
	{
		return -1;
	}

	int len = strlen(ip);
	const char *start = ip;
	const char *end = ip + len - 1;
	int ret = 0;
	int dot_count = 0;
	int cur_val = 0;
	int val = -1;

	while (end > start && isspace(*end))
	{
		--end;
	}

	while (start < end && isspace(*start))
	{
		++start;
	}

	if (start >= end || isdigit(*start) == 0|| isdigit(*end) == 0)
	{
		return -1;
	}

	while (start <= end)
	{
		while (start <= end && '0' <= *start && *start <= '9')
		{
			cur_val = 10 * cur_val + (*start - '0');
			if (cur_val < 
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值