]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - client.c
client: introduce client_open()
[paraslash.git] / client.c
index dba99a28749850ed3f797b8859cdbc8de008920d..3fc673e7c6386238b4cada69e2ca2dff3df4c357 100644 (file)
--- a/client.c
+++ b/client.c
@@ -38,6 +38,9 @@ struct private_client_data {
        char *config_file;
        char *key_file;
        char *user;
+       RC4_KEY rc4_recv_key;
+       RC4_KEY rc4_send_key;
+
        char *in_buf;
        size_t *in_loaded;
        char *out_buf;
@@ -48,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
  */
@@ -75,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)
 {
@@ -116,34 +142,6 @@ out:
        return ret;
 }
 
-static RC4_KEY rc4_recv_key;
-static RC4_KEY rc4_send_key;
-static unsigned char rc4_buf[2 * RC4_KEY_LEN];
-
-static void rc4_send(unsigned long len, const unsigned char *indata, unsigned char *outdata)
-{
-       RC4(&rc4_send_key, len, indata, outdata);
-}
-
-static void rc4_recv(unsigned long len, const unsigned char *indata, unsigned char *outdata)
-{
-       RC4(&rc4_recv_key, len, indata, outdata);
-}
-void (*crypt_function_recv)(unsigned long len, const unsigned char *indata, unsigned char *outdata);
-void (*crypt_function_send)(unsigned long len, const unsigned char *indata, unsigned char *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];
@@ -161,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
  */
@@ -174,6 +202,7 @@ int main(int argc, char *argv[])
        char buf[8192];
        char *auth_str;
        long unsigned challenge_nr;
+       unsigned char rc4_buf[2 * RC4_KEY_LEN] = "";
 
        ret = client_parse_config(argc, argv, &pcd);
        if (ret < 0)
@@ -191,27 +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]);
-       crypt_function_recv = NULL;
-       crypt_function_send = NULL;
-       /* 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 */
@@ -264,13 +273,15 @@ int main(int argc, char *argv[])
                        numbytes - PROCEED_MSG_LEN - 1);
                if (ret < 0)
                        goto out;
-               RC4_set_key(&rc4_send_key, RC4_KEY_LEN, rc4_buf);
-               RC4_set_key(&rc4_recv_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
-               PARA_INFO_LOG("rc4 encrytion activated: %x:%x:%x:%x\n",
+               RC4_set_key(&pcd->rc4_send_key, RC4_KEY_LEN, rc4_buf);
+               RC4_set_key(&pcd->rc4_recv_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
+               PARA_INFO_LOG("rc4 encryption activated: %x:%x:%x:%x\n",
                        rc4_buf[0], rc4_buf[1], rc4_buf[2], rc4_buf[3]);
-               crypt_function_recv = rc4_recv;
-               crypt_function_send = rc4_send;
+               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);