Subversion Repositories shark

Rev

Rev 1294 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1293 giacomo 1
/*
2
 * Project: S.Ha.R.K.
3
 *
4
 * Coordinators:
5
 *   Giorgio Buttazzo    <giorgio@sssup.it>
6
 *   Paolo Gai           <pj@gandalf.sssup.it>
7
 *
8
 * Authors     :
9
 *    Giacomo Guidi      <giacomo@gandalf.sssup.it>
10
 *
11
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
12
 *
13
 * http://www.sssup.it
14
 * http://retis.sssup.it
15
 * http://shark.sssup.it
16
 */
17
 
18
#include <netinet/in.h>
19
 
20
#include <sys/socket.h>
21
#include <netinet/in.h>
22
#include <arpa/inet.h>
23
#include <netdb.h>
24
#include <stdio.h>
25
#include <unistd.h>/* close() */
26
#include <stdlib.h>
27
#include <signal.h>
28
 
29
#define SERVER_PORT 20000
30
#define MAX_MSG 10000
31
 
32
struct tracer_udp_header {
33
  char id[12];
34
  unsigned int pkt_number;
35
  unsigned int events;
36
  unsigned int size;
37
};
38
 
39
FILE *output_file;
40
 
41
void close_and_exit()
42
{
43
 
44
  printf("Closing...\n");
45
 
46
  fclose(output_file);
47
 
48
  exit(0);
49
 
50
}
51
 
52
int main(int argc, char *argv[])
53
{
54
  int sd, rc, n, cliLen;
55
  struct sockaddr_in cliAddr, servAddr;
56
  char msg[MAX_MSG];
57
  char ch;
58
 
59
  struct tracer_udp_header *pkt_head = (struct tracer_udp_header *)(msg);
60
 
61
  if (argc < 2) {
62
    printf("%s: Enter the output file name [%s filename]\n",argv[0],argv[0]);
63
    exit(1);
64
  }
65
 
66
  // socket creation
67
  sd = socket(AF_INET, SOCK_DGRAM, 0);
68
  if(sd < 0) {
69
    printf("%s: cannot open socket \n",argv[0]);
70
    exit(1);
71
  }
72
 
73
  output_file = fopen(argv[1],"w+b");
74
  if (output_file == NULL) {
75
    printf("%s: Cannot open the file\n");
76
    exit(1);
77
  }
78
 
79
  // bind local server port
80
  servAddr.sin_family = AF_INET;
81
 
82
  servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
83
  servAddr.sin_port = htons(SERVER_PORT);
84
 
85
  rc = bind (sd, (struct sockaddr *)&servAddr,sizeof(servAddr));
86
  if(rc < 0) {
87
    printf("%s: cannot bind port number %d \n",
88
           argv[0], SERVER_PORT);
89
    exit(1);
90
  }
91
 
92
  signal(SIGINT, close_and_exit);
93
 
94
  while(1) {
95
 
96
    printf("Wait packet...\n");
97
 
98
    // receive message
99
    cliLen = sizeof(cliAddr);
100
    n = recvfrom(sd, msg, MAX_MSG, 0,(struct sockaddr *)&cliAddr, &cliLen);
101
 
102
    printf("Received %d, length %d(%d), %d tracer events.\n",
103
                pkt_head->pkt_number, n, pkt_head->size, pkt_head->events);
104
 
105
    if(n < 0) {
106
      printf("%s: cannot receive data \n",argv[0]);
107
      continue;
108
    }
109
 
110
    fwrite((void *)(msg+sizeof(struct tracer_udp_header)),n-sizeof(struct tracer_udp_header),1,output_file);
111
 
112
  }
113
 
114
  fclose(output_file);
115
 
116
  return 0;
117
 
118
}
119