home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / tcpiptk / os2ioctl / os2ioctl.c next >
C/C++ Source or Header  |  1999-05-11  |  6KB  |  182 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. /************************** OS2IOCTL.C *****************************************/
  24. /*   Note that no special care is taken to restore the stack parameters.    */
  25. /****************************************************************************/
  26. #define INC_DOS
  27. #define INCL_DOSPROCESS
  28. #define INCL_DOSSEMAPHORES
  29. #include <os2.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <sys\types.h>
  37. #include <sys\stat.h>
  38. #include <types.h>
  39. #include <sys/socket.h>
  40. #include <net/route.h>
  41. #include <sys/ioctl.h>
  42. #include <net/if.h>
  43. #include <arpa/inet.h>
  44. #include <netinet/if_ether.h>
  45. #include <unistd.h>
  46.  
  47. /* Globals */
  48. int failures, success;
  49. char interfaces[256],allinterfaces[512];
  50.  
  51. /* Prototypes */
  52. void make_interface_list(char *intflist, char *allintflist);
  53. void ioctl_synattack(void);
  54. void ioctl_cntrt(void);
  55. int soclose(int);
  56.  
  57. void main()
  58. {
  59.         fflush(stdout);
  60.         fflush(stdin);
  61.  
  62.         printf("Issuing SIOSTAT_CNTRT & SIOSTAT_RT\n");
  63.         ioctl_cntrt();
  64.  
  65.         printf("\nIssuing SIOCSSYN for setting the SYN Attack ON/OFF \n");
  66.         ioctl_synattack();
  67.         printf("Can check the flag being set thru \"inetcfg -g syn\"\n\n");
  68.  
  69.         printf ("Total Successes:%d\n",success);
  70.         printf ("Total Failures :%d\n",failures);
  71.  
  72.         exit(0);
  73. }
  74.  
  75.  
  76. // SYN ATTACK IOCTL
  77. void ioctl_synattack()
  78. {
  79.         int s;
  80.         int on_off =1;    /* set flag ON */
  81.  
  82.         if((s = socket(AF_INET, SOCK_RAW, 0)) < 0) {
  83.            printf(" *** Err in getting a socket\n");
  84.            exit(1);
  85.         } /* endif */
  86.  
  87.         /* Set the SYN attack flag to ON/OFF */
  88.         if (ioctl(s,SIOCSSYN,(char *)&on_off) < 0) {
  89.               printf(" *** IOCTL failure for SIOCSSYN\n");
  90.              failures++;
  91.         }
  92.         else{
  93.               printf ("SYN Attack flag set to %d\n\n", on_off);
  94.               success++;
  95.         }
  96.         soclose (s);
  97. }
  98.  
  99.  
  100. //SIO STAT CNTRT & SIO STAT RT
  101. void ioctl_cntrt()
  102. {
  103.    int s;
  104.    struct rtentries *p;
  105.    int nohosts, nonets, i;
  106.    struct sockaddr_in *sin, *singate;
  107.    struct ortentry *rt;
  108.    u_int cnt = 0;
  109.    u_long size;
  110.    char *ptr =NULL;
  111.    short netmasksupport = 0;
  112.    char *name;
  113.  
  114.    if((s = socket(AF_INET, SOCK_RAW, 0)) < 0) {
  115.       printf(" *** Err in getting a socket\n");
  116.       exit(1);
  117.    } /* endif */
  118.  
  119.    printf("Issuing ioctl SIOSTATCNTRT \n");
  120.    if(ioctl(s, SIOSTATCNTRT, (char *)&cnt) < 0) {
  121.       printf("ioctl(SIOSTATCNTRT) failure\n");
  122.       failures++;
  123.       size=65536;
  124.       return ;
  125.    } else {
  126.       if(!cnt)
  127.           return;
  128.       success++;
  129.       printf ("\nSize of RTables returned by SIOSTATCNTRT : %d\n",cnt);
  130.       size = cnt*(IFNAMSIZ+sizeof(struct ortentry))+4;
  131.    }
  132.  
  133.    if(size > 65535)
  134.       size = (65535 / (IFNAMSIZ + sizeof(struct ortentry))) *
  135.                     sizeof(struct ortentry) + 4;
  136.    printf ("Size of RTables recomputed to be  : %d\n",size);
  137.  
  138.    p =  malloc(size);
  139.  
  140.    if (p==NULL){
  141.       printf("Malloc failed ...\n");
  142.       return;
  143.    }
  144.  
  145.  
  146.    printf("\nIssuing ioctl SIOSTATRT \n");
  147.    if (os2_ioctl(s, SIOSTATRT, (caddr_t)p,(long)size) < 0) {
  148.       free(p);
  149.       printf("SIOSTATRT Err:\n");
  150.       failures++;
  151.       return;
  152.    } else
  153.       success++;
  154.  
  155.    nohosts = p->hostcount;
  156.    nonets  = p->netcount;
  157.    rt      = p->rttable;
  158.  
  159.    printf("\n");
  160.    printf("ROUTE found FOR \tGATEWAY \n" );
  161.    if(nohosts+nonets>0) {
  162.      for(i=0;i<nohosts+nonets;i++) {
  163.         sin = (struct sockaddr_in *)(&rt->rt_dst);
  164.         if(sin->sin_addr.s_addr == 0L && rt->rt_hash == 0)
  165.         printf("%15s ","default");
  166.         else printf("%15s ",inet_ntoa(sin->sin_addr));
  167.         sin = (struct sockaddr_in *)(&rt->rt_gateway);
  168.         printf("%15s ",inet_ntoa(sin->sin_addr));
  169.         rt++;
  170.         ptr = name = (char *) rt;
  171.         name = name + strlen(name) + 1;
  172.         rt = (struct ortentry *) name;
  173.         printf("\n");
  174.      }
  175.    }
  176.  
  177.    free(p);
  178.    soclose(s);
  179. }
  180.  
  181.  
  182.