📄 namespace.c
字号:
int), &ksb->sblock, &ksb->kblock. signature)); /* extra check: verify sig */ GNUNET_RSA_free_key (pk); if (GNUNET_OK != GNUNET_FS_insert (sock, knvalue)) { GNUNET_GE_BREAK (ectx, 0); GNUNET_free (cpy); GNUNET_free (knvalue); GNUNET_free (value); GNUNET_client_connection_destroy (sock); GNUNET_RSA_free_key (namespace_priv_key); return NULL; } /* restore nblock to avoid re-encryption! */ memcpy (&ksb->sblock, cpy, size - sizeof (GNUNET_EC_KBlock) - sizeof (unsigned int)); } GNUNET_free (cpy); } rootURI = GNUNET_malloc (sizeof (URI)); rootURI->type = sks; GNUNET_hash (&sb->subspace, sizeof (GNUNET_RSA_PublicKey), &rootURI->data.sks.namespace); rootURI->data.sks.identifier = GNUNET_strdup (rootEntry); GNUNET_free (knvalue); GNUNET_free (value); GNUNET_client_connection_destroy (sock); GNUNET_RSA_free_key (namespace_priv_key); return rootURI;}static struct GNUNET_RSA_PrivateKey *read_namespace_key (struct GNUNET_GC_Configuration *cfg, const GNUNET_HashCode * pid){ char *fileName; GNUNET_RSA_PrivateKeyEncoded *hke; struct GNUNET_RSA_PrivateKey *hk; char *dst; unsigned long long len; fileName = getPseudonymFileName (NULL, cfg, pid); if (GNUNET_OK != GNUNET_disk_file_size (NULL, fileName, &len, GNUNET_YES)) { GNUNET_free (fileName); return NULL; } if (len < 2) { GNUNET_GE_LOG (NULL, GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER, _("File `%s' does not contain a pseudonym.\n"), fileName); GNUNET_free (fileName); return NULL; } dst = GNUNET_malloc (len); len = GNUNET_disk_file_read (NULL, fileName, len, dst); hke = (GNUNET_RSA_PrivateKeyEncoded *) dst; if (ntohs (hke->len) != len) { GNUNET_GE_LOG (NULL, GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER, _("Format of pseudonym `%s' is invalid.\n"), fileName); GNUNET_free (fileName); GNUNET_free (hke); return NULL; } GNUNET_free (fileName); hk = GNUNET_RSA_decode_key (hke); GNUNET_free (hke); return hk;}/** * Add an entry into a namespace. * * @param dstU to which URI should the namespace entry refer? * @param md what meta-data should be associated with the * entry? * @param thisId name of this entry in the namespace (keyword/identifier) * @param nextId name of the update for this entry (to be published in * the future; maybe NULL) * @param pid unique identifier of the namespace/pseudonym * @return URI on success, NULL on error */struct GNUNET_ECRS_URI *GNUNET_ECRS_namespace_add_content (struct GNUNET_GE_Context *ectx, struct GNUNET_GC_Configuration *cfg, const GNUNET_HashCode * pid, unsigned int anonymityLevel, unsigned int priority, GNUNET_CronTime expiration, const char *thisId, const char *nextId, const struct GNUNET_ECRS_URI *dstU, const struct GNUNET_MetaData *md){ struct GNUNET_ECRS_URI *uri; struct GNUNET_ClientServerConnection *sock; GNUNET_DatastoreValue *value; unsigned int size; unsigned int mdsize; struct GNUNET_RSA_PrivateKey *hk; GNUNET_EC_SBlock *sb; char *dstURI; char *destPos; GNUNET_HashCode hc; /* hash of thisId = key */ GNUNET_HashCode hc2; /* hash of hc = identifier */ int ret; unsigned int nidlen; hk = read_namespace_key (cfg, pid); if (hk == NULL) return NULL; /* THEN: construct GNUNET_EC_SBlock */ dstURI = GNUNET_ECRS_uri_to_string (dstU); mdsize = GNUNET_meta_data_get_serialized_size (md, GNUNET_SERIALIZE_PART); if (nextId == NULL) nextId = ""; nidlen = strlen (nextId) + 1; size = mdsize + sizeof (GNUNET_EC_SBlock) + strlen (dstURI) + 1 + nidlen; if (size > MAX_SBLOCK_SIZE) { size = MAX_SBLOCK_SIZE; mdsize = size - (sizeof (GNUNET_EC_SBlock) + strlen (dstURI) + 1 + nidlen); } value = GNUNET_malloc (sizeof (GNUNET_DatastoreValue) + size); sb = (GNUNET_EC_SBlock *) & value[1]; sb->type = htonl (GNUNET_ECRS_BLOCKTYPE_SIGNED); destPos = (char *) &sb[1]; memcpy (destPos, nextId, nidlen); destPos += nidlen; memcpy (destPos, dstURI, strlen (dstURI) + 1); destPos += strlen (dstURI) + 1; mdsize = GNUNET_meta_data_serialize (ectx, md, destPos, mdsize, GNUNET_SERIALIZE_PART); if (mdsize == -1) { GNUNET_GE_BREAK (ectx, 0); GNUNET_free (dstURI); GNUNET_RSA_free_key (hk); GNUNET_free (value); return NULL; } size = sizeof (GNUNET_EC_SBlock) + mdsize + strlen (dstURI) + 1 + nidlen; value->size = htonl (sizeof (GNUNET_DatastoreValue) + size); value->type = htonl (GNUNET_ECRS_BLOCKTYPE_SIGNED); value->priority = htonl (priority); value->anonymity_level = htonl (anonymityLevel); value->expiration_time = GNUNET_htonll (expiration); GNUNET_hash (thisId, strlen (thisId), &hc); GNUNET_hash (&hc, sizeof (GNUNET_HashCode), &hc2); uri = GNUNET_malloc (sizeof (URI)); uri->type = sks; GNUNET_RSA_get_public_key (hk, &sb->subspace); GNUNET_hash (&sb->subspace, sizeof (GNUNET_RSA_PublicKey), &uri->data.sks.namespace); GNUNET_GE_BREAK (ectx, 0 == memcmp (&uri->data.sks.namespace, pid, sizeof (GNUNET_HashCode))); uri->data.sks.identifier = GNUNET_strdup (thisId); GNUNET_hash_xor (&hc2, &uri->data.sks.namespace, &sb->identifier); GNUNET_ECRS_encryptInPlace (&hc, &sb[1], size - sizeof (GNUNET_EC_SBlock)); GNUNET_GE_ASSERT (ectx, GNUNET_OK == GNUNET_RSA_sign (hk, size - sizeof (GNUNET_RSA_Signature) - sizeof (GNUNET_RSA_PublicKey) - sizeof (unsigned int), &sb->identifier, &sb->signature)); GNUNET_RSA_free_key (hk); sock = GNUNET_client_connection_create (ectx, cfg); ret = GNUNET_FS_insert (sock, value); if (ret != GNUNET_OK) { GNUNET_free (uri); uri = NULL; } GNUNET_client_connection_destroy (sock); GNUNET_free (value); GNUNET_free (dstURI); return uri;}struct lNCLS{ struct GNUNET_GE_Context *ectx; struct GNUNET_GC_Configuration *cfg; GNUNET_ECRS_NamespaceInfoProcessor cb; void *cls; int cnt;};static intprocessFile_ (const char *name, const char *dirName, void *cls){ struct lNCLS *c = cls; struct GNUNET_RSA_PrivateKey *hk; char *fileName; GNUNET_RSA_PrivateKeyEncoded *hke; char *dst; unsigned long long len; GNUNET_HashCode namespace; GNUNET_RSA_PublicKey pk; GNUNET_HashCode pid; if (GNUNET_OK != GNUNET_enc_to_hash (name, &pid)) return GNUNET_OK; /* ignore */ fileName = getPseudonymFileName (c->ectx, c->cfg, &pid); if (GNUNET_OK != GNUNET_disk_file_size (c->ectx, fileName, &len, GNUNET_YES)) { GNUNET_free (fileName); return GNUNET_OK; } if (len < 2) { GNUNET_GE_LOG (c->ectx, GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER, _ ("File `%s' does not contain a pseudonym, trying to remove.\n"), fileName); UNLINK (fileName); GNUNET_free (fileName); return GNUNET_OK; } dst = GNUNET_malloc (len); len = GNUNET_disk_file_read (c->ectx, fileName, len, dst); hke = (GNUNET_RSA_PrivateKeyEncoded *) dst; if (ntohs (hke->len) != len) { GNUNET_GE_LOG (c->ectx, GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER, _("Format of file `%s' is invalid.\n"), fileName); GNUNET_free (hke); GNUNET_free (fileName); return GNUNET_OK; } hk = GNUNET_RSA_decode_key (hke); GNUNET_free (hke); if (hk == NULL) { GNUNET_GE_LOG (c->ectx, GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER, _("Format of file `%s' is invalid, trying to remove.\n"), fileName); UNLINK (fileName); GNUNET_free (fileName); GNUNET_GE_BREAK (c->ectx, 0); return GNUNET_SYSERR; } GNUNET_free (fileName); GNUNET_RSA_get_public_key (hk, &pk); GNUNET_RSA_free_key (hk); GNUNET_hash (&pk, sizeof (GNUNET_RSA_PublicKey), &namespace); if (NULL != c->cb) { if (GNUNET_OK == c->cb (&namespace, name, c->cls)) c->cnt++; else c->cnt = GNUNET_SYSERR; } else c->cnt++; return GNUNET_OK;}/** * Build a list of all available namespaces * * @param list where to store the names (is allocated, caller frees) * @return GNUNET_SYSERR on error, otherwise the number of pseudonyms in list */intGNUNET_ECRS_get_namespaces (struct GNUNET_GE_Context *ectx, struct GNUNET_GC_Configuration *cfg, GNUNET_ECRS_NamespaceInfoProcessor cb, void *cls){ char *dirName; struct lNCLS myCLS; myCLS.cls = cls; myCLS.cb = cb; myCLS.cnt = 0; myCLS.ectx = ectx; myCLS.cfg = cfg; dirName = getPseudonymFileName (ectx, cfg, NULL); GNUNET_disk_directory_scan (ectx, dirName, &processFile_, &myCLS); GNUNET_free (dirName); return myCLS.cnt;}/* end of namespace.c */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -