2 * io_sock.c - SecureTransport sample I/O module, X sockets version
10 #include <sys/types.h>
11 #include <netinet/in.h>
12 #include <sys/socket.h>
14 #include <arpa/inet.h>
17 #include <CoreServices/../Frameworks/CarbonCore.framework/Headers/MacErrors.h>
21 /* debugging for this module */
22 #define SSL_OT_DEBUG 1
24 /* log errors to stdout */
25 #define SSL_OT_ERRLOG 1
27 /* trace all low-level network I/O */
28 #define SSL_OT_IO_TRACE 0
30 /* if SSL_OT_IO_TRACE, only log non-zero length transfers */
31 #define SSL_OT_IO_TRACE_NZ 1
33 /* pause after each I/O (only meaningful if SSL_OT_IO_TRACE == 1) */
34 #define SSL_OT_IO_PAUSE 0
36 /* print a stream of dots while I/O pending */
39 /* dump some bytes of each I/O (only meaningful if SSL_OT_IO_TRACE == 1) */
40 #define SSL_OT_IO_DUMP 0
41 #define SSL_OT_IO_DUMP_SIZE 1024
43 /* indicate errSSLWouldBlock with a '.' */
44 #define SSL_DISPL_WOULD_BLOCK 0
46 /* general, not-too-verbose debugging */
48 #define dprintf(s) printf s
53 /* errors --> stdout */
55 #define eprintf(s) printf s
60 /* trace completion of every r/w */
68 #if SSL_OT_IO_TRACE_NZ
73 printf("%s(%u): moved (%u) bytes\n", str
, (unsigned)req
, (unsigned)act
);
78 for(i
=0; i
<act
; i
++) {
79 printf("%02X ", buf
[i
]);
80 if(i
>= (SSL_OT_IO_DUMP_SIZE
- 1)) {
93 printf("CR to continue: ");
100 #define tprintf(str, req, act, buf)
101 #endif /* SSL_OT_IO_TRACE */
104 * If SSL_OT_DOT, output a '.' every so often while waiting for
105 * connection. This gives user a chance to do something else with the
111 static time_t lastTime
= (time_t)0;
112 #define TIME_INTERVAL 3
114 static void outputDot()
116 time_t thisTime
= time(0);
118 if((thisTime
- lastTime
) >= TIME_INTERVAL
) {
119 printf("."); fflush(stdout
);
129 * One-time only init.
139 #define GETHOST_RETRIES 3
141 OSStatus
MakeServerConnection(
142 const char *hostName
,
144 int nonBlocking
, // 0 or 1
145 otSocket
*socketNo
, // RETURNED
146 PeerSpec
*peer
) // RETURNED
148 struct sockaddr_in addr
;
154 if (hostName
[0] >= '0' && hostName
[0] <= '9')
156 host
.s_addr
= inet_addr(hostName
);
160 /* seeing a lot of soft failures here that I really don't want to track down */
161 for(dex
=0; dex
<GETHOST_RETRIES
; dex
++) {
163 printf("\n...retrying gethostbyname(%s)", hostName
);
165 ent
= gethostbyname(hostName
);
171 printf("\n***gethostbyname(%s) returned: %s\n", hostName
, hstrerror(h_errno
));
174 memcpy(&host
, ent
->h_addr
, sizeof(struct in_addr
));
176 sock
= socket(AF_INET
, SOCK_STREAM
, 0);
177 addr
.sin_addr
= host
;
178 addr
.sin_port
= htons((u_short
)port
);
180 addr
.sin_family
= AF_INET
;
181 if (connect(sock
, (struct sockaddr
*) &addr
, sizeof(struct sockaddr_in
)) != 0)
182 { printf("connect returned error\n");
187 /* OK to do this after connect? */
188 int rtn
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
190 perror("fctnl(O_NONBLOCK)");
195 peer
->ipAddr
= addr
.sin_addr
.s_addr
;
196 peer
->port
= htons((u_short
)port
);
197 *socketNo
= (otSocket
)sock
;
202 * Set up an otSocket to listen for client connections. Call once, then
203 * use multiple AcceptClientConnection calls.
205 OSStatus
ListenForClients(
207 int nonBlocking
, // 0 or 1
208 otSocket
*socketNo
) // RETURNED
210 struct sockaddr_in addr
;
215 sock
= socket(AF_INET
, SOCK_STREAM
, 0);
221 ent
= gethostbyname("localhost");
223 perror("gethostbyname");
226 memcpy(&addr
.sin_addr
, ent
->h_addr
, sizeof(struct in_addr
));
228 addr
.sin_port
= htons((u_short
)port
);
229 addr
.sin_addr
.s_addr
= INADDR_ANY
;
230 addr
.sin_family
= AF_INET
;
231 len
= sizeof(struct sockaddr_in
);
232 if (bind(sock
, (struct sockaddr
*) &addr
, len
)) {
235 if(theErr
== EADDRINUSE
) {
243 int rtn
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
245 perror("fctnl(O_NONBLOCK)");
251 int rtn
= listen(sock
, 1);
254 *socketNo
= (otSocket
)sock
;
271 * Accept a client connection.
275 * Currently we always get back a different peer port number on successive
276 * connections, no matter what the client is doing. To test for resumable
277 * session support, force peer port = 0.
279 #define FORCE_ACCEPT_PEER_PORT_ZERO 1
281 OSStatus
AcceptClientConnection(
282 otSocket listenSock
, // obtained from ListenForClients
283 otSocket
*acceptSock
, // RETURNED
284 PeerSpec
*peer
) // RETURNED
286 struct sockaddr_in addr
;
290 len
= sizeof(struct sockaddr_in
);
292 sock
= accept((int)listenSock
, (struct sockaddr
*) &addr
, &len
);
294 if(errno
== EAGAIN
) {
295 /* nonblocking, no connection yet */
307 *acceptSock
= (otSocket
)sock
;
308 peer
->ipAddr
= addr
.sin_addr
.s_addr
;
309 #if FORCE_ACCEPT_PEER_PORT_ZERO
312 peer
->port
= ntohs(addr
.sin_port
);
318 * Shut down a connection.
320 void endpointShutdown(
327 * R/W. Called out from SSL.
330 SSLConnectionRef connection
,
331 void *data
, /* owned by
334 size_t *dataLength
) /* IN/OUT */
336 UInt32 bytesToGo
= *dataLength
;
337 UInt32 initLen
= bytesToGo
;
338 UInt8
*currData
= (UInt8
*)data
;
339 int sock
= (int)((long)connection
);
340 OSStatus rtn
= noErr
;
348 /* paranoid check, ensure errno is getting written */
350 rrtn
= recv(sock
, currData
, bytesToGo
, 0);
354 rtn
= errSSLClosedGraceful
;
361 * Undocumented but I definitely see this.
362 * Non-blocking sockets only. Definitely retriable
363 * just like an EAGAIN.
365 dprintf(("SocketRead RETRYING on ENOENT, rrtn %d\n",
368 //rtn = errSSLWouldBlock;
369 /* ...for temp testing.... */
373 /* explicit peer abort */
374 rtn
= errSSLClosedAbort
;
377 /* nonblocking, no data */
378 rtn
= errSSLWouldBlock
;
381 dprintf(("SocketRead: read(%u) error %d, rrtn %d\n",
382 (unsigned)bytesToGo
, theErr
, (int)rrtn
));
386 /* in any case, we're done with this call if rrtn <= 0 */
390 bytesToGo
-= bytesRead
;
391 currData
+= bytesRead
;
394 /* filled buffer with incoming data, done */
398 *dataLength
= initLen
- bytesToGo
;
399 tprintf("SocketRead", initLen
, *dataLength
, (UInt8
*)data
);
401 #if SSL_OT_DOT || (SSL_OT_DEBUG && !SSL_OT_IO_TRACE)
402 if((rtn
== 0) && (*dataLength
== 0)) {
407 #if SSL_DISPL_WOULD_BLOCK
408 if(rtn
== errSSLWouldBlock
) {
409 printf("."); fflush(stdout
);
417 OSStatus
SocketWrite(
418 SSLConnectionRef connection
,
420 size_t *dataLength
) /* IN/OUT */
422 size_t bytesSent
= 0;
423 int sock
= (int)((long)connection
);
425 size_t dataLen
= *dataLength
;
426 const UInt8
*dataPtr
= (UInt8
*)data
;
429 if(oneAtATime
&& (*dataLength
> 1)) {
435 for(i
=0; i
<dataLen
; i
++) {
437 ortn
= SocketWrite(connection
, dataPtr
, &thisMove
);
450 (char*)dataPtr
+ bytesSent
,
451 dataLen
- bytesSent
);
452 } while ((length
> 0) &&
453 ( (bytesSent
+= length
) < dataLen
) );
459 ortn
= errSSLWouldBlock
; break;
461 /* as of Leopard 9A312 or so, the error formerly seen as EPIPE is
462 * now reported as ECONNRESET. This happens when we're catching
463 * SIGPIPE and we write to a socket which has been closed by the peer.
466 ortn
= errSSLClosedAbort
; break;
468 dprintf(("SocketWrite: write(%u) error %d\n",
469 (unsigned)(dataLen
- bytesSent
), theErr
));
477 tprintf("SocketWrite", dataLen
, bytesSent
, dataPtr
);
478 *dataLength
= bytesSent
;