Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: Detegr/tapi2p
base: 3687bc82a3
...
head fork: Detegr/tapi2p
compare: 2b1d72c1bc
  • 2 commits
  • 6 files changed
  • 0 commit comments
  • 1 contributor
75 core/core.c
View
@@ -27,8 +27,9 @@
#include <stdlib.h>
#include <semaphore.h>
#include <fcntl.h>
+#include <stdbool.h>
-static volatile sig_atomic_t run_threads=1;
+volatile sig_atomic_t run_threads=1;
static int sock_in;
static int sock_out;
@@ -192,46 +193,46 @@ static int core_init(void)
if(!conf->size)
{
printf("tapi2p needs to be configured before use.\n"
- "Please set your username and port and restart tapi2p.\n");
- return 1;
+ "Waiting for Setup event...\n");
}
-
- const char* md="Metadata";
- struct configsection* files=config_find_section(conf, md);
- if(files)
+ else
{
- char sha_str[SHA_DIGEST_LENGTH*2+1];
- memset(sha_str, 0, SHA_DIGEST_LENGTH*2+1);
- for(int i=0; i<files->itemcount; ++i)
+ const char* md="Metadata";
+ struct configsection* files=config_find_section(conf, md);
+ if(files)
{
- struct configitem* ci=files->items[i];
- struct stat buf;
- char* mdpath=NULL;
- if(ci->val)
+ char sha_str[SHA_DIGEST_LENGTH*2+1];
+ memset(sha_str, 0, SHA_DIGEST_LENGTH*2+1);
+ for(int i=0; i<files->itemcount; ++i)
{
- getpath(metadatapath(), ci->val, &mdpath);
- }
- if(!ci->val || stat(mdpath, &buf) == -1)
- {
- printf("Generating metadata for %s\n", ci->key);
- create_metadata_file(ci->key, sha_str);
- config_add(conf, md, ci->key, sha_str);
- }
- if(mdpath) free(mdpath);
- struct configsection* mdci;
- if(!(mdci=config_find_section(conf, ci->val)))
- {
- config_add(conf, ci->val, "Filename", ci->key);
+ struct configitem* ci=files->items[i];
+ struct stat buf;
+ char* mdpath=NULL;
+ if(ci->val)
+ {
+ getpath(metadatapath(), ci->val, &mdpath);
+ }
+ if(!ci->val || stat(mdpath, &buf) == -1)
+ {
+ printf("Generating metadata for %s\n", ci->key);
+ create_metadata_file(ci->key, sha_str);
+ config_add(conf, md, ci->key, sha_str);
+ }
+ if(mdpath) free(mdpath);
+ struct configsection* mdci;
+ if(!(mdci=config_find_section(conf, ci->val)))
+ {
+ config_add(conf, ci->val, "Filename", ci->key);
+ }
}
+ config_save(conf, configpath());
}
- config_save(conf, configpath());
}
if(create_core_socket() == -1) return -1;
pipe_init();
-
- return 0;
+ return conf->size ? 0 : 1;
}
static int check_peer_key(struct peer* p, char* addr, int newconn, struct config* conf)
@@ -703,7 +704,19 @@ int core_start(void)
}
else if(ci>0)
{// Config file created
- return 0;
+ pipe_event_addlistener(Setup, &handlesetup, NULL);
+ while(run_threads==1)
+ {
+ pipe_accept();
+ pipeevt_t* e=poll_event_from_pipes();
+ if(e) free(e);
+ }
+ pipe_event_removelistener(Setup, &handlesetup);
+ if(!run_threads)
+ {// If core was exited during setup phase
+ return 0;
+ }
+ run_threads=1;
}
if(privkey_load(&deckey, selfkeypath()))
23 core/event.c
View
@@ -125,7 +125,7 @@ pipeevt_t* event_recv(int fd, int* status)
void event_addlistener(EventType t, EventCallback cb, void* data)
{
- EventCallback* cbs=callbacks[t];
+ EventCallback *cbs=callbacks[t];
if(!cbs)
{
cbs=malloc(CBMAX*sizeof(EventCallback*));
@@ -140,10 +140,29 @@ void event_addlistener(EventType t, EventCallback cb, void* data)
return;
}
-void pipe_event_addlistener(EventType t, PipeEventCallback cb, void* data)
+void event_removelistener(EventType t, EventCallback cb)
+{
+ EventCallback *cbs=callbacks[t];
+ if(!cbs) return;
+ for(int i=0; i<CBMAX; ++i)
+ {
+ if(cbs[i] == cb)
+ {
+ cbs[i]=NULL;
+ callbackdatas[t][i]=NULL;
+ return;
+ }
+ }
+}
+
+inline void pipe_event_addlistener(EventType t, PipeEventCallback cb, void* data)
{
event_addlistener(t, (EventCallback)cb, data);
}
+inline void pipe_event_removelistener(EventType t, PipeEventCallback cb)
+{
+ event_removelistener(t, (EventCallback)cb);
+}
static int run_event_thread=1;
static void* event_threadfunc(void* args)
3  core/event.h
View
@@ -18,6 +18,7 @@ typedef enum {
RequestFileList,
FileList,
AddFile,
+ Setup,
EventCount // For iterating through eventtypes
} EventType;
@@ -56,7 +57,9 @@ typedef void (*EventCallback)(evt_t* e, void* data);
typedef void (*PipeEventCallback)(pipeevt_t* e, void* data);
void event_run_callbacks(evt_t* e);
void event_addlistener(EventType t, EventCallback cb, void* data);
+void event_removelistener(EventType t, EventCallback cb);
void pipe_event_addlistener(EventType t, PipeEventCallback cb, void* data);
+void pipe_event_removelistener(EventType t, PipeEventCallback cb);
const char* eventtype_str(evt_t* evt);
66 core/handlers.c
View
@@ -13,6 +13,9 @@
#include <assert.h>
#include <jansson.h>
#include <stdbool.h>
+#include <signal.h>
+
+extern volatile sig_atomic_t run_threads;
struct file_part_thread_data
{
@@ -481,3 +484,66 @@ void handleaddfile(evt_t* e, void* data)
err:
json_decref(root);
}
+
+static bool setup(const char *nick, int port)
+{
+ if(port<=0 || port > 65535)
+ {
+ fprintf(stderr, "Invalid port\n");
+ return false;
+ }
+ uint16_t port16=(uint16_t)port;
+
+ struct config* c = getconfig();
+ char portstr[5];
+ if(!sprintf(portstr, "%hu", port16))
+ {
+ fprintf(stderr, "sprintf failed\n");
+ return false;
+ }
+ config_add(c, "Account", "Nick", nick);
+ config_add(c, "Account", "Port", portstr);
+ FILE* conffile=fopen(configpath(), "w");
+ if(!conffile)
+ {
+ fprintf(stderr, "Couldn't open config file: %s.\n", configpath());
+ return false;
+ }
+ config_flush(c, conffile);
+ fclose(conffile);
+ printf("Account setup done!\nCurrent settings:\nNick: %s\nPort: %d\n", nick, port);
+
+ return true;
+}
+
+void handlesetup(pipeevt_t *e, void *data)
+{
+ struct config *conf=getconfig();
+ json_error_t error;
+ json_t *root=json_loads(e->data, 0, &error);
+ if(!root)
+ {
+#ifndef NDEBUG
+ fprintf(stderr, "%s\n", error.text);
+#endif
+ }
+ if(!json_is_object(root))
+ {
+ printf("JSON not valid object\n");
+ goto err;
+ }
+ json_t *nick, *port;
+ nick=json_object_get(root, "nick");
+ port=json_object_get(root, "port");
+ if(!nick || !port || !json_is_string(nick) || !json_is_integer(port))
+ {
+ printf("Invalid JSON\n");
+ goto err;
+ }
+ if(setup(json_string_value(nick), json_integer_value(port)))
+ {// Special value for indicating that the setup is ready
+ run_threads=2;
+ }
+err:
+ json_decref(root);
+}
1  core/handlers.h
View
@@ -14,5 +14,6 @@ void handlefilepart(evt_t* e, void* data);
void handlerequestfilelistlocal(evt_t* e, void* data);
void handlerequestfilelist(evt_t* e, void* data);
void handleaddfile(evt_t* e, void* data);
+void handlesetup(pipeevt_t *e, void *data);
#endif
54 ui/cli/cli.c
View
@@ -68,38 +68,6 @@ void usage(int usage_page)
}
}
-int check_port(char* portstr)
-{
- char* endptr;
- errno=0;
- int port=strtol(portstr, &endptr, 10);
- if(port<0 || port>65535 || (errno != 0) || (endptr == portstr))
- {
- fprintf(stderr, "Port argument is not a valid port.\n");
- return -1;
- }
- return port;
-}
-
-void setup(char** args)
-{
- int port=check_port(args[1]);
- if(port==-1) return;
-
- struct config* c = getconfig();
- config_add(c, "Account", "Nick", args[0]);
- config_add(c, "Account", "Port", args[1]);
- FILE* conffile=fopen(configpath(), "w");
- if(!conffile)
- {
- fprintf(stderr, "Couldn't open config file: %s. Have you run tapi2p_core first?\n", configpath());
- return;
- }
- config_flush(c, conffile);
- fclose(conffile);
- printf("Account setup done!\nCurrent settings:\nNick: %s\nPort: %d\n", args[0], port);
-}
-
int core_socket()
{
struct sockaddr_un u;
@@ -121,6 +89,19 @@ int core_socket()
return fd;
}
+int check_port(char* portstr)
+{
+ char* endptr;
+ errno=0;
+ int port=strtol(portstr, &endptr, 10);
+ if(port<0 || port>65535 || (errno != 0) || (endptr == portstr))
+ {
+ fprintf(stderr, "Port argument is not a valid port.\n");
+ return -1;
+ }
+ return port;
+}
+
int check_ip(char* ip)
{
struct in_addr dummy;
@@ -226,7 +207,14 @@ int main(int argc, char** argv)
}
if(setup_args[0])
{
- setup(setup_args);
+ int fd=core_socket();
+ json_t *root=json_object();
+ json_object_set_new(root, "nick", json_string(setup_args[0]));
+ json_object_set_new(root, "port", json_integer(atoll(setup_args[1])));
+ char *jsonstr=json_dumps(root, 0);
+ event_send_simple(Setup, (const unsigned char*)jsonstr, strlen(jsonstr), fd);
+ free(jsonstr);
+ json_decref(root);
return 0;
}
break;

No commit comments for this range

Something went wrong with that request. Please try again.