1 /* @(#) $Id: start_agent.c,v 1.17 2009/12/01 15:40: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
16 #include "os_net/os_net.h"
19 /** void connect_server()
20 * Attempts to connect to all configured servers.
22 int connect_server(int initial_id)
28 /* Checking if the initial is zero, meaning we have to rotate to the
31 if(logr->rip[initial_id] == NULL)
38 /* Closing socket if available. */
47 verbose("%s: INFO: Closing connection to server (%s:%d).",
60 /* Checking if we have a hostname. */
61 tmp_str = strchr(logr->rip[rc], '/');
67 f_ip = OS_GetHost(logr->rip[rc], 5);
73 snprintf(ip_str, 127, "%s/%s", logr->rip[rc], f_ip);
78 os_strdup(ip_str, logr->rip[rc]);
79 tmp_str = strchr(logr->rip[rc], '/');
84 merror("%s: WARN: Unable to get hostname for '%s'.",
85 ARGV0, logr->rip[rc]);
92 tmp_str = logr->rip[rc];
96 verbose("%s: INFO: Trying to connect to server (%s:%d).", ARGV0,
100 logr->sock = OS_ConnectUDP(logr->port, tmp_str);
104 merror(CONNS_ERROR, ARGV0, tmp_str);
107 if(logr->rip[rc] == NULL)
111 /* Only log that if we have more than 1 server configured. */
113 merror("%s: ERROR: Unable to connect to any server.",ARGV0);
121 /* Setting socket non-blocking on HPUX */
123 fcntl(logr->sock, O_NONBLOCK);
129 /* Setting socket to non-blocking */
130 ioctlsocket(logr->sock, FIONBIO, (u_long FAR*) &bmode);
143 /* start_agent: Sends the synchronization message to
144 * the server and waits for the ack.
146 void start_agent(int is_startup)
148 int recv_b = 0, attempts = 0, g_attempts = 1;
151 char msg[OS_MAXSTR +2];
152 char buffer[OS_MAXSTR +1];
153 char cleartext[OS_MAXSTR +1];
154 char fmsg[OS_MAXSTR +1];
157 memset(msg, '\0', OS_MAXSTR +2);
158 memset(buffer, '\0', OS_MAXSTR +1);
159 memset(cleartext, '\0', OS_MAXSTR +1);
160 memset(fmsg, '\0', OS_MAXSTR +1);
161 snprintf(msg, OS_MAXSTR, "%s%s", CONTROL_HEADER, HC_STARTUP);
164 /* Sending start message and waiting for the ack */
167 /* Sending start up message */
172 /* Read until our reply comes back */
173 while(((recv_b = recv(logr->sock, buffer, OS_MAXSTR,
174 MSG_DONTWAIT)) >= 0) || (attempts <= 5))
178 /* Sleep five seconds before trying to get the reply from
184 /* Sending message again (after three attempts) */
193 /* Id of zero -- only one key allowed */
194 tmp_msg = ReadSecMSG(&keys, buffer, cleartext, 0, recv_b -1);
197 merror(MSG_ERROR, ARGV0, logr->rip[logr->rip_id]);
202 /* Check for commands */
203 if(IsValidHeader(tmp_msg))
205 /* If it is an ack reply */
206 if(strcmp(tmp_msg, HC_ACK) == 0)
208 available_server = time(0);
210 verbose(AG_CONNECTED, ARGV0, logr->rip[logr->rip_id],
215 /* Send log message about start up */
216 snprintf(msg, OS_MAXSTR, OS_AG_STARTED,
217 keys.keyentries[0]->name,
218 keys.keyentries[0]->ip->ip);
219 snprintf(fmsg, OS_MAXSTR, "%c:%s:%s", LOCALFILE_MQ,
228 /* Waiting for servers reply */
229 merror(AG_WAIT_SERVER, ARGV0, logr->rip[logr->rip_id]);
232 /* If we have more than one server, try all. */
235 int curr_rip = logr->rip_id;
236 merror("%s: INFO: Trying next server ip in the line: '%s'.", ARGV0,
237 logr->rip[logr->rip_id + 1] != NULL?logr->rip[logr->rip_id + 1]:logr->rip[0]);
238 connect_server(logr->rip_id +1);
240 if(logr->rip_id == curr_rip)
243 g_attempts+=(attempts * 3);
254 g_attempts+=(attempts * 3);