OpenSDE Packages Database (without history before r20070)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.4 KiB

  1. // --- T2-COPYRIGHT-NOTE-BEGIN ---
  2. // This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  3. //
  4. // T2 SDE: package/.../iproute2/ipnm2nwbc.c
  5. // Copyright (C) 2004 - 2006 The T2 SDE Project
  6. // Copyright (C) 1998 - 2003 Clifford Wolf
  7. //
  8. // More information can be found in the files COPYING and README.
  9. //
  10. // This program is free software; you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation; version 2 of the License. A copy of the
  13. // GNU General Public License can be found in the file COPYING.
  14. // --- T2-COPYRIGHT-NOTE-END ---
  15. #include <stdio.h>
  16. int getbits(int n) {
  17. int rc=0;
  18. while (n) {
  19. if (n&1) rc++;
  20. n = n >> 1;
  21. }
  22. return rc;
  23. }
  24. int setbits(int n) {
  25. int rc=0;
  26. while (n--) {
  27. rc = (rc>>1)|(1<<7);
  28. }
  29. return rc;
  30. }
  31. int main(int argc, char ** argv) {
  32. int ip[4],nm[4],nw[4],bc[4],c;
  33. int verbose=0,nmbits=0,n;
  34. if ( argc > 1 && !strcmp(argv[1],"-v") ) verbose=1;
  35. if ( argc == 3+verbose &&
  36. sscanf(argv[1+verbose],"%d.%d.%d.%d",ip,ip+1,ip+2,ip+3) == 4 &&
  37. sscanf(argv[2+verbose],"%d.%d.%d.%d",nm,nm+1,nm+2,nm+3) == 4 ) {
  38. nmbits=getbits(nm[0])+getbits(nm[1])+
  39. getbits(nm[2])+getbits(nm[3]);
  40. } else if ( argc == 2+verbose && sscanf(argv[1+verbose],
  41. "%d.%d.%d.%d/%d",ip,ip+1,ip+2,ip+3,&nmbits) == 5 ) {
  42. n=nmbits;
  43. if (n>0) { nm[0]=setbits(n>8?8:n); n-=8; } else nm[0]=0;
  44. if (n>0) { nm[1]=setbits(n>8?8:n); n-=8; } else nm[1]=0;
  45. if (n>0) { nm[2]=setbits(n>8?8:n); n-=8; } else nm[2]=0;
  46. if (n>0) { nm[3]=setbits(n>8?8:n); n-=8; } else nm[3]=0;
  47. } else {
  48. fprintf(stderr,"\n"
  49. "IP and Netmask to Network and Broadcast converter.\n"
  50. "(C) under GPL, 1999 Clifford Wolf\n\n"
  51. "Usage: %s [-v] <IP> <Netmask>\n"
  52. " %s [-v] <IP>/<Mask>\n\n"
  53. "Examples: %s -v 195.170.70.72/25\n"
  54. " %s 195.170.70.72 255.255.255.128\n\n",
  55. argv[0],argv[0],argv[0],argv[0]);
  56. return 1;
  57. }
  58. for (c=0; c<4; c++) {
  59. nw[c]=ip[c]&nm[c];
  60. bc[c]=nw[c]|(255&~nm[c]);
  61. }
  62. if ( verbose ) {
  63. printf("Network: %d.%d.%d.%d / %d\n",
  64. nw[0],nw[1],nw[2],nw[3],nmbits);
  65. printf("Netmask: %d.%d.%d.%d\n",
  66. nm[0],nm[1],nm[2],nm[3]);
  67. printf("Broadcast: %d.%d.%d.%d\n",
  68. bc[0],bc[1],bc[2],bc[3]);
  69. } else {
  70. printf("%d.%d.%d.%d %d.%d.%d.%d\n",nw[0],nw[1],
  71. nw[2],nw[3],bc[0],bc[1],bc[2],bc[3]);
  72. }
  73. return 0;
  74. }