2 * Macintosh OpenTransport networking abstraction
5 #if TARGET_API_MAC_CARBON
6 #define OTCARBONAPPLICATION 1
9 #include <Files.h> /* Needed by OpenTransportInternet.h */
10 #include <OpenTransport.h>
11 #include <OpenTptInternet.h>
15 #define DEFINE_PLUG_METHOD_MACROS
21 struct socket_function_table
*fn
;
30 int frozen
; /* this causes readability notifications to be ignored */
31 int frozen_readable
; /* this means we missed at least one readability
32 * notification while we were frozen */
33 int localhost_only
; /* for listening sockets */
36 int oobpending
; /* is there OOB data available to read?*/
38 int pending_error
; /* in case send() returns error */
40 struct Socket_tag
*next
;
41 struct Socket_tag
**prev
;
44 typedef struct Socket_tag
*Actual_Socket
;
49 InetHostInfo hostinfo
;
56 Actual_Socket socklist
;
64 err
= InitOpenTransport();
65 if (err
!= kOTNoError
) return err
;
66 ot
.inetsvc
= OTOpenInternetServices(kDefaultInternetServicesPath
, 0, &err
);
74 for (s
= ot
.socklist
; s
!=NULL
; s
= s
->next
) {
76 OTCloseProvider(s
->ep
);
82 SockAddr
ot_namelookup(char const *host
, char **canonicalname
)
84 SockAddr ret
= snew(struct SockAddr_tag
);
87 /* Casting away const -- hope OTInetStringToAddress is sensible */
88 ret
->error
= OTInetStringToAddress(ot
.inetsvc
, (char *)host
,
92 if (ret
->error
== kOTNoError
)
93 realhost
= ret
->hostinfo
.name
;
96 *canonicalname
= snewn(1+strlen(realhost
), char);
97 strcpy(*canonicalname
, realhost
);
101 SockAddr
ot_nonamelookup(char const *host
)
103 SockAddr ret
= snew(struct SockAddr_tag
);
105 ret
->resolved
= FALSE
;
106 ret
->error
= kOTNoError
;
107 ret
->hostname
[0] = '\0';
108 strncat(ret
->hostname
, host
, lenof(ret
->hostname
) - 1);
112 void ot_getaddr(SockAddr addr
, char *buf
, int buflen
)
117 if (addr
->resolved
) {
118 /* XXX only return first address */
119 OTInetHostToString(addr
->hostinfo
.addrs
[0], mybuf
);
120 strncat(buf
, mybuf
, buflen
- 1);
122 strncat(buf
, addr
->hostname
, buflen
- 1);
125 /* I think "local" here really means "loopback" */
127 int ot_hostname_is_local(char *name
)
130 return !strcmp(name
, "localhost");
133 int ot_address_is_local(SockAddr addr
)
138 for (i
= 0; i
< kMaxHostAddrs
; i
++)
139 if (addr
->hostinfo
.addrs
[i
] & 0xff000000 == 0x7f000000)
144 int ot_addrtype(SockAddr addr
)
148 return ADDRTYPE_IPV4
;
149 return ADDRTYPE_NAME
;
152 void ot_addrcopy(SockAddr addr
, char *buf
)
155 /* XXX only return first address */
156 memcpy(buf
, &addr
->hostinfo
.addrs
[0], 4);
159 void ot_addr_free(SockAddr addr
)
165 static Plug
ot_tcp_plug(Socket sock
, Plug p
)
167 Actual_Socket s
= (Actual_Socket
) sock
;
174 static void ot_tcp_flush(Socket s
)
177 * We send data to the socket as soon as we can anyway,
178 * so we don't need to do anything here. :-)
182 static void ot_tcp_close(Socket s
);
183 static int ot_tcp_write(Socket s
, char const *data
, int len
);
184 static int ot_tcp_write_oob(Socket s
, char const *data
, int len
);
185 static void ot_tcp_set_private_ptr(Socket s
, void *ptr
);
186 static void *ot_tcp_get_private_ptr(Socket s
);
187 static void ot_tcp_set_frozen(Socket s
, int is_frozen
);
188 static const char *ot_tcp_socket_error(Socket s
);
189 static void ot_recv(Actual_Socket s
);
190 static void ot_listenaccept(Actual_Socket s
);
193 Socket
ot_register(void *sock
, Plug plug
)
195 static struct socket_function_table fn_table
= {
201 ot_tcp_set_private_ptr
,
202 ot_tcp_get_private_ptr
,
209 ret
= snew(struct Socket_tag
);
211 ret
->error
= kOTNoError
;
213 bufchain_init(&ret
->output_data
);
214 ret
->writable
= 1; /* to start with */
215 ret
->sending_oob
= 0;
217 ret
->frozen_readable
= 0;
218 ret
->localhost_only
= 0; /* unused, but best init anyway */
219 ret
->pending_error
= 0;
220 ret
->oobpending
= FALSE
;
223 ret
->ep
= (EndpointRef
)sock
;
225 /* some sort of error checking */
229 /* Add this to the list of all sockets */
230 ret
->next
= ot
.socklist
;
231 ret
->prev
= &ot
.socklist
;
237 Socket
ot_new(SockAddr addr
, int port
, int privport
, int oobinline
,
238 int nodelay
, int keepalive
, Plug plug
)
240 static struct socket_function_table fn_table
= {
246 ot_tcp_set_private_ptr
,
247 ot_tcp_get_private_ptr
,
258 ret
= snew(struct Socket_tag
);
260 ret
->error
= kOTNoError
;
262 bufchain_init(&ret
->output_data
);
263 ret
->connected
= 0; /* to start with */
264 ret
->writable
= 0; /* to start with */
265 ret
->sending_oob
= 0;
267 ret
->frozen_readable
= 0;
268 ret
->localhost_only
= 0; /* unused, but best init anyway */
269 ret
->pending_error
= 0;
270 ret
->oobinline
= oobinline
;
271 ret
->oobpending
= FALSE
;
274 /* Open Endpoint, configure it for TCP over anything */
276 ep
= OTOpenEndpoint(OTCreateConfiguration("tcp"), 0, NULL
, &err
);
285 /* TODO: oobinline, nodelay, keepalive */
288 * Bind to local address.
291 /* FIXME: pay attention to privport */
293 err
= OTBind(ep
, NULL
, NULL
); /* OpenTransport always picks our address */
301 * Connect to remote address.
304 /* XXX Try non-primary addresses */
305 OTInitInetAddress(&dest
, port
, addr
->hostinfo
.addrs
[0]);
307 memset(&connectCall
, 0, sizeof(TCall
));
308 connectCall
.addr
.buf
= (UInt8
*) &dest
;
309 connectCall
.addr
.len
= sizeof(dest
);
311 err
= OTConnect(ep
, &connectCall
, nil
);
321 /* Add this to the list of all sockets */
322 ret
->next
= ot
.socklist
;
323 ret
->prev
= &ot
.socklist
;
324 if (ret
->next
!= NULL
)
325 ret
->next
->prev
= &ret
->next
;
328 /* XXX: don't know whether we can sk_addr_free(addr); */
333 Socket
ot_newlistener(char *srcaddr
, int port
, Plug plug
, int local_host_only
,
336 static struct socket_function_table fn_table
= {
342 ot_tcp_set_private_ptr
,
343 ot_tcp_get_private_ptr
,
354 ret
= snew(struct Socket_tag
);
356 ret
->error
= kOTNoError
;
358 bufchain_init(&ret
->output_data
);
359 ret
->writable
= 0; /* to start with */
360 ret
->sending_oob
= 0;
362 ret
->frozen_readable
= 0;
363 ret
->localhost_only
= local_host_only
;
364 ret
->pending_error
= 0;
366 ret
->oobpending
= FALSE
;
369 /* Open Endpoint, configure it for TCP over anything, and load the
370 * tilisten module to serialize multiple simultaneous
373 ep
= OTOpenEndpoint(OTCreateConfiguration("tilisten,tcp"), 0, NULL
, &err
);
382 /* TODO: set SO_REUSEADDR */
384 OTInitInetAddress(&addr
, port
, kOTAnyInetAddress
);
385 /* XXX: pay attention to local_host_only */
387 tbind
.addr
.buf
= (UInt8
*) &addr
;
388 tbind
.addr
.len
= sizeof(addr
);
391 err
= OTBind(ep
, &tbind
, NULL
); /* XXX: check qlen we got */
398 /* Add this to the list of all sockets */
399 ret
->next
= ot
.socklist
;
400 ret
->prev
= &ot
.socklist
;
401 if (ret
->next
!= NULL
)
402 ret
->next
->prev
= &ret
->next
;
408 static void ot_tcp_close(Socket sock
)
410 Actual_Socket s
= (Actual_Socket
) sock
;
412 OTCloseProvider(s
->ep
);
414 /* Unhitch from list of sockets */
417 s
->next
->prev
= s
->prev
;
422 static void try_send(Actual_Socket s
)
424 while (bufchain_size(&s
->output_data
) > 0) {
429 /* Don't care about oob right now */
431 bufchain_prefix(&s
->output_data
, &data
, &len
);
433 nsent
= OTSnd(s
->ep
, data
, len
, 0);
434 noise_ultralight(nsent
);
437 /* something bad happened, hey ho */
439 /* still don't care about oob */
440 bufchain_consume(&s
->output_data
, nsent
);
445 static int ot_tcp_write(Socket sock
, char const *buf
, int len
)
447 Actual_Socket s
= (Actual_Socket
) sock
;
449 bufchain_add(&s
->output_data
, buf
, len
);
453 return bufchain_size(&s
->output_data
);
456 static int ot_tcp_write_oob(Socket sock
, char const *buf
, int len
)
458 /* Don't care about oob */
464 * Each socket abstraction contains a `void *' private field in
465 * which the client can keep state.
467 static void ot_tcp_set_private_ptr(Socket sock
, void *ptr
)
469 Actual_Socket s
= (Actual_Socket
) sock
;
470 s
->private_ptr
= ptr
;
473 static void *ot_tcp_get_private_ptr(Socket sock
)
475 Actual_Socket s
= (Actual_Socket
) sock
;
476 return s
->private_ptr
;
481 * Special error values are returned from ot_namelookup and ot_new
482 * if there's a problem. These functions extract an error message,
483 * or return NULL if there's no problem.
485 char *ot_addr_error(SockAddr addr
)
487 static char buf
[128];
489 if (addr
->error
== kOTNoError
)
491 sprintf(buf
, "error %d", addr
->error
);
494 static const char *ot_tcp_socket_error(Socket sock
)
496 Actual_Socket s
= (Actual_Socket
) sock
;
497 static char buf
[128];
499 if (s
->error
== kOTNoError
)
501 sprintf(buf
, "error %d", s
->error
);
505 static void ot_tcp_set_frozen(Socket sock
, int is_frozen
)
507 Actual_Socket s
= (Actual_Socket
) sock
;
509 if (s
->frozen
== is_frozen
)
511 s
->frozen
= is_frozen
;
515 * Poll all our sockets from an event loop
523 for (s
= ot
.socklist
; s
!= NULL
; s
= s
->next
) {
527 case T_DATA
: /* Normal Data */
530 case T_EXDATA
: /* Expedited Data (urgent?) */
533 case T_LISTEN
: /* Connection attempt */
540 void ot_recv(Actual_Socket s
)
546 if (s
->frozen
) return;
548 o
= OTRcv(s
->ep
, buf
, sizeof(buf
), &flags
);
550 plug_receive(s
->plug
, 0, buf
, o
);
551 if (o
< 0 && o
!= kOTNoDataErr
)
552 plug_closing(s
->plug
, NULL
, 0, 0); /* XXX Error msg */
555 void ot_listenaccept(Actual_Socket s
)
559 InetAddress remoteaddr
;
563 tcall
.addr
.maxlen
= sizeof(InetAddress
);
564 tcall
.addr
.buf
= (unsigned char *)&remoteaddr
;
565 tcall
.opt
.maxlen
= 0;
566 tcall
.opt
.buf
= NULL
;
567 tcall
.udata
.maxlen
= 0;
568 tcall
.udata
.buf
= NULL
;
570 o
= OTListen(s
->ep
, &tcall
);
575 /* We've found an incoming connection, accept it */
577 ep
= OTOpenEndpoint(OTCreateConfiguration("tcp"), 0, NULL
, &err
);
578 o
= OTAccept(s
->ep
, ep
, &tcall
);
579 if (plug_accepting(s
->plug
, ep
)) {
587 * c-file-style: "simon"