Skip to content

Commit

Permalink
MINOR : sample for ssl fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
krasnov committed Nov 6, 2008
1 parent 7037741 commit ead798f
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 68 deletions.
8 changes: 4 additions & 4 deletions fd.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ 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 wait_conn(struct vzsock_ctx *ctx, void **conn);
static int accept_conn(struct vzsock_ctx *ctx, void *srv_conn, void **new_conn);
static int close_conn(struct vzsock_ctx *ctx, void *conn);
static int set_conn(struct vzsock_ctx *ctx, void *conn,
Expand Down Expand Up @@ -58,7 +58,7 @@ 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->wait_conn = wait_conn;
handlers->accept_conn = accept_conn;
handlers->close_conn = close_conn;
handlers->set_conn = set_conn;
Expand Down Expand Up @@ -97,12 +97,12 @@ static int open_conn(struct vzsock_ctx *ctx, void *unused, void **conn)

return 0;
}

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

*/
static int accept_conn(struct vzsock_ctx *ctx, void *srv_conn, void **new_conn)
{
return -1;
Expand Down
2 changes: 1 addition & 1 deletion libvzsock.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ 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_wait_conn(struct vzsock_ctx *ctx, void **conn);
int vzsock_accept_conn(struct vzsock_ctx *ctx, void *srv_conn, void **conn);
int vzsock_close_conn(struct vzsock_ctx *ctx, void *conn);
int vzsock_set_conn(struct vzsock_ctx *ctx, void *conn,
Expand Down
8 changes: 7 additions & 1 deletion samples/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int logger(int level, const char *fmt, va_list pvar);
}
*/

int server(struct vzsock_ctx *ctx, void *conn)
int server(struct vzsock_ctx *ctx, void *sock)
{
int rc = 0;
char cmd[BUFSIZ];
Expand All @@ -34,6 +34,12 @@ int server(struct vzsock_ctx *ctx, void *conn)
path,
NULL};
char *p;
void *conn;

if ((rc = vzsock_accept_conn(ctx, sock, &conn))) {
syslog(LOG_ERR, "vzsock_accept_conn() return %d", rc);
return rc;
}

/* read command from client */
if ((rc = vzsock_recv_str(ctx, conn, cmd, sizeof(cmd)))) {
Expand Down
141 changes: 118 additions & 23 deletions samples/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,92 @@
char progname[NAME_MAX];
int debug = 0;

char crtfile[PATH_MAX + 1];
char keyfile[PATH_MAX + 1];
char ciphers[BUFSIZ+1];
char CAfile[PATH_MAX + 1];
char CApath[PATH_MAX + 1];

static void usage()
{
fprintf(stderr, "Virtuozzo vzmigrate daemon\n");
fprintf(stderr, "Usage:\n");
fprintf(stderr, "%s [-v] [-t]\n", progname);
fprintf(stderr, "%s -h\n", progname);
fprintf(stderr," Options:\n");
fprintf(stderr," -h/--help show usage and exit\n");
fprintf(stderr," -v/--verbose be verbose\n");
fprintf(stderr, "Virtuozzo vzmigrate daemon\n" \
"Usage:\n" \
"%s [-v] [-t]\n" \
"%s -h\n" \
" Options:\n" \
" --crtfile <file> load the certificate from file into ssl\n" \
" --keyfile <file> load the private key from file into ssl\n" \
" --ciphers <file> sets the list of available ciphers for ssl\n" \
" See format in ciphers(1)\n" \
" --CAfile <file> load CA trusted certificates from <file>\n" \
" --CApath <path> load CA trusted certificates from files from <path>\n" \
" -v/--verbose be verbose\n" \
" -h/--help show usage and exit\n", progname, progname);
}

static int parse_cmd_line(int argc, char *argv[])
{
int c;
struct option options[] =
{
{"crtfile", required_argument, NULL, '1'},
{"keyfile", required_argument, NULL, '2'},
{"ciphers", required_argument, NULL, '3'},
{"CAfile", required_argument, NULL, '4'},
{"CApath", required_argument, NULL, '5'},
{"verbose", no_argument, NULL, 'v'},
{"help", no_argument, NULL, 'h'},
{ NULL, 0, NULL, 0 }
};

crtfile[0] = '\0';
keyfile[0] = '\0';
ciphers[0] = '\0';
CAfile[0] = '\0';
CApath[0] = '\0';

while (1)
{
c = getopt_long(argc, argv, "vht", options, NULL);
c = getopt_long(argc, argv, "vh1:2:3:4:5:", options, NULL);
if (c == -1)
break;
switch (c)
{
case '1':
if (optarg == NULL) {
usage();
exit(EXIT_FAILURE);
}
strncpy(crtfile, optarg, sizeof(crtfile));
break;
case '2':
if (optarg == NULL) {
usage();
exit(EXIT_FAILURE);
}
strncpy(keyfile, optarg, sizeof(keyfile));
break;
case '3':
if (optarg == NULL) {
usage();
exit(EXIT_FAILURE);
}
strncpy(ciphers, optarg, sizeof(ciphers));
break;
case '4':
if (optarg == NULL) {
usage();
exit(EXIT_FAILURE);
}
strncpy(CAfile, optarg, sizeof(CAfile));
break;
case '5':
if (optarg == NULL) {
usage();
exit(EXIT_FAILURE);
}
strncpy(CApath, optarg, sizeof(CApath));
break;
case 'v':
debug = 1;
break;
Expand All @@ -81,11 +139,11 @@ int main(int argc, char *argv[])
{
int rc = 0;

// int type = VZSOCK_SOCK;
int type = VZSOCK_SSL;
struct vzsock_ctx ctx;
char crtfile[PATH_MAX + 1];
char keyfile[PATH_MAX + 1];
char ciphers[BUFSIZ+1];
void *srv_conn, *conn;
// void *srv_conn, *conn;
int srvsock, sock;

struct sockaddr_in addr;
pid_t pid;
Expand All @@ -95,7 +153,7 @@ int main(int argc, char *argv[])
strncpy(progname, basename(argv[0]), sizeof(progname));
parse_cmd_line(argc, argv);

if ((rc = vzsock_init(VZSOCK_SOCK, &ctx, NULL, NULL))) {
if ((rc = vzsock_init(type, &ctx, NULL, NULL))) {
syslog(LOG_ERR, "vzsock_init() return %d", rc);
return rc;
}
Expand All @@ -122,44 +180,81 @@ int main(int argc, char *argv[])
goto cleanup_0;
}
}
if (strlen(CAfile)) {
if ((rc = vzsock_set(&ctx, VZSOCK_DATA_CAFILE,
(void *)CAfile, strlen(CAfile)))) {
syslog(LOG_ERR, "vzsock_set() return %d", rc);
goto cleanup_0;
}
}
if (strlen(CApath)) {
if ((rc = vzsock_set(&ctx, VZSOCK_DATA_CAFILE,
(void *)CApath, strlen(CApath)))) {
syslog(LOG_ERR, "vzsock_set() return %d", rc);
goto cleanup_0;
}
}
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(VZSOCK_TEST_PORT);
if ((rc = vzsock_set(&ctx, VZSOCK_DATA_ADDR, (void *)&addr, sizeof(addr)))) {
/*
if ((rc = vzsock_set(&ctx, VZSOCK_DATA_ADDR,
(void *)&addr, sizeof(addr))))
{
syslog(LOG_ERR, "vzsock_set() return %d", rc);
goto cleanup_0;
}

*/
if ((rc = vzsock_open(&ctx))) {
syslog(LOG_ERR, "vzsock_open() return %d", rc);
goto cleanup_0;
}

if ((rc = vzsock_wait_conn(&ctx, &srv_conn))) {
syslog(LOG_ERR, "vzsock_wait_conn() return %d", rc);
if ((srvsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
rc = -1;
syslog(LOG_ERR, "socket() : %m");
goto cleanup_0;
}

if (bind(srvsock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
rc = -1;
syslog(LOG_ERR, "bind() : %m");
goto cleanup_1;
}

if (listen(srvsock, SOMAXCONN)) {
rc = -1;
syslog(LOG_ERR, "listen() : %m");
goto cleanup_1;
}

syslog(LOG_INFO, "Started");
while (1) {
if ((rc = vzsock_accept_conn(&ctx, srv_conn, &conn))) {
syslog(LOG_ERR, "vzsock_accept_conn() return %d", rc);
struct sockaddr c_addr;
socklen_t addr_len;

addr_len = sizeof(c_addr);
if ((sock = accept(srvsock,
(struct sockaddr *)&c_addr, &addr_len)) == -1)
{
rc = -1;
syslog(LOG_ERR, "accept() : %m");
goto cleanup_1;
}

pid = fork();
if (pid < 0) {
syslog(LOG_ERR, "fork() : %m");
} else if (pid == 0) {
vzsock_close_conn(&ctx, srv_conn);
rc = server(&ctx, conn);
close(srvsock);
rc = server(&ctx, (void *)&sock);
exit(-rc);
}
vzsock_close_conn(&ctx, conn);
close(sock);
}

cleanup_1:
vzsock_close_conn(&ctx, srv_conn);
close(srvsock);

cleanup_0:
vzsock_close(&ctx);
Expand Down
8 changes: 4 additions & 4 deletions sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ 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 _listen(struct vzsock_ctx *ctx, void **conn);
static int _accept(struct vzsock_ctx *ctx, void *srv_conn, void **conn);
static int close_conn(struct vzsock_ctx *ctx, void *conn);
static int set_conn(struct vzsock_ctx *ctx, void *conn,
Expand Down Expand Up @@ -71,7 +71,7 @@ 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->wait_conn = _listen;
handlers->accept_conn = _accept;
handlers->close_conn = close_conn;
handlers->set_conn = set_conn;
Expand Down Expand Up @@ -179,7 +179,7 @@ static int _connect(struct vzsock_ctx *ctx, void *unused, void **conn)
free((void *)cn);
return rc;
}

/*
static int _listen(struct vzsock_ctx *ctx, void **conn)
{
int rc = 0;
Expand Down Expand Up @@ -216,7 +216,7 @@ static int _listen(struct vzsock_ctx *ctx, void **conn)
free((void *)cn);
return rc;
}

*/
static int _accept(struct vzsock_ctx *ctx, void *srv_conn, void **conn)
{
struct sock_conn *cn;
Expand Down
8 changes: 4 additions & 4 deletions ssh.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ 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 wait_conn(struct vzsock_ctx *ctx, void **conn);
static int accept_conn(struct vzsock_ctx *ctx, void *srv_conn, void **new_conn);
static int close_conn(struct vzsock_ctx *ctx, void *conn);
/* set connection parameter(s) */
Expand Down Expand Up @@ -72,7 +72,7 @@ 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->wait_conn = wait_conn;
handlers->accept_conn = accept_conn;
handlers->close_conn = close_conn;
handlers->set_conn = set_conn;
Expand Down Expand Up @@ -462,12 +462,12 @@ static int open_conn(struct vzsock_ctx *ctx, void *arg, void **conn)

return rc;
}

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

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

0 comments on commit ead798f

Please sign in to comment.