summaryrefslogtreecommitdiffstats
path: root/src/zabbix_agent_win32/comm.cpp
blob: f26c049850fa498f2d1ce804130c3dd583ae0ade (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* 
** ZabbixW32 - Win32 agent for Zabbix
** Copyright (C) 2002 Victor Kirhenshtein
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
** $module: comm.cpp
**
**/

#include "zabbixw32.h"


//
// Request structure
//



//
// Global data
//

double statAcceptedRequests=0;
double statRejectedRequests=0;
double statTimedOutRequests=0;
double statAcceptErrors=0;


//
// Validates server's address
//

static BOOL IsValidServerAddr(DWORD addr)
{
   DWORD i;

   for(i=0;i<confServerCount;i++)
      if (addr==confServerAddr[i])
         return TRUE;
   return FALSE;
}


//
// Request processing thread
//

unsigned int __stdcall ProcessingThread(void *arg)
{
   ProcessCommand(((REQUEST *)arg)->cmd,((REQUEST *)arg)->result);
   return 0;
}


//
// Client communication thread
//

static void CommThread(void *param)
{
   SOCKET sock;
   int rc;
   REQUEST rq;
	struct timeval timeout;
	FD_SET rdfs;
   HANDLE hThread=NULL;
   unsigned int tid;

   sock=(SOCKET)param;

   // Wait for command from server
	FD_ZERO(&rdfs);
	FD_SET(sock,&rdfs);
	timeout.tv_sec=COMMAND_TIMEOUT;
	timeout.tv_usec=0;
	if (select(sock+1,&rdfs,NULL,NULL,&timeout)==0)
	{
		WriteLog(MSG_COMMAND_TIMEOUT,EVENTLOG_WARNING_TYPE,NULL);
      goto end_session;
	}

   rc=recv(sock,rq.cmd,MAX_ZABBIX_CMD_LEN,0);
   if (rc<=0)
   {
      WriteLog(MSG_RECV_ERROR,EVENTLOG_ERROR_TYPE,"s",strerror(errno));
      goto end_session;
   }

   rq.cmd[rc-1]=0;

   hThread=(HANDLE)_beginthreadex(NULL,0,ProcessingThread,(void *)&rq,0,&tid);
   if (WaitForSingleObject(hThread,confTimeout)==WAIT_TIMEOUT)
   {
      strcpy(rq.result,"ZBX_ERROR\n");
      WriteLog(MSG_REQUEST_TIMEOUT,EVENTLOG_WARNING_TYPE,"s",rq.cmd);
      statTimedOutRequests++;
   }
   send(sock,rq.result,strlen(rq.result),0);

   // Terminate session
end_session:
   shutdown(sock,2);
   closesocket(sock);

   // Now wait for processing thread completion if we start one
   if (hThread!=NULL)
   {
      WaitForSingleObject(hThread,INFINITE);
      CloseHandle(hThread);
   }
}


//
// TCP/IP Listener
//

void ListenerThread(void *)
{
   SOCKET sock,sockClient;
   struct sockaddr_in servAddr;
   int iSize,errorCount=0;

   // Create socket
   if ((sock=socket(AF_INET,SOCK_STREAM,0))==-1)
   {
      WriteLog(MSG_SOCKET_ERROR,EVENTLOG_ERROR_TYPE,"e",WSAGetLastError());
      exit(1);
   }

   // Fill in local address structure
   memset(&servAddr,0,sizeof(struct sockaddr_in));
   servAddr.sin_family=AF_INET;
   servAddr.sin_addr.s_addr=htonl(INADDR_ANY);
   servAddr.sin_port=htons(confListenPort);

   // Bind socket
   if (bind(sock,(struct sockaddr *)&servAddr,sizeof(struct sockaddr_in))!=0)
   {
      WriteLog(MSG_BIND_ERROR,EVENTLOG_ERROR_TYPE,"e",WSAGetLastError());
      exit(1);
   }

   // Set up queue
   listen(sock,SOMAXCONN);

   // Wait for connection requests
   while(1)
   {
      iSize=sizeof(struct sockaddr_in);
      if ((sockClient=accept(sock,(struct sockaddr *)&servAddr,&iSize))==-1)
      {
         int error=WSAGetLastError();

         if (error!=WSAEINTR)
            WriteLog(MSG_ACCEPT_ERROR,EVENTLOG_ERROR_TYPE,"e",error);
         errorCount++;
         statAcceptErrors++;
         if (errorCount>1000)
         {
            WriteLog(MSG_TOO_MANY_ERRORS,EVENTLOG_WARNING_TYPE,NULL);
            errorCount=0;
         }
         Sleep(500);
      }

      errorCount=0;     // Reset consecutive errors counter

      if (IsValidServerAddr(servAddr.sin_addr.S_un.S_addr))
      {
         statAcceptedRequests++;
         _beginthread(CommThread,0,(void *)sockClient);
      }
      else     // Unauthorized connection
      {
         statRejectedRequests++;
         shutdown(sockClient,2);
         closesocket(sockClient);
      }
   }
}