Debian Codegraph @

snmplib/transports/snmpTCPBaseDomain.c

raw
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 {
48 int rc = -1;
49
50 if (t != NULL && t->sock >= 0) {
51 while (rc < 0) {
52 rc = recvfrom(t->sock, buf, size, 0, NULL, NULL);
53 if (rc < 0 && errno != EINTR) {
54 DEBUGMSGTL(("netsnmp_tcpbase", "recv fd %d err %d (\"%s\")\n",
55 t->sock, errno, strerror(errno)));
56 break;
57 }
58 DEBUGMSGTL(("netsnmp_tcpbase", "recv fd %d got %d bytes\n",
59 t->sock, rc));
60 }
61 } else {
62 return -1;
63 }
64
65 if (opaque != NULL && olength != NULL) {
66 if (t->data_length > 0) {
67 if ((*opaque = malloc(t->data_length)) != NULL) {
68 memcpy(*opaque, t->data, t->data_length);
69 *olength = t->data_length;
70 } else {
71 *olength = 0;
72 }
73 } else {
74 *opaque = NULL;
75 *olength = 0;
76 }
77 }
78
79 return rc;
80 }
81
82 int netsnmp_tcpbase_send(netsnmp_transport *t, const void *buf, int size,
83 void **opaque, int *olength) {
84 int rc = -1;
85
86 if (t != NULL && t->sock >= 0) {
87 while (rc < 0) {
88 rc = sendto(t->sock, buf, size, 0, NULL, 0);
89 if (rc < 0 && errno != EINTR) {
90 break;
91 }
92 }
93 }
94 return rc;
95 }