Subversion Repositories shark

Rev

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