Subversion Repositories shark

Rev

Rev 1294 | 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
 
1304 giacomo 41
int miss;
42
 
1293 giacomo 43
void close_and_exit()
44
{
45
 
46
  printf("Closing...\n");
47
 
1304 giacomo 48
  if (miss == 1) printf("Possible error receiving packets !\n");
49
 
1293 giacomo 50
  fclose(output_file);
51
 
52
  exit(0);
53
 
54
}
55
 
56
int main(int argc, char *argv[])
57
{
1304 giacomo 58
  int sd, rc, n, cliLen,count;
1293 giacomo 59
  struct sockaddr_in cliAddr, servAddr;
60
  char msg[MAX_MSG];
61
 
62
  struct tracer_udp_header *pkt_head = (struct tracer_udp_header *)(msg);
63
 
64
  if (argc < 2) {
65
    printf("%s: Enter the output file name [%s filename]\n",argv[0],argv[0]);
66
    exit(1);
67
  }
68
 
69
  // socket creation
70
  sd = socket(AF_INET, SOCK_DGRAM, 0);
71
  if(sd < 0) {
72
    printf("%s: cannot open socket \n",argv[0]);
73
    exit(1);
74
  }
75
 
76
  output_file = fopen(argv[1],"w+b");
77
  if (output_file == NULL) {
1294 giacomo 78
    printf("%s: Cannot open the file\n",argv[0]);
1293 giacomo 79
    exit(1);
80
  }
81
 
82
  // bind local server port
83
  servAddr.sin_family = AF_INET;
84
 
85
  servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
86
  servAddr.sin_port = htons(SERVER_PORT);
87
 
88
  rc = bind (sd, (struct sockaddr *)&servAddr,sizeof(servAddr));
89
  if(rc < 0) {
90
    printf("%s: cannot bind port number %d \n",
91
           argv[0], SERVER_PORT);
92
    exit(1);
93
  }
94
 
95
  signal(SIGINT, close_and_exit);
96
 
1304 giacomo 97
  count = 1;
98
  miss = 0;
99
 
1293 giacomo 100
  while(1) {
101
 
102
    printf("Wait packet...\n");
103
 
104
    // receive message
105
    cliLen = sizeof(cliAddr);
106
    n = recvfrom(sd, msg, MAX_MSG, 0,(struct sockaddr *)&cliAddr, &cliLen);
107
 
108
    printf("Received %d, length %d(%d), %d tracer events.\n",
109
                pkt_head->pkt_number, n, pkt_head->size, pkt_head->events);
110
 
1304 giacomo 111
    if (pkt_head->pkt_number != count) {
112
      printf("Miss Packet !!!\n");
113
      miss = 1;
114
    }
115
 
116
    count++;
117
 
1293 giacomo 118
    if(n < 0) {
119
      printf("%s: cannot receive data \n",argv[0]);
120
      continue;
121
    }
122
 
123
    fwrite((void *)(msg+sizeof(struct tracer_udp_header)),n-sizeof(struct tracer_udp_header),1,output_file);
124
 
125
  }
126
 
127
  fclose(output_file);
128
 
129
  return 0;
130
 
131
}
132