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 *host, char **canonicalname)
79 SockAddr ret = smalloc(sizeof(struct SockAddr_tag));
82 ret->error = OTInetStringToAddress(ot.inetsvc, host, &ret->hostinfo);
85 if (ret->error == kOTNoError)
86 realhost = ret->hostinfo.name;
89 *canonicalname = smalloc(1+strlen(realhost));
90 strcpy(*canonicalname, realhost);
94 SockAddr ot_nonamelookup(char *host)
96 SockAddr ret = smalloc(sizeof(struct SockAddr_tag));
98 ret->resolved = FALSE;
99 ret->error = kOTNoError;
100 ret->hostname[0] = '\0';
101 strncat(ret->hostname, host, lenof(ret->hostname) - 1);
105 void ot_getaddr(SockAddr addr, char *buf, int buflen)
110 if (addr->resolved) {
111 /* XXX only return first address */
112 OTInetHostToString(addr->hostinfo.addrs[0], mybuf);
113 strncat(buf, mybuf, buflen - 1);
115 strncat(buf, addr->hostname, buflen - 1);
118 /* I think "local" here really means "loopback" */
120 int ot_hostname_is_local(char *name)
123 return !strcmp(name, "localhost");
126 int ot_address_is_local(SockAddr addr)
131 for (i = 0; i < kMaxHostAddrs; i++)
132 if (addr->hostinfo.addrs[i] & 0xff000000 == 0x7f000000)
137 int ot_addrtype(SockAddr addr)
141 return ADDRTYPE_IPV4;
142 return ADDRTYPE_NAME;
145 void ot_addrcopy(SockAddr addr, char *buf)
148 /* XXX only return first address */
149 memcpy(buf, &addr->hostinfo.addrs[0], 4);
152 void ot_addr_free(SockAddr addr)
158 static Plug ot_tcp_plug(Socket sock, Plug p)
160 Actual_Socket s = (Actual_Socket) sock;
167 static void ot_tcp_flush(Socket s)
170 * We send data to the socket as soon as we can anyway,
171 * so we don't need to do anything here. :-)
175 static void ot_tcp_close(Socket s);
176 static int ot_tcp_write(Socket s, char const *data, int len);
177 static int ot_tcp_write_oob(Socket s, char const *data, int len);
178 static void ot_tcp_set_private_ptr(Socket s, void *ptr);
179 static void *ot_tcp_get_private_ptr(Socket s);
180 static void ot_tcp_set_frozen(Socket s, int is_frozen);
181 static char *ot_tcp_socket_error(Socket s);
182 static void ot_recv(Actual_Socket s);
185 Socket ot_register(void *sock, Plug plug)
187 static struct socket_function_table fn_table = {
193 ot_tcp_set_private_ptr,
194 ot_tcp_get_private_ptr,
201 ret = smalloc(sizeof(struct Socket_tag));
203 ret->error = kOTNoError;
205 bufchain_init(&ret->output_data);
206 ret->writable = 1; /* to start with */
207 ret->sending_oob = 0;
209 ret->frozen_readable = 0;
210 ret->localhost_only = 0; /* unused, but best init anyway */
211 ret->pending_error = 0;
212 ret->oobpending = FALSE;
215 ret->ep = (EndpointRef)sock;
217 /* some sort of error checking */
221 /* Add this to the list of all sockets */
222 ret->next = ot.socklist;
223 ret->prev = &ot.socklist;
229 Socket ot_new(SockAddr addr, int port, int privport, int oobinline,
230 int nodelay, Plug plug)
232 static struct socket_function_table fn_table = {
238 ot_tcp_set_private_ptr,
239 ot_tcp_get_private_ptr,
250 ret = smalloc(sizeof(struct Socket_tag));
252 ret->error = kOTNoError;
254 bufchain_init(&ret->output_data);
255 ret->connected = 0; /* to start with */
256 ret->writable = 0; /* to start with */
257 ret->sending_oob = 0;
259 ret->frozen_readable = 0;
260 ret->localhost_only = 0; /* unused, but best init anyway */
261 ret->pending_error = 0;
262 ret->oobinline = oobinline;
263 ret->oobpending = FALSE;
266 /* Open Endpoint, configure it for TCP over anything */
268 ep = OTOpenEndpoint(OTCreateConfiguration("tcp"), 0, NULL, &err);
277 /* TODO: oobinline, nodelay */
280 * Bind to local address.
283 /* FIXME: pay attention to privport */
285 err = OTBind(ep, NULL, NULL); /* OpenTransport always picks our address */
293 * Connect to remote address.
296 /* XXX Try non-primary addresses */
297 OTInitInetAddress(&dest, port, addr->hostinfo.addrs[0]);
299 memset(&connectCall, 0, sizeof(TCall));
300 connectCall.addr.buf = (UInt8 *) &dest;
301 connectCall.addr.len = sizeof(dest);
303 err = OTConnect(ep, &connectCall, nil);
313 /* Add this to the list of all sockets */
314 ret->next = ot.socklist;
315 ret->prev = &ot.socklist;
316 if (ret->next != NULL)
317 ret->next->prev = &ret->next;
323 Socket ot_newlistener(char *foobar, int port, Plug plug, int local_host_only)
330 static void ot_tcp_close(Socket sock)
332 Actual_Socket s = (Actual_Socket) sock;
334 OTCloseProvider(s->ep);
336 /* Unhitch from list of sockets */
339 s->next->prev = s->prev;
344 static void try_send(Actual_Socket s)
346 while (bufchain_size(&s->output_data) > 0) {
351 /* Don't care about oob right now */
353 bufchain_prefix(&s->output_data, &data, &len);
355 nsent = OTSnd(s->ep, data, len, 0);
356 noise_ultralight(nsent);
359 /* something bad happened, hey ho */
361 /* still don't care about oob */
362 bufchain_consume(&s->output_data, nsent);
367 static int ot_tcp_write(Socket sock, char const *buf, int len)
369 Actual_Socket s = (Actual_Socket) sock;
371 bufchain_add(&s->output_data, buf, len);
375 return bufchain_size(&s->output_data);
378 static int ot_tcp_write_oob(Socket sock, char const *buf, int len)
380 /* Don't care about oob */
386 * Each socket abstraction contains a `void *' private field in
387 * which the client can keep state.
389 static void ot_tcp_set_private_ptr(Socket sock, void *ptr)
391 Actual_Socket s = (Actual_Socket) sock;
392 s->private_ptr = ptr;
395 static void *ot_tcp_get_private_ptr(Socket sock)
397 Actual_Socket s = (Actual_Socket) sock;
398 return s->private_ptr;
403 * Special error values are returned from ot_namelookup and ot_new
404 * if there's a problem. These functions extract an error message,
405 * or return NULL if there's no problem.
407 char *ot_addr_error(SockAddr addr)
409 static char buf[128];
411 if (addr->error == kOTNoError)
413 sprintf(buf, "error %d", addr->error);
416 static char *ot_tcp_socket_error(Socket sock)
418 Actual_Socket s = (Actual_Socket) sock;
419 static char buf[128];
421 if (s->error == kOTNoError)
423 sprintf(buf, "error %d", s->error);
427 static void ot_tcp_set_frozen(Socket sock, int is_frozen)
429 Actual_Socket s = (Actual_Socket) sock;
431 if (s->frozen == is_frozen)
433 s->frozen = is_frozen;
437 * Poll all our sockets from an event loop
445 for (s = ot.socklist; s != NULL; s = s->next) {
449 case T_DATA: /* Normal Data */
452 case T_EXDATA: /* Expedited Data (urgent?) */
459 void ot_recv(Actual_Socket s)
465 if (s->frozen) return;
468 o = OTRcv(s->ep, buf, sizeof(buf), &flags);
470 plug_receive(s->plug, 0, buf, sizeof(buf));
471 if (o < 0 && o != kOTNoDataErr)
472 plug_closing(s->plug, NULL, 0, 0); /* XXX Error msg */
479 * c-file-style: "simon"