]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - mac/otnet.c
1033961ddf170c78aba5f73c641b2e790ae139f4
[PuTTY.git] / mac / otnet.c
1 /*
2  * Macintosh OpenTransport networking abstraction
3  */
4
5 #if TARGET_API_MAC_CARBON
6 #define OTCARBONAPPLICATION 1
7 #endif
8
9 #include <OpenTransport.h>
10 #include <OpenTptInternet.h>
11
12 #include <string.h>
13
14 #define DEFINE_PLUG_METHOD_MACROS
15 #include "putty.h"
16 #include "network.h"
17 #include "mac.h"
18
19 struct Socket_tag {
20     struct socket_function_table *fn;
21     /* other stuff... */
22     OSStatus error;
23     EndpointRef ep;
24     Plug plug;
25     void *private_ptr;
26     bufchain output_data;
27     int connected;
28     int writable;
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 */
33     char oobdata[1];
34     int sending_oob;
35     int oobpending;        /* is there OOB data available to read?*/
36     int oobinline;
37     int pending_error;                 /* in case send() returns error */
38     int listener;
39     struct Socket_tag *next;
40     struct Socket_tag **prev;
41 };
42
43 typedef struct Socket_tag *Actual_Socket;
44
45 struct SockAddr_tag {
46     int resolved;
47     OSStatus error;
48     InetHostInfo hostinfo;
49     char hostname[512];
50 };
51
52 /* Globals */
53
54 static struct {
55     Actual_Socket socklist;
56     InetSvcRef inetsvc;
57 } ot;
58
59 OSErr ot_init(void)
60 {
61     OSStatus err;
62
63     err = InitOpenTransport();
64     if (err != kOTNoError) return err;
65     ot.inetsvc = OTOpenInternetServices(kDefaultInternetServicesPath, 0, &err);
66     return err;
67 }
68
69 void ot_cleanup(void)
70 {
71     Actual_Socket s;
72
73     for (s = ot.socklist; s !=NULL; s = s->next) {
74         OTUnbind(s->ep);
75         OTCloseProvider(s->ep);
76     }
77
78     CloseOpenTransport();
79 }
80
81 SockAddr ot_namelookup(char const *host, char **canonicalname)
82 {
83     SockAddr ret = smalloc(sizeof(struct SockAddr_tag));
84     char *realhost;
85
86     /* Casting away const -- hope OTInetStringToAddress is sensible */
87     ret->error = OTInetStringToAddress(ot.inetsvc, (char *)host,
88                                        &ret->hostinfo);
89     ret->resolved = TRUE;
90
91     if (ret->error == kOTNoError)
92         realhost = ret->hostinfo.name;
93     else
94         realhost = "";
95     *canonicalname = smalloc(1+strlen(realhost));
96     strcpy(*canonicalname, realhost);
97     return ret;
98 }
99
100 SockAddr ot_nonamelookup(char const *host)
101 {
102     SockAddr ret = smalloc(sizeof(struct SockAddr_tag));
103     
104     ret->resolved = FALSE;
105     ret->error = kOTNoError;
106     ret->hostname[0] = '\0';
107     strncat(ret->hostname, host, lenof(ret->hostname) - 1);
108     return ret;
109 }
110
111 void ot_getaddr(SockAddr addr, char *buf, int buflen)
112 {
113     char mybuf[16];
114
115     buf[0] = '\0';
116     if (addr->resolved) {
117         /* XXX only return first address */
118         OTInetHostToString(addr->hostinfo.addrs[0], mybuf);
119         strncat(buf, mybuf, buflen - 1);
120     } else
121         strncat(buf, addr->hostname, buflen - 1);
122 }
123
124 /* I think "local" here really means "loopback" */
125
126 int ot_hostname_is_local(char *name)
127 {
128
129     return !strcmp(name, "localhost");
130 }
131
132 int ot_address_is_local(SockAddr addr)
133 {
134     int i;
135
136     if (addr->resolved)
137         for (i = 0; i < kMaxHostAddrs; i++)
138             if (addr->hostinfo.addrs[i] & 0xff000000 == 0x7f000000)
139                 return TRUE;
140     return FALSE;
141 }
142
143 int ot_addrtype(SockAddr addr)
144 {
145
146     if (addr->resolved)
147         return ADDRTYPE_IPV4;
148     return ADDRTYPE_NAME;
149 }
150
151 void ot_addrcopy(SockAddr addr, char *buf)
152 {
153
154     /* XXX only return first address */
155     memcpy(buf, &addr->hostinfo.addrs[0], 4);
156 }
157
158 void ot_addr_free(SockAddr addr)
159 {
160     sfree(addr);
161 }
162
163
164 static Plug ot_tcp_plug(Socket sock, Plug p)
165 {
166     Actual_Socket s = (Actual_Socket) sock;
167     Plug ret = s->plug;
168     if (p)
169         s->plug = p;
170     return ret;
171 }
172
173 static void ot_tcp_flush(Socket s)
174 {
175     /*
176      * We send data to the socket as soon as we can anyway,
177      * so we don't need to do anything here.  :-)
178      */
179 }
180
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);
189 void ot_poll(void);
190
191 Socket ot_register(void *sock, Plug plug)
192 {
193     static struct socket_function_table fn_table = {
194         ot_tcp_plug,
195         ot_tcp_close,
196         ot_tcp_write,
197         ot_tcp_write_oob,
198         ot_tcp_flush,
199         ot_tcp_set_private_ptr,
200         ot_tcp_get_private_ptr,
201         ot_tcp_set_frozen,
202         ot_tcp_socket_error
203     };
204     
205     Actual_Socket ret;
206
207     ret = smalloc(sizeof(struct Socket_tag));
208     ret->fn = &fn_table;
209     ret->error = kOTNoError;
210     ret->plug = plug;
211     bufchain_init(&ret->output_data);
212     ret->writable = 1;                 /* to start with */
213     ret->sending_oob = 0;
214     ret->frozen = 1;
215     ret->frozen_readable = 0;
216     ret->localhost_only = 0;           /* unused, but best init anyway */
217     ret->pending_error = 0;
218     ret->oobpending = FALSE;
219     ret->listener = 0;
220
221     ret->ep = (EndpointRef)sock;
222   
223     /* some sort of error checking */
224
225     ret->oobinline = 0;
226   
227     /* Add this to the list of all sockets */
228     ret->next = ot.socklist;
229     ret->prev = &ot.socklist;
230     ot.socklist = ret;
231
232     return (Socket) ret;
233 }
234
235 Socket ot_new(SockAddr addr, int port, int privport, int oobinline,
236               int nodelay, Plug plug)
237 {
238     static struct socket_function_table fn_table = {
239         ot_tcp_plug,
240         ot_tcp_close,
241         ot_tcp_write,
242         ot_tcp_write_oob,
243         ot_tcp_flush,
244         ot_tcp_set_private_ptr,
245         ot_tcp_get_private_ptr,
246         ot_tcp_set_frozen,
247         ot_tcp_socket_error
248     };
249
250     Actual_Socket ret;
251     EndpointRef ep;
252     OSStatus err;
253     InetAddress dest;
254     TCall connectCall;
255
256     ret = smalloc(sizeof(struct Socket_tag));
257     ret->fn = &fn_table;
258     ret->error = kOTNoError;
259     ret->plug = plug;
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;
264     ret->frozen = 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;
270     ret->listener = 0;
271
272     /* Open Endpoint, configure it for TCP over anything */
273
274     ep = OTOpenEndpoint(OTCreateConfiguration("tcp"), 0, NULL, &err);
275
276     ret->ep = ep;
277
278     if (err) {
279         ret->error = err;
280         return (Socket) ret;
281     }
282
283     /* TODO: oobinline, nodelay */
284
285     /*
286      * Bind to local address.
287      */
288   
289     /* FIXME: pay attention to privport */
290
291     err = OTBind(ep, NULL, NULL); /* OpenTransport always picks our address */
292
293     if (err) {
294         ret->error = err;
295         return (Socket) ret;
296     }
297
298     /*
299      * Connect to remote address.
300      */
301
302     /* XXX Try non-primary addresses */
303     OTInitInetAddress(&dest, port, addr->hostinfo.addrs[0]);
304
305     memset(&connectCall, 0, sizeof(TCall));
306     connectCall.addr.buf = (UInt8 *) &dest;
307     connectCall.addr.len = sizeof(dest);
308
309     err = OTConnect(ep, &connectCall, nil);
310   
311     if (err) {
312         ret->error = err;
313         return (Socket) ret;
314     } else {
315         ret->connected = 1;
316         ret->writable = 1;
317     }
318
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;
324     ot.socklist = ret;
325
326     return (Socket) ret;
327 }
328     
329 Socket ot_newlistener(char *foobar, int port, Plug plug, int local_host_only)
330 {
331     Actual_Socket s;
332
333     return (Socket) s;
334 }
335
336 static void ot_tcp_close(Socket sock)
337 {
338     Actual_Socket s = (Actual_Socket) sock;
339     
340     OTCloseProvider(s->ep);
341
342     /* Unhitch from list of sockets */
343     *s->prev = s->next;
344     if (s->next != NULL)
345         s->next->prev = s->prev;
346
347     sfree(s);
348 }
349
350 static void try_send(Actual_Socket s)
351 {
352     while (bufchain_size(&s->output_data) > 0) {
353         int nsent;
354         void *data;
355         int len;
356
357         /* Don't care about oob right now */
358
359         bufchain_prefix(&s->output_data, &data, &len);
360
361         nsent = OTSnd(s->ep, data, len, 0);
362         noise_ultralight(nsent);
363
364         if (nsent <= 0) {
365             /* something bad happened, hey ho */
366         } else {
367             /* still don't care about oob */
368             bufchain_consume(&s->output_data, nsent);
369         }
370     }
371 }
372
373 static int ot_tcp_write(Socket sock, char const *buf, int len)
374 {
375     Actual_Socket s = (Actual_Socket) sock;
376
377     bufchain_add(&s->output_data, buf, len);
378
379     if (s->writable)
380         try_send(s);
381     return bufchain_size(&s->output_data);
382 }
383
384 static int ot_tcp_write_oob(Socket sock, char const *buf, int len)
385 {
386     /* Don't care about oob */
387     return 0;
388 }
389
390
391 /*
392  * Each socket abstraction contains a `void *' private field in
393  * which the client can keep state.
394  */
395 static void ot_tcp_set_private_ptr(Socket sock, void *ptr)
396 {
397     Actual_Socket s = (Actual_Socket) sock;
398     s->private_ptr = ptr;
399 }
400
401 static void *ot_tcp_get_private_ptr(Socket sock)
402 {
403     Actual_Socket s = (Actual_Socket) sock;
404     return s->private_ptr;
405 }
406
407
408 /*
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.
412  */
413 char *ot_addr_error(SockAddr addr)
414 {
415     static char buf[128];
416
417     if (addr->error == kOTNoError)
418         return NULL;
419     sprintf(buf, "error %d", addr->error);
420     return buf;
421 }
422 static char *ot_tcp_socket_error(Socket sock)
423 {
424     Actual_Socket s = (Actual_Socket) sock;
425     static char buf[128];
426
427     if (s->error == kOTNoError)
428         return NULL;
429     sprintf(buf, "error %d", s->error);
430     return buf;
431 }
432
433 static void ot_tcp_set_frozen(Socket sock, int is_frozen)
434 {
435     Actual_Socket s = (Actual_Socket) sock;
436
437     if (s->frozen == is_frozen)
438         return;
439     s->frozen = is_frozen;
440 }
441
442 /*
443  * Poll all our sockets from an event loop
444  */
445
446 void ot_poll(void)
447 {
448     Actual_Socket s;
449     OTResult o;
450
451     for (s = ot.socklist; s != NULL; s = s->next) {
452         o = OTLook(s->ep);
453
454         switch(o) {
455           case T_DATA: /* Normal Data */
456             ot_recv(s);
457             break;
458           case T_EXDATA: /* Expedited Data (urgent?) */
459             ot_recv(s);
460             break;
461         }
462     }
463 }
464
465 void ot_recv(Actual_Socket s)
466 {
467     OTResult o;
468     char buf[20480];
469     OTFlags flags;
470
471     if (s->frozen) return;
472
473     do {
474         o = OTRcv(s->ep, buf, sizeof(buf), &flags);
475         if (o > 0)
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 */
479     } while (o > 0);
480 }
481
482
483 /*
484  * Local Variables:
485  * c-file-style: "simon"
486  * End:
487  */