- 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
This commit is contained in:
Pawel Jakub Dawidek 2011-02-02 15:53:09 +00:00
parent 1c1933226f
commit 01ab52c021
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=218194
7 changed files with 204 additions and 170 deletions

View File

@ -36,6 +36,7 @@ __FBSDID("$FreeBSD$");
#include <errno.h>
#include <stdint.h>
#include <string.h>
#include <strings.h>
#include "pjdlog.h"
@ -251,7 +252,7 @@ proto_send(const struct proto_conn *conn, const void *data, size_t size)
PJDLOG_ASSERT(conn->pc_proto != NULL);
PJDLOG_ASSERT(conn->pc_proto->hp_send != NULL);
ret = conn->pc_proto->hp_send(conn->pc_ctx, data, size);
ret = conn->pc_proto->hp_send(conn->pc_ctx, data, size, -1);
if (ret != 0) {
errno = ret;
return (-1);
@ -269,7 +270,7 @@ proto_recv(const struct proto_conn *conn, void *data, size_t size)
PJDLOG_ASSERT(conn->pc_proto != NULL);
PJDLOG_ASSERT(conn->pc_proto->hp_recv != NULL);
ret = conn->pc_proto->hp_recv(conn->pc_ctx, data, size);
ret = conn->pc_proto->hp_recv(conn->pc_ctx, data, size, NULL);
if (ret != 0) {
errno = ret;
return (-1);
@ -278,16 +279,26 @@ proto_recv(const struct proto_conn *conn, void *data, size_t size)
}
int
proto_descriptor_send(const struct proto_conn *conn, int fd)
proto_connection_send(const struct proto_conn *conn, struct proto_conn *mconn)
{
int ret;
const char *protoname;
int ret, fd;
PJDLOG_ASSERT(conn != NULL);
PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
PJDLOG_ASSERT(conn->pc_proto != NULL);
PJDLOG_ASSERT(conn->pc_proto->hp_descriptor_send != NULL);
PJDLOG_ASSERT(conn->pc_proto->hp_send != NULL);
PJDLOG_ASSERT(mconn != NULL);
PJDLOG_ASSERT(mconn->pc_magic == PROTO_CONN_MAGIC);
PJDLOG_ASSERT(mconn->pc_proto != NULL);
fd = proto_descriptor(mconn);
PJDLOG_ASSERT(fd >= 0);
protoname = mconn->pc_proto->hp_name;
PJDLOG_ASSERT(protoname != NULL);
ret = conn->pc_proto->hp_descriptor_send(conn->pc_ctx, fd);
ret = conn->pc_proto->hp_send(conn->pc_ctx, protoname,
strlen(protoname) + 1, fd);
proto_close(mconn);
if (ret != 0) {
errno = ret;
return (-1);
@ -296,20 +307,54 @@ proto_descriptor_send(const struct proto_conn *conn, int fd)
}
int
proto_descriptor_recv(const struct proto_conn *conn, int *fdp)
proto_connection_recv(const struct proto_conn *conn, bool client,
struct proto_conn **newconnp)
{
int ret;
char protoname[128];
struct hast_proto *proto;
struct proto_conn *newconn;
int ret, fd;
PJDLOG_ASSERT(conn != NULL);
PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
PJDLOG_ASSERT(conn->pc_proto != NULL);
PJDLOG_ASSERT(conn->pc_proto->hp_descriptor_recv != NULL);
PJDLOG_ASSERT(conn->pc_proto->hp_recv != NULL);
PJDLOG_ASSERT(newconnp != NULL);
ret = conn->pc_proto->hp_descriptor_recv(conn->pc_ctx, fdp);
bzero(protoname, sizeof(protoname));
ret = conn->pc_proto->hp_recv(conn->pc_ctx, protoname,
sizeof(protoname) - 1, &fd);
if (ret != 0) {
errno = ret;
return (-1);
}
PJDLOG_ASSERT(fd >= 0);
TAILQ_FOREACH(proto, &protos, hp_next) {
if (strcmp(proto->hp_name, protoname) == 0)
break;
}
if (proto == NULL) {
errno = EINVAL;
return (-1);
}
newconn = proto_alloc(proto,
client ? PROTO_SIDE_CLIENT : PROTO_SIDE_SERVER_WORK);
if (newconn == NULL)
return (-1);
PJDLOG_ASSERT(newconn->pc_proto->hp_wrap != NULL);
ret = newconn->pc_proto->hp_wrap(fd, client, &newconn->pc_ctx);
if (ret != 0) {
proto_free(newconn);
errno = ret;
return (-1);
}
*newconnp = newconn;
return (0);
}

View File

@ -44,8 +44,10 @@ int proto_server(const char *addr, struct proto_conn **connp);
int proto_accept(struct proto_conn *conn, struct proto_conn **newconnp);
int proto_send(const struct proto_conn *conn, const void *data, size_t size);
int proto_recv(const struct proto_conn *conn, void *data, size_t size);
int proto_descriptor_send(const struct proto_conn *conn, int fd);
int proto_descriptor_recv(const struct proto_conn *conn, int *fdp);
int proto_connection_send(const struct proto_conn *conn,
struct proto_conn *mconn);
int proto_connection_recv(const struct proto_conn *conn, bool client,
struct proto_conn **newconnp);
int proto_descriptor(const struct proto_conn *conn);
bool proto_address_match(const struct proto_conn *conn, const char *addr);
void proto_local_address(const struct proto_conn *conn, char *addr,

View File

@ -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));
}

