- /*
- *
- * $ gcc -g createpersistenttcpportreservation_test1.c -liphlpapi -lws2_32
- * -o createpersistenttcpportreservation_test1
- */
- #ifndef UNICODE
- #define UNICODE
- #endif
- #ifndef WIN32_LEAN_AND_MEAN
- #define WIN32_LEAN_AND_MEAN
- #endif
- #include <Windows.h>
- #include <winsock2.h>
- #include <mstcpip.h>
- #include <ws2ipdef.h>
- #include <iphlpapi.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <wchar.h>
- // Need to link with iphlpapi.lib
- #pragma comment(lib, "iphlpapi.lib")
- // Need to link with Ws2_32.lib for Winsock functions
- #pragma comment(lib, "ws2_32.lib")
- int main(int argc, char *argv[])
- {
- // Declare and initialize variables
- int startPort = 0; // host byte order
- int numPorts = 0;
- USHORT startPortns = 0; // Network byte order
- ULONG64 resToken = { 0 };
- unsigned long status = 0;
- WSADATA wsaData = { 0 };
- int iResult = 0;
- SOCKET sock = INVALID_SOCKET;
- int iFamily = AF_INET;
- int iType = SOCK_STREAM;
- int iProtocol = IPPROTO_TCP;
- DWORD bytesReturned = 0;
- // Note that the sockaddr_in struct works only with AF_INET not AF_INET6
- // An application needs to use the sockaddr_in6 for AF_INET6
- struct sockaddr_in service;
- struct sockaddr_in sockName;
- int nameLen = sizeof(sockName);
- // Validate the parameters
- if (argc != 3) {
- argv[0]);
- return 1;
- }
- if (startPort < 0 || startPort > 65535) {
- return 1;
- }
- startPortns = htons((USHORT) startPort);
- if (numPorts < 0) {
- return 1;
- }
- status =
- CreatePersistentTcpPortReservation((USHORT) startPortns, (USHORT) numPorts,
- &resToken);
- if (status != NO_ERROR) {
- return 1;
- }
- // Comment out this block if you don't want to create a socket and associate it with the
- // persistent reservation
- // Initialize Winsock
- iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
- if (iResult != 0) {
- // return 1;
- }
- sock = socket(iFamily, iType, iProtocol);
- if (sock == INVALID_SOCKET)
- else {
- iResult =
- WSAIoctl(sock, SIO_ASSOCIATE_PORT_RESERVATION, (LPVOID) & resToken,
- sizeof (ULONG64), NULL, 0, &bytesReturned, NULL, NULL);
- if (iResult != 0) {
- ("WSAIoctl(SIO_ASSOCIATE_PORT_RESERVATION) failed with error = %d\n",
- WSAGetLastError());
- } else {
- bytesReturned);
- service.sin_family = AF_INET;
- service.sin_addr.s_addr = INADDR_ANY;
- service.sin_port = 0;
- iResult = bind(sock, (SOCKADDR*) &service, sizeof(service) );
- if (iResult == SOCKET_ERROR)
- else {
- iResult = getsockname(sock, (SOCKADDR*) &sockName, &nameLen);
- if (iResult == SOCKET_ERROR)
- else {
- }
- }
- }
- if (sock != INVALID_SOCKET) {
- iResult = closesocket(sock);
- if (iResult == SOCKET_ERROR) {
- WSACleanup();
- }
- }
- }
- // comment out this block of code if you don't want to delete the reservation just created
- status = DeletePersistentTcpPortReservation((USHORT) startPortns, (USHORT) numPorts);
- if (status != NO_ERROR) {
- return 1;
- }
- return 0;
- }
createpersistenttcpportreservation_test1
Posted by Anonymous on Mon 25th Mar 2024 14:40
raw | new post
modification of post by Anonymous (view diff)
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.