Leaked source code of windows server 2003
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.

120 lines
3.5 KiB

  1. /*++
  2. Copyright (c) 2000, Microsoft Corporation
  3. Module Name:
  4. connotp.c
  5. Abstract:
  6. This module demonstrates the use of the SO_CONNOPT socket option
  7. to include IP-layer options in outgoing TCP and UDP messages.
  8. It allows a TCP connection to be established to a specified target,
  9. such that all segments for the connection contain a source-route.
  10. Author:
  11. Abolade Gbadegesin (aboladeg) 7-October-1999
  12. Revision History
  13. --*/
  14. #include <winsock2.h>
  15. #include <mswsock.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #define IP_OPT_LSRR 0x83
  19. int __cdecl
  20. main(
  21. int argc,
  22. char* argv[]
  23. )
  24. {
  25. SOCKET Socket;
  26. SOCKADDR_IN SockAddrIn;
  27. WSADATA wd;
  28. //
  29. // Check arguments, initialize Windows Sockets,
  30. // and create a new TCP socket for the outgoing connection.
  31. //
  32. if (argc < 3) {
  33. printf("Usage: connopt <server-IP-address> <server-port> <hop>*\n");
  34. return 0;
  35. }
  36. WSAStartup(0x202, &wd);
  37. Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  38. if (Socket == INVALID_SOCKET) {
  39. printf("socket: %d\n", WSAGetLastError());
  40. } else {
  41. //
  42. // Bind the socket in preparation for constructing the source route.
  43. //
  44. SockAddrIn.sin_family = AF_INET;
  45. SockAddrIn.sin_addr.s_addr = INADDR_ANY;
  46. SockAddrIn.sin_port = 0;
  47. if (bind(
  48. Socket, (PSOCKADDR)&SockAddrIn, sizeof(SockAddrIn)
  49. ) == SOCKET_ERROR) {
  50. printf("bind: %d\n", WSAGetLastError());
  51. } else {
  52. ULONG i;
  53. UCHAR IpOptions[256] = {IP_OPT_LSRR, 0, 4, 0};
  54. ULONG IpOptionsLength = 4;
  55. //
  56. // Construct a source route from the command-line parameters,
  57. // in the format in which it would appear in the IP header.
  58. // Install the resulting buffer using SO_CONNOPT.
  59. //
  60. for (i = 0; i < (ULONG)argc - 3; i++) {
  61. *(ULONG UNALIGNED *)(IpOptions + 3 + i * 4) =
  62. inet_addr(argv[i + 3]);
  63. }
  64. {
  65. *(ULONG UNALIGNED *)(IpOptions + 3 + i * 4) =
  66. inet_addr(argv[1]);
  67. i++;
  68. }
  69. IpOptionsLength += i * 4;
  70. IpOptions[1] = (UCHAR)IpOptionsLength - 1;
  71. IpOptions[IpOptionsLength - 1] = 0;
  72. if (setsockopt(
  73. Socket, SOL_SOCKET, SO_CONNOPT, IpOptions, IpOptionsLength
  74. ) == SOCKET_ERROR) {
  75. printf("setsockopt: %d\n", WSAGetLastError());
  76. } else {
  77. //
  78. // Establish a connection the the target,
  79. // and send a few messages.
  80. //
  81. SockAddrIn.sin_family = AF_INET;
  82. SockAddrIn.sin_addr.s_addr = inet_addr(argv[1]);
  83. SockAddrIn.sin_port = htons((SHORT)atol(argv[2]));
  84. if (connect(
  85. Socket, (PSOCKADDR)&SockAddrIn, sizeof(SockAddrIn)
  86. ) == SOCKET_ERROR) {
  87. printf("connect: %d\n", WSAGetLastError());
  88. } else {
  89. CHAR Message[4096] = "Text string.\n";
  90. for (i = 0; i < 20; i++) {
  91. send(Socket, Message, sizeof(Message), 0);
  92. }
  93. shutdown(Socket, SD_SEND);
  94. closesocket(Socket);
  95. }
  96. }
  97. }
  98. }
  99. return 0;
  100. }