summaryrefslogtreecommitdiffstats
path: root/lib/libUPnP/Neptune/Source/Apps/NetPing/NetPing.cpp
blob: 5fc719f10572a207dc400e97dd3ad0b9f211f8a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*****************************************************************
|
|      Neptune Utilities - Network Ping Client
|
|      (c) 2001-2005 Gilles Boccon-Gibod
|      Author: Gilles Boccon-Gibod (bok@bok.net)
|
 ****************************************************************/

/*----------------------------------------------------------------------
|       includes
+---------------------------------------------------------------------*/
#include "NptConfig.h"
#include "Neptune.h"
#include "NptDebug.h"

#if defined(NPT_CONFIG_HAVE_STDLIB_H)
#include <stdlib.h>
#endif

#if defined(NPT_CONFIG_HAVE_STRING_H)
#include <string.h>
#endif

#if defined(NPT_CONFIG_HAVE_STDIO_H)
#include <stdio.h>
#endif

/*----------------------------------------------------------------------
|       types
+---------------------------------------------------------------------*/
typedef enum {
    CLIENT_TYPE_UNKNOWN,
    CLIENT_TYPE_UDP,
    CLIENT_TYPE_TCP
} ClientType;

/*----------------------------------------------------------------------
|       globals
+---------------------------------------------------------------------*/
static struct {
    bool verbose;
} Options;

/*----------------------------------------------------------------------
|       PrintUsageAndExit
+---------------------------------------------------------------------*/
static void
PrintUsageAndExit(void)
{
    fprintf(stderr, 
            "usage: NetPing udp|tcp <hostname> <port>\n");
    exit(1);
}

/*----------------------------------------------------------------------
|       UdpPing
+---------------------------------------------------------------------*/
static void
UdpPing(const char* hostname, int port)
{
    NPT_UdpSocket sender;
    NPT_Result    result;

    // build ping packet
    NPT_DataBuffer packet;
    const char* packet_data = "PING";
    packet.SetData((NPT_Byte*)packet_data, 4);

    // resolve hostname
    NPT_IpAddress ip_address;
    result = ip_address.ResolveName(hostname);
    if (NPT_FAILED(result)) {
        fprintf(stderr, "ERROR: failed to resolve name\n");
        return;
    }

    // get the current timestamp
    NPT_TimeStamp before;
    NPT_System::GetCurrentTimeStamp(before);

    // send the packet
    sender.Connect(NPT_SocketAddress(ip_address, port));
    sender.Send(packet);
    //NPT_SocketAddress destination_address(ip_address, port);
    //sender.Send(packet, &destination_address);
    NPT_SocketInfo socket_info;
    sender.GetInfo(socket_info);
    printf("send from %s:%d to %s:%d\n",
        socket_info.local_address.GetIpAddress().ToString().GetChars(),
        socket_info.local_address.GetPort(),
        socket_info.remote_address.GetIpAddress().ToString().GetChars(),
        socket_info.remote_address.GetPort());
    //sender.Connect(NPT_SocketAddress(ip_address, port));
    NPT_SocketAddress destination_address(ip_address, port);
    sender.Send(packet, &destination_address);
    //NPT_SocketInfo socket_info;
    //sender.GetInfo(socket_info);
    //NPT_Debug("sent from %s:%d to %s:%d\n", 
    //    socket_info.local_address.GetIpAddress().ToString().GetChars(),
    //    socket_info.local_address.GetPort(),
    //    socket_info.remote_address.GetIpAddress().ToString().GetChars(),
    //    socket_info.remote_address.GetPort());
    printf("send %d bytes\n", 4);

    // receive response
    NPT_DataBuffer response(32768);
    result = sender.Receive(response);
    if (NPT_FAILED(result)) {
        fprintf(stderr, "ERROR: failed to receive response (%d)\n", result);
        return;
    }
    sender.GetInfo(socket_info);
    printf("RESPONSE: %d bytes from %s:%d\n", 
           response.GetDataSize(),
           socket_info.remote_address.GetIpAddress().ToString().GetChars(),
           socket_info.remote_address.GetPort());

    NPT_TimeStamp after;
    NPT_System::GetCurrentTimeStamp(after);
    NPT_TimeInterval i = after-before;
    printf("RTT: %f ms\n", ((float)i)*1000.0f);
}

/*----------------------------------------------------------------------
|       TcpPing
+---------------------------------------------------------------------*/
static void
TcpPing(const char* hostname, int port)
{
    NPT_TcpClientSocket sender;
    NPT_Result          result;

    // resolve name
    NPT_IpAddress ip_address;
    result = ip_address.ResolveName(hostname);
    if (NPT_FAILED(result)) {
        fprintf(stderr, "ERROR: failed to resolve name\n");
        return;
    }
    
    // connect to server
    result = sender.Connect(NPT_SocketAddress(ip_address, port));
    if (NPT_FAILED(result)) {
        fprintf(stderr, "ERROR: Connect() failed (%d)\n", result);
        return;
    }
    NPT_OutputStreamReference output;

    // get the current timestamp
    NPT_TimeStamp before;
    NPT_System::GetCurrentTimeStamp(before);

    // send the ping
    sender.GetOutputStream(output);
    output->Write("PING", 4);

    // receive response
    NPT_InputStreamReference input;
    sender.GetInputStream(input);
    char buffer[1024];

    NPT_Size bytes_read;
    result = input->Read(buffer, sizeof(buffer), &bytes_read);
    if (NPT_SUCCEEDED(result)) {
       printf("read %d bytes\n", bytes_read);
    } else {
        fprintf(stderr, "ERROR: Read() failed (%d)\n", result);
    }

    NPT_TimeStamp after;
    NPT_System::GetCurrentTimeStamp(after);
    NPT_TimeInterval i = after-before;
    printf("RTT: %f ms\n", ((float)i)*1000.0f);
}

/*----------------------------------------------------------------------
|       main
+---------------------------------------------------------------------*/
int
main(int argc, char** argv)
{
    // check command line
    if (argc != 4) {
        PrintUsageAndExit();
    }

    // init options
    Options.verbose = false;
    ClientType client_type = CLIENT_TYPE_UNKNOWN;
    int port = -1;
    char* hostname = NULL;

    // parse command line
    if (!strcmp(argv[1], "udp")) {
        client_type = CLIENT_TYPE_UDP;
    } else if (!strcmp(argv[1], "tcp")) {
        client_type = CLIENT_TYPE_TCP;
    } else {
        fprintf(stderr, "ERROR: unknown client type\n");
        exit(1);
    }

    hostname = argv[2];
    port = strtoul(argv[3], NULL, 10);

    switch (client_type) {
        case CLIENT_TYPE_TCP: TcpPing(hostname, port); break;
        case CLIENT_TYPE_UDP: UdpPing(hostname, port); break;
        default: break;
    }

    return 0;
}