new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / os_net / os_net.h
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All rights reserved.
3  *
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
7  * Foundation
8  */
9
10 /* OS_net Library
11  * APIs for many network operations
12  */
13
14 #include "headers/shared.h"
15 #include "config/client-config.h"
16 extern agent *agt;
17 #ifdef WIN32
18 #ifndef AI_ADDRCONFIG
19 #define AI_ADDRCONFIG   0x0400
20 #endif
21 #ifndef AI_V4MAPPED
22 #define AI_V4MAPPED     0x0800
23 #endif
24 typedef unsigned short int sa_family_t;
25 #endif /* WIN32 */
26
27
28 #ifndef __OS_NET_H
29 #define __OS_NET_H
30 #ifdef _WIN32
31 #include <stdint.h>
32 typedef uint8_t u_int8_t;
33 typedef uint16_t u_int16_t;
34 typedef uint32_t u_int32_t;
35 #endif
36
37 /*
38  * OSNetInfo is used to exchange a set of bound sockets for use with the
39  * select() function for monitoring incoming packets on multiple interfaces.
40  * This allows you to support IPv4 and IPv6 simultaneously.
41  */
42
43 typedef struct _OSNetInfo {
44   fd_set fdset;                         /* set of sockets used by select ()*/
45   int fdmax;                            /* fd max socket for select() */
46   int fds[FD_SETSIZE];                  /* array of bound sockets for send() */
47   int fdcnt;                            /* number of sockets in array */
48   int status;                           /* return status (-1 is error) */
49   int retval;                           /* return value (additional info) */
50 } OSNetInfo;
51
52 /*
53  * OS_Bindport*
54  * Bind a specific port (protocol and a ip).
55  * If the IP is not set, it is going to use ADDR_ANY
56  * Return a pointer to the OSNetInfo struct.
57  */
58
59 OSNetInfo *OS_Bindporttcp(char *_port, const char *_ip);
60 OSNetInfo *OS_Bindportudp(char *_port, const char *_ip);
61
62 /* OS_BindUnixDomain
63  * Bind to a specific file, using the "mode" permissions in
64  * a Unix Domain socket.
65  */
66 int OS_BindUnixDomain(const char *path, mode_t mode, int max_msg_size) __attribute__((nonnull));
67 int OS_ConnectUnixDomain(const char *path, int max_msg_size) __attribute__((nonnull));
68 int OS_getsocketsize(int ossock);
69
70 /* OS_Connect
71  * Connect to a TCP/UDP socket
72  */
73 int OS_ConnectTCP(char *_port, const char *_ip);
74 int OS_ConnectUDP(char *_port, const char *_ip);
75
76 /* OS_RecvUDP
77  * Receive a UDP packet. Return NULL if failed
78  */
79 char *OS_RecvUDP(int socket, int sizet);
80 int OS_RecvConnUDP(int socket, char *buffer, int buffer_size) __attribute__((nonnull));
81
82 /* OS_RecvUnix
83  * Receive a message via a Unix socket
84  */
85 int OS_RecvUnix(int socket, int sizet, char *ret) __attribute__((nonnull));
86
87 /* OS_RecvTCP
88  * Receive a TCP packet
89  */
90 int OS_AcceptTCP(int socket, char *srcip, size_t addrsize) __attribute__((nonnull));
91 char *OS_RecvTCP(int socket, int sizet);
92 int OS_RecvTCPBuffer(int socket, char *buffer, int sizet) __attribute__((nonnull));
93
94 /* OS_SendTCP
95  * Send a TCP/UDP/UnixSocket packet (in a open socket)
96  */
97 int OS_SendTCP(int socket, const char *msg) __attribute__((nonnull));
98 int OS_SendTCPbySize(int socket, int size, const char *msg) __attribute__((nonnull));
99
100 int OS_SendUnix(int socket, const char *msg, int size) __attribute__((nonnull));
101
102 int OS_SendUDPbySize(int socket, int size, const char *msg) __attribute__((nonnull));
103
104 /* OS_GetHost
105  * Calls getaddrinfo
106  */
107 char *OS_GetHost(const char *host, unsigned int attempts);
108
109
110 /* satop 
111  * Convert a sockaddr to a printable address.
112  */
113 int satop(struct sockaddr *sa, char *dst, socklen_t size);
114
115
116 /* Close a network socket
117  * Returns 0 on success, else -1 or SOCKET_ERROR
118  */
119 int OS_CloseSocket(int socket);
120
121 #endif /* __OS_NET_H */
122