Skip to content

Commit

Permalink
MINOR: 'check connection state' and 'get fds' functions added. free()…
Browse files Browse the repository at this point in the history
… for connection adat added.
  • Loading branch information
krasnov committed Nov 13, 2008
1 parent c3e0ef3 commit 02fa79d
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 56 deletions.
61 changes: 53 additions & 8 deletions fd.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ static int open_ctx(struct vzsock_ctx *ctx);
static void close_ctx(struct vzsock_ctx *ctx);
static int set_ctx(struct vzsock_ctx *ctx, int type, void *data, size_t size);
static int open_conn(struct vzsock_ctx *ctx, void *data, void **conn);
//static int wait_conn(struct vzsock_ctx *ctx, void **conn);
static int accept_conn(struct vzsock_ctx *ctx, void *srv_conn, void **new_conn);
static int is_open_conn(void *conn);
static int close_conn(struct vzsock_ctx *ctx, void *conn);
static int set_conn(struct vzsock_ctx *ctx, void *conn,
static int set_conn(struct vzsock_ctx *ctx, void *conn,
int type, void *data, size_t size);
static int get_conn(struct vzsock_ctx *ctx, void *conn,
int type, void *data, size_t *size);
static int _send(
struct vzsock_ctx *ctx,
void *conn,
Expand Down Expand Up @@ -63,10 +65,11 @@ int _vzs_fd_init(struct vzsock_ctx *ctx, struct vzs_handlers *handlers)
handlers->close = close_ctx;
handlers->set = set_ctx;
handlers->open_conn = open_conn;
// handlers->wait_conn = wait_conn;
handlers->accept_conn = accept_conn;
handlers->is_open_conn = is_open_conn;
handlers->close_conn = close_conn;
handlers->set_conn = set_conn;
handlers->get_conn = get_conn;
handlers->send = _send;
handlers->send_err_msg = _send_err_msg;
handlers->recv_str = recv_str;
Expand Down Expand Up @@ -99,23 +102,36 @@ static int open_conn(struct vzsock_ctx *ctx, void *unused, void **conn)

if ((cn = (struct fd_conn *)malloc(sizeof(struct fd_conn))) == NULL)
return _vz_error(ctx, VZS_ERR_SYSTEM, "malloc() : %m");
cn->in = -1;
cn->out = -1;
*conn = cn;

return 0;
}
/*
static int wait_conn(struct vzsock_ctx *ctx, void **conn)

static int accept_conn(struct vzsock_ctx *ctx, void *srv_conn, void **new_conn)
{
return -1;
}
*/
static int accept_conn(struct vzsock_ctx *ctx, void *srv_conn, void **new_conn)

static int is_open_conn(void *conn)
{
return -1;
struct fd_conn *cn = (struct fd_conn *)conn;
struct stat st;

if (conn == NULL)
return 0;
if (fstat(cn->in, &st))
return 0;
if (fstat(cn->out, &st))
return 0;

return 1;
}

static int close_conn(struct vzsock_ctx *ctx, void *conn)
{
free(conn);
return 0;
}

Expand All @@ -141,6 +157,35 @@ static int set_conn(struct vzsock_ctx *ctx, void *conn,
return 0;
}

/* get connection parameter(s) */
static int get_conn(struct vzsock_ctx *ctx, void *conn,
int type, void *data, size_t *size)
{
struct fd_conn *cn = (struct fd_conn *)conn;

switch (type) {
case VZSOCK_DATA_FDPAIR:
{
/* get pair of descriptors */
int fd[2];

if (*size < sizeof(fd))
return _vz_error(ctx, VZS_ERR_BAD_PARAM,
"It is't enough buffer size (%d) "\
"for data type : %d", *size, type);
fd[0] = cn->in;
fd[1] = cn->out;
memcpy(data, (void *)fd, sizeof(fd));
*size = sizeof(fd);
break;
}
default:
return _vz_error(ctx, VZS_ERR_BAD_PARAM,
"Unknown data type : %d", type);
}
return 0;
}

static int _send(
struct vzsock_ctx *ctx,
void *conn,
Expand Down
18 changes: 8 additions & 10 deletions libvzsock.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,34 +77,32 @@ int vzsock_open(struct vzsock_ctx *ctx);
void vzsock_close(struct vzsock_ctx *ctx);
int vzsock_set(struct vzsock_ctx *ctx, int type, void *data, size_t size);
int vzsock_open_conn(struct vzsock_ctx *ctx, void *data, void **conn);
//int vzsock_wait_conn(struct vzsock_ctx *ctx, void **conn);
int vzsock_accept_conn(struct vzsock_ctx *ctx, void *srv_conn, void **conn);
int vzsock_is_open_conn(struct vzsock_ctx *ctx, void *conn);
int vzsock_close_conn(struct vzsock_ctx *ctx, void *conn);
int vzsock_set_conn(struct vzsock_ctx *ctx, void *conn,
int type, void *data, size_t size);
int vzsock_get_conn(struct vzsock_ctx *ctx, void *conn,
int type, void *data, size_t *size);
int vzsock_send(
struct vzsock_ctx *ctx,
void *conn,
const char * data,
struct vzsock_ctx *ctx,
void *conn,
const char * data,
size_t size);
int vzsock_send_err_msg(
struct vzsock_ctx *ctx,
void *conn,
const char * data,
size_t size);
/* read string */
int vzsock_recv_str(
struct vzsock_ctx *ctx,
void *conn,
char *data,
size_t size);
/* read string, separated by <separator>. Will write '\0' on end of string */
int vzsock_recv(
struct vzsock_ctx *ctx,
void *conn,
char separator,
char *data,
size_t size);
#define vzsock_recv_str(ctx, conn, data, size) \
vzsock_recv((ctx), (conn), ('\0'), (data), (size))
/*
To read reply from server(destination) side as |errcode|:replymessage
NOTE: use only on client(source) side
Expand Down
33 changes: 28 additions & 5 deletions sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ static int open_ctx(struct vzsock_ctx *ctx);
static void close_ctx(struct vzsock_ctx *ctx);
static int set_ctx(struct vzsock_ctx *ctx, int type, void *data, size_t size);
static int _connect(struct vzsock_ctx *ctx, void *data, void **conn);
//static int _listen(struct vzsock_ctx *ctx, void **conn);
static int _accept(struct vzsock_ctx *ctx, void *srv_conn, void **conn);
static int is_open_conn(void *conn);
static int close_conn(struct vzsock_ctx *ctx, void *conn);
static int set_conn(struct vzsock_ctx *ctx, void *conn,
int type, void *data, size_t size);
static int get_conn(struct vzsock_ctx *ctx, void *conn,
int type, void *data, size_t *size);
static int _send(
struct vzsock_ctx *ctx,
void *conn,
Expand Down Expand Up @@ -76,10 +78,11 @@ int _vzs_sock_init(struct vzsock_ctx *ctx, struct vzs_handlers *handlers)
handlers->close = close_ctx;
handlers->set = set_ctx;
handlers->open_conn = _connect;
// handlers->wait_conn = _listen;
handlers->accept_conn = _accept;
handlers->is_open_conn = is_open_conn;
handlers->close_conn = close_conn;
handlers->set_conn = set_conn;
handlers->get_conn = get_conn;
handlers->send = _send;
handlers->send_err_msg = _send_err_msg;
handlers->recv_str = recv_str;
Expand Down Expand Up @@ -245,27 +248,47 @@ static int _accept(struct vzsock_ctx *ctx, void *srv_conn, void **conn)
return 0;
}

static int is_open_conn(void *conn)
{
struct sock_conn *cn = (struct sock_conn *)conn;
int opt;
socklen_t opt_len;

if (conn == NULL)
return 0;
opt_len = sizeof(opt);
if (getsockopt(cn->sock, SOL_SOCKET, SO_ERROR, (void *)&opt, &opt_len))
return 0;

return 1;
}

static int close_conn(struct vzsock_ctx *ctx, void *conn)
{
struct sock_conn *cn = (struct sock_conn *)conn;
if (cn->sock == -1)

if (!is_open_conn(conn))
/* already closed */
return 0;

while (close(cn->sock) == -1)
if (errno != EINTR)
break;

cn->sock = -1;
free(conn);

return 0;
}

static int set_conn(struct vzsock_ctx *ctx, void *conn,
int type, void *data, size_t size)
{
// struct sock_conn *cn = (struct sock_conn *)conn;
return 0;
}

static int get_conn(struct vzsock_ctx *ctx, void *conn,
int type, void *data, size_t *size)
{
return 0;
}

Expand Down
64 changes: 53 additions & 11 deletions ssh.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ static void close_ctx(struct vzsock_ctx *ctx);
static int set_ctx(struct vzsock_ctx *ctx, int type, void *data, size_t size);

static int open_conn(struct vzsock_ctx *ctx, void *data, void **conn);
//static int wait_conn(struct vzsock_ctx *ctx, void **conn);
static int accept_conn(struct vzsock_ctx *ctx, void *srv_conn, void **new_conn);
static int is_open_conn(void *conn);
static int close_conn(struct vzsock_ctx *ctx, void *conn);
/* set connection parameter(s) */
static int set_conn(struct vzsock_ctx *ctx, void *conn,
int type, void *data, size_t size);
static int get_conn(struct vzsock_ctx *ctx, void *conn,
int type, void *data, size_t *size);
static int send(
struct vzsock_ctx *ctx,
void *conn,
Expand Down Expand Up @@ -77,10 +79,11 @@ int _vzs_ssh_init(struct vzsock_ctx *ctx, struct vzs_handlers *handlers)
handlers->close = close_ctx;
handlers->set = set_ctx;
handlers->open_conn = open_conn;
// handlers->wait_conn = wait_conn;
handlers->accept_conn = accept_conn;
handlers->is_open_conn = is_open_conn;
handlers->close_conn = close_conn;
handlers->set_conn = set_conn;
handlers->get_conn = get_conn;
handlers->send = send;
handlers->send_err_msg = send_err_msg;
handlers->recv_str = recv_str;
Expand Down Expand Up @@ -468,26 +471,36 @@ static int open_conn(struct vzsock_ctx *ctx, void *arg, void **conn)

return rc;
}
/*
static int wait_conn(struct vzsock_ctx *ctx, void **conn)

static int accept_conn(struct vzsock_ctx *ctx, void *srv_conn, void **new_conn)
{
return -1;
}
*/
static int accept_conn(struct vzsock_ctx *ctx, void *srv_conn, void **new_conn)

static int is_open_conn(void *conn)
{
return -1;
struct ssh_conn *cn = (struct ssh_conn *)conn;

if (conn == NULL)
return 0;
if (cn->pid == 0)
return 0;
if (kill(cn->pid, 0))
return 0;

return 1;
}

static int close_conn(struct vzsock_ctx *ctx, void *conn)
{
struct ssh_conn *cn = (struct ssh_conn *)conn;

if (cn->pid != 0) {
if (!is_open_conn(conn))
return 0;

/* TODO: check retcode and SIGKILL ? */
kill(cn->pid, SIGTERM);
cn->pid = 0;
}
kill(cn->pid, SIGTERM);
free(conn);

return 0;
}
Expand All @@ -514,6 +527,35 @@ static int set_conn(struct vzsock_ctx *ctx, void *conn,
return 0;
}

/* get connection parameter(s) */
static int get_conn(struct vzsock_ctx *ctx, void *conn,
int type, void *data, size_t *size)
{
struct ssh_conn *cn = (struct ssh_conn *)conn;

switch (type) {
case VZSOCK_DATA_FDPAIR:
{
/* get pair of descriptors */
int fd[2];

if (*size < sizeof(fd))
return _vz_error(ctx, VZS_ERR_BAD_PARAM,
"It is't enough buffer size (%d) "\
"for data type : %d", *size, type);
fd[0] = cn->in;
fd[1] = cn->out;
memcpy(data, (void *)fd, sizeof(fd));
*size = sizeof(fd);
break;
}
default:
return _vz_error(ctx, VZS_ERR_BAD_PARAM,
"Unknown data type : %d", type);
}
return 0;
}

static int send(
struct vzsock_ctx *ctx,
void *conn,
Expand Down
Loading

0 comments on commit 02fa79d

Please sign in to comment.