1 /* 2 chronyd/chronyc - Programs for keeping computer clocks accurate. 3 4 ********************************************************************** 5 * Copyright (C) Miroslav Lichvar 2019 6 * 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. 10 * 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. 15 * 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. 19 * 20 ********************************************************************** 21 22 ======================================================================= 23 24 This is the header file for socket operations. 25 26 */ 27 28 #ifndef GOT_SOCKET_H 29 #define GOT_SOCKET_H 30 31 #include "addressing.h" 32 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 39 40 /* Flags for receiving and sending messages */ 41 #define SCK_FLAG_MSG_ERRQUEUE 1 42 #define SCK_FLAG_MSG_DESCRIPTOR 2 43 44 typedef enum { 45 SCK_ADDR_UNSPEC = 0, 46 SCK_ADDR_IP, 47 SCK_ADDR_UNIX 48 } SCK_AddressType; 49 50 typedef struct { 51 void *data; 52 int length; 53 SCK_AddressType addr_type; 54 int if_index; 55 56 union { 57 IPSockAddr ip; 58 const char *path; 59 } remote_addr; 60 61 union { 62 IPAddr ip; 63 } local_addr; 64 65 struct { 66 struct timespec kernel; 67 struct timespec hw; 68 int if_index; 69 int l2_length; 70 int tx_flags; 71 } timestamp; 72 73 int descriptor; 74 } SCK_Message; 75 76 /* Initialisation function (the specified IP family is enabled, 77 or all if IPADDR_UNSPEC) */ 78 extern void SCK_Initialise(int family); 79 80 /* Finalisation function */ 81 extern void SCK_Finalise(void); 82 83 /* Check if support for the IP family is enabled */ 84 extern int SCK_IsIpFamilyEnabled(int family); 85 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); 89 90 /* Check if an IP address is a link-local address */ 91 extern int SCK_IsLinkLocalIPAddress(IPAddr *addr); 92 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)); 97 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, 104 int flags); 105 extern int SCK_OpenUnixStreamSocket(const char *remote_addr, const char *local_addr, 106 int flags); 107 extern int SCK_OpenUnixSocketPair(int flags, int *other_fd); 108 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); 112 113 /* Enable RX timestamping socket option */ 114 extern int SCK_EnableKernelRxTimestamping(int sock_fd); 115 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); 120 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); 124 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); 130 131 /* Initialise a new message (e.g. before sending) */ 132 extern void SCK_InitMessage(SCK_Message *message, SCK_AddressType addr_type); 133 134 /* Send a message */ 135 extern int SCK_SendMessage(int sock_fd, SCK_Message *message, int flags); 136 137 /* Remove bound Unix socket */ 138 extern int SCK_RemoveSocket(int sock_fd); 139 140 /* Close the socket */ 141 extern void SCK_CloseSocket(int sock_fd); 142 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); 146 147 #endif 148