home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Entertainment / MacMud / Sockets / testtcpglue.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-05  |  5.5 KB  |  257 lines  |  [TEXT/MPS ]

  1. #include <stdio.h>
  2. #include <events.h>
  3.  
  4. #include "tcpglue.h"
  5.  
  6. static TCPiopb TCPpb;
  7.  
  8. static xCleanup(status) int status;
  9. {
  10.     fprintf(stderr,"exiting status %d\n",status);
  11.     (void) xTCPRelease(&TCPpb);
  12. }
  13.  
  14. /*
  15.  * TCP asynchronous notification routine
  16.  */
  17. static int notified = 0;
  18. static int lastNotifyCount = 0;
  19.  
  20. static StreamPtr notifyTcpStream;
  21. static unsigned short notifyEventCode;
  22. static Ptr notifyUserDataPtr;
  23. static unsigned short notifyTerminReason;
  24. static struct ICMPReport *notifyIcmpMsg;
  25.  
  26. pascal void TCPNotify(tcpStream,eventCode,userDataPtr,terminReason,icmpMsg)
  27.     StreamPtr tcpStream;
  28.     unsigned short eventCode;
  29.     Ptr userDataPtr;
  30.     unsigned short terminReason;
  31.     struct ICMPReport *icmpMsg;
  32. {
  33.     notified++;
  34.  
  35.     notifyTcpStream = tcpStream;
  36.     notifyEventCode = eventCode;
  37.     notifyUserDataPtr = userDataPtr;
  38.     notifyTerminReason = terminReason;
  39.     notifyIcmpMsg = icmpMsg;
  40. }
  41.  
  42. static char *eventNames[] = 
  43. {
  44.     "event 0",
  45.     "closing",
  46.     "ULP timeout",
  47.     "terminate",
  48.     "data arrival",
  49.     "urgent data",
  50.     "ICMP message"
  51. };
  52. static char *terminateReasons[] =
  53. {
  54.     "reason 0",
  55.     "reason 1",
  56.     "remote abort",
  57.     "network failure",
  58.     "security/precedence mismatch",
  59.     "ULP timeout",
  60.     "ULP abort",
  61.     "ULP close",
  62.     "service failure"
  63. };
  64. static char *icmpMessages[] =
  65. {
  66.     "net unreachable",
  67.     "host unreachable",
  68.     "protocol unreachable",
  69.     "port unreachable",
  70.     "fragmentation required",
  71.     "source route failed",
  72.     "time exceeded",
  73.     "parameter problem",
  74.     "missing required option"
  75. };
  76. checkNotify()
  77. {
  78.     if (notified == lastNotifyCount)
  79.         return;
  80.     
  81.     lastNotifyCount = notified;
  82.     fprintf(stderr,"notify count is now %d\n",lastNotifyCount);
  83.     fprintf(stderr,"stream %08x\n",notifyTcpStream);
  84.     fprintf(stderr,"event %d '%s'\n",notifyEventCode,eventNames[notifyEventCode]);
  85.     if (notifyEventCode == TCPTerminate)
  86.         fprintf(stderr,"reason %d '%s'\n",notifyTerminReason,terminateReasons[notifyTerminReason]);
  87.     if (notifyEventCode == TCPDataArrival)
  88.         fprintf(stderr,"%d bytes\n",notifyTerminReason/*!?*/);
  89.     fprintf(stderr,"icmp msg %08x\n",notifyIcmpMsg);
  90.     if (notifyEventCode == TCPICMPReceived)
  91.     {
  92.         fprintf(stderr,"stream %08x\n",notifyIcmpMsg->streamPtr);
  93.         fprintf(stderr,"local %08x/%d\n",notifyIcmpMsg->localHost,notifyIcmpMsg->localPort);
  94.         fprintf(stderr,"remote %08x/%d\n",notifyIcmpMsg->remoteHost,notifyIcmpMsg->remotePort);
  95.         fprintf(stderr,"%s\n",icmpMessages[notifyIcmpMsg->reportType]);
  96.         fprintf(stderr,"optionalAddlInfo %04x\n",notifyIcmpMsg->optionalAddlInfo);
  97.         fprintf(stderr,"optionalAddlInfoPtr %08x\n",notifyIcmpMsg->optionalAddlInfoPtr);
  98.     }
  99.     fprintf(stderr,"userdata %s\n",notifyUserDataPtr);
  100. }
  101.  
  102. /*
  103.  * TCP io completion routine
  104.  */
  105. static OSErr TCPResult;
  106. static long rhost;
  107. static short rport;
  108. static int len;
  109.  
  110. static void TCPDone(pb) 
  111.     struct TCPiopb *pb;
  112. {
  113.     TCPResult = pb->ioResult;
  114.     if (TCPResult != noErr)
  115.         return;
  116.         
  117.     switch (pb->csCode)
  118.     {
  119.         case TCPPassiveOpen:
  120.             rhost = pb->csParam.open.remoteHost;
  121.             rport = pb->csParam.open.remotePort;
  122.             break;
  123.         case TCPRcv:
  124.             len = pb->csParam.receive.rcvBuffLen;
  125.             break;
  126.     }
  127. }
  128.  
  129. static OSErr io;
  130. static char rcvbuf[1024];
  131. static char sendbuf[1024];
  132.  
  133. main()
  134. {
  135.     atexit(xCleanup);
  136. /*
  137.     fprintf(stderr,"address %08x\n",xIPAddr());
  138.     fprintf(stderr,"netmask %08x\n",xNetMask());
  139.     fprintf(stderr,"max mtu %d\n",xMaxMTU());
  140. */
  141.     if ((io = xTCPCreate(8192,TCPNotify,&TCPpb)) != noErr)
  142.     {
  143.         fprintf(stderr,"TCPCreate failed code %d\n",io);
  144.         exit(1);
  145.     }
  146.     fprintf(stderr,"tcpStream %08x\n",TCPpb.tcpStream);
  147.  
  148.     doPassiveOpen();
  149.     
  150.     fprintf(stderr,"notify spin with count %d\n",lastNotifyCount);
  151.     for(;;)
  152.     {
  153.         checkNotify();
  154.     }
  155.  
  156.     if ((io = xTCPRelease(&TCPpb)) != noErr)
  157.     {
  158.         fprintf(stderr,"xTCPRelease failed code %d\n",io);
  159.         exit(1);
  160.     }
  161. }
  162.  
  163. doActiveOpen()
  164. {
  165.     TCPResult = 1;
  166.     if ((io = xTCPActiveOpen(&TCPpb,0,0x80646682,99,TCPDone)) != noErr)
  167.     {
  168.         fprintf(stderr,"xTCPActiveOpen failed code %d\n",io);
  169.         exit(1);
  170.     }
  171.     fprintf(stderr,"about to spin on xTCPActiveOpen\n");
  172.     while (TCPResult == 1)
  173.     {
  174.         checkNotify();
  175.     }
  176.     fprintf(stderr,"xTCPActiveOpen spin done\n");
  177.     if (TCPResult != noErr)
  178.     {
  179.         fprintf(stderr,"xTCPActiveOpen failed code %d\n",TCPResult);
  180. /*
  181.         exit(1);
  182. */
  183.     }
  184. }
  185.  
  186. doRvc()
  187. {
  188.     TCPResult = 1;
  189.     if ((io = xTCPRcv(&TCPpb,rcvbuf,sizeof(rcvbuf)-1,0/*infinity*/,TCPDone)) != noErr)
  190.     {
  191.         fprintf(stderr,"xTCPRcv failed code %d\n",io);
  192.         exit(1);
  193.     }
  194.     fprintf(stderr,"about to spin on xTCPRcv\n");
  195.     while (TCPResult == 1)
  196.     {
  197.         /* handle user i/f events */
  198.     }
  199.     fprintf(stderr,"xTCPRcv spin done\n");
  200.     if (TCPResult != noErr)
  201.     {
  202.         fprintf(stderr,"xTCPRcv failed code %d\n",TCPResult);
  203.         exit(1);
  204.     }
  205.     rcvbuf[len] = '\0';
  206.     fprintf(stderr,"xTCPRcv'd %d bytes\n'%s'\n",len,rcvbuf);
  207. }
  208.  
  209. doSend()
  210. {
  211.     TCPResult = 1;
  212.     strcpy(sendbuf,"HELO milligan.utcs.utoronto.ca");
  213.     wds[0].length = strlen(sendbuf);
  214.     wds[0].ptr = sendbuf;
  215.     wds[1].length = 0;
  216.     if ((io = xTCPSend(&TCPpb,wds,false,false,0)) != noErr)
  217.     {
  218.         fprintf(stderr,"xTCPSend failed code %d\n",io);
  219.         exit(1);
  220.     }
  221. #ifdef ASYNC
  222.     fprintf(stderr,"about to spin on xTCPSend\n");
  223.     while (TCPResult == 1)
  224.     {
  225.         /* handle user i/f events */
  226.     }
  227.     fprintf(stderr,"xTCPSend spin done\n");
  228.     if (TCPResult != noErr)
  229.     {
  230.         fprintf(stderr,"xTCPSend failed code %d\n",TCPResult);
  231.         exit(1);
  232.     }
  233. #endif ASYNC
  234. }
  235.  
  236. doPassiveOpen()
  237. {
  238.     TCPResult = 1;
  239.     if ((io = xTCPPassiveOpen(&TCPpb,25,0/*infinity*/,TCPDone)) != noErr)
  240.     {
  241.         fprintf(stderr,"xTCPPassiveOpen failed code %d\n",io);
  242.         exit(1);
  243.     }
  244.     fprintf(stderr,"about to spin on xTCPPassiveOpen\n");
  245.     while (TCPResult == 1)
  246.     {
  247.         /* handle user i/f events */
  248.     }
  249.     fprintf(stderr,"xTCPPassiveOpen spin done\n");
  250.     if (TCPResult != noErr)
  251.     {
  252.         fprintf(stderr,"xTCPPassiveOpen failed code %d\n",TCPResult);
  253.         exit(1);
  254.     }
  255.     fprintf(stderr,"remote %08x/%d\n",rhost,rport);
  256. }
  257.