Debian Codegraph @

snmplib/transports/snmpIPBaseDomain.c

raw
1 #include <net-snmp/net-snmp-config.h>
2 #include <net-snmp/types.h>
3 #include <net-snmp/library/system.h>
4 #include "snmpIPBaseDomain.h"
5 #include <ctype.h>
6 #include <errno.h>
7 #include <stdlib.h>
8 #include <string.h>
9
10 static int netsnmp_isnumber(const char *cp)
11 {
12 if (!*cp)
13 return 0;
14
15 while (isdigit((unsigned char)*cp))
16 cp++;
17 return *cp == '\0';
18 }
19
20 /**
21 * Parse a Net-SNMP endpoint name.
22 * @ep_str: Parsed endpoint name.
23 * @endpoint: Endpoint specification in the format
24 * <address>[@<iface>]:[<port>], <address>[@<iface>] or <port>.
25 *
26 * Only overwrite those fields of *@ep_str that have been set in
27 * @endpoint. Returns 1 upon success and 0 upon failure.
28 */
29 int netsnmp_parse_ep_str(struct netsnmp_ep_str *ep_str, const char *endpoint)
30 {
31 char *dup, *cp, *addrstr = NULL, *iface = NULL, *portstr = NULL;
32 unsigned port;
33
34 if (!endpoint)
35 return 0;
36
37 dup = strdup(endpoint);
38 if (!dup)
39 return 0;
40
41 cp = dup;
42 if (netsnmp_isnumber(cp)) {
43 portstr = cp;
44 } else {
45 if (*cp == '[') {
46 addrstr = cp + 1;
47 cp = strchr(cp, ']');
48 if (cp) {
49 cp[0] = '\0';
50 cp++;
51 } else {
52 goto err;
53 }
54 } else if (*cp != '@' && (*cp != ':' || cp[1] == ':')) {
55 addrstr = cp;
56 cp = strchr(addrstr, '@');
57 if (!cp) {
58 cp = strrchr(addrstr, ':');
59 if (cp && strchr(dup, ':') < cp)
60 cp = NULL;
61 }
62 }
63 if (cp && *cp == '@') {
64 *cp = '\0';
65 iface = cp + 1;
66 cp = strchr(cp + 1, ':');
67 }
68 if (cp && *cp == ':') {
69 *cp++ = '\0';
70 portstr = cp;
71 if (!netsnmp_isnumber(cp))
72 goto err;
73 } else if (cp && *cp) {
74 goto err;
75 }
76 }
77
78 if (addrstr) {
79 ep_str->addr = strdup(addrstr);
80 if (!ep_str->addr)
81 goto err;
82 }
83 if (iface)
84 strlcpy(ep_str->iface, iface, sizeof(ep_str->iface));
85 if (portstr) {
86 port = atoi(portstr);
87 if (port <= 0xffff)
88 strlcpy(ep_str->port, portstr, sizeof(ep_str->port));
89 else
90 goto err;
91 }
92
93 free(dup);
94 return 1;
95
96 err:
97 free(ep_str->addr);
98 ep_str->addr = NULL;
99 free(dup);
100 return 0;
101 }
102
103 int netsnmp_bindtodevice(int fd, const char *iface)
104 {
105 /* If no interface name has been specified, report success. */
106 if (!iface || iface[0] == '\0')
107 return 0;
108
109 #ifdef HAVE_SO_BINDTODEVICE
110 {
111 /*
112 * +1 to work around the Linux kernel bug that the passed in name is not
113 * '\0'-terminated.
114 */
115 int ifacelen = strlen(iface) + 1;
116 int ret;
117
118 ret = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, iface, ifacelen);
119 if (ret < 0)
120 snmp_log(LOG_ERR, "Binding socket to interface %s failed: %s\n",
121 iface, strerror(errno));
122 return ret;
123 }
124 #else
125 errno = EINVAL;
126 return -1;
127 #endif
128 }
129
130 int netsnmp_ipbase_session_init(struct netsnmp_transport_s *transport,
131 struct snmp_session *sess) {
132 union {
133 struct sockaddr sa;
134 struct sockaddr_in sin;
135 struct sockaddr_in6 sin6;
136 } ss;
137 socklen_t len = sizeof(ss);
138
139 if (!sess) {
140 DEBUGMSGTL(("netsnmp_ipbase", "session pointer is NULL\n"));
141 return SNMPERR_SUCCESS;
142 }
143
144 if (getsockname(transport->sock, (struct sockaddr *)&ss, &len) == -1) {
145 DEBUGMSGTL(("netsnmp_ipbase", "getsockname error %s\n", strerror(errno)));
146 return SNMPERR_SUCCESS;
147 }
148 switch (ss.sa.sa_family) {
149 case AF_INET:
150 sess->local_port = ntohs(ss.sin.sin_port);
151 break;
152 case AF_INET6:
153 sess->local_port = ntohs(ss.sin6.sin6_port);
154 break;
155 default:
156 DEBUGMSGTL(("netsnmp_ipbase", "unsupported address family %d\n",
157 ss.sa.sa_family));
158 return SNMPERR_SUCCESS;
159 }
160
161 DEBUGMSGTL(("netsnmp_ipbase", "local port number %d\n", sess->local_port));
162
163 return SNMPERR_SUCCESS;
164 }