Example #1
0
PTR_MESSAGE generateStartGame(uint8_t type, uint16_t length, char* filename)
{
    PTR_MESSAGE msg;
    
	msg = generateMsg(type, length, filename);
    if(checkStartGame(msg) == true)
    {
       	return msg;
    }
    else
    {
        free(msg);
        return NULL;
    }
}
Example #2
0
PTR_MESSAGE generateQuestionRequest(uint8_t type, uint16_t length, char* content)
{
    PTR_MESSAGE msg;
    
	msg = generateMsg(type, length, content);
    if(checkQuestionRequest(msg) == true)
    {
       	return msg;
    }
    else
    {
        free(msg);
        return NULL;
    }
}
Example #3
0
PTR_MESSAGE generateErrorWarning(uint8_t type, uint16_t length, char* content)
{
	PTR_MESSAGE msg;
    
	msg = generateMsg(type, length, content);
	if(checkErrorWarning(msg) == true)
	{
		return msg;
	}
	else
	{
		free(msg);
		return NULL;
	}
}
Example #4
0
PTR_MESSAGE generateCatalogResponse(uint8_t type, uint16_t length, char* filename)
{
	PTR_MESSAGE msg;

	msg = generateMsg(type, length, filename);
	if(checkCatalogResponse(msg) == true)
	{
		return msg;
	}
	else
	{
		free(msg);
		return NULL;
	}
	return NULL;
}
Example #5
0
PTR_MESSAGE generateLoginResponseOK(uint8_t type, uint16_t length, char* name)
{
	PTR_MESSAGE msg;

	msg = generateMsg(type, length, name);
	if(checkLoginResponseOK(msg) == true)
	{
		return msg;
	}
	else
	{
		free(msg);
		return NULL;
	}
	return NULL;
}
Example #6
0
PTR_MESSAGE generateQuestion(uint8_t type, uint16_t length, char* content)
{
	PTR_MESSAGE msg;
	uint16_t* forHons;
    
	msg = generateMsg(type, length, content);
	if(checkQuestion(msg) == true)
	{
		forHons = (uint16_t *) &msg->content[length-2];
		*forHons = htons(*forHons);
		return msg;
	}
	else
	{
		free(msg);
		return NULL;
	}
}
Example #7
0
int main(int argc, char **argv) {
	Connection con;
	int cnt, ok;
	Message msg, new_msg;
	longnum x, Daemon_y, mdc, new_mdc;
	const char *OurName = "mima";

	/**************  Laden der öffentlichen und privaten Daten  ***************/
	if (!Get_Privat_Key(NULL, &p, &w, &x)
			|| !Get_Public_Key(DAEMON_NAME, &Daemon_y))
		exit(0);
	LSeed(GetCurrentTime());

	/********************  Verbindung zum Dämon aufbauen  *********************/
	//OurName = MakeNetName(NULL); /* gibt in Wirklichkeit Unix-Gruppenname zurück! */
	if (strlen(OverrideNetName) > 0) {
		OurName = OverrideNetName;
	}
	if (!(con = ConnectTo(OurName, DAEMON_NAME))) {
		fprintf(stderr, "Kann keine Verbindung zum Daemon aufbauen: %s\n",
				NET_ErrorText());
		exit(20);
	}

	/***********  Message vom Typ ReportRequest initialisieren  ***************/
	msg.typ = ReportRequest; /* Typ setzten */
	strcpy(msg.body.ReportRequest.Name, OurName); /* Gruppennamen eintragen */
	Generate_MDC(&msg, &p, &mdc); /* MDC generieren ... */
	Generate_Sign(&mdc, &msg.sign_r, &msg.sign_s, &x); /* ... und Nachricht unterschreiben */

	/*************  Nachricht abschicken, Antwort einlesen  *******************/
	if (Transmit(con, &msg, sizeof(msg)) != sizeof(msg)) {
		fprintf(stderr, "Fehler beim Senden des 'ReportRequest': %s\n",
				NET_ErrorText());
		exit(20);
	}

	if (Receive(con, &msg, sizeof(msg)) != sizeof(msg)) {
		fprintf(stderr, "Fehler beim Empfang des 'ReportResponse': %s\n",
				NET_ErrorText());
		exit(20);
	}

	/******************  Überprüfen der Dämon-Signatur  ***********************/
	printf("Nachricht vom Dämon:\n");
	for (cnt = 0; cnt < msg.body.ReportResponse.NumLines; cnt++) {
		printf("\t%s\n", msg.body.ReportResponse.Report[cnt]);
	}

	Generate_MDC(&msg, &p, &mdc);
	ok = Verify_Sign(&mdc, &msg.sign_r, &msg.sign_s, &Daemon_y);
	if (ok)
		printf("Dämon-Signatur ist ok!\n");
	else {
		printf("Dämon-Signatur ist FEHLERHAFT!\n");
		return 0;
	}

	/*>>>>                                      <<<<*
	 *>>>> AUFGABE: Fälschen der Dämon-Signatur <<<<*
	 *>>>>                                      <<<<*/

	printf("Begin zu fälshen:\n");
	memset(&new_msg, 0, sizeof(Message));
	new_msg.typ = VerifyRequest;


	generateMsg(&new_msg);
	toFitMDC(&new_msg, &msg);

	Generate_MDC(&new_msg, &p, &new_mdc);

	if (!LCompare(&mdc, &new_mdc)) {
		printf("Fälschung fertig!\n");

		LCpy(&new_msg.sign_r, &msg.sign_r);
		LCpy(&new_msg.sign_s, &msg.sign_s);
		if (!(con = ConnectTo(OurName, DAEMON_NAME))) {
			fprintf(stderr, "Kann keine Verbindung zum Daemon aufbauen: %s\n",
					NET_ErrorText());
			exit(20);
		}

		if (Transmit(con, &new_msg, sizeof(new_msg)) != sizeof(new_msg)) {
			fprintf(stderr, "Fehler beim Senden des 'VerifyRequest': %s\n",
					NET_ErrorText());
			exit(20);
		}

		if (Receive(con, &msg, sizeof(msg)) != sizeof(msg)) {
			fprintf(stderr, "Fehler beim Empfang des 'VerifyResponse': %s\n",
					NET_ErrorText());
			exit(20);
		}
		printf("Nachricht vom Dämon:\n");
		printf("\t%s\n", msg.body.VerifyResponse.Res);

	} else
		printf("Fälschung gescheitert\n");

	return 0;
}
Example #8
0
/*
 * Méthode qui gère la réception et le traitement des messages reçus côté
 * serveur
 */
