hogports.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. void
  12. hogport(char *proto, int port)
  13. {
  14. char buf[256];
  15. char dir[40];
  16. snprint(buf, sizeof(buf), "%s!%d", proto, port);
  17. if(announce(buf, dir) < 0)
  18. fprint(2, "%s: can't hog %s\n", argv0, buf);
  19. }
  20. void
  21. hogrange(char *str)
  22. {
  23. char *er, *sr;
  24. int start, end;
  25. sr = strrchr(str, '!');
  26. if(sr == nil)
  27. sysfatal("bad range: %s", str);
  28. *sr++ = 0;
  29. er = strchr(sr, '-');
  30. if(er == nil)
  31. er = sr;
  32. else
  33. er++;
  34. start = atoi(sr);
  35. end = atoi(er);
  36. if(end < start)
  37. sysfatal("bad range: %s", sr);
  38. for(; start <= end; start++)
  39. hogport(str, start);
  40. }
  41. void
  42. main(int argc, char **argv)
  43. {
  44. int i;
  45. ARGBEGIN{
  46. }ARGEND;
  47. if(argc == 0){
  48. fprint(2, "usage: %s portrange\n", argv0);
  49. exits("usage");
  50. }
  51. switch(rfork(RFREND|RFNOTEG|RFFDG|RFPROC|RFNAMEG)){
  52. case 0:
  53. close(0);
  54. close(1);
  55. break;
  56. case -1:
  57. abort(); /* "fork failed\n" */;
  58. default:
  59. _exits(0);
  60. }
  61. for(i = 0; i < argc; i++)
  62. hogrange(argv[i]);
  63. close(2);
  64. for(;;)
  65. sleep(10000);
  66. }