1 /* @(#) $Id: sendmsg.c,v 1.4 2009/06/24 18:53:07 dcid Exp $ */
3 /* Copyright (C) 2009 Trend Micro Inc.
6 * This program is a free software; you can redistribute it
7 * and/or modify it under the terms of the GNU General Public
8 * License (version 3) as published by the FSF - Free Software
17 #include "os_net/os_net.h"
20 /* pthread send_msg mutex */
21 pthread_mutex_t sendmsg_mutex;
23 /* pthread key update mutex */
24 pthread_mutex_t keyupdate_mutex;
27 /* void keyupdate_init()
32 /* Initializing mutex */
33 pthread_mutex_init(&keyupdate_mutex, NULL);
37 /* void void key_lock()
39 * Locks/unlocks the update mutex.
43 if(pthread_mutex_lock(&keyupdate_mutex) != 0)
45 merror(MUTEX_ERROR, ARGV0);
50 if(pthread_mutex_unlock(&keyupdate_mutex) != 0)
52 merror(MUTEX_ERROR, ARGV0);
58 * Check for key updates.
62 /* Checking key for updates. */
63 if(!OS_CheckUpdateKeys(&keys))
70 /* Locking before using */
71 if(pthread_mutex_lock(&sendmsg_mutex) != 0)
74 merror(MUTEX_ERROR, ARGV0);
78 if(OS_UpdateKeys(&keys))
80 if(pthread_mutex_unlock(&sendmsg_mutex) != 0)
82 merror(MUTEX_ERROR, ARGV0);
88 if(pthread_mutex_unlock(&sendmsg_mutex) != 0)
90 merror(MUTEX_ERROR, ARGV0);
99 * Initializes send_msg.
103 /* Initializing mutex */
104 pthread_mutex_init(&sendmsg_mutex, NULL);
109 * Send message to an agent.
110 * Returns -1 on error
112 int send_msg(int agentid, char *msg)
115 char crypt_msg[OS_MAXSTR +1];
118 /* If we don't have the agent id, ignore it */
119 if(keys.keyentries[agentid]->rcvd < (time(0) - (2*NOTIFY_TIME)))
125 msg_size = CreateSecMSG(&keys, msg, crypt_msg, agentid);
128 merror(SEC_ERROR,ARGV0);
133 /* Locking before using */
134 if(pthread_mutex_lock(&sendmsg_mutex) != 0)
136 merror(MUTEX_ERROR, ARGV0);
141 /* Sending initial message */
142 if(sendto(logr.sock, crypt_msg, msg_size, 0,
143 (struct sockaddr *)&keys.keyentries[agentid]->peer_info,
146 merror(SEND_ERROR,ARGV0, keys.keyentries[agentid]->id);
150 /* Unlocking mutex */
151 if(pthread_mutex_unlock(&sendmsg_mutex) != 0)
153 merror(MUTEX_ERROR, ARGV0);