Source code of Windows XP (NT5)
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.3 KiB

  1. /*++
  2. Copyright (c) 2000, Microsoft Corporation
  3. Module Name:
  4. limitbcast.c
  5. Abstract:
  6. This module implements a program demonstrating the use of the
  7. SIO_LIMIT_BROADCASTS socket option. The default behavior of the system
  8. is to send limited broadcasts (i.e. broadcasts sent to 255.255.255.255)
  9. on all local interfaces, which violates the IETF Host Requirements stating
  10. that limited broadcasts appear only on the sending socket's interface.
  11. This socket option allows an application to specify that a socket's
  12. limited-broadcasts obey the traditional semantics.
  13. Author:
  14. Abolade Gbadegesin (aboladeg) 29-October-1998
  15. Revision History:
  16. --*/
  17. #include <winsock2.h>
  18. #include <mstcpip.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. int __cdecl
  22. main(
  23. int argc,
  24. char* argv[]
  25. )
  26. {
  27. SOCKADDR_IN DestAddrIn;
  28. SOCKADDR_IN LocalAddrIn;
  29. ULONG Option;
  30. SOCKET Socket;
  31. WSADATA WsaData;
  32. //
  33. // Check command-line arguments
  34. //
  35. ZeroMemory(&LocalAddrIn, sizeof(LocalAddrIn));
  36. LocalAddrIn.sin_family = AF_INET;
  37. if (argc != 3 ||
  38. !(LocalAddrIn.sin_addr.s_addr = inet_addr(argv[1])) ||
  39. !(LocalAddrIn.sin_port = ntohs((USHORT)atol(argv[2])))) {
  40. printf("Usage: %s <local IP address> <target port>\n", argv[0]);
  41. return 0;
  42. }
  43. //
  44. // Initialize Windows sockets, create a UDP socket,
  45. // and enable broadcasts on the socket.
  46. // Then bind it to the specified local IP address.
  47. //
  48. WSAStartup(0x0202, &WsaData);
  49. if ((Socket = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET) {
  50. printf("socket: %d\n", WSAGetLastError());
  51. return 0;
  52. }
  53. Option = 1;
  54. if (setsockopt(
  55. Socket, SOL_SOCKET, SO_BROADCAST, (PCHAR)&Option, sizeof(Option)
  56. ) == SOCKET_ERROR) {
  57. printf("setsockopt: %d\n", WSAGetLastError());
  58. }
  59. if (bind(Socket, (PSOCKADDR)&LocalAddrIn, sizeof(LocalAddrIn))
  60. == SOCKET_ERROR) {
  61. printf("bind: %d\n", WSAGetLastError());
  62. return 0;
  63. }
  64. //
  65. // Initialize the destination address,
  66. // and enter a loop where we prompt for the limit-broadcasts setting
  67. // and send a message using the specified setting.
  68. //
  69. ZeroMemory(&DestAddrIn, sizeof(DestAddrIn));
  70. DestAddrIn.sin_family = AF_INET;
  71. DestAddrIn.sin_addr.s_addr = INADDR_BROADCAST;
  72. DestAddrIn.sin_port = LocalAddrIn.sin_port;
  73. do {
  74. ULONG Length;
  75. ULONG LimitBroadcasts;
  76. printf("Enter a value (0 or 1) for the SIO_LIMIT_BROADCASTS option.\n");
  77. printf("bcast> ");
  78. if (!scanf("%d", &LimitBroadcasts)) {
  79. break;
  80. }
  81. if (WSAIoctl(
  82. Socket, SIO_LIMIT_BROADCASTS,
  83. (PCHAR)&LimitBroadcasts, sizeof(LimitBroadcasts),
  84. NULL, 0, &Length, NULL, NULL
  85. ) == SOCKET_ERROR) {
  86. printf("WSAIoctl: %d\n", WSAGetLastError());
  87. }
  88. if (sendto(
  89. Socket,
  90. (PCHAR)&DestAddrIn,
  91. sizeof(DestAddrIn),
  92. 0,
  93. (PSOCKADDR)&DestAddrIn,
  94. sizeof(DestAddrIn)
  95. ) == SOCKET_ERROR) {
  96. printf("sendto: %d\n", WSAGetLastError());
  97. }
  98. } while(TRUE);
  99. closesocket(Socket);
  100. WSACleanup();
  101. return 0;
  102. }