1 /* Copyright (C) 2009 Trend Micro Inc.
4 * This program is a free software; you can redistribute it
5 * and/or modify it under the terms of the GNU General Public
6 * License (version 2) as published by the FSF - Free Software
14 #include "os_net/os_net.h"
16 /* pthread send_msg mutex */
17 static pthread_mutex_t sendmsg_mutex;
19 /* pthread key update mutex */
20 static pthread_mutex_t keyupdate_mutex;
23 /* Initializes mutex */
26 /* Initialize mutex */
27 pthread_mutex_init(&keyupdate_mutex, NULL);
32 if (pthread_mutex_lock(&keyupdate_mutex) != 0) {
33 merror(MUTEX_ERROR, ARGV0);
39 if (pthread_mutex_unlock(&keyupdate_mutex) != 0) {
40 merror(MUTEX_ERROR, ARGV0);
44 /* Check for key updates */
47 /* Check key for updates */
48 if (!OS_CheckUpdateKeys(&keys)) {
54 /* Lock before using */
55 if (pthread_mutex_lock(&sendmsg_mutex) != 0) {
57 merror(MUTEX_ERROR, ARGV0);
61 if (OS_UpdateKeys(&keys)) {
62 if (pthread_mutex_unlock(&sendmsg_mutex) != 0) {
63 merror(MUTEX_ERROR, ARGV0);
69 if (pthread_mutex_unlock(&sendmsg_mutex) != 0) {
70 merror(MUTEX_ERROR, ARGV0);
77 /* Initialize send_msg */
80 /* Initialize mutex */
81 pthread_mutex_init(&sendmsg_mutex, NULL);
86 * Send message to an agent
90 int send_msg(unsigned int agentid, const char *msg)
92 size_t msg_size, sa_size;
93 char crypt_msg[OS_MAXSTR + 1];
94 struct sockaddr * dest_sa;
96 /* If we don't have the agent id, ignore it */
97 if (keys.keyentries[agentid]->rcvd < (time(0) - (2 * NOTIFY_TIME))) {
101 msg_size = CreateSecMSG(&keys, msg, crypt_msg, agentid);
103 merror(SEC_ERROR, ARGV0);
107 /* Lock before using */
108 if (pthread_mutex_lock(&sendmsg_mutex) != 0) {
109 merror(MUTEX_ERROR, ARGV0);
113 /* Send initial message */
114 dest_sa = (struct sockaddr *)&keys.keyentries[agentid]->peer_info;
115 sa_size = (dest_sa->sa_family == AF_INET) ?
116 sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
119 * Because we handle multiple IP addresses, we won't know what interfaces
120 * are active for network communication until we receive something on one
121 * of them. This is a work around in the event we need to send before
122 * we have identified the working interface in secure.c. (dgs - 2/26/18)
125 if (logr.sock == 0) {
128 /* socket not established - try current sockets */
129 for (i = 0; i < logr.netinfo->fdcnt; i++) {
130 if (sendto(logr.netinfo->fds[i], crypt_msg, msg_size, 0,
131 dest_sa, sa_size) < 0) {
139 /* if we tried all the sockets and noe of them worked, send an error */
141 merror(SEND_ERROR, ARGV0, keys.keyentries[agentid]->id);
144 /* working socket identified in secure.c */
145 if (sendto(logr.sock, crypt_msg, msg_size, 0, dest_sa, sa_size) < 0) {
146 merror(SEND_ERROR, ARGV0, keys.keyentries[agentid]->id);
151 if (pthread_mutex_unlock(&sendmsg_mutex) != 0) {
152 merror(MUTEX_ERROR, ARGV0);