/src/net-snmp/snmplib/transports/snmpTCPBaseDomain.c
Line | Count | Source |
1 | | #include <net-snmp/net-snmp-config.h> |
2 | | |
3 | | #include <net-snmp/types.h> |
4 | | #include <net-snmp/library/snmpTCPBaseDomain.h> |
5 | | |
6 | | #include <stdio.h> |
7 | | #include <sys/types.h> |
8 | | #include <errno.h> |
9 | | |
10 | | #ifdef HAVE_STRING_H |
11 | | #include <string.h> |
12 | | #else |
13 | | #include <strings.h> |
14 | | #endif |
15 | | #ifdef HAVE_STDLIB_H |
16 | | #include <stdlib.h> |
17 | | #endif |
18 | | #ifdef HAVE_UNISTD_H |
19 | | #include <unistd.h> |
20 | | #endif |
21 | | #ifdef HAVE_SYS_SOCKET_H |
22 | | #include <sys/socket.h> |
23 | | #endif |
24 | | #ifdef HAVE_NETINET_IN_H |
25 | | #include <netinet/in.h> |
26 | | #endif |
27 | | #ifdef HAVE_ARPA_INET_H |
28 | | #include <arpa/inet.h> |
29 | | #endif |
30 | | #ifdef HAVE_FCNTL_H |
31 | | #include <fcntl.h> |
32 | | #endif |
33 | | |
34 | | #include <net-snmp/types.h> |
35 | | #include <net-snmp/output_api.h> |
36 | | |
37 | | #include <net-snmp/library/snmp_transport.h> |
38 | | |
39 | | /* |
40 | | * You can write something into opaque that will subsequently get passed back |
41 | | * to your send function if you like. For instance, you might want to |
42 | | * remember where a PDU came from, so that you can send a reply there... |
43 | | */ |
44 | | |
45 | | int netsnmp_tcpbase_recv(netsnmp_transport *t, void *buf, int size, |
46 | | void **opaque, int *olength) |
47 | 0 | { |
48 | 0 | int rc = -1; |
49 | |
|
50 | 0 | if (t && NETSNMP_IS_VALID_SOCKET(t->sock)) { |
51 | 0 | while (rc < 0) { |
52 | 0 | rc = recvfrom(t->sock, buf, size, 0, NULL, NULL); |
53 | 0 | if (rc < 0 && errno != EINTR) { |
54 | 0 | DEBUGMSGTL(("netsnmp_tcpbase", |
55 | 0 | "recv fd %" NETSNMP_FMT_SKT " err %d (\"%s\")\n", |
56 | 0 | t->sock, errno, strerror(errno))); |
57 | 0 | break; |
58 | 0 | } |
59 | 0 | DEBUGMSGTL(("netsnmp_tcpbase", |
60 | 0 | "recv fd %" NETSNMP_FMT_SKT " got %d bytes\n", |
61 | 0 | t->sock, rc)); |
62 | 0 | } |
63 | 0 | } else { |
64 | 0 | return -1; |
65 | 0 | } |
66 | | |
67 | 0 | if (opaque != NULL && olength != NULL) { |
68 | 0 | if (t->data_length > 0) { |
69 | 0 | if ((*opaque = malloc(t->data_length)) != NULL) { |
70 | 0 | memcpy(*opaque, t->data, t->data_length); |
71 | 0 | *olength = t->data_length; |
72 | 0 | } else { |
73 | 0 | *olength = 0; |
74 | 0 | } |
75 | 0 | } else { |
76 | 0 | *opaque = NULL; |
77 | 0 | *olength = 0; |
78 | 0 | } |
79 | 0 | } |
80 | |
|
81 | 0 | return rc; |
82 | 0 | } |
83 | | |
84 | | int netsnmp_tcpbase_send(netsnmp_transport *t, const void *buf, int size, |
85 | 0 | void **opaque, int *olength) { |
86 | 0 | int rc = -1; |
87 | |
|
88 | 0 | if (t && NETSNMP_IS_VALID_SOCKET(t->sock)) { |
89 | 0 | while (rc < 0) { |
90 | 0 | rc = sendto(t->sock, buf, size, 0, NULL, 0); |
91 | 0 | if (rc < 0 && errno != EINTR) { |
92 | 0 | break; |
93 | 0 | } |
94 | 0 | } |
95 | 0 | } |
96 | 0 | return rc; |
97 | 0 | } |