diff --git a/src/client/libdistren.c b/src/client/libdistren.c --- a/src/client/libdistren.c +++ b/src/client/libdistren.c @@ -47,6 +47,9 @@ int distren_init(distren_t *handle) struct distren_request *req; void *data; + const char *username; + const char *pass; + if(!handle) return 1; @@ -64,6 +67,20 @@ int distren_init(distren_t *handle) return 1; } + tmp = remoteio_authinfo_get((*handle)->options->remoteio, + (*handle)->server, + &username, + &pass); + if(tmp + || !username + || !pass) + { + fprintf(stderr, "error: unable to find information necessary to connect to the server named ``%s'', please check your configuration file.\n", + (*handle)->server); + distren_init_cleanup(*handle); + return 1; + } + tmp = remoteio_open_server(&(*handle)->rem, (*handle)->options->remoteio, (remoteio_read_handle_func_t)&libdistren_remoteio_read_handle, @@ -90,9 +107,40 @@ int distren_init(distren_t *handle) distren_request_send((*handle)->rem, req, data); distren_request_free_with_data(req, data); + tmp = distren_request_pass(&req, &data, username, pass); + if(tmp) + { + fprintf(stderr, "error: unable to allocate request"); + + distren_init_cleanup(*handle); + return 1; + } + distren_request_send((*handle)->rem, req, data); + distren_request_free_with_data(req, data); + + /* + * There is no response to the DISTREN_REQUEST_PASS packet. However, + * we want to ensure that the username/password we sent are valid + * before returning to the claler. Thus, we here send a PING + * packet. If we get the DISTREN_REQUEST_PONG, we know we're + * authenticated. Otherwise, we'll receive a quit/discconect packet. + */ + tmp = distren_request_poing(&req, &data, 1, "auth test", strlen("auth test")); + if(tmp) + { + fprintf(stderr, "error: Unable to allocate a DISTREN_REQUEST_PING\n"); + + distren_init_cleanup(*handle); + return 1; + } + distren_request_send((*handle)->rem, req, data); + distren_request_free_with_data(req, data); + + /* flush out the above packets. */ while((*handle)->rem - && (*handle)->state == DISTREN_STATE_VERSION) - multiio_poll((*handle)->multiio, 500); + && ((*handle)->state == DISTREN_STATE_VERSION + || (*handle)->state == DISTREN_STATE_AUTH)) + multiio_poll((*handle)->multiio, 500); if(!(*handle)->rem) {