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
12 #include "os_net/os_net.h"
15 /* Attempt to connect to all configured servers */
16 int connect_server(int initial_id)
18 unsigned int attempts = 2;
21 /* Checking if the initial is zero, meaning we have to
22 * rotate to the beginning
24 if (agt->rip[initial_id] == NULL) {
28 /* Close socket if available */
31 CloseSocket(agt->sock);
35 verbose("%s: INFO: Closing connection to server %s, port %s.",
43 while (agt->rip[rc]) {
45 /* Connect to any useable address of the server */
46 verbose("%s: INFO: Trying to connect to server %s, port %s.", ARGV0,
50 agt->sock = OS_ConnectUDP(agt->port, agt->rip[rc]);
54 merror(CONNS_ERROR, ARGV0, agt->rip[rc]);
57 if (agt->rip[rc] == NULL) {
60 /* Only log that if we have more than 1 server configured */
62 merror("%s: ERROR: Unable to connect to any server.", ARGV0);
70 /* Set socket non-blocking on HPUX */
71 // fcntl(agt->sock, O_NONBLOCK);
77 /* Set socket to non-blocking */
78 ioctlsocket(agt->sock, FIONBIO, (u_long FAR *) &bmode);
89 /* Send synchronization message to the server and wait for the ack */
90 void start_agent(int is_startup)
93 unsigned int attempts = 0, g_attempts = 1;
96 char msg[OS_MAXSTR + 2];
97 char buffer[OS_MAXSTR + 1];
98 char cleartext[OS_MAXSTR + 1];
99 char fmsg[OS_MAXSTR + 1];
101 memset(msg, '\0', OS_MAXSTR + 2);
102 memset(buffer, '\0', OS_MAXSTR + 1);
103 memset(cleartext, '\0', OS_MAXSTR + 1);
104 memset(fmsg, '\0', OS_MAXSTR + 1);
105 snprintf(msg, OS_MAXSTR, "%s%s", CONTROL_HEADER, HC_STARTUP);
107 #ifdef ONEWAY_ENABLED
112 /* Send start up message */
116 /* Read until our reply comes back */
117 while (((recv_b = recv(agt->sock, buffer, OS_MAXSTR,
118 MSG_DONTWAIT)) >= 0) || (attempts <= 5)) {
120 /* Sleep five seconds before trying to get the reply from
126 /* Send message again (after three attempts) */
134 /* Id of zero -- only one key allowed */
135 tmp_msg = ReadSecMSG(&keys, buffer, cleartext, 0, recv_b - 1);
136 if (tmp_msg == NULL) {
137 merror(MSG_ERROR, ARGV0, agt->rip[agt->rip_id]);
141 /* Check for commands */
142 if (IsValidHeader(tmp_msg)) {
143 /* If it is an ack reply */
144 if (strcmp(tmp_msg, HC_ACK) == 0) {
145 available_server = time(0);
147 verbose(AG_CONNECTED, ARGV0, agt->rip[agt->rip_id],
151 /* Send log message about start up */
152 snprintf(msg, OS_MAXSTR, OS_AG_STARTED,
153 keys.keyentries[0]->name,
154 keys.keyentries[0]->ip->ip);
155 snprintf(fmsg, OS_MAXSTR, "%c:%s:%s", LOCALFILE_MQ,
164 /* Wait for server reply */
165 merror(AG_WAIT_SERVER, ARGV0, agt->rip[agt->rip_id]);
167 /* If we have more than one server, try all */
169 int curr_rip = agt->rip_id;
170 merror("%s: INFO: Trying next server in the line: '%s'.", ARGV0,
171 agt->rip[agt->rip_id + 1] != NULL ? agt->rip[agt->rip_id + 1] : agt->rip[0]);
172 connect_server(agt->rip_id + 1);
174 if (agt->rip_id == curr_rip) {
176 g_attempts += (attempts * 3);
183 g_attempts += (attempts * 3);