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 2) 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. */
42 CloseSocket(logr->sock);
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);
169 /* Sending start message and waiting for the ack */
172 /* Sending start up message */
177 /* Read until our reply comes back */
178 while(((recv_b = recv(logr->sock, buffer, OS_MAXSTR,
179 MSG_DONTWAIT)) >= 0) || (attempts <= 5))
183 /* Sleep five seconds before trying to get the reply from
189 /* Sending message again (after three attempts) */
198 /* Id of zero -- only one key allowed */
199 tmp_msg = ReadSecMSG(&keys, buffer, cleartext, 0, recv_b -1);
202 merror(MSG_ERROR, ARGV0, logr->rip[logr->rip_id]);
207 /* Check for commands */
208 if(IsValidHeader(tmp_msg))
210 /* If it is an ack reply */
211 if(strcmp(tmp_msg, HC_ACK) == 0)
213 available_server = time(0);
215 verbose(AG_CONNECTED, ARGV0, logr->rip[logr->rip_id],
220 /* Send log message about start up */
221 snprintf(msg, OS_MAXSTR, OS_AG_STARTED,
222 keys.keyentries[0]->name,
223 keys.keyentries[0]->ip->ip);
224 snprintf(fmsg, OS_MAXSTR, "%c:%s:%s", LOCALFILE_MQ,
233 /* Waiting for servers reply */
234 merror(AG_WAIT_SERVER, ARGV0, logr->rip[logr->rip_id]);
237 /* If we have more than one server, try all. */
240 int curr_rip = logr->rip_id;
241 merror("%s: INFO: Trying next server ip in the line: '%s'.", ARGV0,
242 logr->rip[logr->rip_id + 1] != NULL?logr->rip[logr->rip_id + 1]:logr->rip[0]);
243 connect_server(logr->rip_id +1);
245 if(logr->rip_id == curr_rip)
248 g_attempts+=(attempts * 3);
259 g_attempts+=(attempts * 3);