2 * Netgrind -- Saving to Files
4 * (c) 2003 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU General Public License.
11 #include "netgrind/pkt.h"
12 #include "netgrind/netgrind.h"
22 static void save_open(struct flow *f, u64 when UNUSED)
24 struct save_state *s = xmalloc(sizeof(*s));
25 static uns save_counter;
26 for (uns dir=0; dir<2; dir++)
29 sprintf(name, "flows/%06u-%04x:%d-%04x:%d-%c", save_counter, f->saddr, f->sport, f->daddr, f->dport, 'A' + dir);
30 if (!(s->file[dir] = fopen(name, "w")))
31 die("Unable to create %s: %m", name);
37 static void save_close(struct flow *f, int cause UNUSED, u64 when UNUSED)
39 struct save_state *s = f->appl_data;
45 static void save_input(struct flow *f, int dir, struct pkt *p)
47 struct save_state *s = f->appl_data;
48 fwrite(p->data, pkt_len(p), 1, s->file[dir]);
52 struct appl_hooks appl_save = {
65 static void asave_event(struct asave_state *s, u64 time, byte *msg, ...)
70 time -= s->start_time;
71 fprintf(s->file, "%04d.%03d ", (int)(time/1000000), (int)(time%1000000)%1000);
72 vfprintf(s->file, msg, args);
76 static void asave_open(struct flow *f, u64 when)
78 struct asave_state *s = xmalloc(sizeof(*s));
80 static uns asave_counter;
81 sprintf(name, "flows/%06u-%04x:%d-%04x:%d", asave_counter++, f->saddr, f->sport, f->daddr, f->dport);
82 if (!(s->file = fopen(name, "w")))
83 die("Unable to create %s: %m", name);
86 asave_event(s, when, "Established\n");
89 static void asave_close(struct flow *f, int cause, u64 when)
91 struct asave_state *s = f->appl_data;
92 static byte *close_reasons[] = { "Closed", "Connection reset", "Timed out", "Doomsday" };
93 asave_event(s, when, "%s\n", close_reasons[cause]);
94 asave_event(s, when, "TX %Ld bytes in %Ld packets, RX %Ld bytes in %Ld packets\n",
101 static void asave_input(struct flow *f, int dir, struct pkt *p)
103 struct asave_state *s = f->appl_data;
104 asave_event(s, p->timestamp, (dir ? ">>> " : "<<< "));
105 uns len = pkt_len(p);
108 for (uns i=0; i<len; i++)
110 if (cnt >= asave_width)
112 if (last_char != '\n')
113 fputc('\\', s->file);
114 fputc('\n', s->file);
115 asave_event(s, p->timestamp, (dir ? ">>> " : "<<< "));
121 else if (c >= 0x20 && c < 0x7f || c >= 0xa0)
128 fprintf(s->file, "<%02x>", c);
135 if (last_char != '\n')
136 fputc('\\', s->file);
137 fputc('\n', s->file);
142 struct appl_hooks appl_asave = {
144 .input = asave_input,