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
;
49 Actual_Socket socklist
;
54 return InitOpenTransport();
61 for (s
= ot
.socklist
; s
!=NULL
; s
= s
->next
) {
63 OTCloseProvider(s
->ep
);
69 SockAddr
ot_namelookup(char *host
, char **canonicalname
)
71 SockAddr ret
= smalloc(sizeof(struct SockAddr_tag
));
73 ret
->error
= kOTNoError
;
74 OTInitDNSAddress(&(ret
->address
), host
);
76 /* for now we'll pretend canonicalname is always just host */
78 *canonicalname
= smalloc(1+strlen(host
));
79 strcpy(*canonicalname
, host
);
83 SockAddr
ot_nonamelookup(char *host
)
85 SockAddr ret
= smalloc(sizeof(struct SockAddr_tag
));
87 OTInitDNSAddress(&(ret
->address
), host
);
92 void ot_getaddr(SockAddr addr
, char *buf
, int buflen
)
94 strncpy(buf
, (addr
->address
).fName
, buflen
);
97 /* I think "local" here really means "loopback" */
99 int ot_hostname_is_local(char *name
)
102 return !strcmp(name
, "localhost");
105 int ot_address_is_local(SockAddr addr
)
112 int ot_addrtype(SockAddr addr
)
114 return ADDRTYPE_IPV4
;
117 void ot_addrcopy(SockAddr addr
, char *buf
)
122 void ot_addr_free(SockAddr addr
)
128 static Plug
ot_tcp_plug(Socket sock
, Plug p
)
130 Actual_Socket s
= (Actual_Socket
) sock
;
137 static void ot_tcp_flush(Socket s
)
140 * We send data to the socket as soon as we can anyway,
141 * so we don't need to do anything here. :-)
145 static void ot_tcp_close(Socket s
);
146 static int ot_tcp_write(Socket s
, char const *data
, int len
);
147 static int ot_tcp_write_oob(Socket s
, char const *data
, int len
);
148 static void ot_tcp_set_private_ptr(Socket s
, void *ptr
);
149 static void *ot_tcp_get_private_ptr(Socket s
);
150 static void ot_tcp_set_frozen(Socket s
, int is_frozen
);
151 static char *ot_tcp_socket_error(Socket s
);
152 static void ot_recv(Actual_Socket s
);
155 Socket
ot_register(void *sock
, Plug plug
)
157 static struct socket_function_table fn_table
= {
163 ot_tcp_set_private_ptr
,
164 ot_tcp_get_private_ptr
,
171 ret
= smalloc(sizeof(struct Socket_tag
));
173 ret
->error
= kOTNoError
;
175 bufchain_init(&ret
->output_data
);
176 ret
->writable
= 1; /* to start with */
177 ret
->sending_oob
= 0;
179 ret
->frozen_readable
= 0;
180 ret
->localhost_only
= 0; /* unused, but best init anyway */
181 ret
->pending_error
= 0;
182 ret
->oobpending
= FALSE
;
185 ret
->ep
= (EndpointRef
)sock
;
187 /* some sort of error checking */
191 /* Add this to the list of all sockets */
192 ret
->next
= ot
.socklist
;
193 ret
->prev
= &ot
.socklist
;
199 Socket
ot_new(SockAddr addr
, int port
, int privport
, int oobinline
,
200 int nodelay
, Plug plug
)
202 static struct socket_function_table fn_table
= {
208 ot_tcp_set_private_ptr
,
209 ot_tcp_get_private_ptr
,
219 ret
= smalloc(sizeof(struct Socket_tag
));
221 ret
->error
= kOTNoError
;
223 bufchain_init(&ret
->output_data
);
224 ret
->connected
= 0; /* to start with */
225 ret
->writable
= 0; /* to start with */
226 ret
->sending_oob
= 0;
228 ret
->frozen_readable
= 0;
229 ret
->localhost_only
= 0; /* unused, but best init anyway */
230 ret
->pending_error
= 0;
231 ret
->oobinline
= oobinline
;
232 ret
->oobpending
= FALSE
;
235 /* Open Endpoint, configure it for TCP over anything */
237 ep
= OTOpenEndpoint(OTCreateConfiguration("tcp"), 0, NULL
, &err
);
246 /* TODO: oobinline, nodelay */
249 * Bind to local address.
252 /* FIXME: pay attention to privport */
254 err
= OTBind(ep
, NULL
, NULL
); /* OpenTransport always picks our address */
262 * Connect to remote address.
265 /* FIXME: bolt the port onto the end */
267 memset(&connectCall
, 0, sizeof(TCall
));
268 connectCall
.addr
.buf
= (UInt8
*) &(addr
->address
);
269 connectCall
.addr
.len
= sizeof(DNSAddress
);
271 err
= OTConnect(ep
, &connectCall
, nil
);
281 /* Add this to the list of all sockets */
282 ret
->next
= ot
.socklist
;
283 ret
->prev
= &ot
.socklist
;
289 Socket
ot_newlistener(char *foobar
, int port
, Plug plug
, int local_host_only
)
296 static void ot_tcp_close(Socket sock
)
298 Actual_Socket s
= (Actual_Socket
) sock
;
300 OTCloseProvider(s
->ep
);
302 /* Unhitch from list of sockets */
305 s
->next
->prev
= s
->prev
;
310 static void try_send(Actual_Socket s
)
312 while (bufchain_size(&s
->output_data
) > 0) {
317 /* Don't care about oob right now */
319 bufchain_prefix(&s
->output_data
, &data
, &len
);
321 nsent
= OTSnd(s
->ep
, data
, len
, 0);
322 noise_ultralight(nsent
);
325 /* something bad happened, hey ho */
327 /* still don't care about oob */
328 bufchain_consume(&s
->output_data
, nsent
);
333 static int ot_tcp_write(Socket sock
, char const *buf
, int len
)
335 Actual_Socket s
= (Actual_Socket
) sock
;
337 bufchain_add(&s
->output_data
, buf
, len
);
341 return bufchain_size(&s
->output_data
);
344 static int ot_tcp_write_oob(Socket sock
, char const *buf
, int len
)
346 /* Don't care about oob */
352 * Each socket abstraction contains a `void *' private field in
353 * which the client can keep state.
355 static void ot_tcp_set_private_ptr(Socket sock
, void *ptr
)
357 Actual_Socket s
= (Actual_Socket
) sock
;
358 s
->private_ptr
= ptr
;
361 static void *ot_tcp_get_private_ptr(Socket sock
)
363 Actual_Socket s
= (Actual_Socket
) sock
;
364 return s
->private_ptr
;
369 * Special error values are returned from ot_namelookup and ot_new
370 * if there's a problem. These functions extract an error message,
371 * or return NULL if there's no problem.
373 char *ot_addr_error(SockAddr addr
)
375 static char buf
[128];
377 if (addr
->error
== kOTNoError
)
379 sprintf(buf
, "error %d", addr
->error
);
382 static char *ot_tcp_socket_error(Socket sock
)
384 Actual_Socket s
= (Actual_Socket
) sock
;
385 static char buf
[128];
387 if (s
->error
== kOTNoError
)
389 sprintf(buf
, "error %d", s
->error
);
393 static void ot_tcp_set_frozen(Socket sock
, int is_frozen
)
395 Actual_Socket s
= (Actual_Socket
) sock
;
397 if (s
->frozen
== is_frozen
)
399 s
->frozen
= is_frozen
;
403 * Poll all our sockets from an event loop
411 for (s
= ot
.socklist
; s
!= NULL
; s
= s
->next
) {
415 case T_DATA
: /* Normal Data */
418 case T_EXDATA
: /* Expedited Data (urgent?) */
425 void ot_recv(Actual_Socket s
)
431 if (s
->frozen
) return;
433 while ((o
= OTRcv(s
->ep
, buf
, sizeof(buf
), &flags
)) != kOTNoDataErr
) {
434 plug_receive(s
->plug
, 0, buf
, sizeof(buf
));
441 * c-file-style: "simon"