2 * Macintosh OpenTransport networking abstraction
5 #if TARGET_API_MAC_CARBON
6 #define OTCARBONAPPLICATION 1
9 #include <OpenTransport.h>
10 #include <OpenTptInternet.h>
14 #define DEFINE_PLUG_METHOD_MACROS
20 struct socket_function_table
*fn
;
29 int frozen
; /* this causes readability notifications to be ignored */
30 int frozen_readable
; /* this means we missed at least one readability
31 * notification while we were frozen */
32 int localhost_only
; /* for listening sockets */
35 int oobpending
; /* is there OOB data available to read?*/
37 int pending_error
; /* in case send() returns error */
39 struct Socket_tag
*next
;
40 struct Socket_tag
**prev
;
43 typedef struct Socket_tag
*Actual_Socket
;
48 InetHostInfo hostinfo
;
55 Actual_Socket socklist
;
63 err
= InitOpenTransport();
64 if (err
!= kOTNoError
) return err
;
65 ot
.inetsvc
= OTOpenInternetServices(kDefaultInternetServicesPath
, 0, &err
);
73 for (s
= ot
.socklist
; s
!=NULL
; s
= s
->next
) {
75 OTCloseProvider(s
->ep
);
81 SockAddr
ot_namelookup(char const *host
, char **canonicalname
)
83 SockAddr ret
= smalloc(sizeof(struct SockAddr_tag
));
86 /* Casting away const -- hope OTInetStringToAddress is sensible */
87 ret
->error
= OTInetStringToAddress(ot
.inetsvc
, (char *)host
,
91 if (ret
->error
== kOTNoError
)
92 realhost
= ret
->hostinfo
.name
;
95 *canonicalname
= smalloc(1+strlen(realhost
));
96 strcpy(*canonicalname
, realhost
);
100 SockAddr
ot_nonamelookup(char const *host
)
102 SockAddr ret
= smalloc(sizeof(struct SockAddr_tag
));
104 ret
->resolved
= FALSE
;
105 ret
->error
= kOTNoError
;
106 ret
->hostname
[0] = '\0';
107 strncat(ret
->hostname
, host
, lenof(ret
->hostname
) - 1);
111 void ot_getaddr(SockAddr addr
, char *buf
, int buflen
)
116 if (addr
->resolved
) {
117 /* XXX only return first address */
118 OTInetHostToString(addr
->hostinfo
.addrs
[0], mybuf
);
119 strncat(buf
, mybuf
, buflen
- 1);
121 strncat(buf
, addr
->hostname
, buflen
- 1);
124 /* I think "local" here really means "loopback" */
126 int ot_hostname_is_local(char *name
)
129 return !strcmp(name
, "localhost");
132 int ot_address_is_local(SockAddr addr
)
137 for (i
= 0; i
< kMaxHostAddrs
; i
++)
138 if (addr
->hostinfo
.addrs
[i
] & 0xff000000 == 0x7f000000)
143 int ot_addrtype(SockAddr addr
)
147 return ADDRTYPE_IPV4
;
148 return ADDRTYPE_NAME
;
151 void ot_addrcopy(SockAddr addr
, char *buf
)
154 /* XXX only return first address */
155 memcpy(buf
, &addr
->hostinfo
.addrs
[0], 4);
158 void ot_addr_free(SockAddr addr
)
164 static Plug
ot_tcp_plug(Socket sock
, Plug p
)
166 Actual_Socket s
= (Actual_Socket
) sock
;
173 static void ot_tcp_flush(Socket s
)
176 * We send data to the socket as soon as we can anyway,
177 * so we don't need to do anything here. :-)
181 static void ot_tcp_close(Socket s
);
182 static int ot_tcp_write(Socket s
, char const *data
, int len
);
183 static int ot_tcp_write_oob(Socket s
, char const *data
, int len
);
184 static void ot_tcp_set_private_ptr(Socket s
, void *ptr
);
185 static void *ot_tcp_get_private_ptr(Socket s
);
186 static void ot_tcp_set_frozen(Socket s
, int is_frozen
);
187 static char *ot_tcp_socket_error(Socket s
);
188 static void ot_recv(Actual_Socket s
);
191 Socket
ot_register(void *sock
, Plug plug
)
193 static struct socket_function_table fn_table
= {
199 ot_tcp_set_private_ptr
,
200 ot_tcp_get_private_ptr
,
207 ret
= smalloc(sizeof(struct Socket_tag
));
209 ret
->error
= kOTNoError
;
211 bufchain_init(&ret
->output_data
);
212 ret
->writable
= 1; /* to start with */
213 ret
->sending_oob
= 0;
215 ret
->frozen_readable
= 0;
216 ret
->localhost_only
= 0; /* unused, but best init anyway */
217 ret
->pending_error
= 0;
218 ret
->oobpending
= FALSE
;
221 ret
->ep
= (EndpointRef
)sock
;
223 /* some sort of error checking */
227 /* Add this to the list of all sockets */
228 ret
->next
= ot
.socklist
;
229 ret
->prev
= &ot
.socklist
;
235 Socket
ot_new(SockAddr addr
, int port
, int privport
, int oobinline
,
236 int nodelay
, Plug plug
)
238 static struct socket_function_table fn_table
= {
244 ot_tcp_set_private_ptr
,
245 ot_tcp_get_private_ptr
,
256 ret
= smalloc(sizeof(struct Socket_tag
));
258 ret
->error
= kOTNoError
;
260 bufchain_init(&ret
->output_data
);
261 ret
->connected
= 0; /* to start with */
262 ret
->writable
= 0; /* to start with */
263 ret
->sending_oob
= 0;
265 ret
->frozen_readable
= 0;
266 ret
->localhost_only
= 0; /* unused, but best init anyway */
267 ret
->pending_error
= 0;
268 ret
->oobinline
= oobinline
;
269 ret
->oobpending
= FALSE
;
272 /* Open Endpoint, configure it for TCP over anything */
274 ep
= OTOpenEndpoint(OTCreateConfiguration("tcp"), 0, NULL
, &err
);
283 /* TODO: oobinline, nodelay */
286 * Bind to local address.
289 /* FIXME: pay attention to privport */
291 err
= OTBind(ep
, NULL
, NULL
); /* OpenTransport always picks our address */
299 * Connect to remote address.
302 /* XXX Try non-primary addresses */
303 OTInitInetAddress(&dest
, port
, addr
->hostinfo
.addrs
[0]);
305 memset(&connectCall
, 0, sizeof(TCall
));
306 connectCall
.addr
.buf
= (UInt8
*) &dest
;
307 connectCall
.addr
.len
= sizeof(dest
);
309 err
= OTConnect(ep
, &connectCall
, nil
);
319 /* Add this to the list of all sockets */
320 ret
->next
= ot
.socklist
;
321 ret
->prev
= &ot
.socklist
;
322 if (ret
->next
!= NULL
)
323 ret
->next
->prev
= &ret
->next
;
329 Socket
ot_newlistener(char *foobar
, int port
, Plug plug
, int local_host_only
)
336 static void ot_tcp_close(Socket sock
)
338 Actual_Socket s
= (Actual_Socket
) sock
;
340 OTCloseProvider(s
->ep
);
342 /* Unhitch from list of sockets */
345 s
->next
->prev
= s
->prev
;
350 static void try_send(Actual_Socket s
)
352 while (bufchain_size(&s
->output_data
) > 0) {
357 /* Don't care about oob right now */
359 bufchain_prefix(&s
->output_data
, &data
, &len
);
361 nsent
= OTSnd(s
->ep
, data
, len
, 0);
362 noise_ultralight(nsent
);
365 /* something bad happened, hey ho */
367 /* still don't care about oob */
368 bufchain_consume(&s
->output_data
, nsent
);
373 static int ot_tcp_write(Socket sock
, char const *buf
, int len
)
375 Actual_Socket s
= (Actual_Socket
) sock
;
377 bufchain_add(&s
->output_data
, buf
, len
);
381 return bufchain_size(&s
->output_data
);
384 static int ot_tcp_write_oob(Socket sock
, char const *buf
, int len
)
386 /* Don't care about oob */
392 * Each socket abstraction contains a `void *' private field in
393 * which the client can keep state.
395 static void ot_tcp_set_private_ptr(Socket sock
, void *ptr
)
397 Actual_Socket s
= (Actual_Socket
) sock
;
398 s
->private_ptr
= ptr
;
401 static void *ot_tcp_get_private_ptr(Socket sock
)
403 Actual_Socket s
= (Actual_Socket
) sock
;
404 return s
->private_ptr
;
409 * Special error values are returned from ot_namelookup and ot_new
410 * if there's a problem. These functions extract an error message,
411 * or return NULL if there's no problem.
413 char *ot_addr_error(SockAddr addr
)
415 static char buf
[128];
417 if (addr
->error
== kOTNoError
)
419 sprintf(buf
, "error %d", addr
->error
);
422 static char *ot_tcp_socket_error(Socket sock
)
424 Actual_Socket s
= (Actual_Socket
) sock
;
425 static char buf
[128];
427 if (s
->error
== kOTNoError
)
429 sprintf(buf
, "error %d", s
->error
);
433 static void ot_tcp_set_frozen(Socket sock
, int is_frozen
)
435 Actual_Socket s
= (Actual_Socket
) sock
;
437 if (s
->frozen
== is_frozen
)
439 s
->frozen
= is_frozen
;
443 * Poll all our sockets from an event loop
451 for (s
= ot
.socklist
; s
!= NULL
; s
= s
->next
) {
455 case T_DATA
: /* Normal Data */
458 case T_EXDATA
: /* Expedited Data (urgent?) */
465 void ot_recv(Actual_Socket s
)
471 if (s
->frozen
) return;
474 o
= OTRcv(s
->ep
, buf
, sizeof(buf
), &flags
);
476 plug_receive(s
->plug
, 0, buf
, o
);
477 if (o
< 0 && o
!= kOTNoDataErr
)
478 plug_closing(s
->plug
, NULL
, 0, 0); /* XXX Error msg */
485 * c-file-style: "simon"