2 * Macintosh OpenTransport networking abstraction
5 #include <OpenTransport.h>
6 #include <OpenTptInternet.h>
10 #define DEFINE_PLUG_METHOD_MACROS
16 struct socket_function_table
*fn
;
25 int frozen
; /* this causes readability notifications to be ignored */
26 int frozen_readable
; /* this means we missed at least one readability
27 * notification while we were frozen */
28 int localhost_only
; /* for listening sockets */
31 int oobpending
; /* is there OOB data available to read?*/
33 int pending_error
; /* in case send() returns error */
35 struct Socket_tag
*next
;
36 struct Socket_tag
**prev
;
39 typedef struct Socket_tag
*Actual_Socket
;
44 InetHostInfo hostinfo
;
51 Actual_Socket socklist
;
59 err
= InitOpenTransport();
60 if (err
!= kOTNoError
) return err
;
61 ot
.inetsvc
= OTOpenInternetServices(kDefaultInternetServicesPath
, 0, &err
);
69 for (s
= ot
.socklist
; s
!=NULL
; s
= s
->next
) {
71 OTCloseProvider(s
->ep
);
77 SockAddr
ot_namelookup(char const *host
, char **canonicalname
)
79 SockAddr ret
= smalloc(sizeof(struct SockAddr_tag
));
82 /* Casting away const -- hope OTInetStringToAddress is sensible */
83 ret
->error
= OTInetStringToAddress(ot
.inetsvc
, (char *)host
,
87 if (ret
->error
== kOTNoError
)
88 realhost
= ret
->hostinfo
.name
;
91 *canonicalname
= smalloc(1+strlen(realhost
));
92 strcpy(*canonicalname
, realhost
);
96 SockAddr
ot_nonamelookup(char const *host
)
98 SockAddr ret
= smalloc(sizeof(struct SockAddr_tag
));
100 ret
->resolved
= FALSE
;
101 ret
->error
= kOTNoError
;
102 ret
->hostname
[0] = '\0';
103 strncat(ret
->hostname
, host
, lenof(ret
->hostname
) - 1);
107 void ot_getaddr(SockAddr addr
, char *buf
, int buflen
)
112 if (addr
->resolved
) {
113 /* XXX only return first address */
114 OTInetHostToString(addr
->hostinfo
.addrs
[0], mybuf
);
115 strncat(buf
, mybuf
, buflen
- 1);
117 strncat(buf
, addr
->hostname
, buflen
- 1);
120 /* I think "local" here really means "loopback" */
122 int ot_hostname_is_local(char *name
)
125 return !strcmp(name
, "localhost");
128 int ot_address_is_local(SockAddr addr
)
133 for (i
= 0; i
< kMaxHostAddrs
; i
++)
134 if (addr
->hostinfo
.addrs
[i
] & 0xff000000 == 0x7f000000)
139 int ot_addrtype(SockAddr addr
)
143 return ADDRTYPE_IPV4
;
144 return ADDRTYPE_NAME
;
147 void ot_addrcopy(SockAddr addr
, char *buf
)
150 /* XXX only return first address */
151 memcpy(buf
, &addr
->hostinfo
.addrs
[0], 4);
154 void ot_addr_free(SockAddr addr
)
160 static Plug
ot_tcp_plug(Socket sock
, Plug p
)
162 Actual_Socket s
= (Actual_Socket
) sock
;
169 static void ot_tcp_flush(Socket s
)
172 * We send data to the socket as soon as we can anyway,
173 * so we don't need to do anything here. :-)
177 static void ot_tcp_close(Socket s
);
178 static int ot_tcp_write(Socket s
, char const *data
, int len
);
179 static int ot_tcp_write_oob(Socket s
, char const *data
, int len
);
180 static void ot_tcp_set_private_ptr(Socket s
, void *ptr
);
181 static void *ot_tcp_get_private_ptr(Socket s
);
182 static void ot_tcp_set_frozen(Socket s
, int is_frozen
);
183 static char *ot_tcp_socket_error(Socket s
);
184 static void ot_recv(Actual_Socket s
);
187 Socket
ot_register(void *sock
, Plug plug
)
189 static struct socket_function_table fn_table
= {
195 ot_tcp_set_private_ptr
,
196 ot_tcp_get_private_ptr
,
203 ret
= smalloc(sizeof(struct Socket_tag
));
205 ret
->error
= kOTNoError
;
207 bufchain_init(&ret
->output_data
);
208 ret
->writable
= 1; /* to start with */
209 ret
->sending_oob
= 0;
211 ret
->frozen_readable
= 0;
212 ret
->localhost_only
= 0; /* unused, but best init anyway */
213 ret
->pending_error
= 0;
214 ret
->oobpending
= FALSE
;
217 ret
->ep
= (EndpointRef
)sock
;
219 /* some sort of error checking */
223 /* Add this to the list of all sockets */
224 ret
->next
= ot
.socklist
;
225 ret
->prev
= &ot
.socklist
;
231 Socket
ot_new(SockAddr addr
, int port
, int privport
, int oobinline
,
232 int nodelay
, Plug plug
)
234 static struct socket_function_table fn_table
= {
240 ot_tcp_set_private_ptr
,
241 ot_tcp_get_private_ptr
,
252 ret
= smalloc(sizeof(struct Socket_tag
));
254 ret
->error
= kOTNoError
;
256 bufchain_init(&ret
->output_data
);
257 ret
->connected
= 0; /* to start with */
258 ret
->writable
= 0; /* to start with */
259 ret
->sending_oob
= 0;
261 ret
->frozen_readable
= 0;
262 ret
->localhost_only
= 0; /* unused, but best init anyway */
263 ret
->pending_error
= 0;
264 ret
->oobinline
= oobinline
;
265 ret
->oobpending
= FALSE
;
268 /* Open Endpoint, configure it for TCP over anything */
270 ep
= OTOpenEndpoint(OTCreateConfiguration("tcp"), 0, NULL
, &err
);
279 /* TODO: oobinline, nodelay */
282 * Bind to local address.
285 /* FIXME: pay attention to privport */
287 err
= OTBind(ep
, NULL
, NULL
); /* OpenTransport always picks our address */
295 * Connect to remote address.
298 /* XXX Try non-primary addresses */
299 OTInitInetAddress(&dest
, port
, addr
->hostinfo
.addrs
[0]);
301 memset(&connectCall
, 0, sizeof(TCall
));
302 connectCall
.addr
.buf
= (UInt8
*) &dest
;
303 connectCall
.addr
.len
= sizeof(dest
);
305 err
= OTConnect(ep
, &connectCall
, nil
);
315 /* Add this to the list of all sockets */
316 ret
->next
= ot
.socklist
;
317 ret
->prev
= &ot
.socklist
;
318 if (ret
->next
!= NULL
)
319 ret
->next
->prev
= &ret
->next
;
325 Socket
ot_newlistener(char *foobar
, int port
, Plug plug
, int local_host_only
)
332 static void ot_tcp_close(Socket sock
)
334 Actual_Socket s
= (Actual_Socket
) sock
;
336 OTCloseProvider(s
->ep
);
338 /* Unhitch from list of sockets */
341 s
->next
->prev
= s
->prev
;
346 static void try_send(Actual_Socket s
)
348 while (bufchain_size(&s
->output_data
) > 0) {
353 /* Don't care about oob right now */
355 bufchain_prefix(&s
->output_data
, &data
, &len
);
357 nsent
= OTSnd(s
->ep
, data
, len
, 0);
358 noise_ultralight(nsent
);
361 /* something bad happened, hey ho */
363 /* still don't care about oob */
364 bufchain_consume(&s
->output_data
, nsent
);
369 static int ot_tcp_write(Socket sock
, char const *buf
, int len
)
371 Actual_Socket s
= (Actual_Socket
) sock
;
373 bufchain_add(&s
->output_data
, buf
, len
);
377 return bufchain_size(&s
->output_data
);
380 static int ot_tcp_write_oob(Socket sock
, char const *buf
, int len
)
382 /* Don't care about oob */
388 * Each socket abstraction contains a `void *' private field in
389 * which the client can keep state.
391 static void ot_tcp_set_private_ptr(Socket sock
, void *ptr
)
393 Actual_Socket s
= (Actual_Socket
) sock
;
394 s
->private_ptr
= ptr
;
397 static void *ot_tcp_get_private_ptr(Socket sock
)
399 Actual_Socket s
= (Actual_Socket
) sock
;
400 return s
->private_ptr
;
405 * Special error values are returned from ot_namelookup and ot_new
406 * if there's a problem. These functions extract an error message,
407 * or return NULL if there's no problem.
409 char *ot_addr_error(SockAddr addr
)
411 static char buf
[128];
413 if (addr
->error
== kOTNoError
)
415 sprintf(buf
, "error %d", addr
->error
);
418 static char *ot_tcp_socket_error(Socket sock
)
420 Actual_Socket s
= (Actual_Socket
) sock
;
421 static char buf
[128];
423 if (s
->error
== kOTNoError
)
425 sprintf(buf
, "error %d", s
->error
);
429 static void ot_tcp_set_frozen(Socket sock
, int is_frozen
)
431 Actual_Socket s
= (Actual_Socket
) sock
;
433 if (s
->frozen
== is_frozen
)
435 s
->frozen
= is_frozen
;
439 * Poll all our sockets from an event loop
447 for (s
= ot
.socklist
; s
!= NULL
; s
= s
->next
) {
451 case T_DATA
: /* Normal Data */
454 case T_EXDATA
: /* Expedited Data (urgent?) */
461 void ot_recv(Actual_Socket s
)
467 if (s
->frozen
) return;
470 o
= OTRcv(s
->ep
, buf
, sizeof(buf
), &flags
);
472 plug_receive(s
->plug
, 0, buf
, o
);
473 if (o
< 0 && o
!= kOTNoDataErr
)
474 plug_closing(s
->plug
, NULL
, 0, 0); /* XXX Error msg */
481 * c-file-style: "simon"