home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / tcpiptk / r0lib32 / driver.c < prev    next >
C/C++ Source or Header  |  1999-05-11  |  13KB  |  432 lines

  1. /********************************************************copyrite.xic********/
  2. /*                                                                          */
  3. /*   Licensed Materials - Property of IBM                                   */
  4. /*   IBM TCP/IP for OS/2                                                    */
  5. /*   (C) Copyright IBM Corporation. 1990,1996.                              */
  6. /*                                                                          */
  7. /*   All rights reserved.                                                   */
  8. /*                                                                          */
  9. /*   US Government Users Restricted Rights -                                */
  10. /*   Use, duplication or disclosure restricted by GSA ADP Schedule          */
  11. /*   Contract with IBM Corp.                                                */
  12. /*                                                                          */
  13. /*--------------------------------------------------------------------------*/
  14. /*  DISCLAIMER OF WARRANTIES.  The following [enclosed] code is             */
  15. /*  sample code created by IBM Corporation. This sample code is not         */
  16. /*  part of any standard or IBM product and is provided to you solely       */
  17. /*  for  the purpose of assisting you in the development of your            */
  18. /*  applications.  The code is provided "AS IS", without                    */
  19. /*  warranty of any kind.  IBM shall not be liable for any damages          */
  20. /*  arising out of your use of the sample code, even if they have been      */
  21. /*  advised of the possibility of such damages.                             */
  22. /*--------------------------------------------------------------------------*/
  23. /************************** DRIVER.C ****************************************/
  24. #define INCL_DOS
  25. #define INCL_DOSINFOSEG
  26. #undef  KERNEL
  27.  
  28. #define INADDR_ANY              (u_long)0x00000000
  29. #define BUFF_SIZE       1024
  30.  
  31.  
  32. #include <sys\itypes.h>
  33. #include <driver.h>
  34. #include <genio.h>
  35. #include <sys\socket.h>
  36. #include <sys\uio.h>
  37. #include <sys\ioctl.h>
  38. #include <sys\un.h>
  39. #include <netinet\in.h>
  40. #include <unistd.h>
  41.  
  42. void devopen(lpRequest);
  43. void GenIOCtrl(lpRequest);
  44. void devclose(lpRequest);
  45. void init(lpRequest);
  46.  
  47. extern void int3break(void);
  48. void test_server();
  49.  
  50.  
  51.  
  52. void strategy32_c(lpreq)
  53. lpRequest lpreq;
  54. {
  55.    switch(lpreq->reqcommand) {
  56.       case INIT:
  57.            lpreq->reqstatus = GEN_ERR + ERR_UNKNOWNCOMMAND + DONE_BIT;
  58.            break;
  59.       case OPEN:
  60.            devopen(lpreq);
  61.            break;
  62.       case CLOSE:
  63.            devclose(lpreq);
  64.            break;
  65.       case GENIO:
  66.            GenIOCtrl(lpreq);
  67.            break;
  68.       default:
  69.            lpreq->reqstatus = GEN_ERR + ERR_UNKNOWNCOMMAND + DONE_BIT;
  70.            break;
  71.    } /* end switch */
  72. }
  73.  
  74.  
  75. typedef unsigned long physaddr;  /* A physical address is 4 bytes */
  76. typedef void  * farptr;
  77.  
  78. typedef struct _2words_def {
  79.   unsigned low;
  80.   unsigned high;
  81.   } _2words_type;
  82.  
  83. typedef struct _segaddr_def {
  84.   unsigned offset;
  85.   unsigned segment;
  86.   } _segadr_type;
  87.  
  88. typedef union _32bits_def {
  89.    physaddr phys;
  90.    void * fptr;
  91.    _2words_type _2words;
  92.    _segadr_type _segadr;
  93.    } _32bits;
  94.  
  95.  
  96. void devopen(lpreq)
  97. lpRequest lpreq;
  98. {
  99.    lpreq->reqstatus |= DONE_BIT;
  100. }
  101.  
  102.  
  103. void devclose(lpreq)
  104. lpRequest lpreq;
  105. {
  106.    lpreq->reqstatus |= DONE_BIT;
  107. }
  108.  
  109.  
  110. void GenIOCtrl(lpreq)
  111. lpRequest lpreq;
  112. {
  113.    switch(lpreq->fcode) {
  114.           case CALL_INIT:
  115.                init(lpreq);
  116.                lpreq->reqstatus |= DONE_BIT;
  117.             break;
  118.           default:
  119.             lpreq->reqstatus = GEN_ERR+ERR_UNKNOWNCOMMAND+DONE_BIT;
  120.             return;
  121.    }
  122. }
  123.  
  124.  
  125. void getmem(u_long base, u_long* offset, char* ptr, u_long size)
  126. {
  127.   *ptr = (char*)(base + *offset);
  128.   *offset += size;
  129. }
  130.  
  131.  
  132. void init(lpRequest lpreq)
  133. {
  134.        test_server();/* calling the test program */
  135.  
  136.  /*
  137.   int s1,s2, s3, namelen;
  138.   long result=-1;
  139.   char buf[50];
  140.   char buf2[50];
  141.   struct sockaddr_un name1, name2;
  142.  
  143.   int3break();
  144.  
  145.   namelen = 110;
  146.  
  147.  
  148.   result = sock_init();
  149.   if ((result>>16)==0xffffffff) goto done;
  150.  
  151.  
  152.   result = socket(1,1,0);
  153.   if ((result>>16)==0xffffffff) goto done;
  154.   s1 = (int)result;
  155.  
  156.   result = socket(1,1,0);
  157.   if ((result>>16)==0xffffffff) goto done;
  158.   s2 = (int)result;
  159.  
  160.  
  161.   name1.sun_family=1;
  162.   memcpy(sstods(name1.sun_path), "\\socket\\name1\0", 14);
  163.   result = bind(s1, (struct sockaddr *)sstods(&name1), namelen);
  164.   if ((result>>16)==0xffffffff) goto done;
  165.  
  166.   name2.sun_family=1;
  167.   memcpy(sstods(name2.sun_path), "\\socket\\name2\0", 14);
  168.   result = bind(s2, (struct sockaddr *)sstods(&name2), namelen);
  169.   if ((result>>16)==0xffffffff) goto done;
  170.  
  171.   result = listen(s2, 5);
  172.   if ((result>>16)==0xffffffff) goto done;
  173.  
  174.   result = connect(s1, (struct sockaddr *)sstods(&name2), namelen);
  175.   if ((result>>16)==0xffffffff) goto done;
  176.  
  177.   result = accept(s2, sstods(&name1), sstods(&namelen));
  178.   if ((result>>16)==0xffffffff) goto done;
  179.   s3 = (int)result;
  180.  
  181.   memcpy(sstods(buf2),"Hello World\0", 12);
  182.   result = sendto(s3, sstods(buf2), 12, 0, (struct sockaddr *)sstods(&name1), namelen);
  183.   if ((result>>16)==0xffffffff) goto done;
  184.  
  185.   recvfrom(s1, sstods(buf), 12, 0, (struct sockaddr *)sstods(&name2), sstods(&namelen));
  186.   if ((result>>16)==0xffffffff) goto done;
  187.  
  188. done:
  189.  
  190.   shutdown(s1, 2);
  191.   shutdown(s2, 1);
  192.  
  193.   soclose(s1);
  194.   soclose(s2);
  195.  
  196.         */
  197. }
  198.  
  199. u_long seltoflat(u_long seladdr)
  200. {
  201.    u_short sel;
  202.    u_short off;
  203.    u_short stackseg;
  204.    u_long linear = 0L;
  205.    struct GlobalDescReg * gdr;
  206.    struct DescInfo * dinfo;
  207.    extern u_short retss(void);
  208.    extern u_long virt2lin(u_long);
  209.    extern char _gdtrtab[8];
  210.  
  211.    if(seladdr) {
  212.       sel = seladdr>>16;
  213.       off = seladdr&0x0ffff;
  214.    }
  215.  
  216.    stackseg = retss(); /* in thunk32.asm, vacpp icks... */
  217.  
  218.    /* check if it is a ldt */
  219.    if(sel & 0x4) {
  220.       /* ldt */
  221.       linear = (LDT2FLAT(seladdr));
  222.    }
  223.    else if (sel == stackseg) {
  224.       /* ptkssbase */
  225.       if(ptkssbase)
  226.          linear = (u_long)(sstods(off));
  227.       else
  228.          linear = (virt2lin(seladdr));
  229.    }
  230.    else {
  231.       /* gdt */
  232.       sel &= 0xfff8;     /* for safety reasons get the ring 0 gdt sel */
  233.       gdr = (struct GlobalDescReg *) &(_gdtrtab);
  234.       dinfo = (struct DescInfo *)((u_long)gdr->base+(u_long)sel);
  235.       if(dinfo){
  236.          /* optimize it later */
  237.          linear = dinfo->base32;
  238.          linear = linear << 8;
  239.          linear |= dinfo->base24;
  240.          linear = linear << 16;
  241.          linear |= dinfo->base16;
  242.  
  243.          linear+=(u_long)off;
  244.       }
  245.    }
  246.  
  247.    return linear;
  248. }
  249.  
  250. void test_server()
  251. {
  252.  
  253.         char buf[BUFF_SIZE];/* buffer used for recv() and send() calls */
  254.  
  255.         struct sockaddr_in client,server;
  256.  
  257.  
  258.         int sockd,sockd_c,optval = 1,result;
  259.         int client_len,count;
  260.  
  261.         /********************************************************
  262.          Initialize with sockets.This call is mandatory before any socket call
  263.         **********************************************************/
  264.  
  265.         sock_init();
  266.  
  267.         result =  socket(AF_INET,SOCK_STREAM,0);
  268.         if ((result>>16)==0xffffffff)
  269.                 goto done;
  270.  
  271.         else
  272.                 sockd = (int)result;
  273.  
  274.         /********************************************************
  275.          Bind the socket to the server address.
  276.         *********************************************************/
  277.         server.sin_family = AF_INET;
  278.         server.sin_port   = htons(5000);
  279.         server.sin_addr.s_addr = INADDR_ANY;
  280.  
  281.         /*******************************************************
  282.  
  283. If we have a local variable declared as a pointer ,when ever we are assigning
  284. this pointer to any other varible or passing the pointer as a paramater
  285. to a function then we have  to use sstods(is a macro).
  286.  
  287.         ********************************************************/
  288.  
  289.  
  290.         result = setsockopt(sockd,SOL_SOCKET,SO_REUSEADDR,(char *)sstods(&optval),sizeof(int) );
  291.         if ((result>>16)==0xffffffff)
  292.                 goto done;
  293.  
  294.  
  295.         result = bind(sockd, (struct sockaddr *)sstods(&server),sizeof(server));
  296.  
  297.         if ((result>>16)==0xffffffff)
  298.                 goto done;
  299.  
  300.         result = listen(sockd, 5);
  301.  
  302.         if ((result>>16)==0xffffffff)
  303.                 goto done;
  304.  
  305.  
  306.         /*************************************************
  307.                 Accept a connection.
  308.         ***************************************************/
  309.         client_len = sizeof((client));
  310.         result = accept(sockd,(struct sockaddr *)sstods(&client),sstods(&client_len));
  311.  
  312.         if ((result>>16)==0xffffffff)
  313.                 goto done;
  314.         else
  315.                 sockd_c = (int)result;
  316.  
  317.         /****************************************************
  318.          Receive the data on the newly connected socket.
  319.         *******************************************************/
  320.         result = recv(sockd_c,sstods(buf),BUFF_SIZE, 0);
  321.         if ((result>>16)==0xffffffff)
  322.                 goto done;
  323.  
  324.  
  325.         /****************************************************
  326.                 alter the recived data
  327.         ******************************************************/
  328.  
  329.         for(count = 0;count < BUFF_SIZE;count++)
  330.                 buf[count] = (buf[count] + 1);
  331.  
  332.         /****************************************************
  333.                 sending the processd data
  334.         ******************************************************/
  335.         result = send(sockd_c, sstods(buf),BUFF_SIZE, 0);
  336.         if ((result>>16)==0xffffffff)
  337.                 goto done;
  338.  
  339.         /*********************************************
  340.                 To test recvmsg() function
  341.         ***********************************************/
  342.         {
  343.                 char buff[50];
  344.                 struct msghdr   msg;
  345.                 struct iovec iov;
  346.  
  347.                 iov.iov_base = sstods(&buff[0]);
  348.                 iov.iov_len  = sizeof(buff)-1;
  349.  
  350.                 msg.msg_iov        = sstods(&iov);
  351.                 msg.msg_iovlen     = 1;
  352.  
  353.                 result = recvmsg(sockd_c, (struct msghdr *) sstods(&msg),0);
  354.                 if ((result>>16)==0xffffffff)
  355.                         goto done;
  356.  
  357.         }
  358.  
  359.         /********************************************************
  360.                 To test getsockname(),getpeername() and getsocktopt()
  361.         **********************************************************/
  362.  
  363.         {
  364.                 struct sockaddr_in name;
  365.                 long hostid;
  366.                 int namelen,optval2 = 0,optlen;
  367.  
  368.                 hostid = gethostid();
  369.  
  370.                 result = getsockname(sockd_c,(struct sockaddr *)sstods(&name),(int *)sstods(&namelen));
  371.  
  372.                 if ((result>>16)==0xffffffff)
  373.                         goto done;
  374.  
  375.                 result = getpeername(sockd_c,(struct sockaddr *)sstods(&name),(int *)sstods(&namelen));
  376.  
  377.                 if ((result>>16)==0xffffffff)
  378.                         goto done;
  379.  
  380.                 optlen = sizeof(int);
  381.  
  382.                 result = getsockopt(sockd,SOL_SOCKET,SO_REUSEADDR,(char *)sstods(&optval2),(int *)sstods(&optlen));
  383.  
  384.                 if ((result>>16)==0xffffffff)
  385.                         goto done;
  386.         }
  387.  
  388.  
  389.         /*********************************************************
  390.                 To test if this IOCTL worked or not U can use
  391.                 inetcfg -g synattack and it should not show it as SET
  392.         **********************************************************/
  393.         {
  394.                 int on_off =1; /* set flag ON */
  395.  
  396.                 /* Set the SYN attack flag to ON/OFF */
  397.                 result = ioctl(sockd_c,SIOCSSYN,(char *)sstods(&on_off));
  398.  
  399.                 if ((result>>16)==0xffffffff)
  400.                         goto done;
  401.         }
  402.         /**********************************
  403.                 To test  Sendmsg()
  404.         ***********************************/
  405.         {
  406.                 struct msghdr   msg;
  407.                 struct iovec iov;
  408.                 char buffer[50] = "GANESHA";
  409.  
  410.                 iov.iov_base = sstods(buffer);
  411.                 iov.iov_len  = sizeof(buffer);
  412.  
  413.                 msg.msg_name       = (caddr_t) sstods(&server);
  414.                 msg.msg_namelen    = sizeof(server);
  415.  
  416.                 msg.msg_iov        = (struct iovec *)sstods(&iov);
  417.                 msg.msg_iovlen     = 1;
  418.  
  419.                 result = sendmsg(sockd_c,(struct msghdr *)sstods(&msg),0);
  420.                 if ((result>>16)==0xffffffff)
  421.                         goto done;
  422.         }
  423.  
  424. done:
  425.  
  426.         shutdown(sockd, 2);
  427.         shutdown(sockd_c, 1);
  428.  
  429.         soclose(sockd_c);
  430.         soclose(sockd);
  431. }
  432.