View File

@ -44,10 +44,9 @@ typedef int hp_connect_t(void *, int);
typedef int hp_connect_wait_t(void *, int);
typedef int hp_server_t(const char *, void **);
typedef int hp_accept_t(void *, void **);
typedef int hp_send_t(void *, const unsigned char *, size_t);
typedef int hp_recv_t(void *, unsigned char *, size_t);
typedef int hp_descriptor_send_t(void *, int);
typedef int hp_descriptor_recv_t(void *, int *);
typedef int hp_wrap_t(int, bool, void **);
typedef int hp_send_t(void *, const unsigned char *, size_t, int);
typedef int hp_recv_t(void *, unsigned char *, size_t, int *);
typedef int hp_descriptor_t(const void *);
typedef bool hp_address_match_t(const void *, const char *);
typedef void hp_local_address_t(const void *, char *, size_t);
@ -55,29 +54,26 @@ typedef void hp_remote_address_t(const void *, char *, size_t);
typedef void hp_close_t(void *);
struct hast_proto {
const char *hp_name;
hp_client_t *hp_client;
hp_connect_t *hp_connect;
hp_connect_wait_t *hp_connect_wait;
hp_server_t *hp_server;
hp_accept_t *hp_accept;
hp_send_t *hp_send;
hp_recv_t *hp_recv;
hp_descriptor_send_t *hp_descriptor_send;
hp_descriptor_recv_t *hp_descriptor_recv;
hp_descriptor_t *hp_descriptor;
hp_address_match_t *hp_address_match;
hp_local_address_t *hp_local_address;
hp_remote_address_t *hp_remote_address;
hp_close_t *hp_close;
TAILQ_ENTRY(hast_proto) hp_next;
const char *hp_name;
hp_client_t *hp_client;
hp_connect_t *hp_connect;
hp_connect_wait_t *hp_connect_wait;
hp_server_t *hp_server;
hp_accept_t *hp_accept;
hp_wrap_t *hp_wrap;
hp_send_t *hp_send;
hp_recv_t *hp_recv;
hp_descriptor_t *hp_descriptor;
hp_address_match_t *hp_address_match;
hp_local_address_t *hp_local_address;
hp_remote_address_t *hp_remote_address;
hp_close_t *hp_close;
TAILQ_ENTRY(hast_proto) hp_next;
};
void proto_register(struct hast_proto *proto, bool isdefault);
int proto_common_send(int sock, const unsigned char *data, size_t size);
int proto_common_recv(int sock, unsigned char *data, size_t size);
int proto_common_descriptor_send(int sock, int fd);
int proto_common_descriptor_recv(int sock, int *fdp);
int proto_common_send(int sock, const unsigned char *data, size_t size, int fd);
int proto_common_recv(int sock, unsigned char *data, size_t size, int *fdp);
#endif /* !_PROTO_IMPL_H_ */

View File

@ -83,10 +83,10 @@ sp_client(const char *addr, void **ctxp)
}
static int
sp_send(void *ctx, const unsigned char *data, size_t size)
sp_send(void *ctx, const unsigned char *data, size_t size, int fd)
{
struct sp_ctx *spctx = ctx;
int fd;
int sock;
PJDLOG_ASSERT(spctx != NULL);
PJDLOG_ASSERT(spctx->sp_magic == SP_CTX_MAGIC);
@ -95,7 +95,7 @@ sp_send(void *ctx, const unsigned char *data, size_t size)
case SP_SIDE_UNDEF:
/*
* If the first operation done by the caller is proto_send(),
* we assume this the client.
* we assume this is the client.
*/
/* FALLTHROUGH */
spctx->sp_side = SP_SIDE_CLIENT;
@ -104,11 +104,11 @@ sp_send(void *ctx, const unsigned char *data, size_t size)
spctx->sp_fd[1] = -1;
case SP_SIDE_CLIENT:
PJDLOG_ASSERT(spctx->sp_fd[0] >= 0);
fd = spctx->sp_fd[0];
sock = spctx->sp_fd[0];
break;
case SP_SIDE_SERVER:
PJDLOG_ASSERT(spctx->sp_fd[1] >= 0);
fd = spctx->sp_fd[1];
sock = spctx->sp_fd[1];
break;
default:
PJDLOG_ABORT("Invalid socket side (%d).", spctx->sp_side);
@ -118,11 +118,11 @@ sp_send(void *ctx, const unsigned char *data, size_t size)
if (data == NULL)
return (0);
return (proto_common_send(fd, data, size));
return (proto_common_send(sock, data, size, fd));
}
static int
sp_recv(void *ctx, unsigned char *data, size_t size)
sp_recv(void *ctx, unsigned char *data, size_t size, int *fdp)
{
struct sp_ctx *spctx = ctx;
int fd;
@ -134,7 +134,7 @@ sp_recv(void *ctx, unsigned char *data, size_t size)
case SP_SIDE_UNDEF:
/*
* If the first operation done by the caller is proto_recv(),
* we assume this the server.
* we assume this is the server.
*/
/* FALLTHROUGH */
spctx->sp_side = SP_SIDE_SERVER;
@ -157,35 +157,7 @@ sp_recv(void *ctx, unsigned char *data, size_t size)
if (data == NULL)
return (0);
return (proto_common_recv(fd, data, size));
}
static int
sp_descriptor_send(void *ctx, int fd)
{
struct sp_ctx *spctx = ctx;
PJDLOG_ASSERT(spctx != NULL);
PJDLOG_ASSERT(spctx->sp_magic == SP_CTX_MAGIC);
PJDLOG_ASSERT(spctx->sp_side == SP_SIDE_CLIENT);
PJDLOG_ASSERT(spctx->sp_fd[0] >= 0);
PJDLOG_ASSERT(fd > 0);
return (proto_common_descriptor_send(spctx->sp_fd[0], fd));
}
static int
sp_descriptor_recv(void *ctx, int *fdp)
{
struct sp_ctx *spctx = ctx;
PJDLOG_ASSERT(spctx != NULL);
PJDLOG_ASSERT(spctx->sp_magic == SP_CTX_MAGIC);
PJDLOG_ASSERT(spctx->sp_side == SP_SIDE_SERVER);
PJDLOG_ASSERT(spctx->sp_fd[1] >= 0);
PJDLOG_ASSERT(fdp != NULL);
return (proto_common_descriptor_recv(spctx->sp_fd[1], fdp));
return (proto_common_recv(fd, data, size, fdp));
}
static int
@ -252,8 +224,6 @@ static struct hast_proto sp_proto = {
.hp_client = sp_client,
.hp_send = sp_send,
.hp_recv = sp_recv,
.hp_descriptor_send = sp_descriptor_send,
.hp_descriptor_recv = sp_descriptor_recv,
.hp_descriptor = sp_descriptor,
.hp_close = sp_close
};

View File

@ -168,11 +168,16 @@ tcp4_addr(const char *addr, struct sockaddr_in *sinp)
}
static int
tcp4_common_setup(const char *addr, void **ctxp, int side)
tcp4_setup_new(const char *addr, int side, void **ctxp)
{
struct tcp4_ctx *tctx;
int ret, nodelay;
PJDLOG_ASSERT(addr != NULL);
PJDLOG_ASSERT(side == TCP4_SIDE_CLIENT ||
side == TCP4_SIDE_SERVER_LISTEN);
PJDLOG_ASSERT(ctxp != NULL);
tctx = malloc(sizeof(*tctx));
if (tctx == NULL)
return (errno);
@ -183,6 +188,8 @@ tcp4_common_setup(const char *addr, void **ctxp, int side)
return (ret);
}
PJDLOG_ASSERT(tctx->tc_sin.sin_family != AF_UNSPEC);
tctx->tc_fd = socket(AF_INET, SOCK_STREAM, 0);
if (tctx->tc_fd == -1) {
ret = errno;
@ -194,7 +201,7 @@ tcp4_common_setup(const char *addr, void **ctxp, int side)
nodelay = 1;
if (setsockopt(tctx->tc_fd, IPPROTO_TCP, TCP_NODELAY, &nodelay,
sizeof(nodelay)) == -1) {
pjdlog_warning("Unable to set TCP_NOELAY on %s", addr);
pjdlog_errno(LOG_WARNING, "Unable to set TCP_NOELAY");
}
tctx->tc_side = side;
@ -204,11 +211,34 @@ tcp4_common_setup(const char *addr, void **ctxp, int side)
return (0);
}
static int
tcp4_setup_wrap(int fd, int side, void **ctxp)
{
struct tcp4_ctx *tctx;
PJDLOG_ASSERT(fd >= 0);
PJDLOG_ASSERT(side == TCP4_SIDE_CLIENT ||
side == TCP4_SIDE_SERVER_WORK);
PJDLOG_ASSERT(ctxp != NULL);
tctx = malloc(sizeof(*tctx));
if (tctx == NULL)
return (errno);
tctx->tc_fd = fd;
tctx->tc_sin.sin_family = AF_UNSPEC;
tctx->tc_side = side;
tctx->tc_magic = TCP4_CTX_MAGIC;
*ctxp = tctx;
return (0);
}
static int
tcp4_client(const char *addr, void **ctxp)
{
return (tcp4_common_setup(addr, ctxp, TCP4_SIDE_CLIENT));
return (tcp4_setup_new(addr, TCP4_SIDE_CLIENT, ctxp));
}
static int
@ -338,7 +368,7 @@ tcp4_server(const char *addr, void **ctxp)
struct tcp4_ctx *tctx;
int ret, val;
ret = tcp4_common_setup(addr, ctxp, TCP4_SIDE_SERVER_LISTEN);
ret = tcp4_setup_new(addr, TCP4_SIDE_SERVER_LISTEN, ctxp);
if (ret != 0)
return (ret);
@ -349,6 +379,8 @@ tcp4_server(const char *addr, void **ctxp)
(void)setsockopt(tctx->tc_fd, SOL_SOCKET, SO_REUSEADDR, &val,
sizeof(val));
PJDLOG_ASSERT(tctx->tc_sin.sin_family != AF_UNSPEC);
if (bind(tctx->tc_fd, (struct sockaddr *)&tctx->tc_sin,
sizeof(tctx->tc_sin)) < 0) {
ret = errno;
@ -376,6 +408,7 @@ tcp4_accept(void *ctx, void **newctxp)
PJDLOG_ASSERT(tctx->tc_magic == TCP4_CTX_MAGIC);
PJDLOG_ASSERT(tctx->tc_side == TCP4_SIDE_SERVER_LISTEN);
PJDLOG_ASSERT(tctx->tc_fd >= 0);
PJDLOG_ASSERT(tctx->tc_sin.sin_family != AF_UNSPEC);
newtctx = malloc(sizeof(*newtctx));
if (newtctx == NULL)
@ -398,27 +431,37 @@ tcp4_accept(void *ctx, void **newctxp)
}
static int
tcp4_send(void *ctx, const unsigned char *data, size_t size)
tcp4_wrap(int fd, bool client, void **ctxp)
{
struct tcp4_ctx *tctx = ctx;
PJDLOG_ASSERT(tctx != NULL);
PJDLOG_ASSERT(tctx->tc_magic == TCP4_CTX_MAGIC);
PJDLOG_ASSERT(tctx->tc_fd >= 0);
return (proto_common_send(tctx->tc_fd, data, size));
return (tcp4_setup_wrap(fd,
client ? TCP4_SIDE_CLIENT : TCP4_SIDE_SERVER_WORK, ctxp));
}
static int
tcp4_recv(void *ctx, unsigned char *data, size_t size)
tcp4_send(void *ctx, const unsigned char *data, size_t size, int fd)
{
struct tcp4_ctx *tctx = ctx;
PJDLOG_ASSERT(tctx != NULL);
PJDLOG_ASSERT(tctx->tc_magic == TCP4_CTX_MAGIC);
PJDLOG_ASSERT(tctx->tc_fd >= 0);
PJDLOG_ASSERT(fd == -1);
return (proto_common_recv(tctx->tc_fd, data, size));
return (proto_common_send(tctx->tc_fd, data, size, -1));
}
static int
tcp4_recv(void *ctx, unsigned char *data, size_t size, int *fdp)
{
struct tcp4_ctx *tctx = ctx;
PJDLOG_ASSERT(tctx != NULL);
PJDLOG_ASSERT(tctx->tc_magic == TCP4_CTX_MAGIC);
PJDLOG_ASSERT(tctx->tc_fd >= 0);
PJDLOG_ASSERT(fdp == NULL);
return (proto_common_recv(tctx->tc_fd, data, size, NULL));
}
static int
@ -528,6 +571,7 @@ static struct hast_proto tcp4_proto = {
.hp_connect_wait = tcp4_connect_wait,
.hp_server = tcp4_server,
.hp_accept = tcp4_accept,
.hp_wrap = tcp4_wrap,
.hp_send = tcp4_send,
.hp_recv = tcp4_recv,
.hp_descriptor = tcp4_descriptor,

View File

@ -217,7 +217,7 @@ uds_accept(void *ctx, void **newctxp)
}
static int
uds_send(void *ctx, const unsigned char *data, size_t size)
uds_send(void *ctx, const unsigned char *data, size_t size, int fd)
{
struct uds_ctx *uctx = ctx;
@ -225,11 +225,11 @@ uds_send(void *ctx, const unsigned char *data, size_t size)
PJDLOG_ASSERT(uctx->uc_magic == UDS_CTX_MAGIC);
PJDLOG_ASSERT(uctx->uc_fd >= 0);
return (proto_common_send(uctx->uc_fd, data, size));
return (proto_common_send(uctx->uc_fd, data, size, fd));
}
static int
uds_recv(void *ctx, unsigned char *data, size_t size)
uds_recv(void *ctx, unsigned char *data, size_t size, int *fdp)
{
struct uds_ctx *uctx = ctx;
@ -237,33 +237,7 @@ uds_recv(void *ctx, unsigned char *data, size_t size)
PJDLOG_ASSERT(uctx->uc_magic == UDS_CTX_MAGIC);
PJDLOG_ASSERT(uctx->uc_fd >= 0);
return (proto_common_recv(uctx->uc_fd, data, size));
}
static int
uds_descriptor_send(void *ctx, int fd)
{
struct uds_ctx *uctx = ctx;
PJDLOG_ASSERT(uctx != NULL);
PJDLOG_ASSERT(uctx->uc_magic == UDS_CTX_MAGIC);
PJDLOG_ASSERT(uctx->uc_fd >= 0);
PJDLOG_ASSERT(fd >= 0);
return (proto_common_descriptor_send(uctx->uc_fd, fd));
}
static int
uds_descriptor_recv(void *ctx, int *fdp)
{
struct uds_ctx *uctx = ctx;
PJDLOG_ASSERT(uctx != NULL);
PJDLOG_ASSERT(uctx->uc_magic == UDS_CTX_MAGIC);
PJDLOG_ASSERT(uctx->uc_fd >= 0);
PJDLOG_ASSERT(fdp != NULL);
return (proto_common_descriptor_recv(uctx->uc_fd, fdp));
return (proto_common_recv(uctx->uc_fd, data, size, fdp));
}
static int
@ -349,8 +323,6 @@ static struct hast_proto uds_proto = {
.hp_accept = uds_accept,
.hp_send = uds_send,
.hp_recv = uds_recv,
.hp_descriptor_send = uds_descriptor_send,
.hp_descriptor_recv = uds_descriptor_recv,
.hp_descriptor = uds_descriptor,
.hp_local_address = uds_local_address,
.hp_remote_address = uds_remote_address,