1 /* @(#) $Id: ./src/client-agent/start_agent.c, 2011/09/08 dcid Exp $
4 /* Copyright (C) 2009 Trend Micro Inc.
7 * This program is a free software; you can redistribute it
8 * and/or modify it under the terms of the GNU General Public
9 * License (version 2) as published by the FSF - Free Software
17 #include "os_net/os_net.h"
20 /** void connect_server()
21 * Attempts to connect to all configured servers.
23 int connect_server(int initial_id)
29 /* Checking if the initial is zero, meaning we have to rotate to the
32 if(logr->rip[initial_id] == NULL)
39 /* Closing socket if available. */
43 CloseSocket(logr->sock);
48 verbose("%s: INFO: Closing connection to server (%s:%d).",
61 /* Checking if we have a hostname. */
62 tmp_str = strchr(logr->rip[rc], '/');
68 f_ip = OS_GetHost(logr->rip[rc], 5);
74 snprintf(ip_str, 127, "%s/%s", logr->rip[rc], f_ip);
79 os_strdup(ip_str, logr->rip[rc]);
80 tmp_str = strchr(logr->rip[rc], '/');
85 merror("%s: WARN: Unable to get hostname for '%s'.",
86 ARGV0, logr->rip[rc]);
93 tmp_str = logr->rip[rc];
97 verbose("%s: INFO: Trying to connect to server (%s:%d).", ARGV0,
102 if(strchr(tmp_str,':') != NULL)
104 verbose("%s: INFO: Using IPv6 for: %s .", ARGV0, tmp_str);
105 logr->sock = OS_ConnectUDP(logr->port, tmp_str, 1);
109 verbose("%s: INFO: Using IPv4 for: %s .", ARGV0, tmp_str);
110 logr->sock = OS_ConnectUDP(logr->port, tmp_str, 0);
116 merror(CONNS_ERROR, ARGV0, tmp_str);
119 if(logr->rip[rc] == NULL)
123 /* Only log that if we have more than 1 server configured. */
125 merror("%s: ERROR: Unable to connect to any server.",ARGV0);
133 /* Setting socket non-blocking on HPUX */
135 //fcntl(logr->sock, O_NONBLOCK);
141 /* Setting socket to non-blocking */
142 ioctlsocket(logr->sock, FIONBIO, (u_long FAR*) &bmode);
155 /* start_agent: Sends the synchronization message to
156 * the server and waits for the ack.
158 void start_agent(int is_startup)
160 int recv_b = 0, attempts = 0, g_attempts = 1;
163 char msg[OS_MAXSTR +2];
164 char buffer[OS_MAXSTR +1];
165 char cleartext[OS_MAXSTR +1];
166 char fmsg[OS_MAXSTR +1];
169 memset(msg, '\0', OS_MAXSTR +2);
170 memset(buffer, '\0', OS_MAXSTR +1);
171 memset(cleartext, '\0', OS_MAXSTR +1);
172 memset(fmsg, '\0', OS_MAXSTR +1);
173 snprintf(msg, OS_MAXSTR, "%s%s", CONTROL_HEADER, HC_STARTUP);
181 /* Sending start message and waiting for the ack */
184 /* Sending start up message */
189 /* Read until our reply comes back */
190 while(((recv_b = recv(logr->sock, buffer, OS_MAXSTR,
191 MSG_DONTWAIT)) >= 0) || (attempts <= 5))
195 /* Sleep five seconds before trying to get the reply from
201 /* Sending message again (after three attempts) */
210 /* Id of zero -- only one key allowed */
211 tmp_msg = ReadSecMSG(&keys, buffer, cleartext, 0, recv_b -1);
214 merror(MSG_ERROR, ARGV0, logr->rip[logr->rip_id]);
219 /* Check for commands */
220 if(IsValidHeader(tmp_msg))
222 /* If it is an ack reply */
223 if(strcmp(tmp_msg, HC_ACK) == 0)
225 available_server = time(0);
227 verbose(AG_CONNECTED, ARGV0, logr->rip[logr->rip_id],
232 /* Send log message about start up */
233 snprintf(msg, OS_MAXSTR, OS_AG_STARTED,
234 keys.keyentries[0]->name,
235 keys.keyentries[0]->ip->ip);
236 snprintf(fmsg, OS_MAXSTR, "%c:%s:%s", LOCALFILE_MQ,
245 /* Waiting for servers reply */
246 merror(AG_WAIT_SERVER, ARGV0, logr->rip[logr->rip_id]);
249 /* If we have more than one server, try all. */
252 int curr_rip = logr->rip_id;
253 merror("%s: INFO: Trying next server ip in the line: '%s'.", ARGV0,
254 logr->rip[logr->rip_id + 1] != NULL?logr->rip[logr->rip_id + 1]:logr->rip[0]);
255 connect_server(logr->rip_id +1);
257 if(logr->rip_id == curr_rip)
260 g_attempts+=(attempts * 3);
271 g_attempts+=(attempts * 3);