1 /* $Id: macnet.c,v 1.1.2.3 1999/04/04 18:23:34 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
28 * macnet.c -- PuTTY-to-MacTCP glue
32 #include <AddressXlation.h>
35 #include <MixedMode.h>
37 #include <Processes.h>
46 * The theory behind this stuff:
48 * net_recv attempts to deliver any incoming data waiting on the
49 * queue. Since MacTCP maintains a buffer for incoming data, there's
50 * no need for us to run asynchronous TCPRcvs, and we just do a
51 * synchronous one if we detect some data waiting. Since TCPRcv can't
52 * be given a timeout of zero, we use TCPStatus to work out if there's
53 * anything waiting first.
55 * Sending data is trickier. TCPSend reserves the right to block
56 * until everything we've sent is ACKed, which means we have to use it
57 * asynchronously. In order to make life easier for backends, and to
58 * save a proliferation of circular buffers, we guarantee to take data
59 * off the hands of the backend as soon as it gives it to us. This is
60 * reasonable because currently there's no way for the backend to say
61 * it can't take data, and once it's got them, it may as well give
64 * Anyway, in order to avoid a fixed-size buffer overflowing, the send
65 * buffer is kept as a queue of blocks. When net_send is called, we
66 * malloc a new block and stick it on the queue. If the queue was
67 * empty, we kick off a new asynchronous TCPSend to handle our block.
71 typedef struct Socket {
72 TCPiopb iopb; /* current MacTCP operation */
73 TCPiopb spareiopb; /* for closing etc */
76 ProcessSerialNumber psn;
79 QHdr sendq; /* Blocks waiting to be sent */
90 * Yes, I know the struct QElem has a short[1] to represent the user
91 * data. I'm ignoring it because it makes my code prettier and
92 * improves the alignment.
101 #define TCPBUF_SIZE 8192
103 static QHdr macnet_eventq;
104 static QHdr macnet_freeq;
106 static short mtcp_refnum;
107 static int mtcp_initted = FALSE;
109 static OSErr macnet_init(void);
110 static pascal void macnet_resolved(hostInfo *, char *);
111 static void macnet_completed_open(TCPiopb*);
112 static void macnet_completed_send(TCPiopb*);
113 static void macnet_sent(Socket *);
114 static void macnet_startsend(Socket *);
115 static void macnet_completed_close(TCPiopb*);
116 static pascal void macnet_asr(StreamPtr, unsigned short, Ptr, unsigned short,
118 static void macnet_sendevent(Socket *, Net_Event_Type);
120 #if TARGET_RT_MAC_CFM
121 static RoutineDescriptor macnet_resolved_upp =
122 BUILD_ROUTINE_DESCRIPTOR(uppResultProcInfo, (ProcPtr)macnet_resolved);
123 static RoutineDescriptor macnet_completed_open_upp =
124 BUILD_ROUTINE_DESCRIPTOR(uppTCPIOCompletionProcInfo,
125 (ProcPtr)macnet_completed_open);
126 static RoutineDescriptor macnet_complete_send_upp =
127 BUILD_ROUTINE_DESCRIPTOR(uppTCPIOCompletionProcInfo,
128 (ProcPtr)macnet_completed_send);
129 static RoutineDescriptor macnet_completed_close_upp =
130 BUILD_ROUTINE_DESCRIPTOR(uppTCPIOCompletionProcInfo,
131 (ProcPtr)macnet_completed_close);
132 static RoutineDescriptor macnet_asr_upp =
133 BUILD_ROUTINE_DESCRIPTOR(uppTCPNotifyProcInfo, (ProcPtr)macnet_asr);
135 #define macnet_resolved_upp macnet_resolved
136 #define macnet_completed_open_upp macnet_completed_open
137 #define macnet_completed_send_upp macnet_completed_send
138 #define macnet_completed_close_upp macnet_completed_close
139 #define macnet_asr_upp macnet_asr
143 * Number of outstanding network events allowed.
145 #define NUM_EVENTS 16
148 * Initialise networking. Set mtcp_initted if it goes OK.
150 static OSErr macnet_init(void) {
152 NetEvent *eventblock;
156 * FIXME: This is hideously broken, in that we're meant to faff
157 * with unit numbers and stuff, and we blatantly don't.
159 err = opendriver(".IPP", &mtcp_refnum);
162 err = OpenResolver(NULL);
165 /* Set up the event queues, and fill the free queue with events
167 macnet_eventq.qFlags = 0;
168 macnet_eventq.qHead = macnet_eventq.qTail = NULL;
169 macnet_freeq.qFlags = 0;
170 macnet_freeq.qHead = macnet_eventq.qTail = NULL;
171 eventblock = smalloc(NUM_EVENTS * sizeof(NetEvent));
172 for (i = 0; i < NUM_EVENTS; i++)
173 Enqueue(&eventblock[i].qelem, &macnet_freeq);
178 Socket *net_open(Session *s, char *host, int port) {
185 * First, get hold of all the memory we'll need (a lot of the
186 * later stuff happens at interrupt time)
188 sock = smalloc(sizeof(struct Socket));
189 memset(sock, 0, sizeof(*sock));
190 tcpbuf = smalloc(TCPBUF_SIZE);
192 /* Make a note of anything we don't want to forget */
194 GetCurrentProcess(&sock->psn);
195 sock->a5 = SetCurrentA5();
197 /* Get MacTCP running if it's not already */
199 if ((err = macnet_init()) != noErr)
200 fatalbox("Couldn't init network (%d)", err);
202 /* Get ourselves a TCP stream to play with */
203 sock->iopb.ioCRefNum = mtcp_refnum;
204 sock->iopb.csCode = TCPCreate;
205 sock->iopb.csParam.create.rcvBuff = tcpbuf;
206 sock->iopb.csParam.create.rcvBuffLen = TCPBUF_SIZE;
207 sock->iopb.csParam.create.notifyProc = macnet_asr_upp;
208 sock->iopb.csParam.create.userDataPtr = (Ptr)sock;
209 /* This could be done asynchronously, but I doubt it'll take long. */
210 err = PBControlSync((ParmBlkPtr)&sock->iopb);
212 fatalbox("TCP stream open failed (%d)", err);
214 err = StrToAddr(host, &sock->hostinfo, &macnet_resolved_upp, (char *)sock);
216 fatalbox("Host lookup failed (%d)", err);
217 if (sock->hostinfo.rtnCode != cacheFault)
218 macnet_resolved(&sock->hostinfo, (char *)sock);
222 static pascal void macnet_resolved(hostInfo *hi, char *cookie) {
223 Socket *sock = (Socket *)cookie;
228 olda5 = SetA5(sock->a5);
231 * We've resolved a name, so now we'd like to connect to it (or
234 switch (sock->hostinfo.rtnCode) {
236 /* Open a connection */
237 sock->iopb.ioCompletion = macnet_completed_open_upp;
238 sock->iopb.csCode = TCPActiveOpen;
239 sock->iopb.csParam.open.validityFlags = typeOfService;
240 sock->iopb.csParam.open.commandTimeoutValue = 0; /* unused */
241 sock->iopb.csParam.open.remoteHost = sock->hostinfo.addr[0]; /*XXX*/
242 sock->iopb.csParam.open.remotePort = sock->port;
243 /* localHost is set by MacTCP. */
244 sock->iopb.csParam.open.localPort = 0;
245 sock->iopb.csParam.open.tosFlags = lowDelay;
246 sock->iopb.csParam.open.dontFrag = 0;
247 sock->iopb.csParam.open.timeToLive = 0; /* default */
248 sock->iopb.csParam.open.security = 0;
249 sock->iopb.csParam.open.optionCnt = 0;
250 sock->iopb.csParam.open.userDataPtr = (char *)sock;
251 err = PBControlSync((ParmBlkPtr)&sock->iopb);
253 macnet_sendevent(sock, NE_NOOPEN);
255 default: /* Something went wrong */
256 macnet_sendevent(sock, NE_NOHOST);
264 static void macnet_completed_open(TCPiopb *iopb) {
265 Socket *sock = (Socket *)iopb->csParam.open.userDataPtr;
269 olda5 = SetA5(sock->a5);
271 switch (iopb->ioResult) {
273 macnet_sendevent(sock, NE_OPEN);
276 macnet_sendevent(sock, NE_NOOPEN);
284 static pascal void macnet_asr(StreamPtr tcpstream, unsigned short eventcode,
285 Ptr cookie, unsigned short terminreason,
286 ICMPReport *icmpmsg) {
287 Socket *sock = (Socket *)cookie;
291 olda5 = SetA5(sock->a5);
295 macnet_sendevent(sock, NE_CLOSING);
298 macnet_sendevent(sock, NE_TIMEOUT);
301 switch (terminreason) {
303 macnet_sendevent(sock, NE_ABORT);
306 macnet_sendevent(sock, NE_DIED);
311 macnet_sendevent(sock, NE_DATA);
314 macnet_sendevent(sock, NE_URGENT);
316 case TCPICMPReceived:
317 switch (icmpmsg->reportType) {
319 macnet_sendevent(sock, NE_REFUSED);
330 * Send a block of data.
333 int net_send(Socket *sock, void *buf, int buflen, int flags) {
337 buff = smalloc(sizeof(Send_Buffer) + buflen);
339 buff->wds.length = buflen;
340 buff->wds.ptr = (Ptr)&buff[1]; /* after the end of the struct */
342 memcpy(&buff[1], buf, buflen);
343 Enqueue(&buff->qelem, &sock->sendq);
344 /* Kick off the transmit if the queue was empty */
345 if (sock->sendq.qHead == &buff->qelem)
346 macnet_startsend(sock);
350 * This is called once every time round the event loop to check for
351 * network events and handle them.
353 void macnet_eventcheck() {
358 ne = (NetEvent *)macnet_eventq.qHead;
361 Dequeue(&ne->qelem, &macnet_eventq);
364 macnet_sent(ne->sock);
367 (ne->sock->s->back->msg)(ne->sock->s, ne->sock, ne->type);
370 Enqueue(&ne->qelem, &macnet_freeq);
374 * The block at the head of the send queue has finished sending, so we
375 * can free it. Kick off the next transmission if there is one.
377 static void macnet_sent(Socket *sock) {
380 assert(sock->sendq.qHead != NULL);
381 buff = (Send_Buffer *)sock->sendq.qHead;
382 Dequeue(&buff->qelem, &sock->sendq);
384 if (sock->sendq.qHead != NULL)
385 macnet_startsend(sock);
389 * There's a block on the head of the send queue which needs to be
393 static void macnet_startsend(Socket *sock) {
397 buff = (Send_Buffer *)sock->sendq.qHead;
398 sock->iopb.csCode = TCPSend;
399 sock->iopb.csParam.send.validityFlags = 0;
400 sock->iopb.csParam.send.pushFlag = buff->flags & SEND_PUSH ? true : false;
401 sock->iopb.csParam.send.urgentFlag = buff->flags & SEND_URG ? true : false;
402 sock->iopb.csParam.send.wdsPtr = (Ptr)&buff->wds;
403 sock->iopb.csParam.send.userDataPtr = (char *)sock;
404 err = PBControlAsync((ParmBlkPtr)&sock->iopb);
407 int net_recv(Socket *sock, void *buf, int buflen, int flags) {
410 int avail, want, got;
412 memcpy(&iopb, &sock->iopb, sizeof(TCPiopb));
413 /* Work out if there's anything to recieve (we don't want to block)
415 iopb.csCode = TCPStatus;
416 err = PBControlSync((ParmBlkPtr)&iopb);
418 return 0; /* macnet_asr should catch it anyway */
419 avail = iopb.csParam.status.amtUnreadData;
422 want = avail < buflen ? avail : buflen;
423 iopb.csCode = TCPRcv;
424 iopb.csParam.receive.rcvBuff = buf;
425 iopb.csParam.receive.rcvBuffLen = want;
426 err = PBControlSync((ParmBlkPtr)&iopb);
429 return iopb.csParam.receive.rcvBuffLen;
433 void net_close(Socket *sock) {
437 * This might get called in the middle of processing another
438 * request on the socket, so we have a spare parameter block for
439 * this purpose (allocating one dynamically would mean having to
440 * free it, which we can't do at interrupt time).
442 memcpy(&sock->spareiopb, &sock->iopb, sizeof(TCPiopb));
443 sock->spareiopb.ioCompletion = macnet_completed_close_upp;
444 sock->spareiopb.csCode = TCPClose;
445 sock->spareiopb.csParam.close.validityFlags = 0;
446 sock->spareiopb.csParam.close.userDataPtr = (char *)sock;
447 err = PBControlAsync((ParmBlkPtr)&sock->spareiopb);
450 case connectionClosing:
451 case connectionTerminated: /* We'll get an ASR */
454 macnet_sendevent(sock, NE_DIED);
459 static void macnet_completed_close(TCPiopb* iopb) {
460 Socket *sock = (Socket *)iopb->csParam.close.userDataPtr;
464 olda5 = SetA5(sock->a5);
466 switch (iopb->ioResult) {
468 macnet_sendevent(sock, NE_CLOSED);
470 case connectionClosing:
471 case connectionTerminated:
474 macnet_sendevent(sock, NE_DIED);
483 * Free all the data structures associated with a socket and tear down
484 * any connection through it.
486 void net_destroy(Socket *sock) {
491 * Yes, we need _another_ iopb, as there may be a send _and_ a
492 * close outstanding. Luckily, destroying a socket is
493 * synchronous, so we can allocate this one dynamically.
495 memcpy(&iopb, &sock->iopb, sizeof(TCPiopb));
496 iopb.csCode = TCPRelease;
497 err = PBControlSync((ParmBlkPtr)&iopb);
498 sfree(iopb.csParam.create.rcvBuff);
502 static void macnet_sendevent(Socket *sock, Net_Event_Type type) {
505 ne = (NetEvent *)macnet_freeq.qHead;
507 Dequeue(&ne->qelem, &macnet_freeq);
510 Enqueue(&ne->qelem, &macnet_eventq);
511 WakeUpProcess(&sock->psn);
516 * c-file-style: "simon"