Subversion Repositories shark

Rev

Rev 1293 | Rev 1304 | Go to most recent revision | Details | Compare with Previous | 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
 
58
  struct tracer_udp_header *pkt_head = (struct tracer_udp_header *)(msg);
59
 
60
  if (argc < 2) {
61
    printf("%s: Enter the output file name [%s filename]\n",argv[0],argv[0]);
62
    exit(1);
63
  }
64
 
65
  // socket creation
66
  sd = socket(AF_INET, SOCK_DGRAM, 0);
67
  if(sd < 0) {
68
    printf("%s: cannot open socket \n",argv[0]);
69
    exit(1);
70
  }
71
 
72
  output_file = fopen(argv[1],"w+b");
73
  if (output_file == NULL) {
1294 giacomo 74
    printf("%s: Cannot open the file\n",argv[0]);
1293 giacomo 75
    exit(1);
76
  }
77
 
78
  // bind local server port
79
  servAddr.sin_family = AF_INET;
80
 
81
  servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
82
  servAddr.sin_port = htons(SERVER_PORT);
83
 
84
  rc = bind (sd, (struct sockaddr *)&servAddr,sizeof(servAddr));
85
  if(rc < 0) {
86
    printf("%s: cannot bind port number %d \n",
87
           argv[0], SERVER_PORT);
88
    exit(1);
89
  }
90
 
91
  signal(SIGINT, close_and_exit);
92
 
93
  while(1) {
94
 
95
    printf("Wait packet...\n");
96
 
97
    // receive message
98
    cliLen = sizeof(cliAddr);
99
    n = recvfrom(sd, msg, MAX_MSG, 0,(struct sockaddr *)&cliAddr, &cliLen);
100
 
101
    printf("Received %d, length %d(%d), %d tracer events.\n",
102
                pkt_head->pkt_number, n, pkt_head->size, pkt_head->events);
103
 
104
    if(n < 0) {
105
      printf("%s: cannot receive data \n",argv[0]);
106
      continue;
107
    }
108
 
109
    fwrite((void *)(msg+sizeof(struct tracer_udp_header)),n-sizeof(struct tracer_udp_header),1,output_file);
110
 
111
  }
112
 
113
  fclose(output_file);
114
 
115
  return 0;
116
 
117
}
118