2 chronyd/chronyc - Programs for keeping computer clocks accurate.
4 **********************************************************************
5 * Copyright (C) Miroslav Lichvar 2019
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 **********************************************************************
22 =======================================================================
24 This is the header file for socket operations.
31 #include "addressing.h"
33 /* Flags for opening sockets */
34 #define SCK_FLAG_BLOCK 1
35 #define SCK_FLAG_BROADCAST 2
36 #define SCK_FLAG_RX_DEST_ADDR 4
37 #define SCK_FLAG_ALL_PERMISSIONS 8
38 #define SCK_FLAG_PRIV_BIND 16
40 /* Flags for receiving and sending messages */
41 #define SCK_FLAG_MSG_ERRQUEUE 1
42 #define SCK_FLAG_MSG_DESCRIPTOR 2
53 SCK_AddressType addr_type
;
66 struct timespec kernel
;
76 /* Initialisation function (the specified IP family is enabled,
77 or all if IPADDR_UNSPEC) */
78 extern void SCK_Initialise(int family
);
80 /* Finalisation function */
81 extern void SCK_Finalise(void);
83 /* Check if support for the IP family is enabled */
84 extern int SCK_IsIpFamilyEnabled(int family
);
86 /* Get the 0.0.0.0/::0 or 127.0.0.1/::1 address */
87 extern void SCK_GetAnyLocalIPAddress(int family
, IPAddr
*local_addr
);
88 extern void SCK_GetLoopbackIPAddress(int family
, IPAddr
*local_addr
);
90 /* Check if an IP address is a link-local address */
91 extern int SCK_IsLinkLocalIPAddress(IPAddr
*addr
);
93 /* Specify a bind()-like function for binding sockets to privileged ports when
94 running in a restricted process (e.g. after dropping root privileges) */
95 extern void SCK_SetPrivBind(int (*function
)(int sock_fd
, struct sockaddr
*address
,
96 socklen_t address_len
));
98 /* Open a socket (addresses and iface may be NULL) */
99 extern int SCK_OpenUdpSocket(IPSockAddr
*remote_addr
, IPSockAddr
*local_addr
,
100 const char *iface
, int flags
);
101 extern int SCK_OpenTcpSocket(IPSockAddr
*remote_addr
, IPSockAddr
*local_addr
,
102 const char *iface
, int flags
);
103 extern int SCK_OpenUnixDatagramSocket(const char *remote_addr
, const char *local_addr
,
105 extern int SCK_OpenUnixStreamSocket(const char *remote_addr
, const char *local_addr
,
107 extern int SCK_OpenUnixSocketPair(int flags
, int *other_fd
);
109 /* Set and get a socket option of int size */
110 extern int SCK_SetIntOption(int sock_fd
, int level
, int name
, int value
);
111 extern int SCK_GetIntOption(int sock_fd
, int level
, int name
, int *value
);
113 /* Enable RX timestamping socket option */
114 extern int SCK_EnableKernelRxTimestamping(int sock_fd
);
116 /* Operate on a stream socket - listen()/accept()/shutdown() wrappers */
117 extern int SCK_ListenOnSocket(int sock_fd
, int backlog
);
118 extern int SCK_AcceptConnection(int sock_fd
, IPSockAddr
*remote_addr
);
119 extern int SCK_ShutdownConnection(int sock_fd
);
121 /* Receive and send data on connected sockets - recv()/send() wrappers */
122 extern int SCK_Receive(int sock_fd
, void *buffer
, int length
, int flags
);
123 extern int SCK_Send(int sock_fd
, const void *buffer
, int length
, int flags
);
125 /* Receive a single message or multiple messages. The functions return
126 a pointer to static buffers, or NULL on error. The buffers are valid until
127 another call of the functions and can be reused for sending messages. */
128 extern SCK_Message
*SCK_ReceiveMessage(int sock_fd
, int flags
);
129 extern SCK_Message
*SCK_ReceiveMessages(int sock_fd
, int flags
, int *num_messages
);
131 /* Initialise a new message (e.g. before sending) */
132 extern void SCK_InitMessage(SCK_Message
*message
, SCK_AddressType addr_type
);
135 extern int SCK_SendMessage(int sock_fd
, SCK_Message
*message
, int flags
);
137 /* Remove bound Unix socket */
138 extern int SCK_RemoveSocket(int sock_fd
);
140 /* Close the socket */
141 extern void SCK_CloseSocket(int sock_fd
);
143 /* Convert between IPSockAddr and sockaddr_in/in6 */
144 extern void SCK_SockaddrToIPSockAddr(struct sockaddr
*sa
, int sa_length
, IPSockAddr
*ip_sa
);
145 extern int SCK_IPSockAddrToSockaddr(IPSockAddr
*ip_sa
, struct sockaddr
*sa
, int sa_length
);