nxappc internal header file removed
[public/netxms.git] / src / agent / libnxappc / nxappc.c
1 /*
2 ** NetXMS Application Connector Library
3 ** Copyright (C) 2015-2016 Raden Solutions
4 **
5 ** Permission is hereby granted, free of charge, to any person obtaining
6 ** a copy of this software and associated documentation files
7 ** (the "Software"), to deal in the Software without restriction, including
8 ** without limitation the rights to use, copy, modify, merge, publish,
9 ** distribute, sublicense, and/or sell copies of the Software, and to permit
10 ** persons to whom the Software is furnished to do so, subject to the
11 ** following conditions:
12 **
13 ** The above copyright notice and this permission notice shall be included in
14 ** all copies or substantial portions of the Software.
15 **
16 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 ** THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 ** OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 ** ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 ** OTHER DEALINGS IN THE SOFTWARE.
23 **/
24
25 #define _CRT_SECURE_NO_WARNINGS
26
27 #if HAVE_CONFIG_H
28 #include <config.h>
29 #else
30 #define HAVE_ALLOCA_H 1
31 #endif
32
33 #include <stdio.h>
34 #include <string.h>
35 #include <errno.h>
36
37 #ifdef _WIN32
38
39 #include <winsock2.h>
40 #include <windows.h>
41 #include <malloc.h>
42
43 #define SELECT_NFDS(f) (0)
44
45 #else /* _WIN32 */
46
47 #include <unistd.h>
48 #include <sys/time.h>
49 #include <sys/socket.h>
50 #include <sys/un.h>
51 #include <fcntl.h>
52
53 #if HAVE_ALLOCA_H
54 #include <alloca.h>
55 #endif
56
57 #define SOCKET int
58 #define closesocket(x) close(x)
59
60 #define SELECT_NFDS(f) ((f) + 1)
61
62 #ifndef SUN_LEN
63 #define SUN_LEN(su) (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
64 #endif
65
66 #endif /* _WIN32 */
67
68 #include "nxappc.h"
69
70 static char s_channel[128] = "";
71 static SOCKET s_socket = -1;
72
73 /**
74 * Check if channel is ready
75 */
76 #define CHECK_CHANNEL do { if (s_socket == -1) { nxappc_open_channel(); if (s_socket == -1) return NXAPPC_FAIL; } } while(0)
77
78 /**
79 * Set channel name
80 */
81 int LIBNXAPPC_EXPORTABLE nxappc_set_channel_name(const char *channel)
82 {
83 if (!strcmp(channel, s_channel))
84 return NXAPPC_SUCCESS; // already connected
85 strncpy(s_channel, channel, 128);
86 if (s_socket != -1)
87 {
88 closesocket(s_socket);
89 s_socket = -1;
90 }
91 return NXAPPC_SUCCESS;
92 }
93
94 /**
95 * Open channel for communication
96 */
97 int LIBNXAPPC_EXPORTABLE nxappc_open_channel(void)
98 {
99 #ifdef _WIN32
100 struct sockaddr_in addrLocal;
101 u_long one = 1;
102 #else
103 struct sockaddr_un addrLocal;
104 int f;
105 #endif
106
107 if (s_socket != -1)
108 {
109 closesocket(s_socket);
110 }
111
112 #ifdef _WIN32
113 s_socket = socket(AF_INET, SOCK_DGRAM, 0);
114 if (s_socket == -1)
115 return NXAPPC_FAIL;
116
117 addrLocal.sin_family = AF_INET;
118 addrLocal.sin_addr.s_addr = inet_addr("127.0.0.1");
119 addrLocal.sin_port = htons(atoi(s_channel));
120 if (connect(s_socket, (struct sockaddr *)&addrLocal, sizeof(addrLocal)) == -1)
121 {
122 closesocket(s_socket);
123 s_socket = -1;
124 return NXAPPC_FAIL;
125 }
126
127 #else
128 s_socket = socket(AF_UNIX, SOCK_DGRAM, 0);
129 if (s_socket == -1)
130 return NXAPPC_FAIL;
131
132 addrLocal.sun_family = AF_UNIX;
133 sprintf(addrLocal.sun_path, "/tmp/.nxappc.%s", s_channel);
134 if (connect(s_socket, (struct sockaddr *)&addrLocal, SUN_LEN(&addrLocal)) == -1)
135 {
136 closesocket(s_socket);
137 s_socket = -1;
138 return NXAPPC_FAIL;
139 }
140
141 #endif
142
143 return NXAPPC_SUCCESS;
144 }
145
146 /**
147 * Reset channel
148 */
149 int LIBNXAPPC_EXPORTABLE nxappc_reset_channel(void)
150 {
151 if (s_socket != -1)
152 {
153 closesocket(s_socket);
154 s_socket = -1;
155 }
156 return nxappc_open_channel();
157 }
158
159 /**
160 * Send with retry
161 */
162 static int send_data(void *data, int size)
163 {
164 int rc = send(s_socket, data, size, 0);
165 if (rc >= 0)
166 return rc;
167 rc = nxappc_reset_channel();
168 if (rc < 0)
169 return rc;
170 return send(s_socket, data, size, 0);
171 }
172
173 /**
174 * Send event
175 */
176 int LIBNXAPPC_EXPORTABLE nxappc_send_event(int code, const char *name, const char *format, ...)
177 {
178 CHECK_CHANNEL;
179 return NXAPPC_FAIL;
180 }
181
182 /**
183 * Send binary data
184 */
185 int LIBNXAPPC_EXPORTABLE nxappc_send_data(void *data, int size)
186 {
187 char *msg;
188
189 if ((size < 0) || (size > 65532))
190 return NXAPPC_FAIL;
191
192 CHECK_CHANNEL;
193
194 msg = alloca(size + 4);
195 msg[0] = NXAPPC_CMD_SEND_DATA;
196 msg[1] = 0; // reserved
197 *((unsigned short *)&msg[2]) = (unsigned short)size;
198 memcpy(&msg[4], data, size);
199 return (send_data(msg, size + 4) == size + 4) ? NXAPPC_SUCCESS : NXAPPC_FAIL;
200 }
201
202 /**
203 * Register counter
204 */
205 int LIBNXAPPC_EXPORTABLE nxappc_register_counter(const char *counter, const char *instance)
206 {
207 CHECK_CHANNEL;
208 return NXAPPC_FAIL;
209 }
210
211 /**
212 * Update integer counter
213 */
214 int LIBNXAPPC_EXPORTABLE nxappc_update_counter_long(const char *counter, const char *instance, long diff)
215 {
216 CHECK_CHANNEL;
217 return NXAPPC_FAIL;
218 }
219
220 /**
221 * Update floating point counter
222 */
223 int LIBNXAPPC_EXPORTABLE nxappc_update_counter_double(const char *counter, const char *instance, double diff)
224 {
225 CHECK_CHANNEL;
226 return NXAPPC_FAIL;
227 }
228
229 /**
230 * Reset counter
231 */
232 int LIBNXAPPC_EXPORTABLE nxappc_reset_counter(const char *counter, const char *instance)
233 {
234 CHECK_CHANNEL;
235 return NXAPPC_FAIL;
236 }
237
238 /**
239 * Update integer counter
240 */
241 int LIBNXAPPC_EXPORTABLE nxappc_set_counter_long(const char *counter, const char *instance, long value)
242 {
243 CHECK_CHANNEL;
244
245 return NXAPPC_FAIL;
246 }
247
248 /**
249 * Set floating point counter
250 */
251 int LIBNXAPPC_EXPORTABLE nxappc_set_counter_double(const char *counter, const char *instance, double value)
252 {
253 CHECK_CHANNEL;
254 return NXAPPC_FAIL;
255 }