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;
321 Socket ot_newlistener(char *foobar, int port, Plug plug, int local_host_only)
328 static void ot_tcp_close(Socket sock)
330 Actual_Socket s = (Actual_Socket) sock;
332 OTCloseProvider(s->ep);
334 /* Unhitch from list of sockets */
337 s->next->prev = s->prev;
342 static void try_send(Actual_Socket s)
344 while (bufchain_size(&s->output_data) > 0) {
349 /* Don't care about oob right now */
351 bufchain_prefix(&s->output_data, &data, &len);
353 nsent = OTSnd(s->ep, data, len, 0);
354 noise_ultralight(nsent);
357 /* something bad happened, hey ho */
359 /* still don't care about oob */
360 bufchain_consume(&s->output_data, nsent);
365 static int ot_tcp_write(Socket sock, char const *buf, int len)
367 Actual_Socket s = (Actual_Socket) sock;
369 bufchain_add(&s->output_data, buf, len);
373 return bufchain_size(&s->output_data);
376 static int ot_tcp_write_oob(Socket sock, char const *buf, int len)
378 /* Don't care about oob */
384 * Each socket abstraction contains a `void *' private field in
385 * which the client can keep state.
387 static void ot_tcp_set_private_ptr(Socket sock, void *ptr)
389 Actual_Socket s = (Actual_Socket) sock;
390 s->private_ptr = ptr;
393 static void *ot_tcp_get_private_ptr(Socket sock)
395 Actual_Socket s = (Actual_Socket) sock;
396 return s->private_ptr;
401 * Special error values are returned from ot_namelookup and ot_new
402 * if there's a problem. These functions extract an error message,
403 * or return NULL if there's no problem.
405 char *ot_addr_error(SockAddr addr)
407 static char buf[128];
409 if (addr->error == kOTNoError)
411 sprintf(buf, "error %d", addr->error);
414 static char *ot_tcp_socket_error(Socket sock)
416 Actual_Socket s = (Actual_Socket) sock;
417 static char buf[128];
419 if (s->error == kOTNoError)
421 sprintf(buf, "error %d", s->error);
425 static void ot_tcp_set_frozen(Socket sock, int is_frozen)
427 Actual_Socket s = (Actual_Socket) sock;
429 if (s->frozen == is_frozen)
431 s->frozen = is_frozen;
435 * Poll all our sockets from an event loop
443 for (s = ot.socklist; s != NULL; s = s->next) {
447 case T_DATA: /* Normal Data */
450 case T_EXDATA: /* Expedited Data (urgent?) */
457 void ot_recv(Actual_Socket s)
463 if (s->frozen) return;
465 while ((o = OTRcv(s->ep, buf, sizeof(buf), &flags)) != kOTNoDataErr) {
466 plug_receive(s->plug, 0, buf, sizeof(buf));
473 * c-file-style: "simon"