mirror of the now-defunct rocklinux.org
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.

89 lines
2.7 KiB

  1. // --- ROCK-COPYRIGHT-NOTE-BEGIN ---
  2. //
  3. // This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  4. // Please add additional copyright information _after_ the line containing
  5. // the ROCK-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
  6. // the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
  7. //
  8. // ROCK Linux: rock-src/package/base/iproute2/ipnm2nwbc.c
  9. // ROCK Linux is Copyright (C) 1998 - 2004 Clifford Wolf
  10. //
  11. // This program is free software; you can redistribute it and/or modify
  12. // it under the terms of the GNU General Public License as published by
  13. // the Free Software Foundation; either version 2 of the License, or
  14. // (at your option) any later version. A copy of the GNU General Public
  15. // License can be found at Documentation/COPYING.
  16. //
  17. // Many people helped and are helping developing ROCK Linux. Please
  18. // have a look at http://www.rocklinux.org/ and the Documentation/TEAM
  19. // file for details.
  20. //
  21. // --- ROCK-COPYRIGHT-NOTE-END ---
  22. #include <stdio.h>
  23. int getbits(int n) {
  24. int rc=0;
  25. while (n) {
  26. if (n&1) rc++;
  27. n = n >> 1;
  28. }
  29. return rc;
  30. }
  31. int setbits(int n) {
  32. int rc=0;
  33. while (n--) {
  34. rc = (rc>>1)|(1<<7);
  35. }
  36. return rc;
  37. }
  38. int main(int argc, char ** argv) {
  39. int ip[4],nm[4],nw[4],bc[4],c;
  40. int verbose=0,nmbits=0,n;
  41. if ( argc > 1 && !strcmp(argv[1],"-v") ) verbose=1;
  42. if ( argc == 3+verbose &&
  43. sscanf(argv[1+verbose],"%d.%d.%d.%d",ip,ip+1,ip+2,ip+3) == 4 &&
  44. sscanf(argv[2+verbose],"%d.%d.%d.%d",nm,nm+1,nm+2,nm+3) == 4 ) {
  45. nmbits=getbits(nm[0])+getbits(nm[1])+
  46. getbits(nm[2])+getbits(nm[3]);
  47. } else if ( argc == 2+verbose && sscanf(argv[1+verbose],
  48. "%d.%d.%d.%d/%d",ip,ip+1,ip+2,ip+3,&nmbits) == 5 ) {
  49. n=nmbits;
  50. if (n>0) { nm[0]=setbits(n>8?8:n); n-=8; } else nm[0]=0;
  51. if (n>0) { nm[1]=setbits(n>8?8:n); n-=8; } else nm[1]=0;
  52. if (n>0) { nm[2]=setbits(n>8?8:n); n-=8; } else nm[2]=0;
  53. if (n>0) { nm[3]=setbits(n>8?8:n); n-=8; } else nm[3]=0;
  54. } else {
  55. fprintf(stderr,"\n"
  56. "IP and Netmask to Network and Broadcast converter.\n"
  57. "(C) under GPL, 1999 Clifford Wolf\n\n"
  58. "Usage: %s [-v] <IP> <Netmask>\n"
  59. " %s [-v] <IP>/<Mask>\n\n"
  60. "Examples: %s -v 195.170.70.72/25\n"
  61. " %s 195.170.70.72 255.255.255.128\n\n",
  62. argv[0],argv[0],argv[0],argv[0]);
  63. return 1;
  64. }
  65. for (c=0; c<4; c++) {
  66. nw[c]=ip[c]&nm[c];
  67. bc[c]=nw[c]|(255&~nm[c]);
  68. }
  69. if ( verbose ) {
  70. printf("Network: %d.%d.%d.%d / %d\n",
  71. nw[0],nw[1],nw[2],nw[3],nmbits);
  72. printf("Netmask: %d.%d.%d.%d\n",
  73. nm[0],nm[1],nm[2],nm[3]);
  74. printf("Broadcast: %d.%d.%d.%d\n",
  75. bc[0],bc[1],bc[2],bc[3]);
  76. } else {
  77. printf("%d.%d.%d.%d %d.%d.%d.%d\n",nw[0],nw[1],
  78. nw[2],nw[3],bc[0],bc[1],bc[2],bc[3]);
  79. }
  80. return 0;
  81. }