2013-01-24 16:36:10 +01:00
|
|
|
/*
|
|
|
|
* \brief Ping-client
|
|
|
|
* \author Josef Soentgen
|
|
|
|
* \date 2013-01-24
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 Genode Labs GmbH
|
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
|
|
|
* under the terms of the GNU General Public License version 2.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* libc includes */
|
2013-04-08 20:51:28 +02:00
|
|
|
#include <stdio.h>
|
2013-01-24 16:36:10 +01:00
|
|
|
|
|
|
|
#include "../pingpong.h"
|
|
|
|
|
|
|
|
unsigned int verbose;
|
|
|
|
|
|
|
|
int
|
|
|
|
dial(const char *addr)
|
|
|
|
{
|
|
|
|
int s;
|
|
|
|
struct sockaddr_in in_addr;
|
|
|
|
|
2013-04-08 20:51:28 +02:00
|
|
|
printf("Create new socket...\n");
|
2013-01-24 16:36:10 +01:00
|
|
|
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
|
|
|
if (s == -1) {
|
2013-04-08 20:51:28 +02:00
|
|
|
printf("ERROR: Could not create socket!\n");
|
2013-01-24 16:36:10 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-04-08 20:51:28 +02:00
|
|
|
printf("Connect to server %s:%d...\n", addr, Sport);
|
2013-01-24 16:36:10 +01:00
|
|
|
in_addr.sin_port = htons(Sport);
|
|
|
|
in_addr.sin_family = AF_INET;
|
|
|
|
in_addr.sin_addr.s_addr = inet_addr(addr);
|
|
|
|
if (connect(s, (struct sockaddr *)&in_addr, sizeof (in_addr)) == -1) {
|
2013-04-08 20:51:28 +02:00
|
|
|
printf("ERROR: Could not connect to server!\n");
|
2013-01-24 16:36:10 +01:00
|
|
|
close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-04-08 20:51:28 +02:00
|
|
|
printf("Sucessfully connected to server.\n");
|
2013-01-24 16:36:10 +01:00
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2013-09-04 11:27:22 +02:00
|
|
|
sendping(const char *addr, size_t dsize, size_t count)
|
2013-01-24 16:36:10 +01:00
|
|
|
{
|
|
|
|
Packet p;
|
|
|
|
int s;
|
|
|
|
size_t i;
|
2013-09-04 11:27:22 +02:00
|
|
|
size_t n = 0;
|
2013-01-24 16:36:10 +01:00
|
|
|
|
|
|
|
s = dial(addr);
|
|
|
|
if (s == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
p.h.type = Tping;
|
|
|
|
p.h.dsize = dsize;
|
|
|
|
p.d = (char *)malloc(p.h.dsize);
|
|
|
|
if (p.d == NULL) {
|
2013-04-08 20:51:28 +02:00
|
|
|
printf("ERROR: Out of memory!\n");
|
2013-01-24 16:36:10 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-09-04 11:27:22 +02:00
|
|
|
printf("Trying to send %zd packets...\n", count);
|
2013-04-08 20:51:28 +02:00
|
|
|
for (i = 0; i < count; i++) {
|
2013-01-24 16:36:10 +01:00
|
|
|
forgepacket(&p, i + 1);
|
|
|
|
|
|
|
|
n = sendpacket(s, &p);
|
2013-09-04 11:27:22 +02:00
|
|
|
if (n == 0)
|
2013-01-24 16:36:10 +01:00
|
|
|
break;
|
|
|
|
if (n != (sizeof (Packetheader) + p.h.dsize)) {
|
2013-04-08 20:51:28 +02:00
|
|
|
printf("ERROR: size mismatch: %ld != %lu\n", n, sizeof (Packetheader) + p.h.dsize);
|
2013-01-24 16:36:10 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (verbose)
|
2013-04-08 20:51:28 +02:00
|
|
|
printf("%u %ld\n", p.h.id, n);
|
2013-01-24 16:36:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
close(s);
|
|
|
|
free(p.d);
|
|
|
|
|
|
|
|
switch (n) {
|
|
|
|
case 0:
|
2013-09-04 11:27:22 +02:00
|
|
|
printf("Disconnect, sent packets: %zu\n", i);
|
2013-01-24 16:36:10 +01:00
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
default:
|
2013-09-04 11:27:22 +02:00
|
|
|
printf("Sucessfull, sent packets: %zu\n", i);
|
2013-01-24 16:36:10 +01:00
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* never reached */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2013-04-08 20:51:28 +02:00
|
|
|
char serverip[16] = "0.0.0.0";
|
2013-01-24 16:36:10 +01:00
|
|
|
unsigned int i;
|
|
|
|
unsigned int startsize, endsize;
|
2013-04-08 20:51:28 +02:00
|
|
|
unsigned int count;
|
2013-01-24 16:36:10 +01:00
|
|
|
|
|
|
|
/* default settings */
|
|
|
|
startsize = 1;
|
|
|
|
endsize = 32768;
|
2013-04-08 20:51:28 +02:00
|
|
|
count = 1024;
|
2013-01-24 16:36:10 +01:00
|
|
|
verbose = 0;
|
|
|
|
|
2013-04-08 20:51:28 +02:00
|
|
|
#if 0
|
2013-01-24 16:36:10 +01:00
|
|
|
Genode::Xml_node argv_node = Genode::config()->xml_node().sub_node("argv");
|
|
|
|
try {
|
|
|
|
argv_node.attribute("serverip" ).value(serverip, sizeof(serverip));
|
|
|
|
argv_node.attribute("startsize").value( &startsize );
|
|
|
|
argv_node.attribute("endsize").value( &endsize );
|
|
|
|
argv_node.attribute("verbose").value( &verbose );
|
|
|
|
} catch(...) { }
|
2013-04-08 20:51:28 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
for (int i = 1; i < argc; i += 2) {
|
|
|
|
if (strcmp(argv[i], "-serverip") == 0)
|
|
|
|
strncpy(serverip, argv[i+1], sizeof(serverip));
|
|
|
|
else if (strcmp(argv[i], "-startsize") == 0)
|
|
|
|
startsize = atoi(argv[i+1]);
|
|
|
|
else if (strcmp(argv[i], "-endsize") == 0)
|
|
|
|
endsize = atoi(argv[i+1]);
|
|
|
|
else if (strcmp(argv[i], "-count") == 0)
|
|
|
|
count = atoi(argv[i+1]);
|
|
|
|
}
|
2013-01-24 16:36:10 +01:00
|
|
|
|
|
|
|
if ((endsize + sizeof (Packetheader)) > Databuf) {
|
2013-04-08 20:51:28 +02:00
|
|
|
printf("ERROR: endsize is greater than the servers' data buffer\n");
|
2013-01-24 16:36:10 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = startsize; i <= endsize; i <<= 1)
|
2013-04-08 20:51:28 +02:00
|
|
|
sendping(serverip, i, count);
|
2013-01-24 16:36:10 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|