添加EAGAIN处理
This commit is contained in:
@@ -164,8 +164,14 @@ int readNlSock(int sockFd, char *bufPtr, int seqNum, int pId)
|
||||
do{
|
||||
//收到内核的应答
|
||||
if((readLen = recv(sockFd, bufPtr, BUFSIZE - msgLen, 0)) < 0)
|
||||
{
|
||||
printf("sock read error!");
|
||||
{
|
||||
#if 1
|
||||
if(errno == EAGAIN)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
printf("sock recv return for readLen < 0!");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -173,11 +179,11 @@ int readNlSock(int sockFd, char *bufPtr, int seqNum, int pId)
|
||||
//检查header是否有效
|
||||
if((NLMSG_OK(nlHdr, readLen) == 0) || (nlHdr->nlmsg_type == NLMSG_ERROR))
|
||||
{
|
||||
printf("error in recieved packet!\n");
|
||||
printf("sock recv return for header error!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(nlHdr->nlmsg_type == NLMSG_DONE)
|
||||
if(nlHdr->nlmsg_type == NLMSG_DONE)//内核返回最后一条消息的类型为NLMSG_DONE
|
||||
{
|
||||
break;
|
||||
}
|
||||
@@ -197,21 +203,26 @@ int readNlSock(int sockFd, char *bufPtr, int seqNum, int pId)
|
||||
}
|
||||
|
||||
//分析返回的路由信息
|
||||
void parseRoutes(struct nlmsghdr *nlHdr, struct route_info *rtInfo,char *gateway,unsigned int len)
|
||||
void parseRoutes(struct nlmsghdr *nlHdr,char *gateway,unsigned int len)
|
||||
{
|
||||
struct rtmsg *rtMsg = NULL;
|
||||
struct rtattr *rtAttr = NULL;
|
||||
int rtLen;
|
||||
char dst_address[MAX_STR_LEN] = "";
|
||||
struct route_info rtInfo;
|
||||
|
||||
memset(&rtInfo, 0, sizeof(struct route_info));
|
||||
rtMsg = (struct rtmsg *)NLMSG_DATA(nlHdr);
|
||||
// If the route is not for AF_INET or does not belong to main routing table
|
||||
//then return.
|
||||
//RT_TABLE_LOCAL存储目的地址是本机的路由表项,这些目的地址就是为各个网卡配置的IP地址;
|
||||
//RT_TABLE_MAIN存储到其它主机的路由表项;
|
||||
if((rtMsg->rtm_family != AF_INET) || (rtMsg->rtm_table != RT_TABLE_MAIN))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
rtAttr = (struct rtattr *)RTM_RTA(rtMsg);
|
||||
rtAttr = (struct rtattr *)RTM_RTA(rtMsg); //路由信息
|
||||
rtLen = RTM_PAYLOAD(nlHdr);
|
||||
|
||||
for(;RTA_OK(rtAttr,rtLen);rtAttr = RTA_NEXT(rtAttr,rtLen))
|
||||
@@ -219,43 +230,43 @@ void parseRoutes(struct nlmsghdr *nlHdr, struct route_info *rtInfo,char *gateway
|
||||
switch(rtAttr->rta_type)
|
||||
{
|
||||
case RTA_OIF:
|
||||
if_indextoname(*(int *)RTA_DATA(rtAttr), rtInfo->ifName);
|
||||
if_indextoname(*(int *)RTA_DATA(rtAttr), rtInfo.ifName);
|
||||
break;
|
||||
case RTA_GATEWAY:
|
||||
rtInfo->gateWay = *(u_int *)RTA_DATA(rtAttr);
|
||||
rtInfo.gateWay = *(uint32_t *)RTA_DATA(rtAttr);
|
||||
break;
|
||||
case RTA_PREFSRC:
|
||||
rtInfo->srcAddr = *(u_int *)RTA_DATA(rtAttr);
|
||||
rtInfo.srcAddr = *(uint32_t *)RTA_DATA(rtAttr);
|
||||
break;
|
||||
case RTA_DST:
|
||||
rtInfo->dstAddr = *(u_int *)RTA_DATA(rtAttr);
|
||||
rtInfo.dstAddr = *(uint32_t *)RTA_DATA(rtAttr);
|
||||
break;
|
||||
}
|
||||
}
|
||||
inet_ntop(AF_INET, &rtInfo->dstAddr, dst_address, MAX_STR_LEN);
|
||||
inet_ntop(AF_INET, &rtInfo.dstAddr, dst_address, MAX_STR_LEN);
|
||||
if (strstr(dst_address, "0.0.0.0"))
|
||||
{
|
||||
inet_ntop(AF_INET, &rtInfo->gateWay, gateway, len);
|
||||
inet_ntop(AF_INET, &rtInfo.gateWay, gateway, len);
|
||||
//printf("%s\n",gateway);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
int get_gateway(char *gateway, unsigned int length)//传入长度
|
||||
int get_gateway(char *gateway, unsigned int length)
|
||||
{
|
||||
struct nlmsghdr *nlMsg = NULL;
|
||||
struct route_info *rtInfo = NULL;
|
||||
struct nlmsghdr *nlMsg = NULL;
|
||||
char msgBuf[BUFSIZE] = "";
|
||||
int sock, len, msgSeq = 0;
|
||||
|
||||
if((sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0)
|
||||
{
|
||||
printf("socket creation error!\n");
|
||||
MESA_handle_runtime_log(error_log_handler, RLOG_LV_FATAL, module_name, "socket create error.");
|
||||
printf("====line:%d,%s\n",__LINE__,"socket create error.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(msgBuf, 0, BUFSIZE);
|
||||
//memset(msgBuf, 0, BUFSIZE);
|
||||
nlMsg = (struct nlmsghdr *)msgBuf;
|
||||
nlMsg->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); // Length of message.
|
||||
nlMsg->nlmsg_type = RTM_GETROUTE; // Get the routes from kernel routing table
|
||||
@@ -265,23 +276,22 @@ int get_gateway(char *gateway, unsigned int length)//传入长度
|
||||
|
||||
if(send(sock, nlMsg, nlMsg->nlmsg_len, 0) < 0)
|
||||
{
|
||||
printf("Write To Socket Failed…\n");
|
||||
MESA_handle_runtime_log(error_log_handler, RLOG_LV_FATAL, module_name, "write to socket failed.");
|
||||
printf("====line:%d,%s\n",__LINE__,"write to socket failed.");
|
||||
goto OUT;
|
||||
}
|
||||
|
||||
if((len = readNlSock(sock, msgBuf, msgSeq, getpid())) < 0)
|
||||
{
|
||||
printf("Read From Socket Failed…\n");
|
||||
MESA_handle_runtime_log(error_log_handler, RLOG_LV_FATAL, module_name, "read from socket failed.");
|
||||
printf("====line:%d,%s\n",__LINE__,"read from socket failed.");
|
||||
goto OUT;
|
||||
}
|
||||
|
||||
rtInfo = (struct route_info *)malloc(sizeof(struct route_info));
|
||||
|
||||
for(;NLMSG_OK(nlMsg,len);nlMsg = NLMSG_NEXT(nlMsg,len))
|
||||
{
|
||||
memset(rtInfo, 0, sizeof(struct route_info));
|
||||
parseRoutes(nlMsg, rtInfo,gateway, length);
|
||||
parseRoutes(nlMsg, gateway, length);
|
||||
}
|
||||
free(rtInfo);
|
||||
|
||||
OUT:
|
||||
close(sock);
|
||||
@@ -459,7 +469,7 @@ int http_check_init()
|
||||
MESA_load_profile_string_nodef(http_check_conf_file, "HTTP", "HTTP_CHECK_ERR_LOG_PATH", http_check_error_log_path, MAX_STR_LEN);
|
||||
MESA_load_profile_string_nodef(http_check_conf_file, "HTTP", "MODULE_NAME", module_name, MAX_STR_LEN);
|
||||
MESA_load_profile_int_def(http_check_conf_file, "HTTP", "LOG_LEVEL", &log_level, 30);
|
||||
printf("*v22*******read_ip:%s,read_port:%d\n",g_http_address, g_http_port);
|
||||
printf("*v6*******read_ip:%s,read_port:%d\n",g_http_address, g_http_port);
|
||||
|
||||
http_check_log_handler = MESA_create_runtime_log_handle(http_check_log_path, RLOG_LV_INFO);
|
||||
error_log_handler = MESA_create_runtime_log_handle(http_check_error_log_path, log_level);
|
||||
|
||||
Reference in New Issue
Block a user