void *server_handler(void *client_datas) {
  client_datas_t *datas = (client_datas_t *)client_datas;
  message_parsed_t *message;
  char buffer[MAX_BUFFER_SIZE];
  char response[MAX_BUFFER_SIZE];
  char temp_buffer[MAX_BUFFER_SIZE];
  int i, longueur;

  memset(buffer, 0, MAX_BUFFER_SIZE);
  memset(response, 0, MAX_BUFFER_SIZE);
  memset(temp_buffer, 0, MAX_BUFFER_SIZE);

  for (;;) {

    /* lecture du message rentrant */
    if ((longueur = read(datas->client->socket, buffer, sizeof(buffer))) <= 0) {
      perror("Erreur : impossible de lire le message entrant");
      pthread_exit(0);
    }

    /*on décode le message */
    message = decode(buffer);

    switch (message->code) {
    /* cas d'un message de multicast */
    case Multicast: {
      if (datas->client->is_logged) {
        /* on transmet le message à chaque client */
        for (i = 0; i < datas->array_client->count; i++) {
          write(datas->array_client->clients[i]->socket, message->text,
                strlen(message->text) + 1);
        }
      }
    } break;
    /* cas d'un message de connexion */
    case Login: {
      if (!datas->client->is_logged) {
        /* si le mot de passe est correct */
        if (strcmp(message->password, datas->server_password) == 0) {
          /* set de l'username du client */
          array_client_setName(datas->array_client, datas->client->socket,
                               message->username);
          memcpy(datas->client->username, message->username,
                 strlen(message->username) + 1);

          /* envoi d'un message de type 1 au client pour valider l'échange */
          generateAckLogin(response, 1);
          write(datas->client->socket, response, strlen(response) + 1);

          /* multicast aux autres users pour leur signaler l'arrivée du nouvel
           * user */
          concat(temp_buffer, message->username, " has join the channel");
          memset(response, 0, MAX_BUFFER_SIZE);
          generateMulticast(response, temp_buffer);
          for (i = 0; i < datas->array_client->count; i++) {
            if (datas->array_client->clients[i]->socket !=
                datas->client->socket) {
              write(datas->array_client->clients[i]->socket, response,
                    strlen(response) + 1);
            }
          }

          /* on signale que le client est identifié */
          datas->client->is_logged = 1;
        } else {
          /* envoi d'un message d'erreur au client */
          generateAckLogin(response, 0);
          write(datas->client->socket, response, strlen(response) + 1);
        }
      }
    } break;
    /* cas d'un message de déconnexion */
    case Leave: {
      if (datas->client->is_logged) {
        /* on signale au client q'il peut terminer la connexion de son côté */
        generateLeave(response, "user can leave the channel");
        write(datas->client->socket, response, strlen(response) + 1);

        /* multicast d'un message pour annoncer le départ du client */
        concat(temp_buffer, message->username, " has leave the channel");
        memset(response, 0, MAX_BUFFER_SIZE);
        generateMulticast(response, temp_buffer);

        for (i = 0; i < datas->array_client->count; i++) {
          if (datas->array_client->clients[i]->socket !=
              datas->client->socket) {
            write(datas->array_client->clients[i]->socket, response,
                  strlen(response) + 1);
          }
        }

        /* on termine la connexion avec le client */
        close(datas->client->socket);
        array_client_delete(datas->array_client, datas->client->socket);

        free(datas);
        pthread_exit(0);
      }
    } break;
    /* cas d'un message classique */
    case Say: {
      if (datas->client->is_logged) {
        /* transmission du message aux autres clients */
        generateMsg(response, message->username, message->text);
        for (i = 0; i < datas->array_client->count; i++) {
          write(datas->array_client->clients[i]->socket, response,
                strlen(response) + 1);
        }
      }
    } break;
    /*  cas d'un message de privé */
    case Whisper: {
      if (datas->client->is_logged) {
        /* recherche du destinataire via son username */
        client_t *destinataire = NULL;
        for (i = 0; i < datas->array_client->count; i++) {
          if (strcmp(datas->array_client->clients[i]->username,
                     message->destinataire) == 0) {
            destinataire = datas->array_client->clients[i];
            break;
          }
        }
        /* envoi du message au destinataire si ce dernier existe */
        if (destinataire != NULL) {
          generateWhisp(response, message->username, message->destinataire,
                        message->text);
          write(destinataire->socket, response, strlen(response) + 1);
        } else {
          /* envoi d'un message d'erreur au client */
          generateMulticast(
              response, "Erreur , le destinataire en question n'existe pas");
          write(datas->client->socket, response, strlen(response) + 1);
        }
      }
    } break;
    default: {
      printf("Warning : message entrant non reconnu par le protocole : %s\n",
             buffer);
    } break;
    }
    message_parsed_free(message);
  }
}