Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1655 | 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 | * Paolo Gai <pj@gandalf.sssup.it> |
||
10 | * Massimiliano Giorgi <massy@gandalf.sssup.it> |
||
11 | * Luca Abeni <luca@gandalf.sssup.it> |
||
12 | * (see the web pages for full authors list) |
||
13 | * |
||
14 | * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy) |
||
15 | * |
||
16 | * http://www.sssup.it |
||
17 | * http://retis.sssup.it |
||
18 | * http://shark.sssup.it |
||
19 | */ |
||
20 | |||
21 | /* |
||
22 | * Copyright (C) 2000 Massimiliano Giorgi |
||
23 | * Copyright (C) 2002 Paolo Gai |
||
24 | * Copyright (C) 2002 Tomas Lenvall |
||
25 | * |
||
26 | * This program is free software; you can redistribute it and/or modify |
||
27 | * it under the terms of the GNU General Public License as published by |
||
28 | * the Free Software Foundation; either version 2 of the License, or |
||
29 | * (at your option) any later version. |
||
30 | * |
||
31 | * This program is distributed in the hope that it will be useful, |
||
32 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
33 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
34 | * GNU General Public License for more details. |
||
35 | * |
||
36 | * You should have received a copy of the GNU General Public License |
||
37 | * along with this program; if not, write to the Free Software |
||
38 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
39 | * |
||
40 | * CVS : $Id: udpdump.c,v 1.1.1.1 2004-05-24 18:03:44 giacomo Exp $ |
||
41 | */ |
||
42 | |||
43 | /* this example simply prints a Shark trace file */ |
||
44 | |||
45 | // INCLUDES |
||
46 | #include <netinet/in.h> |
||
47 | |||
48 | #include "types.h" |
||
49 | #include "trace.h" |
||
50 | #include "util.h" |
||
51 | |||
52 | #include <sys/socket.h> |
||
53 | #include <netinet/in.h> |
||
54 | #include <arpa/inet.h> |
||
55 | #include <netdb.h> |
||
56 | #include <stdio.h> |
||
57 | #include <unistd.h>/* close() */ |
||
58 | #include <string.h> /* memset() */ |
||
59 | #include <stdlib.h> |
||
60 | |||
61 | |||
62 | // GLOBALS |
||
63 | char server_ipaddr[20]; // Store the IP adress of the server |
||
64 | |||
65 | // DEFINES |
||
66 | #define LOCAL_SERVER_PORT 20000 |
||
67 | #define MAX_MSG 10000 |
||
68 | |||
69 | // FUNCTIONS |
||
70 | /* */ |
||
71 | int dumpsys(trc_system_event_t *sys) |
||
72 | { |
||
73 | printf("%02i\n",sys->task); |
||
74 | return 0; |
||
75 | } |
||
76 | |||
77 | /* */ |
||
78 | int dumpusr(trc_user_event_t *usr) |
||
79 | { |
||
80 | printf("%8li ",usr->n); |
||
81 | printf("\n"); |
||
82 | return 0; |
||
83 | } |
||
84 | |||
85 | /* */ |
||
86 | int dumpsem(trc_sem_event_t *sem) |
||
87 | { |
||
88 | printf("on [%i]\n",sem->id); |
||
89 | return 0; |
||
90 | } |
||
91 | |||
92 | /* */ |
||
93 | int dumpfunc(trc_event_t *ev) |
||
94 | { |
||
95 | static int counter=0; |
||
96 | |||
97 | printf("\t%4i ",counter); |
||
98 | counter++; |
||
99 | printf("%12s ",format_time(ev->time)); |
||
100 | printf("%-10s ",event_name(ev->event)); |
||
101 | |||
102 | //printf("%08x\n",(unsigned)ev->sys.event); |
||
103 | //return 0; |
||
104 | |||
105 | switch(event_class(ev->event)) { |
||
106 | case TRC_CLASS_SYSTEM: return dumpsys(&ev->x.sys); |
||
107 | case TRC_CLASS_USER: return dumpusr(&ev->x.usr); |
||
108 | case TRC_CLASS_SEM: return dumpsem(&ev->x.sem); |
||
109 | } |
||
110 | printf("\nEVENT %i... CLASS %i UNKNOWN!\n",ev->event,event_class(ev->event)); |
||
111 | return -1; |
||
112 | } |
||
113 | |||
114 | |||
115 | /* Use UDP/IP to receive the events from the client computer */ |
||
116 | int main(int argc, char *argv[]) |
||
117 | { |
||
118 | int sd, rc, n, cliLen; |
||
119 | struct sockaddr_in cliAddr, servAddr; |
||
120 | char msg[MAX_MSG]; |
||
121 | |||
122 | // socket creation |
||
123 | sd = socket(AF_INET, SOCK_DGRAM, 0); |
||
124 | if(sd < 0) { |
||
125 | printf("%s: cannot open socket \n",argv[0]); |
||
126 | exit(1); |
||
127 | } |
||
128 | |||
129 | // bind local server port |
||
130 | servAddr.sin_family = AF_INET; |
||
131 | |||
132 | servAddr.sin_addr.s_addr = htonl(INADDR_ANY); |
||
133 | servAddr.sin_port = htons(LOCAL_SERVER_PORT); |
||
134 | |||
135 | rc = bind (sd, (struct sockaddr *)&servAddr,sizeof(servAddr)); |
||
136 | if(rc < 0) { |
||
137 | printf("%s: cannot bind port number %d \n", |
||
138 | argv[0], LOCAL_SERVER_PORT); |
||
139 | exit(1); |
||
140 | } |
||
141 | |||
142 | while(1){ |
||
143 | |||
144 | // init buffer |
||
145 | memset(msg, 0x0, MAX_MSG); |
||
146 | |||
147 | // receive message |
||
148 | cliLen = sizeof(cliAddr); |
||
149 | n = recvfrom(sd, msg, MAX_MSG, 0,(struct sockaddr *)&cliAddr, &cliLen); |
||
150 | |||
151 | printf("Packet received, length %d, %d tracer events.\n", n, |
||
152 | *((short int *)msg)); |
||
153 | |||
154 | if(n < 0) { |
||
155 | printf("%s: cannot receive data \n",argv[0]); |
||
156 | continue; |
||
157 | } |
||
158 | |||
159 | // Read the trace we got from the network |
||
160 | read_udp_trace(msg, dumpfunc); |
||
161 | } |
||
162 | |||
163 | return 0; |
||
164 | } |
||
165 | |||
166 | |||
167 | // test main: testing this program from a file (simulating a network package) |
||
168 | // ONLY USED FOR TESTING!!! |
||
169 | |||
170 | /*int main(int argc, char *argv[]) |
||
171 | { |
||
172 | FILE* fin; |
||
173 | int nr_of_events = 0; |
||
174 | trc_event_t buffer; |
||
175 | char all_events[1000]; |
||
176 | char tmpbuf[2]; |
||
177 | |||
178 | if(argc != 2) { |
||
179 | printf("Missing filename!\n"); |
||
180 | return -1; |
||
181 | } |
||
182 | |||
183 | // open the file and send event by event |
||
184 | fin = fopen(argv[1], "r"); |
||
185 | if (fin == NULL) { |
||
186 | printf("Cannot open file\n"); |
||
187 | return -1; |
||
188 | } |
||
189 | |||
190 | // check how many events |
||
191 | while (!feof(fin)) { |
||
192 | fread(&buffer, sizeof(trc_event_t), 1, fin); |
||
193 | nr_of_events++; |
||
194 | } |
||
195 | |||
196 | //printf("Nr of events: %d\n", nr_of_events); |
||
197 | |||
198 | rewind(fin); // start from the beginning again... |
||
199 | //strncpy(tmpbuf, itoa(nr_of_events), 2); |
||
200 | sprintf(tmpbuf, "%d", nr_of_events); |
||
201 | //printf("tmpbuf: %s\n", tmpbuf); |
||
202 | |||
203 | memset(all_events, 0, 1000); |
||
204 | memcpy(all_events, tmpbuf, 2); |
||
205 | printf("all_events(1): %s\n", all_events); |
||
206 | // read all tracer events and store them in an array |
||
207 | fread(all_events + 2, sizeof(trc_event_t), nr_of_events, fin); |
||
208 | fclose(fin); |
||
209 | |||
210 | printf("all_events(2): %s\n", all_events); |
||
211 | // send it to the event reader |
||
212 | read_trace(all_events, dumpfunc); |
||
213 | |||
214 | return 1; |
||
215 | } |
||
216 | */ |