1 /* $Copyright:
2  *
3  * Copyright 1998-2000 by the Massachusetts Institute of Technology.
4  *
5  * All rights reserved.
6  *
7  * Permission to use, copy, modify, and distribute this software and its
8  * documentation for any purpose and without fee is hereby granted,
9  * provided that the above copyright notice appear in all copies and that
10  * both that copyright notice and this permission notice appear in
11  * supporting documentation, and that the name of M.I.T. not be used in
12  * advertising or publicity pertaining to distribution of the software
13  * without specific, written prior permission.  Furthermore if you modify
14  * this software you must label your software as modified software and not
15  * distribute it in such a fashion that it might be confused with the
16  * original MIT software. M.I.T. makes no representations about the
17  * suitability of this software for any purpose.  It is provided "as is"
18  * without express or implied warranty.
19  *
20  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
21  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
22  * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23  *
24  * Individual source code files are copyright MIT, Cygnus Support,
25  * OpenVision, Oracle, Sun Soft, FundsXpress, and others.
26  *
27  * Project Athena, Athena, Athena MUSE, Discuss, Hesiod, Kerberos, Moira,
28  * and Zephyr are trademarks of the Massachusetts Institute of Technology
29  * (MIT).  No commercial use of these trademarks may be made without prior
30  * written permission of MIT.
31  *
32  * "Commercial use" means use of a name in a product or other for-profit
33  * manner.  It does NOT prevent a commercial firm from referring to the MIT
34  * trademarks in order to convey information (although in doing so,
35  * recognition of their trademark status should be given).
36  * $
37  */
38 
39 /* $Header: /cvs/src/sasl/mac/CommonKClient/mac_kclient3/Headers/KerberosSupport/Sockets.h,v 1.2 2001/12/04 02:05:57 rjs3 Exp $ */
40 
41 /*
42  *
43  * Sockets.h -- Main external header file for the sockets library.
44  *
45  */
46 
47 
48 #include <KerberosSupport/ErrorLib.h>
49 #include <KerberosSupport/Utilities.h>
50 #include <KerberosSupport/SocketErrors.h>
51 #include <unix.h>
52 
53 #include <MacTypes.h>
54 #include <Events.h>
55 #include <OpenTptInternet.h>
56 
57 #ifndef _SOCKETS_
58 #define _SOCKETS_
59 
60 #ifdef __cplusplus
61 extern "C" {
62 #endif
63 
64 #if defined(__CFM68K__) && !defined(__USING_STATIC_LIBS__)
65 #	pragma import on
66 #endif
67 
68 /*******************/
69 /* API Definitions */
70 /*******************/
71 
72 #define FD_SETSIZE		256    	/*	The maximum # of sockets -- cannot be changed	*/
73 
74 /* socket types */
75 #define SOCK_STREAM		0		/*	stream socket -- connection oriented 			*/
76 #define SOCK_DGRAM		1		/*	datagram socket -- connectionless				*/
77 #define SOCK_RAW		2		/*	raw socket 										*/
78 #define SOCK_SEQPACKET	3		/*	sequenced packet socket							*/
79 #define SOCK_RDM		4		/*	reliably delivered message socket				*/
80 
81 /* address families  -- get AF_INET from OpenTptInternet.h */
82 #define AF_UNSPEC		0		/*	Unspecified										*/
83 #define AF_UNIX			1		/*	Unix internal protocols							*/
84 
85 /* protocol families */
86 #define PF_UNSPEC	AF_UNSPEC	/*	Unspecified										*/
87 #define PF_UNIX		AF_UNIX		/*	Unix internal protocols							*/
88 #define PF_INET		AF_INET		/*	Internet protocols								*/
89 
90 /* IP Address Wildcard */
91 #define INADDR_ANY      kOTAnyInetAddress
92 #define INADDR_NONE		0xffffffff
93 
94 /* recv and send flags */
95 #define MSG_DONTROUTE	1
96 #define MSG_DONTWAIT	2
97 #define MSG_OOB			4
98 #define MSG_PEEK		8
99 #define MSG_WAITALL		16
100 
101 #define NUMBITSPERBYTE	8		/* 	Number of bits per byte							*/
102 
103 /* socket_fnctl() requests */
104 #define F_GETFL			3		/* Get file flags 									*/
105 #define F_SETFL			4		/* Set file flags 									*/
106 
107 /* shutdown() flags */
108 #define SHUT_RD			0		/* Shutdown read side of the connection				*/
109 #define SHUT_WR			1		/* Shutdown write side of the connection			*/
110 #define SHUT_RDWR		2		/* Shutdown read and write sides of the connection	*/
111 
112 /* IP address sizes */
113 #define INET_ADDRSTRLEN		16	/* for IPv4 dotted decimal							*/
114 #define INET6_ADDRSTRLEN	46	/* for IPv6 dhex string								*/
115 #define INADDRSZ			4	/* size of IPv4 addr in bytes 						*/
116 #define IN6ADDRSZ			16	/* size of IPv6 addr in bytes						*/
117 
118 /* host name size */
119 #define MAXHOSTNAMESIZE		kMaxHostNameLen
120 #define MAXHOSTNAMELEN		kMaxHostNameLen
121 
122 #ifdef USE_STREAMS
123 /* Constants for poll() */
124 #define POLLIN          0x001   /* A non-priority message is available */
125 #define POLLPRI         0x002   /* A high priority message is available */
126 #define POLLOUT         0x004   /* The stream is writable for non-priority messages */
127 #define POLLERR         0x008   /* A error message has arrived */
128 #define POLLHUP         0x010   /* A hangup has occurred */
129 #define POLLNVAL        0x020   /* This fd is bogus */
130 #define POLLRDNORM      0x040   /* A non-priority message is available */
131 #define POLLRDBAND      0x080   /* A priority message (band > 0) message is available */
132 #define POLLWRNORM      0x100   /* Same as POLLOUT */
133 #define POLLWRBAND      0x200   /* A priority band exists and is writable */
134 #define POLLMSG         0x400   /* A signal message has reached the front of the queue */
135 #endif /* USE_STREAMS */
136 
137 /**********/
138 /* Macros */
139 /**********/
140 
141 /* network byte order conversion [none since we're already big-endian] */
142 #define	ntohl(x) 		(x)
143 #define	ntohs(x) 		(x)
144 #define	htonl(x) 		(x)
145 #define	htons(x) 		(x)
146 
147 /* macros for select */
148 #define FD_SET(fd, fdset)	((fdset)->fds_bits[(fd) / NFDBITS] |= ((unsigned)1 << ((fd) % NFDBITS)))
149 #define FD_CLR(fd, fdset)	((fdset)->fds_bits[(fd) / NFDBITS] &= ~((unsigned)1 << ((fd) % NFDBITS)))
150 #define FD_ISSET(fd, fdset)	((fdset)->fds_bits[(fd) / NFDBITS] & ((unsigned)1 << ((fd) % NFDBITS)))
151 #define FD_ZERO(fdset)		memset((char *)(fdset), 0, sizeof(*(fdset)))
152 
153 
154 /****************************/
155 /* API Structures and Types */
156 /****************************/
157 
158 /* An internet address */
159 typedef UInt32  in_addr_t;
160 
161 /* size of address structures */
162 typedef UInt32 socklen_t;
163 
164 /* structure used to store addresses */
165 struct sockaddr {
166 	u_short	sa_family;
167 	char	sa_data[14];
168 };
169 
170 /* INET protocol structures */
171 struct in_addr {
172 	in_addr_t	s_addr;
173 };
174 
175 /* A TCP address -- the same as a OT InetAddress */
176 struct sockaddr_in {				/* struct InetAddress {				*/
177 	u_short 		sin_family;		/* OTAddressType	fAddressType	*/
178 	u_short 		sin_port;		/* InetPort			fPort			*/
179 	struct in_addr 	sin_addr;		/* InetHost			fHost			*/
180 	char 			sin_zero[8];	/* UInt8			fUnused			*/
181 };									/* };								*/
182 
183 /* structures for select */
184 typedef long fd_mask;
185 #define NFDBITS			(sizeof(fd_mask) * NUMBITSPERBYTE)      /* bits per mask */
186 
187 typedef struct fd_set {
188   fd_mask fds_bits[(FD_SETSIZE + NFDBITS - 1) / NFDBITS];
189 } fd_set;
190 
191 /* Structure for non-contiguous data */
192 struct iovec {
193   struct iovec *next;			/* For compatibility with Open Transport */
194   void         *iov_base;		/* Starting address of buffer */
195   size_t        iov_len;		/* size of buffer */
196 };
197 
198 /* For poll() */
199 struct pollfd {
200   int   fd;
201   short events;
202   short revents;
203 };
204 
205 /***********************/
206 /* Function Prototypes */
207 /***********************/
208 
209 #if !TARGET_RT_MAC_CFM
210 #   pragma d0_pointers on
211 #else
212 #   define SocketsLibraryIsPresent() ((Ptr) (socket) != (Ptr) (kUnresolvedCFragSymbolAddress))
213 #endif
214 
215 /* Sockets Control API calls */
216 OSStatus 			   AbortSocketOperation(int sockFD);
217 OSStatus 			   AbortAllDNSOperations(void);
218 Boolean                IsValidSocket(int sockFD);
219 
220 /* Sockets API calls */
221 int socket(int family, int type, int protocol);
222 int socket_bind(int sockFD, const struct sockaddr *myAddr, int addrLength);
223 int socket_fcntl(int sockFD, int command, int flags);
224 int socket_close(int sockFD);
225 int socket_shutdown(int sockFD, int howTo);
226 int socket_connect(int sockFD, struct sockaddr *servAddr, int addrLength);
227 int socket_select(int maxFDsExamined, fd_set *readFDs, fd_set *writeFDs, fd_set *exceptFDs,
228                   struct timeval *timeOut);
229 #ifdef USE_STREAMS
230 int socket_poll (struct pollfd *fds, unsigned int nfds, int timeout);
231 #endif /* USE_STREAMS */
232 int socket_getpeername(int sockFD, struct sockaddr *peerAddr, int *addrLength);
233 int socket_getsockname(int sockFD, struct sockaddr *localAddr, int *addrLength);
234 int socket_read(int sockFD, void *buffer, UInt32 numBytes);
235 int socket_write(int sockFD, const void *buffer, UInt32 numBytes);
236 int socket_readv(int sockFD, struct iovec *iov, UInt32 iovCount);
237 int socket_writev(int sockFD, struct iovec *iov, UInt32 iovCount);
238 int socket_recv(int sockFD, void *buffer, UInt32 numBytes, int flags);
239 int socket_send(int sockFD, const void *buffer, UInt32 numBytes, int flags);
240 int socket_recvfrom(int sockFD, void *buffer, UInt32 numBytes, int flags, struct sockaddr *fromAddr,
241                     socklen_t *addrLength);
242 int socket_sendto(int sockFD, const void *buffer, UInt32 numBytes, int flags, struct sockaddr *toAddr,
243                   socklen_t addrLength);
244 
245 /* Utilites API calls */
246 char 	   *inet_ntoa(struct in_addr addr);
247 int 	    inet_aton(const char *str, struct in_addr *addr);
248 in_addr_t 	inet_addr(const char *str);
249 int 	    inet_pton(int family, const char *str, void *addr);
250 const char *inet_ntop(int family, const void *addr, char *str, size_t len);
251 
252 #if !TARGET_RT_MAC_CFM
253 #   pragma d0_pointers reset
254 #endif
255 
256 #if defined(__CFM68K__) && !defined(__USING_STATIC_LIBS__)
257 #	pragma import reset
258 #endif
259 
260 #ifdef __cplusplus
261 }
262 #endif
263 
264 #endif /* _SOCKETS_ */
265