]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - opentpt.c
sunc with reality
[PuTTY.git] / opentpt.c
1 /* $Id: opentpt.c,v 1.1.2.2 1999/08/02 22:32:39 ben Exp $ */
2 /*
3  * Copyright (c) 1999 Ben Harris
4  * All rights reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation
8  * files (the "Software"), to deal in the Software without
9  * restriction, including without limitation the rights to use,
10  * copy, modify, merge, publish, distribute, sublicense, and/or
11  * sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following
13  * conditions:
14  * 
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  * 
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
23  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25  * SOFTWARE.
26  */
27
28 #include <MacTypes.h>
29 #include <CodeFragments.h>
30 #include <OpenTransport.h>
31 #include <OpenTptInternet.h>
32
33 #include <stdio.h>
34 #include <string.h>
35
36 #include "putty.h"
37
38 /* See the top of macnet.c for some idea of how this is meant to work. */
39
40 struct otpt_socket {
41     EndpointRef ep;
42     Session *sess;
43     OTLIFO sendq;
44     OTLIFO eventq;
45     long eventhandler;
46 };
47
48 struct otpt_netevent {
49     OTLink next;
50     Net_Event_Type type;
51 };
52
53 static int otpt_init(void);
54 static void otpt_shutdown(void);
55 static void otpt_poll(void);
56 static void *otpt_open(Session *, char const *, int);
57 static int otpt_recv(void *, void *, int, int);
58 static int otpt_send(void *, void *, int, int);
59 static void otpt_close(void *);
60 static void otpt_destroy(void *);
61 static pascal void otpt_notifier(void *, OTEventCode, OTResult , void *);
62 static void otpt_sendevent(struct otpt_socket *, Net_Event_Type);
63 static pascal void otpt_rcvevent(void *);
64
65 Network_Stack otpt_stack = {
66     otpt_init, otpt_open, otpt_recv, otpt_send, otpt_poll, otpt_close,
67     otpt_destroy, otpt_shutdown
68 };
69
70 static OTConfiguration *otpt_config = kOTInvalidConfigurationPtr;
71
72 static int otpt_init(void) {
73     OSErr err;
74
75     /* Check that the OpenTransport libraries were there (really just ppc) */
76     if (&InitOpenTransport == kUnresolvedCFragSymbolAddress)
77         return 1;
78     err = InitOpenTransport();
79     if (err != noErr)
80         return err;
81     otpt_config = OTCreateConfiguration("tcp");
82     if (otpt_config == kOTInvalidConfigurationPtr ||
83         otpt_config == kOTNoMemoryConfigurationPtr)
84         return 1;
85     return 0;
86 }
87
88 /*
89  * This should only be called once all the connections have been
90  * closed (we don't bother keeping a table of them).
91  */
92
93 void otpt_shutdown(void) {
94
95     CloseOpenTransport();
96 }
97
98 static void *otpt_open(Session *sess, char const *host, int port) {
99     struct otpt_socket *s = NULL;
100     OSStatus err;
101     TCall remote;
102     DNSAddress *remoteaddr;
103     
104     s = smalloc(sizeof(*s));
105     memset(s, 0, sizeof(*s));
106
107     /* Get a TCP endpoint (equiv of socket()) */
108     s->ep = OTOpenEndpoint(OTCloneConfiguration(otpt_config), 0, NULL, &err);
109     if (err != kOTNoError || s->ep == NULL) goto splat;
110
111     /* Attach our notifier function (note that this is _not_ a UPP) */
112     err = OTInstallNotifier(s->ep, otpt_notifier, (void *)s);
113     if (err != kOTNoError) goto splat;
114     s->eventhandler = OTCreateSystemTask(&otpt_rcvevent, (void *)s);
115     if (s->eventhandler == 0) goto splat;
116
117     /* Bind to any local address */
118     err = OTBind(s->ep, NULL, NULL);
119     if (err != kOTNoError) goto splat;
120     memset(&remote, 0, sizeof(remote));
121     remoteaddr = smalloc(sizeof(*remoteaddr) - sizeof(remoteaddr->fName) +
122                          strlen(host) + 7); /* allow space for port no. */
123     remote.addr.buf = (UInt8 *)remoteaddr;
124     /* XXX: I don't _think_ OTInitDNSAddress can modify the hostname. */
125     remote.addr.len = OTInitDNSAddress(remoteaddr, (char *)host);
126     remote.addr.len += sprintf(&remoteaddr->fName[strlen(remoteaddr->fName)],
127                                ":%d", port);
128     /* Asynchronous blocking mode, so we don't have to wait */
129     err = OTSetAsynchronous(s->ep);
130     if (err != kOTNoError) goto splat;
131     err = OTSetBlocking(s->ep);
132     if (err != kOTNoError) goto splat;
133     err = OTConnect(s->ep, &remote, NULL);
134     if (err != kOTNoDataErr)
135         goto splat;
136     return s;
137
138   splat:
139     otpt_destroy(s);
140     return NULL;
141 }
142
143 static int otpt_recv(void *sock, void *buf, int buflen, int flags) {
144     struct otpt_socket *s = (struct otpt_socket *)sock;
145     OTResult result;
146     OTFlags otflags;
147
148     OTSetNonBlocking(s->ep);
149     OTSetSynchronous(s->ep);
150     result = OTRcv(s->ep, buf, buflen, &otflags);
151     if (result >= 0)
152         return result;
153     else if (result == kOTNoDataErr)
154         return 0;
155     else /* confusion! */
156         return 0;
157 }
158
159 static void otpt_poll(void) {
160
161 }
162
163 static int otpt_send(void *sock, void *buf, int buflen, int flags) {
164     struct otpt_socket *s = (struct otpt_socket *)sock;
165
166     /* XXX: using blocking mode is bogus, but it's far easier than not. */
167     OTSetSynchronous(s->ep);
168     OTSetBlocking(s->ep);
169     return OTSnd(s->ep, buf, buflen, flags);
170 }
171
172 /*
173  * Politely ask the other end to close the connection.
174  */
175
176 static void otpt_close(void *sock) {
177     struct otpt_socket *s = (struct otpt_socket *)sock;
178
179     /* XXX: using blocking mode is bogus, but it's far easier than not. */
180     OTSetSynchronous(s->ep);
181     OTSetBlocking(s->ep);
182     OTSndOrderlyDisconnect(s->ep);
183 }
184
185 /*
186  * This should take a socket in any state and undo it all, freeing any
187  * allocated memory and generally making it safe to forget about it.
188  * It should onlu be called at system task time.
189  */
190
191 static void otpt_destroy(void *sock) {
192     struct otpt_socket *s = (struct otpt_socket *)sock;
193     OSStatus err;
194     OTLink *link;
195
196     if (s == NULL)
197         return;
198
199     /* Tear down the connection */
200     /* If we ever start using T_MEMORYRELEASED, we need to be careful here. */
201     err = OTSetSynchronous(s->ep);
202     if (err == kOTNoError)
203         err = OTSetNonBlocking(s->ep);
204     if (err == kOTNoError)
205         err = OTCloseProvider(s->ep);
206     
207     /* Stop the event handler running */
208     if (s->eventhandler != 0)
209         OTDestroySystemTask(s->eventhandler);
210
211     /* Flush the event and send queues */
212     while ((link = OTLIFODequeue(&s->eventq)) != NULL)
213         OTFreeMem(link);
214     while ((link = OTLIFODequeue(&s->sendq)) != NULL)
215         OTFreeMem(link);
216
217     /* Finally, free the socket record itself */
218     sfree(s);
219 }
220
221 /*
222  * Any asynchronous events OpenTransport wants to tell us about end up
223  * here.  This function may be called at deferred task or system task
224  * time, and must be re-entrant.
225  */
226
227 static pascal void otpt_notifier(void *contextPtr, OTEventCode code,
228                                     OTResult result, void *cookie) {
229     struct otpt_socket *s = (struct otpt_socket *)contextPtr;
230     OSStatus status;
231     TDiscon discon;
232
233     switch (code) {
234       case T_CONNECT: /* OTConnect completed */
235         status = OTRcvConnect(s->ep, NULL); /* XXX do we want the new TCall? */
236         if (status == kOTNoDataErr)
237             break;
238         else if (status != kOTNoError) {
239             otpt_sendevent(s, NE_DIED);
240             break;
241         }
242         /* Synchronous non-blocking mode for normal data transfer */
243         OTSetSynchronous(s->ep);
244         OTSetNonBlocking(s->ep);
245         otpt_sendevent(s, NE_OPEN);
246         break;
247       case T_DATA:
248         otpt_sendevent(s, NE_DATA);
249         break;
250       case T_EXDATA:
251         otpt_sendevent(s, NE_URGENT);
252         break;
253       case T_DISCONNECT: /* Disconnection complete or OTConnect rejected */
254         memset(&discon, 0, sizeof(discon));
255         /*
256          * This function returns a positive error code. To obtain the
257          * negative error code, subtract that positive value from
258          * -3199.
259          */
260         status = OTRcvDisconnect(s->ep, &discon);
261         if (cookie == NULL) /* spontaneous disconnect */
262             switch (E2OSStatus(discon.reason)) {
263               case kECONNRESETErr:
264                 otpt_sendevent(s, NE_ABORT);
265                 break;
266               case kETIMEDOUTErr:
267                 otpt_sendevent(s, NE_TIMEOUT);
268                 break;
269               default:
270                 otpt_sendevent(s, NE_DIED);
271                 break;
272             }
273         else /* failed connect */
274             otpt_sendevent(s, NE_NOOPEN);
275       case T_ORDREL:
276         OTRcvOrderlyDisconnect(s->ep);
277         otpt_sendevent(s, NE_CLOSING);
278     }
279 }
280
281 /*
282  * This function is called at interrupt time (or thereabouts) to
283  * dispatch an event that has to be handled at system task time.
284  * Network backends will expect their msg entries to be called then.
285  */
286
287 static void otpt_sendevent(struct otpt_socket *s, Net_Event_Type type) {
288     struct otpt_netevent *ne;
289
290     ne = OTAllocMem(sizeof(*ne));
291     if (ne == NULL)
292         fatalbox("OTAllocMem failed.  Aargh!");
293     ne->type = type;
294     OTLIFOEnqueue(&s->eventq, &ne->next);
295     /* Schedule something */
296     OTScheduleSystemTask(s->eventhandler);
297 }
298
299 /*
300  * Pull one or more network events off a socket's queue and handle
301  * them.  Keep gong until we run out (events may be getting enqueued
302  * while we're running).  This is mildly evil as it'll prevent any
303  * other task running if we're under heavy load.
304  */
305
306 static pascal void otpt_rcvevent(void *arg) {
307     struct otpt_socket *s = (struct otpt_socket *)arg;
308     OTLink *link;
309     struct otpt_netevent *ne;
310
311     while ((link = OTLIFOStealList(&s->eventq)) != NULL) {
312         link = OTReverseList(link);
313         while (link != NULL) {
314             ne = (struct otpt_netevent *)link;
315             link = ne->next.fNext;
316             switch (ne->type) {
317               default:
318                 (s->sess->back->msg)(s->sess, s, ne->type);
319                 break;
320             }
321             OTFreeMem(ne);
322         }
323     }
324 }    
325
326 /*
327  * Local Variables:
328  * c-file-style: "simon"
329  * End:
330  */ 
331
332