-
Notifications
You must be signed in to change notification settings - Fork 0
/
mac.c
147 lines (129 loc) · 3.19 KB
/
mac.c
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#ifndef _PLUGIN_API_H
#define _PLUGIN_API_H
#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <regex.h>
#define PATTERN "^[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}$"
struct plugin_option {
struct option opt;
const char *opt_descr;
};
struct plugin_info {
const char *plugin_name;
size_t sup_opts_len;
struct plugin_option *sup_opts;
};
int plugin_get_info(struct plugin_info* ppi)
{
if (ppi == NULL)
{
return 1;
}
ppi->plugin_name = "mac";
ppi->sup_opts_len = 1;
struct plugin_option *mac_option = malloc(sizeof(struct plugin_option) * ppi->sup_opts_len);
mac_option->opt_descr = "Searches mac address";
struct option *mac_opt = malloc(sizeof(struct option));
mac_opt->name = "macv4-addr";
mac_opt->has_arg = required_argument;
mac_opt->flag = NULL;
mac_opt->val = 0;
mac_option->opt = *mac_opt;
ppi->sup_opts = malloc(sizeof(struct plugin_option));
ppi->sup_opts->opt.name = "mac-addr";
ppi->sup_opts->opt.has_arg = required_argument;
ppi->sup_opts->opt.flag = NULL;
ppi->sup_opts->opt.val = 0;
return 0;
};
int plugin_process_file(const char *fname,
struct option *in_opts[],
size_t in_opts_len,
char *out_buff,
size_t out_buff_len)
{
FILE *current_file;
if ((strstr(fname, "share") != NULL) || (strstr(fname, "/.") != NULL))
{
return 10;
}
current_file = fopen(fname, "r");
if (current_file == NULL)
{
fclose(current_file);
return -1;
}
if (in_opts[0]->flag == NULL)
{
printf("Error: missing argument\n");
fclose(current_file);
return -1;
}
char *mac1 = (char *) in_opts[0]->flag;
regex_t preg;
int err,regerr;
err = regcomp (&preg, PATTERN, REG_EXTENDED);
if (err != 0)
{
char buff[512];
regerror(err, &preg, buff, sizeof(buff));
printf("%s\n", buff);
}
regmatch_t pm;
regerr = regexec (&preg, mac1, 0, &pm, 0);
if (regerr != 0)
{
char errbuf[512];
regerror(regerr, &preg, errbuf, sizeof(errbuf));
//printf("%s\n", errbuf);
printf("Error: invalid syntax of mac address\n");
strcpy(out_buff, "invalid syntax");
exit(1);
return -1;
}
char *s;
s = malloc(sizeof(char) * 255);
if (s == NULL)
{
return 10;
}
if (strlen(s) > 255)
{
return 10;
}
int i = 0;
while (feof(current_file) == 0)
{
i++;
if (i > 400)
{
fclose(current_file);
return -1;
}
int yy;
yy = fscanf(current_file, "%s", s);
if (yy == 0)
{
return 10;
}
if (strlen(s) > 255)
{
return 10;
}
if (s == NULL)
{
return 0;
}
if (strstr(s, mac1) != NULL)
{
fclose(current_file);
return 0;
}
}
fclose(current_file);
return 19;
};
#endif