Subversion Repositories shark

Rev

Rev 1322 | 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
 
1606 tullio 18
/*
19
 * This program is free software; you can redistribute it and/or modify
20
 * it under the terms of the GNU General Public License as published by
21
 * the Free Software Foundation; either version 2 of the License, or
22
 * (at your option) any later version.
23
 *
24
 * This program is distributed in the hope that it will be useful,
25
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27
 * GNU General Public License for more details.
28
 *
29
 * You should have received a copy of the GNU General Public License
30
 * along with this program; if not, write to the Free Software
31
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
32
 *
33
 */
34
 
1293 giacomo 35
#include <netinet/in.h>
36
 
37
#include <sys/socket.h>
38
#include <netinet/in.h>
39
#include <arpa/inet.h>
40
#include <netdb.h>
41
#include <stdio.h>
42
#include <unistd.h>/* close() */
43
#include <stdlib.h>
44
#include <signal.h>
1322 giacomo 45
#include <string.h>
1293 giacomo 46
 
47
#define SERVER_PORT 20000
48
#define MAX_MSG 10000
49
 
50
struct tracer_udp_header {
51
  char id[12];
52
  unsigned int pkt_number;
53
  unsigned int events;
54
  unsigned int size;
55
};
56
 
57
FILE *output_file;
58
 
1304 giacomo 59
int miss;
60
 
1293 giacomo 61
void close_and_exit()
62
{
63
 
64
  printf("Closing...\n");
65
 
1304 giacomo 66
  if (miss == 1) printf("Possible error receiving packets !\n");
67
 
1293 giacomo 68
  fclose(output_file);
69
 
70
  exit(0);
71
 
72
}
73
 
74
int main(int argc, char *argv[])
75
{
1304 giacomo 76
  int sd, rc, n, cliLen,count;
1293 giacomo 77
  struct sockaddr_in cliAddr, servAddr;
78
  char msg[MAX_MSG];
79
 
80
  struct tracer_udp_header *pkt_head = (struct tracer_udp_header *)(msg);
81
 
82
  if (argc < 2) {
83
    printf("%s: Enter the output file name [%s filename]\n",argv[0],argv[0]);
84
    exit(1);
85
  }
86
 
87
  // socket creation
88
  sd = socket(AF_INET, SOCK_DGRAM, 0);
89
  if(sd < 0) {
90
    printf("%s: cannot open socket \n",argv[0]);
91
    exit(1);
92
  }
93
 
94
  output_file = fopen(argv[1],"w+b");
95
  if (output_file == NULL) {
1294 giacomo 96
    printf("%s: Cannot open the file\n",argv[0]);
1293 giacomo 97
    exit(1);
98
  }
99
 
100
  // bind local server port
101
  servAddr.sin_family = AF_INET;
102
 
103
  servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
104
  servAddr.sin_port = htons(SERVER_PORT);
105
 
106
  rc = bind (sd, (struct sockaddr *)&servAddr,sizeof(servAddr));
107
  if(rc < 0) {
108
    printf("%s: cannot bind port number %d \n",
109
           argv[0], SERVER_PORT);
110
    exit(1);
111
  }
112
 
113
  signal(SIGINT, close_and_exit);
114
 
1304 giacomo 115
  count = 1;
116
  miss = 0;
117
 
1293 giacomo 118
  while(1) {
119
 
120
    printf("Wait packet...\n");
121
 
122
    // receive message
123
    cliLen = sizeof(cliAddr);
124
    n = recvfrom(sd, msg, MAX_MSG, 0,(struct sockaddr *)&cliAddr, &cliLen);
125
 
1322 giacomo 126
    if (strncmp(pkt_head->id,"TRACER",6)) continue;
127
 
1293 giacomo 128
    printf("Received %d, length %d(%d), %d tracer events.\n",
129
                pkt_head->pkt_number, n, pkt_head->size, pkt_head->events);
130
 
1304 giacomo 131
    if (pkt_head->pkt_number != count) {
132
      printf("Miss Packet !!!\n");
133
      miss = 1;
134
    }
135
 
1322 giacomo 136
    if (n > 0) {
1304 giacomo 137
 
1322 giacomo 138
      count++;
139
 
140
      fwrite((void *)(msg+sizeof(struct tracer_udp_header)),n-sizeof(struct tracer_udp_header),1,output_file);
141
 
1293 giacomo 142
    }
143
 
144
  }
145
 
146
  fclose(output_file);
147
 
148
  return 0;
149
 
150
}
151