1 /* $Id: opentpt.c,v 1.1.2.2 1999/08/02 22:32:39 ben Exp $ */
3 * Copyright (c) 1999 Ben Harris
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
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
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
29 #include <CodeFragments.h>
30 #include <OpenTransport.h>
31 #include <OpenTptInternet.h>
38 /* See the top of macnet.c for some idea of how this is meant to work. */
48 struct otpt_netevent {
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 *);
65 Network_Stack otpt_stack = {
66 otpt_init, otpt_open, otpt_recv, otpt_send, otpt_poll, otpt_close,
67 otpt_destroy, otpt_shutdown
70 static OTConfiguration *otpt_config = kOTInvalidConfigurationPtr;
72 static int otpt_init(void) {
75 /* Check that the OpenTransport libraries were there (really just ppc) */
76 if (&InitOpenTransport == kUnresolvedCFragSymbolAddress)
78 err = InitOpenTransport();
81 otpt_config = OTCreateConfiguration("tcp");
82 if (otpt_config == kOTInvalidConfigurationPtr ||
83 otpt_config == kOTNoMemoryConfigurationPtr)
89 * This should only be called once all the connections have been
90 * closed (we don't bother keeping a table of them).
93 void otpt_shutdown(void) {
98 static void *otpt_open(Session *sess, char const *host, int port) {
99 struct otpt_socket *s = NULL;
102 DNSAddress *remoteaddr;
104 s = smalloc(sizeof(*s));
105 memset(s, 0, sizeof(*s));
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;
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;
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)],
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)
143 static int otpt_recv(void *sock, void *buf, int buflen, int flags) {
144 struct otpt_socket *s = (struct otpt_socket *)sock;
148 OTSetNonBlocking(s->ep);
149 OTSetSynchronous(s->ep);
150 result = OTRcv(s->ep, buf, buflen, &otflags);
153 else if (result == kOTNoDataErr)
155 else /* confusion! */
159 static void otpt_poll(void) {
163 static int otpt_send(void *sock, void *buf, int buflen, int flags) {
164 struct otpt_socket *s = (struct otpt_socket *)sock;
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);
173 * Politely ask the other end to close the connection.
176 static void otpt_close(void *sock) {
177 struct otpt_socket *s = (struct otpt_socket *)sock;
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);
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.
191 static void otpt_destroy(void *sock) {
192 struct otpt_socket *s = (struct otpt_socket *)sock;
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);
207 /* Stop the event handler running */
208 if (s->eventhandler != 0)
209 OTDestroySystemTask(s->eventhandler);
211 /* Flush the event and send queues */
212 while ((link = OTLIFODequeue(&s->eventq)) != NULL)
214 while ((link = OTLIFODequeue(&s->sendq)) != NULL)
217 /* Finally, free the socket record itself */
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.
227 static pascal void otpt_notifier(void *contextPtr, OTEventCode code,
228 OTResult result, void *cookie) {
229 struct otpt_socket *s = (struct otpt_socket *)contextPtr;
234 case T_CONNECT: /* OTConnect completed */
235 status = OTRcvConnect(s->ep, NULL); /* XXX do we want the new TCall? */
236 if (status == kOTNoDataErr)
238 else if (status != kOTNoError) {
239 otpt_sendevent(s, NE_DIED);
242 /* Synchronous non-blocking mode for normal data transfer */
243 OTSetSynchronous(s->ep);
244 OTSetNonBlocking(s->ep);
245 otpt_sendevent(s, NE_OPEN);
248 otpt_sendevent(s, NE_DATA);
251 otpt_sendevent(s, NE_URGENT);
253 case T_DISCONNECT: /* Disconnection complete or OTConnect rejected */
254 memset(&discon, 0, sizeof(discon));
256 * This function returns a positive error code. To obtain the
257 * negative error code, subtract that positive value from
260 status = OTRcvDisconnect(s->ep, &discon);
261 if (cookie == NULL) /* spontaneous disconnect */
262 switch (E2OSStatus(discon.reason)) {
264 otpt_sendevent(s, NE_ABORT);
267 otpt_sendevent(s, NE_TIMEOUT);
270 otpt_sendevent(s, NE_DIED);
273 else /* failed connect */
274 otpt_sendevent(s, NE_NOOPEN);
276 OTRcvOrderlyDisconnect(s->ep);
277 otpt_sendevent(s, NE_CLOSING);
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.
287 static void otpt_sendevent(struct otpt_socket *s, Net_Event_Type type) {
288 struct otpt_netevent *ne;
290 ne = OTAllocMem(sizeof(*ne));
292 fatalbox("OTAllocMem failed. Aargh!");
294 OTLIFOEnqueue(&s->eventq, &ne->next);
295 /* Schedule something */
296 OTScheduleSystemTask(s->eventhandler);
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.
306 static pascal void otpt_rcvevent(void *arg) {
307 struct otpt_socket *s = (struct otpt_socket *)arg;
309 struct otpt_netevent *ne;
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;
318 (s->sess->back->msg)(s->sess, s, ne->type);
328 * c-file-style: "simon"