-
Notifications
You must be signed in to change notification settings - Fork 2
/
pcapsipdump_lib.cpp
130 lines (122 loc) · 4.38 KB
/
pcapsipdump_lib.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <libgen.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "pcapsipdump_lib.h"
// equivalent of "mkdir -p"
int mkdir_p(const char *path, mode_t mode) {
char s[256];
char *p;
struct stat sb;
if (stat(path, &sb) == 0) {
return 0;
} else {
strlcpy(s, path, sizeof(s));
p = strrchr(s, '/');
if (p != NULL) {
*p = '\0';
if (mkdir_p(s, mode) != 0) {
return -1;
}
}
return mkdir(path, mode);
}
return -1;
}
size_t expand_dir_template(char *s, size_t max, const char *format,
const char *from,
const char *to,
const char *callid,
const time_t t) {
struct tm *tm = localtime(&t);
size_t fl = strlen(format);
size_t s1l = fl + 256;
char *s1 = (char *)malloc(s1l);
char *s1p = s1;
char asciisan[128] = {
'_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_',
'_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_',
'_', '_', '_', '#', '_', '_', '&', '_', '(', ')', '_', '+', ',', '-', '.', '_',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '_', ';', '_', '=', '_', '_',
'@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '_', ']', '^', '_',
'_', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '_', '}', '~', '_'
};
for (size_t i = 0; i <= fl; i++) {
char c0 = format[i];
if (c0 == '%' && i < fl - 1) {
char c1 = format[i+1];
if (c1 == 'f' && (s1l - (s1p - s1)) > strlen(from) ){
strcpy(s1p, from);
for(;*s1p;s1p++){
*s1p = asciisan[*s1p & 0x7f];
}
i++;
} else if (c1 == 't' && (s1l - (s1p - s1)) > strlen(to) ){
strcpy(s1p, to);
for(;*s1p;s1p++){
*s1p = asciisan[*s1p & 0x7f];
}
i++;
} else if (c1 == 'i' && (s1l - (s1p - s1)) > strlen(callid) ){
strcpy(s1p, callid);
for(;*s1p;s1p++){
*s1p = asciisan[*s1p & 0x7f];
}
i++;
} else {
*(s1p++) = c0;
}
} else {
*(s1p++) = c0;
}
}
{
size_t r = strftime(s, max, s1, tm);
free(s1);
return r;
}
}
int opts_sanity_check_d(char **opt_fntemplate)
{
char s[2048];
char *orig_opt_fntemplate = *opt_fntemplate;
struct stat sb;
FILE *f;
expand_dir_template(s, sizeof(s), *opt_fntemplate, "", "", "", (int)time(NULL));
if (stat(s, &sb) == 0) {
if (!S_ISDIR(sb.st_mode)) {
fprintf(stderr, "Bad option '-d %s': File exists (expecting directory name or filename template)", orig_opt_fntemplate);
return(2);
}
// Looks like user has specified bare directory in '-d' option.
// First, make sure we can create files in that directory
strcat(s, "/tmp-siJNlSdiugQ3iyjaPNW");
if ((f = fopen(s, "a")) == NULL){
fprintf(stderr, "Can't create file for '-d %s': ", orig_opt_fntemplate);
perror (s);
return(2);
}
fclose(f);
unlink(s);
// Then append some default filename template to be backwards-compatible
*opt_fntemplate = (char *)malloc(strlen(s) + 128);
strcpy(*opt_fntemplate, orig_opt_fntemplate);
strcat(*opt_fntemplate, "/%Y%m%d/%H/%Y%m%d-%H%M%S-%f-%t-%i.pcap");
expand_dir_template(s, sizeof(s), orig_opt_fntemplate, "", "", "", 0);
} else {
if (!strchr(*opt_fntemplate, '%') || !strchr(s, '/')) {
fprintf(stderr, "Bad option '-d %s': Neither directory nor filename template", orig_opt_fntemplate);
return(2);
// (try to) create directory hierarchy
} else if (mkdir_p(dirname(s), 0777)) {
fprintf(stderr, "Can't create directory for '-d %s': ", orig_opt_fntemplate);
perror (s);
return(2);
}
}
return(0);
}