/** * Create a hello-Message for the current node. The hello is created * without signature and without a timestamp. The GNUnet core will * GNUNET_RSA_sign the message and add an expiration time. * * @return hello on success, NULL on error */ static GNUNET_MessageHello * api_create_hello () { GNUNET_MessageHello *msg; char *filter; EmailAddress *haddr; int i; GNUNET_GC_get_configuration_value_string (core_api->cfg, "SMTP", "FILTER", "X-mailer: GNUnet", &filter); if (NULL == strstr (filter, ": ")) { GNUNET_GE_LOG (ectx, GNUNET_GE_WARNING | GNUNET_GE_BULK | GNUNET_GE_USER, _("SMTP filter string to invalid, lacks ': '\n")); GNUNET_free (filter); return NULL; } if (strlen (filter) > FILTER_STRING_SIZE) { filter[FILTER_STRING_SIZE] = '\0'; GNUNET_GE_LOG (ectx, GNUNET_GE_WARNING | GNUNET_GE_BULK | GNUNET_GE_USER, _("SMTP filter string to long, capped to `%s'\n"), filter); } i = (strlen (email) + 8) & (~7); /* make multiple of 8 */ msg = GNUNET_malloc (sizeof (GNUNET_MessageHello) + sizeof (EmailAddress) + i); memset (msg, 0, sizeof (GNUNET_MessageHello) + sizeof (EmailAddress) + i); haddr = (EmailAddress *) &msg[1]; memset (&haddr->filter[0], 0, FILTER_STRING_SIZE); strcpy (&haddr->filter[0], filter); GNUNET_memcpy (&haddr->senderAddress[0], email, strlen (email) + 1); msg->senderAddressSize = htons (strlen (email) + 1 + sizeof (EmailAddress)); msg->protocol = htons (GNUNET_TRANSPORT_PROTOCOL_NUMBER_SMTP); msg->MTU = htonl (smtpAPI.mtu); msg->header.size = htons (GNUNET_sizeof_hello (msg)); if (api_verify_hello (msg) == GNUNET_SYSERR) GNUNET_GE_ASSERT (ectx, 0); GNUNET_free (filter); return msg; }
int main (int argc, char *argv[]) { #if START_DAEMON struct GNUNET_OS_Process *daemon; #endif int ok; struct GNUNET_ECRS_URI *uri; char *filename = NULL; char *keywords[] = { "fsui_foo", "fsui_bar", }; char keyword[40]; char *fn; int prog; struct GNUNET_MetaData *meta; struct GNUNET_ECRS_URI *kuri; struct GNUNET_GC_Configuration *cfg; struct GNUNET_FSUI_UploadList *upload = NULL; struct GNUNET_FSUI_SearchList *search = NULL; struct GNUNET_FSUI_UnindexList *unindex = NULL; struct GNUNET_FSUI_DownloadList *download = NULL; cfg = GNUNET_GC_create (); if (-1 == GNUNET_GC_parse_configuration (cfg, "check.conf")) { GNUNET_GC_free (cfg); return -1; } #if START_DAEMON daemon = GNUNET_daemon_start (NULL, cfg, "peer.conf", GNUNET_NO); GNUNET_GE_ASSERT (NULL, daemon != NULL); CHECK (GNUNET_OK == GNUNET_wait_for_daemon_running (NULL, cfg, 60 * GNUNET_CRON_SECONDS)); #endif GNUNET_thread_sleep (5 * GNUNET_CRON_SECONDS); /* give apps time to start */ ok = GNUNET_YES; /* ACTUAL TEST CODE */ ctx = GNUNET_FSUI_start (NULL, cfg, "basic_fsui_test", 32, /* thread pool size */ GNUNET_NO, /* no resume */ &eventCallback, NULL); CHECK (ctx != NULL); filename = makeName (42); GNUNET_disk_file_write (NULL, filename, "foo bar test!", strlen ("foo bar test!"), "600"); meta = GNUNET_meta_data_create (); kuri = GNUNET_ECRS_keyword_command_line_to_uri (NULL, 2, (const char **) keywords); /* upload */ upload = GNUNET_FSUI_upload_start (ctx, filename, (GNUNET_FSUI_DirectoryScanCallback) & GNUNET_disk_directory_scan, NULL, 0, /* anonymity */ 0, /* priority */ GNUNET_YES, GNUNET_NO, GNUNET_NO, GNUNET_get_time () + 5 * GNUNET_CRON_HOURS, meta, kuri, kuri); CHECK (upload != NULL); GNUNET_ECRS_uri_destroy (kuri); GNUNET_meta_data_destroy (meta); prog = 0; while (lastEvent != GNUNET_FSUI_upload_completed) { prog++; CHECK (prog < 10000) GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS); if (GNUNET_shutdown_test () == GNUNET_YES) break; } /* search */ GNUNET_snprintf (keyword, 40, "+%s +%s", keywords[0], keywords[1]); uri = GNUNET_ECRS_keyword_string_to_uri (NULL, keyword); search = GNUNET_FSUI_search_start (ctx, 0, uri); GNUNET_ECRS_uri_destroy (uri); CHECK (search != NULL); prog = 0; while (lastEvent != GNUNET_FSUI_search_result) { prog++; CHECK (prog < 10000); GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS); if (GNUNET_shutdown_test () == GNUNET_YES) break; } GNUNET_FSUI_search_abort (search); GNUNET_FSUI_search_stop (search); /* download */ fn = makeName (43); download = GNUNET_FSUI_download_start (ctx, 0, GNUNET_NO, search_uri, search_meta, fn, NULL, NULL); GNUNET_free (fn); prog = 0; while (lastEvent != GNUNET_FSUI_download_completed) { prog++; CHECK (prog < 10000); GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS); if (GNUNET_shutdown_test () == GNUNET_YES) break; } GNUNET_FSUI_download_stop (download); download = NULL; GNUNET_ECRS_uri_destroy (search_uri); GNUNET_meta_data_destroy (search_meta); /* unindex */ unindex = GNUNET_FSUI_unindex_start (ctx, filename); prog = 0; while (lastEvent != GNUNET_FSUI_unindex_completed) { prog++; CHECK (prog < 10000); GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS); if (GNUNET_shutdown_test () == GNUNET_YES) break; } if (lastEvent != GNUNET_FSUI_unindex_completed) GNUNET_FSUI_unindex_abort (unindex); GNUNET_FSUI_unindex_stop (unindex); /* END OF TEST CODE */ FAILURE: if (ctx != NULL) GNUNET_FSUI_stop (ctx); if (filename != NULL) { UNLINK (filename); GNUNET_free (filename); } if (download != NULL) { GNUNET_FSUI_download_abort (download); GNUNET_FSUI_download_stop (download); } filename = makeName (43); /* TODO: verify file 'filename(42)' == file 'filename(43)' */ UNLINK (filename); GNUNET_free (filename); #if START_DAEMON GNUNET_GE_ASSERT (NULL, GNUNET_OK == GNUNET_daemon_stop (NULL, daemon)); GNUNET_OS_process_destroy (daemon); #endif GNUNET_GC_free (cfg); return (ok == GNUNET_YES) ? 0 : 1; }
/** * Send a message to the specified remote node. * * @param tsession the GNUNET_MessageHello identifying the remote node * @param msg what to send * @param size the size of the message * @param important is this message important enough to override typical limits? * @return GNUNET_SYSERR on error, GNUNET_OK on success */ static int api_send (GNUNET_TSession * tsession, const void *msg, const unsigned int size, int important) { const GNUNET_MessageHello *hello; const EmailAddress *haddr; char *m; char *filter; char *fvalue; SMTPMessage *mp; struct GetMessageClosure gm_cls; smtp_session_t session; smtp_message_t message; smtp_recipient_t recipient; #define EBUF_LEN 128 char ebuf[EBUF_LEN]; GNUNET_CronTime now; if (smtp_shutdown == GNUNET_YES) return GNUNET_SYSERR; if ((size == 0) || (size > smtpAPI.mtu)) { GNUNET_GE_BREAK (ectx, 0); return GNUNET_SYSERR; } now = GNUNET_get_time (); if ((important != GNUNET_YES) && ((now - last_transmission) * rate_limit) < GNUNET_CRON_HOURS) return GNUNET_NO; /* rate too high */ last_transmission = now; hello = (const GNUNET_MessageHello *) tsession->internal; if (hello == NULL) return GNUNET_SYSERR; GNUNET_mutex_lock (lock); session = smtp_create_session (); if (session == NULL) { GNUNET_GE_LOG (ectx, GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_USER | GNUNET_GE_IMMEDIATE, _("SMTP: `%s' failed: %s.\n"), "smtp_create_session", smtp_strerror (smtp_errno (), ebuf, EBUF_LEN)); GNUNET_mutex_unlock (lock); return GNUNET_SYSERR; } if (0 == smtp_set_server (session, smtp_server_name)) { GNUNET_GE_LOG (ectx, GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_USER | GNUNET_GE_IMMEDIATE, _("SMTP: `%s' failed: %s.\n"), "smtp_set_server", smtp_strerror (smtp_errno (), ebuf, EBUF_LEN)); smtp_destroy_session (session); GNUNET_mutex_unlock (lock); return GNUNET_SYSERR; } haddr = (const EmailAddress *) &hello[1]; message = smtp_add_message (session); if (message == NULL) { GNUNET_GE_LOG (ectx, GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER | GNUNET_GE_BULK, _("SMTP: `%s' failed: %s.\n"), "smtp_add_message", smtp_strerror (smtp_errno (), ebuf, EBUF_LEN)); smtp_destroy_session (session); GNUNET_mutex_unlock (lock); return GNUNET_SYSERR; } smtp_set_header (message, "To", NULL, haddr->senderAddress); smtp_set_header (message, "From", NULL, email); filter = GNUNET_strdup (haddr->filter); fvalue = strstr (filter, ": "); GNUNET_GE_ASSERT (NULL, NULL != fvalue); fvalue[0] = '\0'; fvalue += 2; if (0 == smtp_set_header (message, filter, fvalue)) { GNUNET_GE_LOG (ectx, GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER | GNUNET_GE_BULK, _("SMTP: `%s' failed: %s.\n"), "smtp_set_header", smtp_strerror (smtp_errno (), ebuf, EBUF_LEN)); smtp_destroy_session (session); GNUNET_mutex_unlock (lock); GNUNET_free (filter); return GNUNET_SYSERR; } GNUNET_free (filter); m = GNUNET_malloc (size + sizeof (SMTPMessage)); GNUNET_memcpy (m, msg, size); mp = (SMTPMessage *) &m[size]; mp->header.size = htons (size + sizeof (SMTPMessage)); mp->header.type = htons (0); mp->sender = *core_api->my_identity; gm_cls.ebody = NULL; gm_cls.pos = 0; gm_cls.esize = GNUNET_STRINGS_base64_encode (m, size + sizeof (SMTPMessage), &gm_cls.ebody); GNUNET_free (m); if (0 == smtp_size_set_estimate (message, gm_cls.esize)) { GNUNET_GE_LOG (ectx, GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER | GNUNET_GE_BULK, _("SMTP: `%s' failed: %s.\n"), "smtp_size_set_estimate", smtp_strerror (smtp_errno (), ebuf, EBUF_LEN)); } if (0 == smtp_set_messagecb (message, &get_message, &gm_cls)) { GNUNET_GE_LOG (ectx, GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER | GNUNET_GE_BULK, _("SMTP: `%s' failed: %s.\n"), "smtp_set_messagecb", smtp_strerror (smtp_errno (), ebuf, EBUF_LEN)); smtp_destroy_session (session); GNUNET_mutex_unlock (lock); GNUNET_free (gm_cls.ebody); return GNUNET_SYSERR; } recipient = smtp_add_recipient (message, haddr->senderAddress); if (recipient == NULL) { GNUNET_GE_LOG (ectx, GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER | GNUNET_GE_BULK, _("SMTP: `%s' failed: %s.\n"), "smtp_add_recipient", smtp_strerror (smtp_errno (), ebuf, EBUF_LEN)); smtp_destroy_session (session); GNUNET_mutex_unlock (lock); return GNUNET_SYSERR; } if (0 == smtp_start_session (session)) { GNUNET_GE_LOG (ectx, GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER | GNUNET_GE_BULK, _("SMTP: `%s' failed: %s.\n"), "smtp_start_session", smtp_strerror (smtp_errno (), ebuf, EBUF_LEN)); smtp_destroy_session (session); GNUNET_mutex_unlock (lock); GNUNET_free (gm_cls.ebody); return GNUNET_SYSERR; } if (stats != NULL) stats->change (stat_bytesSent, size); if (core_api->load_monitor != NULL) GNUNET_network_monitor_notify_transmission (core_api->load_monitor, GNUNET_ND_UPLOAD, gm_cls.esize); smtp_message_reset_status (message); /* this is needed to plug a 28-byte/message memory leak in libesmtp */ smtp_destroy_session (session); GNUNET_mutex_unlock (lock); GNUNET_free (gm_cls.ebody); return GNUNET_OK; }