Advertisement
phlud

tcpprobe.c

Aug 17th, 2020
3,135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.21 KB | None | 0 0
  1. /* -*-C-*- tcpprobe.c */
  2. /* tcpprobe - report on which tcp ports accept connections */
  3. /* IO ERROR, error@axs.net, Sep 15, 1995 */
  4. #include
  5. #include
  6. #include
  7. #include
  8. #include
  9. #include
  10. <stdio.h>
  11. <sys/socket.h>
  12. <netinet/in.h>
  13. <errno.h>
  14. <netdb.h>
  15. <signal.h>
  16. int main(int argc, char **argv)
  17. {
  18. int probeport = 0;
  19. struct hostent *host;
  20. int err, i, net;
  21. struct sockaddr_in sa;
  22. if (argc != 2) {
  23. printf("Usage: %s hostname\n", argv[0]);
  24. exit(1);
  25. }
  26. for (i = 1; i < 1024; i++) {
  27. strncpy((char *)&sa, "", sizeof sa);
  28. sa.sin_family = AF_INET;
  29. if (isdigit(*argv[1]))
  30. sa.sin_addr.s_addr = inet_addr(argv[1]);
  31. else if ((host = gethostbyname(argv[1])) != 0)
  32. strncpy((char *)&sa.sin_addr, (char *)host->h_addr, sizeof sa.sin_addr);
  33. else {
  34. herror(argv[1]);
  35. exit(2);
  36. }
  37. sa.sin_port = htons(i);
  38. net = socket(AF_INET, SOCK_STREAM, 0);
  39. if (net < 0) {
  40. perror("\nsocket");
  41. exit(2);
  42. }
  43. err = connect(net, (struct sockaddr *) &sa, sizeof sa);
  44. if (err < 0) {
  45. printf("%s %-5d %s\r", argv[1], i, strerror(errno));
  46. fflush(stdout);
  47. } else {
  48. printf("%s %-5d accepted.
  49. \n", argv[1], i);
  50. if (shutdown(net, 2) < 0) {
  51. perror("\nshutdown");
  52. exit(2);
  53. }
  54. }
  55. close(net);
  56. }
  57. printf("
  58. \r");
  59. fflush(stdout);
  60. return (0);
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement