- 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:
parent
1c1933226f
commit
01ab52c021
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=218194
@ -36,6 +36,7 @@ __FBSDID("$FreeBSD$");
|
|||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
#include <strings.h>
|
#include <strings.h>
|
||||||
|
|
||||||
#include "pjdlog.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 != NULL);
|
||||||
PJDLOG_ASSERT(conn->pc_proto->hp_send != 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) {
|
if (ret != 0) {
|
||||||
errno = ret;
|
errno = ret;
|
||||||
return (-1);
|
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 != NULL);
|
||||||
PJDLOG_ASSERT(conn->pc_proto->hp_recv != 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) {
|
if (ret != 0) {
|
||||||
errno = ret;
|
errno = ret;
|
||||||
return (-1);
|
return (-1);
|
||||||
@ -278,16 +279,26 @@ proto_recv(const struct proto_conn *conn, void *data, size_t size)
|
|||||||
}
|
}
|
||||||
|
|
||||||
int
|
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 != NULL);
|
||||||
PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
|
PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
|
||||||
PJDLOG_ASSERT(conn->pc_proto != NULL);
|
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) {
|
if (ret != 0) {
|
||||||
errno = ret;
|
errno = ret;
|
||||||
return (-1);
|
return (-1);
|
||||||
@ -296,20 +307,54 @@ proto_descriptor_send(const struct proto_conn *conn, int fd)
|
|||||||
}
|
}
|
||||||
|
|
||||||
int
|
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 != NULL);
|
||||||
PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
|
PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
|
||||||
PJDLOG_ASSERT(conn->pc_proto != NULL);
|
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) {
|
if (ret != 0) {
|
||||||
errno = ret;
|
errno = ret;
|
||||||
return (-1);
|
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);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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_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_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_recv(const struct proto_conn *conn, void *data, size_t size);
|
||||||
int proto_descriptor_send(const struct proto_conn *conn, int fd);
|
int proto_connection_send(const struct proto_conn *conn,
|
||||||
int proto_descriptor_recv(const struct proto_conn *conn, int *fdp);
|
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);
|
int proto_descriptor(const struct proto_conn *conn);
|
||||||
bool proto_address_match(const struct proto_conn *conn, const char *addr);
|
bool proto_address_match(const struct proto_conn *conn, const char *addr);
|
||||||
void proto_local_address(const struct proto_conn *conn, char *addr,
|
void proto_local_address(const struct proto_conn *conn, char *addr,
|
||||||
|
@ -45,54 +45,8 @@ __FBSDID("$FreeBSD$");
|
|||||||
#define MAX_SEND_SIZE 32768
|
#define MAX_SEND_SIZE 32768
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int
|
static int
|
||||||
proto_common_send(int sock, const unsigned char *data, size_t size)
|
proto_descriptor_send(int sock, 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);
|
|
||||||
|
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
unsigned char ctrl[CMSG_SPACE(sizeof(fd))];
|
unsigned char ctrl[CMSG_SPACE(sizeof(fd))];
|
||||||
struct msghdr msg;
|
struct msghdr msg;
|
||||||
@ -122,7 +76,37 @@ proto_common_descriptor_send(int sock, int fd)
|
|||||||
}
|
}
|
||||||
|
|
||||||
int
|
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))];
|
unsigned char ctrl[CMSG_SPACE(sizeof(*fdp))];
|
||||||
struct msghdr msg;
|
struct msghdr msg;
|
||||||
@ -144,10 +128,31 @@ proto_common_descriptor_recv(int sock, int *fdp)
|
|||||||
|
|
||||||
cmsg = CMSG_FIRSTHDR(&msg);
|
cmsg = CMSG_FIRSTHDR(&msg);
|
||||||
if (cmsg->cmsg_level != SOL_SOCKET ||
|
if (cmsg->cmsg_level != SOL_SOCKET ||
|
||||||
cmsg->cmsg_type == SCM_RIGHTS) {
|
cmsg->cmsg_type != SCM_RIGHTS) {
|
||||||
return (EINVAL);
|
return (EINVAL);
|
||||||
}
|
}
|
||||||
bcopy(CMSG_DATA(cmsg), fdp, sizeof(*fdp));
|
bcopy(CMSG_DATA(cmsg), fdp, sizeof(*fdp));
|
||||||
|
|
||||||
return (0);
|
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));
|
||||||
|
}
|
||||||
|
@ -44,10 +44,9 @@ typedef int hp_connect_t(void *, int);
|
|||||||
typedef int hp_connect_wait_t(void *, int);
|
typedef int hp_connect_wait_t(void *, int);
|
||||||
typedef int hp_server_t(const char *, void **);
|
typedef int hp_server_t(const char *, void **);
|
||||||
typedef int hp_accept_t(void *, void **);
|
typedef int hp_accept_t(void *, void **);
|
||||||
typedef int hp_send_t(void *, const unsigned char *, size_t);
|
typedef int hp_wrap_t(int, bool, void **);
|
||||||
typedef int hp_recv_t(void *, unsigned char *, size_t);
|
typedef int hp_send_t(void *, const unsigned char *, size_t, int);
|
||||||
typedef int hp_descriptor_send_t(void *, int);
|
typedef int hp_recv_t(void *, unsigned char *, size_t, int *);
|
||||||
typedef int hp_descriptor_recv_t(void *, int *);
|
|
||||||
typedef int hp_descriptor_t(const void *);
|
typedef int hp_descriptor_t(const void *);
|
||||||
typedef bool hp_address_match_t(const void *, const char *);
|
typedef bool hp_address_match_t(const void *, const char *);
|
||||||
typedef void hp_local_address_t(const void *, char *, size_t);
|
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 *);
|
typedef void hp_close_t(void *);
|
||||||
|
|
||||||
struct hast_proto {
|
struct hast_proto {
|
||||||
const char *hp_name;
|
const char *hp_name;
|
||||||
hp_client_t *hp_client;
|
hp_client_t *hp_client;
|
||||||
hp_connect_t *hp_connect;
|
hp_connect_t *hp_connect;
|
||||||
hp_connect_wait_t *hp_connect_wait;
|
hp_connect_wait_t *hp_connect_wait;
|
||||||
hp_server_t *hp_server;
|
hp_server_t *hp_server;
|
||||||
hp_accept_t *hp_accept;
|
hp_accept_t *hp_accept;
|
||||||
hp_send_t *hp_send;
|
hp_wrap_t *hp_wrap;
|
||||||
hp_recv_t *hp_recv;
|
hp_send_t *hp_send;
|
||||||
hp_descriptor_send_t *hp_descriptor_send;
|
hp_recv_t *hp_recv;
|
||||||
hp_descriptor_recv_t *hp_descriptor_recv;
|
hp_descriptor_t *hp_descriptor;
|
||||||
hp_descriptor_t *hp_descriptor;
|
hp_address_match_t *hp_address_match;
|
||||||
hp_address_match_t *hp_address_match;
|
hp_local_address_t *hp_local_address;
|
||||||
hp_local_address_t *hp_local_address;
|
hp_remote_address_t *hp_remote_address;
|
||||||
hp_remote_address_t *hp_remote_address;
|
hp_close_t *hp_close;
|
||||||
hp_close_t *hp_close;
|
TAILQ_ENTRY(hast_proto) hp_next;
|
||||||
TAILQ_ENTRY(hast_proto) hp_next;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
void proto_register(struct hast_proto *proto, bool isdefault);
|
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_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 proto_common_recv(int sock, unsigned char *data, size_t size, int *fdp);
|
||||||
int proto_common_descriptor_send(int sock, int fd);
|
|
||||||
int proto_common_descriptor_recv(int sock, int *fdp);
|
|
||||||
|
|
||||||
#endif /* !_PROTO_IMPL_H_ */
|
#endif /* !_PROTO_IMPL_H_ */
|
||||||
|
@ -83,10 +83,10 @@ sp_client(const char *addr, void **ctxp)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int
|
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;
|
struct sp_ctx *spctx = ctx;
|
||||||
int fd;
|
int sock;
|
||||||
|
|
||||||
PJDLOG_ASSERT(spctx != NULL);
|
PJDLOG_ASSERT(spctx != NULL);
|
||||||
PJDLOG_ASSERT(spctx->sp_magic == SP_CTX_MAGIC);
|
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:
|
case SP_SIDE_UNDEF:
|
||||||
/*
|
/*
|
||||||
* If the first operation done by the caller is proto_send(),
|
* If the first operation done by the caller is proto_send(),
|
||||||
* we assume this the client.
|
* we assume this is the client.
|
||||||
*/
|
*/
|
||||||
/* FALLTHROUGH */
|
/* FALLTHROUGH */
|
||||||
spctx->sp_side = SP_SIDE_CLIENT;
|
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;
|
spctx->sp_fd[1] = -1;
|
||||||
case SP_SIDE_CLIENT:
|
case SP_SIDE_CLIENT:
|
||||||
PJDLOG_ASSERT(spctx->sp_fd[0] >= 0);
|
PJDLOG_ASSERT(spctx->sp_fd[0] >= 0);
|
||||||
fd = spctx->sp_fd[0];
|
sock = spctx->sp_fd[0];
|
||||||
break;
|
break;
|
||||||
case SP_SIDE_SERVER:
|
case SP_SIDE_SERVER:
|
||||||
PJDLOG_ASSERT(spctx->sp_fd[1] >= 0);
|
PJDLOG_ASSERT(spctx->sp_fd[1] >= 0);
|
||||||
fd = spctx->sp_fd[1];
|
sock = spctx->sp_fd[1];
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
PJDLOG_ABORT("Invalid socket side (%d).", spctx->sp_side);
|
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)
|
if (data == NULL)
|
||||||
return (0);
|
return (0);
|
||||||
|
|
||||||
return (proto_common_send(fd, data, size));
|
return (proto_common_send(sock, data, size, fd));
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
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;
|
struct sp_ctx *spctx = ctx;
|
||||||
int fd;
|
int fd;
|
||||||
@ -134,7 +134,7 @@ sp_recv(void *ctx, unsigned char *data, size_t size)
|
|||||||
case SP_SIDE_UNDEF:
|
case SP_SIDE_UNDEF:
|
||||||
/*
|
/*
|
||||||
* If the first operation done by the caller is proto_recv(),
|
* If the first operation done by the caller is proto_recv(),
|
||||||
* we assume this the server.
|
* we assume this is the server.
|
||||||
*/
|
*/
|
||||||
/* FALLTHROUGH */
|
/* FALLTHROUGH */
|
||||||
spctx->sp_side = SP_SIDE_SERVER;
|
spctx->sp_side = SP_SIDE_SERVER;
|
||||||
@ -157,35 +157,7 @@ sp_recv(void *ctx, unsigned char *data, size_t size)
|
|||||||
if (data == NULL)
|
if (data == NULL)
|
||||||
return (0);
|
return (0);
|
||||||
|
|
||||||
return (proto_common_recv(fd, data, size));
|
return (proto_common_recv(fd, data, size, fdp));
|
||||||
}
|
|
||||||
|
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
@ -252,8 +224,6 @@ static struct hast_proto sp_proto = {
|
|||||||
.hp_client = sp_client,
|
.hp_client = sp_client,
|
||||||
.hp_send = sp_send,
|
.hp_send = sp_send,
|
||||||
.hp_recv = sp_recv,
|
.hp_recv = sp_recv,
|
||||||
.hp_descriptor_send = sp_descriptor_send,
|
|
||||||
.hp_descriptor_recv = sp_descriptor_recv,
|
|
||||||
.hp_descriptor = sp_descriptor,
|
.hp_descriptor = sp_descriptor,
|
||||||
.hp_close = sp_close
|
.hp_close = sp_close
|
||||||
};
|
};
|
||||||
|
@ -168,11 +168,16 @@ tcp4_addr(const char *addr, struct sockaddr_in *sinp)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int
|
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;
|
struct tcp4_ctx *tctx;
|
||||||
int ret, nodelay;
|
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));
|
tctx = malloc(sizeof(*tctx));
|
||||||
if (tctx == NULL)
|
if (tctx == NULL)
|
||||||
return (errno);
|
return (errno);
|
||||||
@ -183,6 +188,8 @@ tcp4_common_setup(const char *addr, void **ctxp, int side)
|
|||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PJDLOG_ASSERT(tctx->tc_sin.sin_family != AF_UNSPEC);
|
||||||
|
|
||||||
tctx->tc_fd = socket(AF_INET, SOCK_STREAM, 0);
|
tctx->tc_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (tctx->tc_fd == -1) {
|
if (tctx->tc_fd == -1) {
|
||||||
ret = errno;
|
ret = errno;
|
||||||
@ -194,7 +201,7 @@ tcp4_common_setup(const char *addr, void **ctxp, int side)
|
|||||||
nodelay = 1;
|
nodelay = 1;
|
||||||
if (setsockopt(tctx->tc_fd, IPPROTO_TCP, TCP_NODELAY, &nodelay,
|
if (setsockopt(tctx->tc_fd, IPPROTO_TCP, TCP_NODELAY, &nodelay,
|
||||||
sizeof(nodelay)) == -1) {
|
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;
|
tctx->tc_side = side;
|
||||||
@ -204,11 +211,34 @@ tcp4_common_setup(const char *addr, void **ctxp, int side)
|
|||||||
return (0);
|
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
|
static int
|
||||||
tcp4_client(const char *addr, void **ctxp)
|
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
|
static int
|
||||||
@ -338,7 +368,7 @@ tcp4_server(const char *addr, void **ctxp)
|
|||||||
struct tcp4_ctx *tctx;
|
struct tcp4_ctx *tctx;
|
||||||
int ret, val;
|
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)
|
if (ret != 0)
|
||||||
return (ret);
|
return (ret);
|
||||||
|
|
||||||
@ -349,6 +379,8 @@ tcp4_server(const char *addr, void **ctxp)
|
|||||||
(void)setsockopt(tctx->tc_fd, SOL_SOCKET, SO_REUSEADDR, &val,
|
(void)setsockopt(tctx->tc_fd, SOL_SOCKET, SO_REUSEADDR, &val,
|
||||||
sizeof(val));
|
sizeof(val));
|
||||||
|
|
||||||
|
PJDLOG_ASSERT(tctx->tc_sin.sin_family != AF_UNSPEC);
|
||||||
|
|
||||||
if (bind(tctx->tc_fd, (struct sockaddr *)&tctx->tc_sin,
|
if (bind(tctx->tc_fd, (struct sockaddr *)&tctx->tc_sin,
|
||||||
sizeof(tctx->tc_sin)) < 0) {
|
sizeof(tctx->tc_sin)) < 0) {
|
||||||
ret = errno;
|
ret = errno;
|
||||||
@ -376,6 +408,7 @@ tcp4_accept(void *ctx, void **newctxp)
|
|||||||
PJDLOG_ASSERT(tctx->tc_magic == TCP4_CTX_MAGIC);
|
PJDLOG_ASSERT(tctx->tc_magic == TCP4_CTX_MAGIC);
|
||||||
PJDLOG_ASSERT(tctx->tc_side == TCP4_SIDE_SERVER_LISTEN);
|
PJDLOG_ASSERT(tctx->tc_side == TCP4_SIDE_SERVER_LISTEN);
|
||||||
PJDLOG_ASSERT(tctx->tc_fd >= 0);
|
PJDLOG_ASSERT(tctx->tc_fd >= 0);
|
||||||
|
PJDLOG_ASSERT(tctx->tc_sin.sin_family != AF_UNSPEC);
|
||||||
|
|
||||||
newtctx = malloc(sizeof(*newtctx));
|
newtctx = malloc(sizeof(*newtctx));
|
||||||
if (newtctx == NULL)
|
if (newtctx == NULL)
|
||||||
@ -398,27 +431,37 @@ tcp4_accept(void *ctx, void **newctxp)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int
|
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);
|
return (tcp4_setup_wrap(fd,
|
||||||
PJDLOG_ASSERT(tctx->tc_magic == TCP4_CTX_MAGIC);
|
client ? TCP4_SIDE_CLIENT : TCP4_SIDE_SERVER_WORK, ctxp));
|
||||||
PJDLOG_ASSERT(tctx->tc_fd >= 0);
|
|
||||||
|
|
||||||
return (proto_common_send(tctx->tc_fd, data, size));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
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;
|
struct tcp4_ctx *tctx = ctx;
|
||||||
|
|
||||||
PJDLOG_ASSERT(tctx != NULL);
|
PJDLOG_ASSERT(tctx != NULL);
|
||||||
PJDLOG_ASSERT(tctx->tc_magic == TCP4_CTX_MAGIC);
|
PJDLOG_ASSERT(tctx->tc_magic == TCP4_CTX_MAGIC);
|
||||||
PJDLOG_ASSERT(tctx->tc_fd >= 0);
|
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
|
static int
|
||||||
@ -528,6 +571,7 @@ static struct hast_proto tcp4_proto = {
|
|||||||
.hp_connect_wait = tcp4_connect_wait,
|
.hp_connect_wait = tcp4_connect_wait,
|
||||||
.hp_server = tcp4_server,
|
.hp_server = tcp4_server,
|
||||||
.hp_accept = tcp4_accept,
|
.hp_accept = tcp4_accept,
|
||||||
|
.hp_wrap = tcp4_wrap,
|
||||||
.hp_send = tcp4_send,
|
.hp_send = tcp4_send,
|
||||||
.hp_recv = tcp4_recv,
|
.hp_recv = tcp4_recv,
|
||||||
.hp_descriptor = tcp4_descriptor,
|
.hp_descriptor = tcp4_descriptor,
|
||||||
|
@ -217,7 +217,7 @@ uds_accept(void *ctx, void **newctxp)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int
|
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;
|
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_magic == UDS_CTX_MAGIC);
|
||||||
PJDLOG_ASSERT(uctx->uc_fd >= 0);
|
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
|
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;
|
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_magic == UDS_CTX_MAGIC);
|
||||||
PJDLOG_ASSERT(uctx->uc_fd >= 0);
|
PJDLOG_ASSERT(uctx->uc_fd >= 0);
|
||||||
|
|
||||||
return (proto_common_recv(uctx->uc_fd, data, size));
|
return (proto_common_recv(uctx->uc_fd, data, size, fdp));
|
||||||
}
|
|
||||||
|
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
@ -349,8 +323,6 @@ static struct hast_proto uds_proto = {
|
|||||||
.hp_accept = uds_accept,
|
.hp_accept = uds_accept,
|
||||||
.hp_send = uds_send,
|
.hp_send = uds_send,
|
||||||
.hp_recv = uds_recv,
|
.hp_recv = uds_recv,
|
||||||
.hp_descriptor_send = uds_descriptor_send,
|
|
||||||
.hp_descriptor_recv = uds_descriptor_recv,
|
|
||||||
.hp_descriptor = uds_descriptor,
|
.hp_descriptor = uds_descriptor,
|
||||||
.hp_local_address = uds_local_address,
|
.hp_local_address = uds_local_address,
|
||||||
.hp_remote_address = uds_remote_address,
|
.hp_remote_address = uds_remote_address,
|
||||||
|
Loading…
Reference in New Issue
Block a user