tcpcli01.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "unp.h"
  2. int
  3. main(int argc, char **argv)
  4. {
  5. int c, sockfd, len = 0;
  6. u_char *ptr = NULL;
  7. struct addrinfo *ai;
  8. if (argc < 2)
  9. err_quit("usage: tcpcli01 [ -[gG] <hostname> ... ] <hostname>");
  10. opterr = 0; /* don't want getopt() writing to stderr */
  11. while ( (c = getopt(argc, argv, "gG")) != -1) {
  12. switch (c) {
  13. case 'g': /* loose source route */
  14. if (ptr)
  15. err_quit("can't use both -g and -G");
  16. ptr = inet_srcrt_init(0);
  17. break;
  18. case 'G': /* strict source route */
  19. if (ptr)
  20. err_quit("can't use both -g and -G");
  21. ptr = inet_srcrt_init(1);
  22. break;
  23. case '?':
  24. err_quit("unrecognized option: %c", c);
  25. }
  26. }
  27. if (ptr)
  28. while (optind < argc-1)
  29. len = inet_srcrt_add(argv[optind++]);
  30. else
  31. if (optind < argc-1)
  32. err_quit("need -g or -G to specify route");
  33. if (optind != argc-1)
  34. err_quit("missing <hostname>");
  35. ai = Host_serv(argv[optind], SERV_PORT_STR, AF_INET, SOCK_STREAM);
  36. sockfd = Socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
  37. if (ptr) {
  38. len = inet_srcrt_add(argv[optind]); /* dest at end */
  39. Setsockopt(sockfd, IPPROTO_IP, IP_OPTIONS, ptr, len);
  40. free(ptr);
  41. }
  42. Connect(sockfd, ai->ai_addr, ai->ai_addrlen);
  43. str_cli(stdin, sockfd); /* do it all */
  44. exit(0);
  45. }