1/* $NetBSD: socket.h,v 1.3 2021/08/14 16:14:55 christos Exp $ */ 2 3/* Generic socket.h */ 4/* $OpenLDAP$ */ 5/* This work is part of OpenLDAP Software <http://www.openldap.org/>. 6 * 7 * Copyright 1998-2021 The OpenLDAP Foundation. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted only as authorized by the OpenLDAP 12 * Public License. 13 * 14 * A copy of this license is available in file LICENSE in the 15 * top-level directory of the distribution or, alternatively, at 16 * <http://www.OpenLDAP.org/license.html>. 17 */ 18 19#ifndef _AC_SOCKET_H_ 20#define _AC_SOCKET_H_ 21 22#ifdef HAVE_SYS_TYPES_H 23#include <sys/types.h> 24#endif 25 26#ifdef HAVE_POLL_H 27#include <poll.h> 28#elif defined(HAVE_SYS_POLL_H) 29#include <sys/poll.h> 30#endif 31 32#ifdef HAVE_SYS_SOCKET_H 33#include <sys/socket.h> 34 35#ifdef HAVE_SYS_UN_H 36#include <sys/un.h> 37#endif 38 39#ifdef HAVE_SYS_SELECT_H 40#include <sys/select.h> 41#endif 42 43#include <netinet/in.h> 44 45#ifdef HAVE_NETINET_TCP_H 46#include <netinet/tcp.h> 47#endif 48 49#ifdef HAVE_ARPA_INET_H 50#include <arpa/inet.h> 51#endif 52 53#ifdef HAVE_ARPA_NAMESER_H 54#include <arpa/nameser.h> 55#endif 56 57#include <netdb.h> 58 59#ifdef HAVE_RESOLV_H 60#include <resolv.h> 61#endif 62 63#endif /* HAVE_SYS_SOCKET_H */ 64 65#ifdef HAVE_WINSOCK2 66#include <winsock2.h> 67#include <ws2tcpip.h> 68#elif HAVE_WINSOCK 69#include <winsock.h> 70#endif 71 72#ifdef HAVE_PCNFS 73#include <tklib.h> 74#endif /* HAVE_PCNFS */ 75 76#ifndef INADDR_LOOPBACK 77#define INADDR_LOOPBACK (0x7f000001UL) 78#endif 79 80#ifndef MAXHOSTNAMELEN 81#define MAXHOSTNAMELEN 64 82#endif 83 84#undef sock_errno 85#undef sock_errstr 86#define sock_errno() errno 87#define sock_errstr(e, b, l) AC_STRERROR_R(e, b, l) 88#define sock_errset(e) ((void) (errno = (e))) 89 90#ifdef HAVE_WINSOCK 91# define tcp_read( s, buf, len ) recv( s, buf, len, 0 ) 92# define tcp_write( s, buf, len ) send( s, buf, len, 0 ) 93# define ioctl( s, c, a ) ioctlsocket( (s), (c), (a) ) 94# define ioctl_t u_long 95# define AC_SOCKET_INVALID ((unsigned int) -1) 96 97# ifdef SD_BOTH 98# define tcp_close( s ) (shutdown( s, SD_BOTH ), closesocket( s )) 99# else 100# define tcp_close( s ) closesocket( s ) 101# endif 102 103#define EWOULDBLOCK WSAEWOULDBLOCK 104#define EINPROGRESS WSAEINPROGRESS 105#define ETIMEDOUT WSAETIMEDOUT 106 107#undef sock_errno 108#undef sock_errstr 109#undef sock_errset 110#define sock_errno() WSAGetLastError() 111#define sock_errstr(e, b, l) ber_pvt_wsa_err2string(e) 112#define sock_errset(e) WSASetLastError(e) 113 114LBER_F( char * ) ber_pvt_wsa_err2string LDAP_P((int)); 115 116#elif MACOS 117# define tcp_close( s ) tcpclose( s ) 118# define tcp_read( s, buf, len ) tcpread( s, buf, len ) 119# define tcp_write( s, buf, len ) tcpwrite( s, buf, len ) 120 121#elif DOS 122# ifdef PCNFS 123# define tcp_close( s ) close( s ) 124# define tcp_read( s, buf, len ) recv( s, buf, len, 0 ) 125# define tcp_write( s, buf, len ) send( s, buf, len, 0 ) 126# endif /* PCNFS */ 127# ifdef NCSA 128# define tcp_close( s ) do { netclose( s ); netshut() } while(0) 129# define tcp_read( s, buf, len ) nread( s, buf, len ) 130# define tcp_write( s, buf, len ) netwrite( s, buf, len ) 131# endif /* NCSA */ 132 133#elif defined(HAVE_CLOSESOCKET) 134# define tcp_close( s ) closesocket( s ) 135 136# ifdef __BEOS__ 137# define tcp_read( s, buf, len ) recv( s, buf, len, 0 ) 138# define tcp_write( s, buf, len ) send( s, buf, len, 0 ) 139# endif 140 141#else 142# define tcp_read( s, buf, len) read( s, buf, len ) 143# define tcp_write( s, buf, len) write( s, buf, len ) 144 145# ifdef SHUT_RDWR 146# define tcp_close( s ) (shutdown( s, SHUT_RDWR ), close( s )) 147# else 148# define tcp_close( s ) close( s ) 149# endif 150 151#ifdef HAVE_PIPE 152/* 153 * Only use pipe() on systems where file and socket descriptors 154 * are interchangeable 155 */ 156# define USE_PIPE HAVE_PIPE 157#endif 158 159#endif /* MACOS */ 160 161#ifndef ioctl_t 162# define ioctl_t int 163#endif 164 165#ifndef AC_SOCKET_INVALID 166# define AC_SOCKET_INVALID (-1) 167#endif 168#ifndef AC_SOCKET_ERROR 169# define AC_SOCKET_ERROR (-1) 170#endif 171 172#if !defined( HAVE_INET_ATON ) && !defined( inet_aton ) 173# define inet_aton ldap_pvt_inet_aton 174struct in_addr; 175LDAP_F (int) ldap_pvt_inet_aton LDAP_P(( const char *, struct in_addr * )); 176#endif 177 178#if defined(__WIN32) && defined(_ALPHA) 179/* NT on Alpha is hosed. */ 180# define AC_HTONL( l ) \ 181 ((((l)&0xffU)<<24) + (((l)&0xff00U)<<8) + \ 182 (((l)&0xff0000U)>>8) + (((l)&0xff000000U)>>24)) 183# define AC_NTOHL(l) AC_HTONL(l) 184 185#else 186# define AC_HTONL( l ) htonl( l ) 187# define AC_NTOHL( l ) ntohl( l ) 188#endif 189 190/* htons()/ntohs() may be broken much like htonl()/ntohl() */ 191#define AC_HTONS( s ) htons( s ) 192#define AC_NTOHS( s ) ntohs( s ) 193 194#ifdef LDAP_PF_LOCAL 195# if !defined( AF_LOCAL ) && defined( AF_UNIX ) 196# define AF_LOCAL AF_UNIX 197# endif 198# if !defined( PF_LOCAL ) && defined( PF_UNIX ) 199# define PF_LOCAL PF_UNIX 200# endif 201#endif 202 203#ifndef INET_ADDRSTRLEN 204# define INET_ADDRSTRLEN 16 205#endif 206#ifndef INET6_ADDRSTRLEN 207# define INET6_ADDRSTRLEN 46 208#endif 209 210#if defined( HAVE_GETADDRINFO ) || defined( HAVE_GETNAMEINFO ) 211# ifdef HAVE_GAI_STRERROR 212# define AC_GAI_STRERROR(x) (gai_strerror((x))) 213# else 214# define AC_GAI_STRERROR(x) (ldap_pvt_gai_strerror((x))) 215 LDAP_F (char *) ldap_pvt_gai_strerror( int ); 216# endif 217#endif 218 219#if defined(LDAP_PF_LOCAL) && \ 220 !defined(HAVE_GETPEEREID) && \ 221 !defined(HAVE_GETPEERUCRED) && \ 222 !defined(SO_PEERCRED) && !defined(LOCAL_PEERCRED) && \ 223 defined(HAVE_SENDMSG) && (defined(HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTSLEN) || \ 224 defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)) 225# define LDAP_PF_LOCAL_SENDMSG 1 226#endif 227 228#ifdef HAVE_GETPEEREID 229#define LUTIL_GETPEEREID( s, uid, gid, bv ) getpeereid( s, uid, gid ) 230#elif defined(LDAP_PF_LOCAL_SENDMSG) 231struct berval; 232LDAP_LUTIL_F( int ) lutil_getpeereid( int s, uid_t *, gid_t *, struct berval *bv ); 233#define LUTIL_GETPEEREID( s, uid, gid, bv ) lutil_getpeereid( s, uid, gid, bv ) 234#else 235LDAP_LUTIL_F( int ) lutil_getpeereid( int s, uid_t *, gid_t * ); 236#define LUTIL_GETPEEREID( s, uid, gid, bv ) lutil_getpeereid( s, uid, gid ) 237#endif 238 239union Sockaddr { 240 struct sockaddr sa_addr; 241 struct sockaddr_in sa_in_addr; 242#ifdef LDAP_PF_INET6 243 struct sockaddr_storage sa_storage; 244 struct sockaddr_in6 sa_in6_addr; 245#endif 246#ifdef LDAP_PF_LOCAL 247 struct sockaddr_un sa_un_addr; 248#endif 249}; 250 251/* DNS RFC defines max host name as 255. New systems seem to use 1024 */ 252#ifndef NI_MAXHOST 253#define NI_MAXHOST 256 254#endif 255 256#ifdef HAVE_POLL 257# ifndef INFTIM 258# define INFTIM (-1) 259# endif 260#undef POLL_OTHER 261#define POLL_OTHER (POLLERR|POLLHUP) 262#undef POLL_READ 263#define POLL_READ (POLLIN|POLLPRI|POLL_OTHER) 264#undef POLL_WRITE 265#define POLL_WRITE (POLLOUT|POLL_OTHER) 266#endif 267 268#endif /* _AC_SOCKET_H_ */ 269