diff --git a/src/server/distrend.c b/src/server/distrend.c --- a/src/server/distrend.c +++ b/src/server/distrend.c @@ -31,6 +31,7 @@ #include "common/execio.h" #include "common/options.h" #include "common/protocol.h" +#include "common/request.h" #include #include @@ -89,7 +90,7 @@ int distrend_handle_request(struct distr /** client request handlers */ -int distrend_handle_version(struct general_info *geninfo, struct distrend_client *client, size_t req_len, void *req_data); +int distrend_handle_version(struct general_info *geninfo, struct distrend_client *client, struct distren_request *req, void *req_data); /* **************XML Functions**************** */ void update_general_info(struct general_info *geninfo); @@ -207,46 +208,48 @@ int main(int argc, char *argv[]) /* ********************** Functions ************************* */ -int distrend_handle_version(struct general_info *geninfo, struct distrend_client *client, size_t req_len, void *req_data) +int distrend_handle_version(struct general_info *geninfo, struct distrend_client *client, struct distren_request *req, void *req_data) { char *tmp_str; - char fixedbuf[32]; + struct distren_request_version version; - struct distren_request *newreq; + if(distren_request_parse_version(req, req_data, &version)) + { + distrend_send_disconnect(client, "Invalid DISTREN_REQUEST_VERSION packet."); + return 1; + } if(client->state != DISTREND_CLIENT_PREVERSION) { distrend_send_disconnect(client, "You have already sent the VERSION command."); + return 1; } - if(strlen(PACKAGE_STRING) == req_len - && !strncmp(PACKAGE_STRING, req_data, req_len)) + if(!strncmp(PACKAGE_STRING, version.package_string, DISTREN_REQUEST_VERSION_PACKAGE_STRING_LEN)) { /** - The client and I claim to be of the same version of distren :-D - Now we will mark the client as valid. - - We won't increment his time to live, though, because it shouldn't take - him that long to auth. - */ + * The client and I claim to be of the same version of distren :-D + * Now we will mark the client as valid. + * + * We won't increment his time to live, though, because it shouldn't take + * him that long to auth. + */ client->state = DISTREND_CLIENT_PREAUTH; - - distren_request_new(&newreq, strlen(PACKAGE_STRING), DISTREN_REQUEST_VERSION); - distrend_client_write_request(client, newreq, PACKAGE_STRING); - distren_request_free(newreq); } else { /** - The client claims to be of a different version of distren. - Now we will just send a disconnect packet and disconnect the client. - */ - strncpy(fixedbuf, req_data, 31); - fixedbuf[31] = '\0'; - if(req_len < 31) - fixedbuf[req_len] = '\0'; - - _distren_asprintf(&tmp_str, "You have tried to connect to a %s server when your client claims to be running %s. Bye ;-)\n", PACKAGE_STRING, fixedbuf); - distrend_send_disconnect(client, tmp_str); + * The client claims to be of a different version of distren. + * Now we will just send a disconnect packet and disconnect the client. + */ + _distren_asprintf(&tmp_str, "You have tried to connect to a %s server when your client claims to be running %s. Bye ;-)\n", PACKAGE_STRING, version.package_string); + if(tmp_str) + { + distrend_send_disconnect(client, tmp_str); + free(tmp_str); + } + else + distrend_send_disconnect(client, "Invalid PACKAGE_VERSION :-|."); + return 1; } return 0;