pastebin - collaborative debugging tool
nrubsig.kpaste.net RSS

Difference between
modified post 11c4e by Anonymous on Mon 25th Mar 2024 14:40
original post 6c180 by Anonymous on Mon 25th Mar 2024 14:37
Download diff
Show old version | new version | both versions

    
11
22
/*
33
 *
44
 * $ gcc -g createpersistenttcpportreservation_test1.c -liphlpapi -lws2_32
55
 * -o createpersistenttcpportreservation_test1
66
 */
77
88
#ifndef UNICODE
99
#define UNICODE
1010
#endif
1111
1212
#ifndef WIN32_LEAN_AND_MEAN
1313
#define WIN32_LEAN_AND_MEAN
1414
#endif
1515
1616
#include <Windows.h>
1717
#include <winsock2.h>
1818
#include <mstcpip.h>
1919
#include <ws2ipdef.h>
2020
#include <iphlpapi.h>
2121
#include <stdio.h>
2222
#include <stdlib.h>
2323
#include <wchar.h>
2424
2525
2626
// Need to link with iphlpapi.lib
2727
#pragma comment(lib, "iphlpapi.lib")
2828
2929
// Need to link with Ws2_32.lib for Winsock functions
3030
#pragma comment(lib, "ws2_32.lib")
3131
3232
int main(int argc, char *argv[])
3333
{
3434
3535
    // Declare and initialize variables
3636
3737
    int startPort = 0;         // host byte order
3838
    int numPorts = 0;
3939
    USHORT startPortns = 0;    // Network byte order
4040
    ULONG64 resToken = { 0 };
4141
4242
    unsigned long status = 0;
4343
4444
    WSADATA wsaData = { 0 };
4545
    int iResult = 0;
4646
4747
    SOCKET sock = INVALID_SOCKET;
4848
    int iFamily = AF_INET;
4949
    int iType = SOCK_STREAM;
5050
    int iProtocol = IPPROTO_TCP;
5151
5252
    DWORD bytesReturned = 0;
5353
5454
5555
    // Note that the sockaddr_in struct works only with AF_INET not AF_INET6
5656
    // An application needs to use the sockaddr_in6 for AF_INET6
5757
    struct sockaddr_in service;
5858
    struct sockaddr_in sockName;
5959
    int nameLen = sizeof(sockName);
6060
6161
    // Validate the parameters
6262
    if (argc != 3) {
6363
        printf("usage: %s <Starting Port> <Number of Ports>\n",
6464
             argv[0]);
6565
        printf("Creates a persistent TCP port reservation\n");
6666
        printf("Example usage:\n");
6767
        printf("   %s 5000 20\n", argv[0]);
6868
        printf("   where StartPort=5000 NumPorts=20");
6969
        return 1;
7070
    }
7171
7272
    startPort = atoi(argv[1]);
7373
    if (startPort < 0 || startPort > 65535) {
7474
        printf("Starting point must be either 0 or between 1 and 65,535\n");
7575
        return 1;
7676
    }
7777
    startPortns = htons((USHORT) startPort);
7878
7979
    numPorts = atoi(argv[2]);
8080
    if (numPorts < 0) {
8181
        printf("Number of ports must be a positive number\n");
8282
        return 1;
8383
8484
    }
8585
8686
    status =
8787
        CreatePersistentTcpPortReservation((USHORT) startPortns, (USHORT) numPorts,
8888
                                           &resToken);
8989
    if (status != NO_ERROR) {
9090
        printf("CreatePersistentTcpPortReservation returned error: %ld\n", status);
9191
        return 1;
9292
    }
9393
9494
    printf("CreatePersistentTcpPortReservation call succeeded\n");
95-
    printf("  Token = %I64d\n", resToken);
95+
    printf("  Token = %llx\n", (long long)resToken);
9696
9797
    // Comment out this block if you don't want to create a socket and associate it with the
9898
    // persistent reservation
9999
100100
    // Initialize Winsock
101101
    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
102102
    if (iResult != 0) {
103103
        printf("WSAStartup failed with error = %d\n", iResult);
104104
        // return 1;
105105
    }
106106
107107
    sock = socket(iFamily, iType, iProtocol);
108108
    if (sock == INVALID_SOCKET)
109109
        printf("socket function failed with error = %d\n", WSAGetLastError());
110110
    else {
111111
        printf("socket function succeeded\n");
112112
113113
        iResult =
114114
115115
            WSAIoctl(sock, SIO_ASSOCIATE_PORT_RESERVATION, (LPVOID) & resToken,
116116
                     sizeof (ULONG64), NULL, 0, &bytesReturned, NULL, NULL);
117117
        if (iResult != 0) {
118118
            printf
119119
                ("WSAIoctl(SIO_ASSOCIATE_PORT_RESERVATION) failed with error = %d\n",
120120
                 WSAGetLastError());
121121
        } else {
122122
            printf("WSAIoctl(SIO_ASSOCIATE_PORT_RESERVATION) succeeded, bytesReturned = %u\n",
123123
                bytesReturned);
124124
125125
            service.sin_family = AF_INET;
126126
            service.sin_addr.s_addr = INADDR_ANY;
127127
            service.sin_port = 0;
128128
129129
            iResult = bind(sock, (SOCKADDR*) &service, sizeof(service) );
130130
            if (iResult == SOCKET_ERROR)
131131
                printf("bind failed with error = %d\n", WSAGetLastError());
132132
            else {
133133
                printf("bind succeeded\n");
134134
                iResult = getsockname(sock, (SOCKADDR*) &sockName, &nameLen);
135135
                if (iResult == SOCKET_ERROR)
136136
                    printf("getsockname failed with error = %d\n", WSAGetLastError() );
137137
                else {
138138
                    printf("getsockname succeeded\n");
139139
                    printf("Port number allocated = %u\n", ntohs(sockName.sin_port) );
140140
                }
141141
            }
142142
        }
143143
144144
        if (sock != INVALID_SOCKET) {
145145
            iResult = closesocket(sock);
146146
            if (iResult == SOCKET_ERROR) {
147147
                printf("closesocket failed with error = %d\n", WSAGetLastError());
148148
                WSACleanup();
149149
            }
150150
        }
151151
    }
152152
153153
    // comment out this block of code if you don't want to delete the reservation just created
154154
    status = DeletePersistentTcpPortReservation((USHORT) startPortns, (USHORT) numPorts);
155155
    if (status != NO_ERROR) {
156156
        printf("DeletePersistentTcpPortReservation returned error: %ld\n", status);
157157
        return 1;
158158
    }
159159
    printf("DeletePersistentTcpPortReservation call succeeded\n");
160160
161161
    return 0;
162162
}
163163
164164

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.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at