解决网卡段错误

This commit is contained in:
shaozhimin 2024-04-25 20:20:27 +08:00
parent 018fcfa11f
commit 6f5749f26c
1 changed files with 31 additions and 25 deletions

View File

@ -837,6 +837,7 @@ char *kdk_nc_get_duplex(char *nc)
{
char path[1024] = {0}, real_path[1024];
char res[1024] = {0};
char *result = NULL;
sprintf(path, "/sys/class/net/%s/duplex", nc);
if (NULL == realpath(path, real_path))
return NULL;
@ -845,7 +846,8 @@ char *kdk_nc_get_duplex(char *nc)
return NULL;
fgets(res, 1024, fp);
strstripspace(res);
char *result = strdup(res);
if(strlen(res) != 0)
result = strdup(res);
fclose(fp);
return result;
}
@ -962,14 +964,6 @@ char *kdk_nc_get_broadAddr(const char *eth_name, const char *address)
memset(ip, 0, 32);
strcpy(ip, address);
char *broadAddr = (char *)malloc(sizeof(char) * 64);
if(!broadAddr)
{
free(ip);
return NULL;
}
memset(broadAddr, 0, 64);
struct nl_sock *sk = nl_socket_alloc();
nl_connect(sk, NETLINK_ROUTE);
@ -997,8 +991,19 @@ char *kdk_nc_get_broadAddr(const char *eth_name, const char *address)
rtnl_link_put(link);
free(ip);
strcpy(broadAddr, ctx.broadaddr);
free(ctx.broadaddr);
char *broadAddr = NULL;
if(ctx.broadaddr)
{
broadAddr = (char *)malloc(sizeof(ctx.broadaddr) + 1);
if(!broadAddr)
{
return NULL;
}
memset(broadAddr, 0, sizeof(ctx.broadaddr) + 1);
strcpy(broadAddr, ctx.broadaddr);
free(ctx.broadaddr);
}
nl_close(sk);
nl_socket_free(sk);
return broadAddr;
@ -1007,7 +1012,6 @@ error_out:
nl_close(sk);
nl_socket_free(sk);
free(ip);
free(broadAddr);
return NULL;
}
@ -1020,21 +1024,15 @@ char *kdk_nc_get_netmask(const char *eth_name, const char *addr)
int ret = 0;
struct nl_cache *addr_cache;
struct rtnl_link *link;
char *ip = (char *)malloc(sizeof(char) * 32);
char *ip = (char *)malloc(sizeof(addr) + 1);
if(!ip)
{
return NULL;
}
memset(ip, 0, 32);
memset(ip, 0, sizeof(addr) + 1);
strcpy(ip, addr);
char *netmask = (char *)malloc(sizeof(char) * 64);
if(!netmask)
{
free(ip);
return NULL;
}
memset(netmask, 0, 64);
char *netmask = NULL;
struct nl_sock *sk = nl_socket_alloc();
nl_connect(sk, NETLINK_ROUTE);
@ -1062,9 +1060,18 @@ char *kdk_nc_get_netmask(const char *eth_name, const char *addr)
nl_cache_put(addr_cache);
rtnl_link_put(link);
free(ip);
strcpy(netmask, ctx.netmask);
free(ctx.netmask);
if(ctx.netmask)
{
netmask = (char *)malloc(sizeof(char) * 64);
if(!netmask)
{
return NULL;
}
memset(netmask, 0, 64);
strcpy(netmask, ctx.netmask);
free(ctx.netmask);
}
nl_close(sk);
nl_socket_free(sk);
return netmask;
@ -1073,7 +1080,6 @@ error_out:
nl_close(sk);
nl_socket_free(sk);
free(ip);
free(netmask);
return NULL;
}