void Message_Destroy(Message *message) { if (message == NULL) return; free(message->t); switch (message->type) { case QPing: break; case QFindNode: Hash_Destroy(message->data.qfindnode.target); break; case QGetPeers: Hash_Destroy(message->data.qgetpeers.info_hash); break; case QAnnouncePeer: Hash_Destroy(message->data.qannouncepeer.info_hash); free(message->data.qannouncepeer.token.data); break; case RPing: break; case RFindNode: free(message->data.rfindnode.nodes); break; case RGetPeers: free(message->data.rgetpeers.token.data); free(message->data.rgetpeers.values); /* TODO: destroy? */ free(message->data.rgetpeers.nodes); break; case RAnnouncePeer: break; case RError: bdestroy(message->data.rerror.message); break; case MUnknown: break; } free(message); }
int SetQueryAnnouncePeerData(Message *message, BNode *arguments) { assert(message != NULL && "NULL Message pointer"); assert(message->type == QAnnouncePeer && "Wrong Message type"); assert(arguments != NULL && "NULL BNode dictionary pointer"); assert(arguments->type == BDictionary && "Not a dictionary"); BNode *info_hash = BNode_GetValue(arguments, "info_hash", 9); if (!IsInfoHashNode(info_hash)) { message->errors |= MERROR_INVALID_DATA; return 0; } BNode *port = BNode_GetValue(arguments, "port", 4); if (port == NULL || port->type != BInteger || port->value.integer < 0 || port->value.integer > 0xffff) { message->errors |= MERROR_INVALID_DATA; return 0; } BNode *token = BNode_GetValue(arguments, "token", 5); if (token == NULL || token->type != BString) { message->errors |= MERROR_INVALID_DATA; return 0; } QAnnouncePeerData *data = &message->data.qannouncepeer; data->info_hash = malloc(HASH_BYTES); check_mem(data->info_hash); memcpy(data->info_hash->value, info_hash->value.string, HASH_BYTES); data->port = port->value.integer; data->token.data = BNode_CopyString(token); check(data->token.data != NULL, "Failed to copy token"); data->token.len = token->count; return 0; error: Hash_Destroy(data->info_hash); return -1; }
int main(int argc, char* argv[]) { FILE * pfWords = stdin; //fopen("words.txt", "rb"); PHashTable pHT = Hash_Create(); char line[2048]; while (fgets(line, sizeof(line), pfWords)) { if (strlen(line)) line[strlen(line) - 1] = '\0'; { char * pTmp; //Collision check if ( (pTmp = Hash_LookupStr(pHT, line)) ) { printf("Collision found!\r\n->\t\"%s\"\r\n->\t\"%s\"\r\n", pTmp, line); } else { char * dup = strrev(mystrdup(line)); Hash_AddStrKey(pHT, line, dup); } } } fprintf(stderr, "Item count: %d\r\n", pHT->count); if (argc > 1) fprintf(stderr, "Lookup item: %s -> %s\r\n", argv[1], Hash_LookupStr(pHT, argv[1])); if (argc > 2) { PHashItem pItem; for (pItem = pHT->pListHead; pItem; pItem = pItem->pNext) puts(pItem->pItem); } Hash_Destroy(pHT); fclose(pfWords); return 0; }