static int create_socket(env_t *environment, int *sockfd)
{
struct addrinfo hints = {0};
*sockfd = socket(AF_INET, SOCK_RAW | SOCK_NONBLOCK, IPPROTO_ICMP);
if (*sockfd == -1)
{
LOG("Error in creating socket: %s\n", strerror(errno));
TEST(0, ERR_SYSCALL);
}
hints.ai_family = AF_INET;
hints.ai_flags = AI_NUMERICHOST;
bind_socket(environment, sockfd, &hints, TRUE);
}
static int ping_send(const char* ip, task_t *task)
{
struct icmp *icmp;
icmp_send_data_t send_data = {{0}};
char packet[PACKET_DATA_SIZE + sizeof(struct icmp)] = {0};
ib_uint16_t sum = 0;
struct sockaddr_in serv_addr = {0};
icmp = (struct icmp *)packet;
icmp_data_t *icmp_d = NULL;
check_t *monitor = NULL;
monitor = task->monitor;
icmp_d = (icmp_data_t *) task->data;
icmp->icmp_type = ICMP_ECHO;
icmp->icmp_code = 0;
icmp->icmp_id = icmp_d->pid;
icmp->icmp_seq = 0;
gettimeofday(&send_data.current_time, NULL);
send_data.task_id = task->id;
TEST(sizeof(send_data) < PACKET_DATA_SIZE, FAILURE);
memcpy(icmp->icmp_data, &send_data, sizeof(send_data));
icmp->icmp_cksum = 0;
iutil_uint16_cksum((ib_uint16_t *)icmp, sizeof(struct icmp), &sum);
icmp->icmp_cksum = sum;
ITEST(inet_pton(AF_INET, ip, &serv_addr.sin_addr.s_addr) == 1, IERR_ARG);
if (sendto(icmp_d->socketfd, packet, sizeof(struct icmp), 0,
(struct sockaddr *)&serv_addr,
sizeof(struct sockaddr_in)) < 0)
{
LOG("Error(%s) Failed to send ping message to %s", strerror(errno), monitor->host);
status_update(task, STATUS_OFFLINE));
TEST(0, IERR_FAILURE);
}
}
Иногда пинг не шлется, с сообщением в сислоге из сабжа. Чо за?