forked from richardcochran/linuxptp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnmp4lptp.c
192 lines (164 loc) · 4.13 KB
/
snmp4lptp.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
* @file snmp4lptp.c
* @brief Implements SNMP sub agent program for linuxptp
* @note Copyright (C) 2018 Anders Selhammer <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <errno.h>
#include <poll.h>
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include "config.h"
#include "pmc_common.h"
#include "print.h"
#include "snmp4lptp_mib.h"
#include "util.h"
#define SNMP_NFD 1
static struct pmc *pmc;
struct ptp_message* snmp4lptp_run_pmc(char *cmd)
{
struct pollfd pollfd[SNMP_NFD];
int cnt, tmo = 100;
pollfd[0].fd = pmc_get_transport_fd(pmc);
pollfd[0].events = POLLIN | POLLPRI;
if (cmd && pmc_do_command(pmc, cmd)) {
pr_err("bad command: %s", cmd);
}
while (is_running()) {
cnt = poll(pollfd, SNMP_NFD, tmo);
if (cnt < 0) {
if (EINTR == errno) {
continue;
} else {
pr_emerg("poll failed");
break;
}
} else if (!cnt) {
break;
}
if (pollfd[0].revents & (POLLIN|POLLPRI)) {
return pmc_recv(pmc);
}
}
return NULL;
}
static int open_pmc(struct config *cfg)
{
char uds_local[MAX_IFNAME_SIZE + 1];
snprintf(uds_local, sizeof(uds_local), "/var/run/snmp4lptp.%d", getpid());
pmc = pmc_create(cfg, TRANS_UDS, uds_local, 0,
config_get_int(cfg, NULL, "domainNumber"),
config_get_int(cfg, NULL, "transportSpecific") << 4,
1);
return pmc ? 0 : -1;
}
static int open_snmp()
{
snmp_enable_calllog();
netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID,
NETSNMP_DS_AGENT_ROLE, 1);
init_agent("linuxptpAgent");
init_snmp("linuxptpAgent");
return 0;
}
static void usage(char *progname)
{
fprintf(stderr,
"\nusage: %s [options]\n\n"
" -f [file] read configuration from 'file'\n"
" -h prints this message and exits\n"
" -m print messages to stdout\n"
" -q do not print messages to the syslog\n"
"\n",
progname);
}
int main(int argc, char *argv[])
{
char *config = NULL, *progname;
int c, err = 0, index;
struct option *opts;
struct config *cfg;
if (handle_term_signals()) {
return -1;
}
cfg = config_create();
if (!cfg) {
return -1;
}
opts = config_long_options(cfg);
print_set_verbose(1);
print_set_syslog(0);
/* Process the command line arguments. */
progname = strrchr(argv[0], '/');
progname = progname ? 1+progname : argv[0];
while (EOF != (c = getopt_long(argc, argv, "f:hmq", opts, &index))) {
switch (c) {
case 0:
if (config_parse_option(cfg, opts[index].name, optarg)) {
config_destroy(cfg);
return -1;
}
break;
case 'f':
config = optarg;
break;
case 'h':
usage(progname);
err = -1;
goto out;
case 'm':
config_set_int(cfg, "verbose", 1);
break;
case 'q':
config_set_int(cfg, "use_syslog", 0);
break;
case '?':
default:
usage(progname);
err = -1;
goto out;
}
}
if (config && (err = config_read(config, cfg))) {
err = -1;
goto out;
}
print_set_progname(progname);
print_set_tag(config_get_string(cfg, NULL, "message_tag"));
print_set_verbose(config_get_int(cfg, NULL, "verbose"));
print_set_syslog(config_get_int(cfg, NULL, "use_syslog"));
print_set_level(config_get_int(cfg, NULL, "logging_level"));
if (open_pmc(cfg)) {
err = -1;
goto pmc_out;
}
if (open_snmp()) {
err = -1;
goto snmp_out;
}
while (is_running()) {
agent_check_and_process(1);
}
snmp_shutdown("linuxptpAgent");
snmp_out:
pmc_destroy(pmc);
msg_cleanup();
pmc_out:
out:
config_destroy(cfg);
return err;
}