解决网卡段错误

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