summaryrefslogtreecommitdiffstats
path: root/sbin/hastd/proto_common.c
diff options
context:
space:
mode:
authorpjd <pjd@FreeBSD.org>2011-02-02 15:53:09 +0000
committerpjd <pjd@FreeBSD.org>2011-02-02 15:53:09 +0000
commit95c40e7f0914505b7f251ea147472c9de5e76cec (patch)
treeb73620e3d4537cc1721f8ed469dde072dacf2fe0 /sbin/hastd/proto_common.c
parent1267c20f911e08de277a22060d9bc42148f9afc0 (diff)
downloadFreeBSD-src-95c40e7f0914505b7f251ea147472c9de5e76cec.zip
FreeBSD-src-95c40e7f0914505b7f251ea147472c9de5e76cec.tar.gz
- Rename proto_descriptor_{send,recv}() functions to
proto_connection_{send,recv} and change them to return proto_conn structure. We don't operate directly on descriptors, but on proto_conns. - Add wrap method to wrap descriptor with proto_conn. - Remove methods to send and receive descriptors and implement this functionality as additional argument to send and receive methods. MFC after: 1 week
Diffstat (limited to 'sbin/hastd/proto_common.c')
-rw-r--r--sbin/hastd/proto_common.c105
1 files changed, 55 insertions, 50 deletions
diff --git a/sbin/hastd/proto_common.c b/sbin/hastd/proto_common.c
index bce3fbf..b22ea17 100644
--- a/sbin/hastd/proto_common.c
+++ b/sbin/hastd/proto_common.c
@@ -45,54 +45,8 @@ __FBSDID("$FreeBSD$");
#define MAX_SEND_SIZE 32768
#endif
-int
-proto_common_send(int sock, const unsigned char *data, size_t size)
-{
- ssize_t done;
- size_t sendsize;
-
- PJDLOG_ASSERT(sock >= 0);
- PJDLOG_ASSERT(data != NULL);
- PJDLOG_ASSERT(size > 0);
-
- do {
- sendsize = size < MAX_SEND_SIZE ? size : MAX_SEND_SIZE;
- done = send(sock, data, sendsize, MSG_NOSIGNAL);
- if (done == 0)
- return (ENOTCONN);
- else if (done < 0) {
- if (errno == EINTR)
- continue;
- return (errno);
- }
- data += done;
- size -= done;
- } while (size > 0);
-
- return (0);
-}
-
-int
-proto_common_recv(int sock, unsigned char *data, size_t size)
-{
- ssize_t done;
-
- PJDLOG_ASSERT(sock >= 0);
- PJDLOG_ASSERT(data != NULL);
- PJDLOG_ASSERT(size > 0);
-
- do {
- done = recv(sock, data, size, MSG_WAITALL);
- } while (done == -1 && errno == EINTR);
- if (done == 0)
- return (ENOTCONN);
- else if (done < 0)
- return (errno);
- return (0);
-}
-
-int
-proto_common_descriptor_send(int sock, int fd)
+static int
+proto_descriptor_send(int sock, int fd)
{
unsigned char ctrl[CMSG_SPACE(sizeof(fd))];
struct msghdr msg;
@@ -122,7 +76,37 @@ proto_common_descriptor_send(int sock, int fd)
}
int
-proto_common_descriptor_recv(int sock, int *fdp)
+proto_common_send(int sock, const unsigned char *data, size_t size, int fd)
+{
+ ssize_t done;
+ size_t sendsize;
+
+ PJDLOG_ASSERT(sock >= 0);
+ PJDLOG_ASSERT(data != NULL);
+ PJDLOG_ASSERT(size > 0);
+
+ do {
+ sendsize = size < MAX_SEND_SIZE ? size : MAX_SEND_SIZE;
+ done = send(sock, data, sendsize, MSG_NOSIGNAL);
+ if (done == 0)
+ return (ENOTCONN);
+ else if (done < 0) {
+ if (errno == EINTR)
+ continue;
+ return (errno);
+ }
+ data += done;
+ size -= done;
+ } while (size > 0);
+
+ if (fd == -1)
+ return (0);
+ return (proto_descriptor_send(sock, fd));
+}
+
+#include <stdio.h>
+static int
+proto_descriptor_recv(int sock, int *fdp)
{
unsigned char ctrl[CMSG_SPACE(sizeof(*fdp))];
struct msghdr msg;
@@ -144,10 +128,31 @@ proto_common_descriptor_recv(int sock, int *fdp)
cmsg = CMSG_FIRSTHDR(&msg);
if (cmsg->cmsg_level != SOL_SOCKET ||
- cmsg->cmsg_type == SCM_RIGHTS) {
+ cmsg->cmsg_type != SCM_RIGHTS) {
return (EINVAL);
}
bcopy(CMSG_DATA(cmsg), fdp, sizeof(*fdp));
return (0);
}
+
+int
+proto_common_recv(int sock, unsigned char *data, size_t size, int *fdp)
+{
+ ssize_t done;
+
+ PJDLOG_ASSERT(sock >= 0);
+ PJDLOG_ASSERT(data != NULL);
+ PJDLOG_ASSERT(size > 0);
+
+ do {
+ done = recv(sock, data, size, MSG_WAITALL);
+ } while (done == -1 && errno == EINTR);
+ if (done == 0)
+ return (ENOTCONN);
+ else if (done < 0)
+ return (errno);
+ if (fdp == NULL)
+ return (0);
+ return (proto_descriptor_recv(sock, fdp));
+}
OpenPOWER on IntegriCloud