From 3a343b3f6bdf852848b2eb9b7d2f4f3661aa78cd Mon Sep 17 00:00:00 2001 From: Andre Date: Mon, 12 Jun 2006 06:01:55 +0200 Subject: [PATCH] client: introduce client_open() and reorder some functions. --- client.c | 99 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 57 insertions(+), 42 deletions(-) diff --git a/client.c b/client.c index 0380d567..3fc673e7 100644 --- a/client.c +++ b/client.c @@ -51,6 +51,19 @@ INIT_CLIENT_ERRLISTS; static struct private_client_data *pcd; +static void rc4_send(unsigned long len, const unsigned char *indata, + unsigned char *outdata) +{ + RC4(&pcd->rc4_send_key, len, indata, outdata); +} + +static void rc4_recv(unsigned long len, const unsigned char *indata, + unsigned char *outdata) +{ + RC4(&pcd->rc4_recv_key, len, indata, outdata); +} + + /* * client log function */ @@ -78,6 +91,16 @@ static void client_close(struct private_client_data *pcd) free(pcd); } +static void append_str(char **data, const char* append) +{ + if (*data) { + char *tmp = make_message("%s\n%s", *data, append); + free(*data); + *data = tmp; + } else + *data = para_strdup(append); +} + static int client_parse_config(int argc, char *argv[], struct private_client_data **pcd_ptr) { @@ -119,29 +142,6 @@ out: return ret; } -static void rc4_send(unsigned long len, const unsigned char *indata, - unsigned char *outdata) -{ - RC4(&pcd->rc4_send_key, len, indata, outdata); -} - -static void rc4_recv(unsigned long len, const unsigned char *indata, - unsigned char *outdata) -{ - RC4(&pcd->rc4_recv_key, len, indata, outdata); -} - -static void append_str(char **data, const char* append) -{ - if (*data) { - char *tmp = make_message("%s\n%s", *data, append); - free(*data); - *data = tmp; - } else - *data = para_strdup(append); -} - - static int send_stdin(int fd) { char buf[8192]; @@ -159,6 +159,36 @@ static int send_stdin(int fd) return 1; } +static int client_open(struct private_client_data *pcd) +{ + int ret; + struct hostent *he; + struct sockaddr_in their_addr; + + /* get the host info */ + PARA_NOTICE_LOG("getting host info of %s\n", + pcd->conf.hostname_arg); + ret = get_host_info(pcd->conf.hostname_arg, &he); + if (ret < 0) + goto out; + /* get new socket */ + ret = get_socket(); + if (ret < 0) + goto out; + pcd->fd = ret; + /* init their_addr */ + init_sockaddr(&their_addr, pcd->conf.server_port_arg, he); + /* connect */ + PARA_NOTICE_LOG("connecting to %s\n", pcd->conf.hostname_arg); + ret = para_connect(pcd->fd, &their_addr); + if (ret < 0) + goto out; + ret = 1; +out: + return ret; +} + + /* * MAIN */ @@ -190,25 +220,7 @@ int main(int argc, char *argv[]) pcd->conf.hostname_arg, pcd->conf.server_port_arg ); - /* concat args */ - for (i = 0; i < pcd->conf.inputs_num; i++) - append_str(&command, pcd->conf.inputs[i]); - /* get the host info */ - PARA_NOTICE_LOG("getting host info of %s\n", - pcd->conf.hostname_arg); - ret = get_host_info(pcd->conf.hostname_arg, &he); - if (ret < 0) - goto out; - /* get new socket */ - ret = get_socket(); - if (ret < 0) - goto out; - pcd->fd = ret; - /* init their_addr */ - init_sockaddr(&their_addr, pcd->conf.server_port_arg, he); - /* connect */ - PARA_NOTICE_LOG("connecting to %s\n", pcd->conf.hostname_arg); - ret = para_connect(pcd->fd, &their_addr); + ret = client_open(pcd); if (ret < 0) goto out; /* receive welcome message */ @@ -267,6 +279,9 @@ int main(int argc, char *argv[]) rc4_buf[0], rc4_buf[1], rc4_buf[2], rc4_buf[3]); enable_crypt(pcd->fd, rc4_recv, rc4_send); } + /* concat args */ + for (i = 0; i < pcd->conf.inputs_num; i++) + append_str(&command, pcd->conf.inputs[i]); /* send command */ PARA_INFO_LOG("--> %s\n", command); ret = send_buffer(pcd->fd, command); -- 2.39.5