// ----------------------------------------------------------------------
// File: EosUdpDumper.cc
// Author: Andreas-Joachim Peters - CERN
// ----------------------------------------------------------------------
/************************************************************************
* EOS - the CERN Disk Storage System *
* Copyright (C) 2011 CERN/Switzerland *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see .*
************************************************************************/
/*-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/
#include
#include
#include
#include
#include
/*-----------------------------------------------------------------------------*/
// a UDP server listening by default on port 32.000 dumping UDP packets of max. 64k
int main(int argc, char** argv)
{
int sockfd, n;
struct sockaddr_in servaddr, cliaddr;
socklen_t len;
char mesg[65536];
int port = 32000;
if (argc > 1) {
if (atoi(argv[1]) && (argc == 2)) {
port = atoi(argv[1]);
} else {
fprintf(stderr, "usage: eos-udp-dumper [port]\n");
exit(-1);
}
}
fprintf(stdout, "[eos-udp-dumper]: listening on port %d (max_message_size"
"=64k)\n", port);
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd == -1) {
fprintf(stderr, "error: failed while calling socket\n");
return 1;
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(port);
bind(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr));
for (;;) {
len = sizeof(cliaddr);
n = recvfrom(sockfd, mesg, 65536, 0, (struct sockaddr*)&cliaddr, &len);
if (n == -1) {
fprintf(stderr, "error: failed while calling recvfrom\n");
return 1;
}
if (sendto(sockfd, mesg, n, 0, (struct sockaddr*)&cliaddr,
sizeof(cliaddr)) == -1) {
fprintf(stderr, "error: failed while calling sendto\n");
return 1;
}
printf("-------------------------------------------------------\n");
mesg[n] = 0;
printf("%s", mesg);
printf("-------------------------------------------------------\n");
}
}