Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
  • 2 commits
  • 3 files changed
  • 0 commit comments
  • 1 contributor
Commits on Jan 30, 2013
@Detegr Removed obsolete core.cpp 0c7ccd7
Commits on Feb 04, 2013
@Detegr Peer connections + cli ui 952d70c
Showing with 135 additions and 546 deletions.
  1. +25 −0 core/core.c
  2. +0 −545 core/core.cpp
  3. +110 −1 ui/cli/cli.c
View
25 core/core.c
@@ -414,6 +414,25 @@ void* read_thread(void* args)
}
}
+static int connect_to_peer(struct configitem* peer, char* peer_ip_str)
+{
+ if(!peer) return -1;
+ unsigned short port;
+}
+
+static int connect_to_peers()
+{
+ struct config* conf=getconfig();
+ struct configsection* sect;
+ if(sect=config_find_section(conf, "Peers"))
+ {
+ for(int i=0; i<sect->size; ++i)
+ {
+ connect_to_peer(config_find_item(conf, sect->name, NULL), sect->name);
+ }
+ }
+}
+
int core_start(void)
{
run_threads=1;
@@ -438,6 +457,12 @@ int core_start(void)
return -1;
}
+ if(connect_to_peers())
+ {
+ fprintf(stderr, "Failure when connecting to peers!\n");
+ return -1;
+ }
+
pthread_t accept_connections;
pthread_t read_connections;
pthread_create(&accept_connections, NULL, &connection_thread, NULL);
View
545 core/core.cpp
@@ -1,545 +0,0 @@
-#include "core.h"
-#include <arpa/inet.h>
-#include <sys/un.h>
-#include <locale.h>
-
-using namespace dtglib;
-RSA_PrivateKey Core::pkey;
-bool Core::run_threads;
-int Core::core_fd;
-
-void network_startup(void* args);
-void connect_to_peers(void*);
-void pipe_accept(void*);
-Event poll_event();
-void sendall(const std::wstring& msg);
-void generate_self_keypair(Config& c, const std::string kp);
-int setup_local_socket(const std::string& name);
-
-int Core::Start(int argc, char** argv)
-{
- run_threads=true;
- if(!setlocale(LC_CTYPE, ""))
- {
- std::cerr << "Cannot set specified locale!" << std::endl;
- }
- if(argc>1)
- {
- startup_init(argv[1]);
- }
- else startup_init();
- try
- {
- pkey.Load(PathManager::SelfKeyPath());
- }
- catch(KeyException& e)
- {
- std::cout << "Failed to start tapi2p!" << std::endl;
- std::cout << e.what() << std::endl;
- return 1;
- }
-
- C_Thread network_thread(&network_startup);
- C_Thread connection_thread(connect_to_peers);
- C_Thread pipe_thread(pipe_accept);
-
- Config& c = PathManager::GetConfig();
- while(run_threads)
- {
- Event evtlist=poll_event();
- for(Event* e=&evtlist; e; e=e->next)
- {
- if(e->Type() == Event::Message)
- {
- sendall(e->Data());
- }
- }
- }
- connection_thread.M_Join();
- network_thread.M_Join();
- PathManager::Destroy();
-}
-
-void Core::Stop()
-{
- run_threads=false;
-}
-
-int Core::setup_local_socket(const std::string& name)
-{
- struct sockaddr_un u;
- unlink(name.c_str());
- int fd=socket(AF_UNIX, SOCK_STREAM, 0);
- if(fd<=0)
- {
- std::cerr << "Error creating unix socket!" << std::endl;
- return -1;
- }
- memset(&u, 0, sizeof(struct sockaddr_un));
- u.sun_family=AF_UNIX;
- strncpy(u.sun_path, name.c_str(), sizeof(u.sun_path)-1);
- if(bind(fd, (struct sockaddr*)&u, sizeof(u)) == -1)
- {
- std::cerr << "Failed to bind unix socket" << std::endl;
- unlink(name.c_str());
- return -1;
- }
- if(listen(fd, 10) == -1)
- {
- std::cerr << "Failed to listen unix socket" << std::endl;
- unlink(name.c_str());
- return -1;
- }
- return fd;
-}
-
-void Core::startup_init(const char* custompath)
-{
- std::string tapipath;
- struct passwd* pw=NULL;
- if(!custompath)
- {
- pw = getpwuid(getuid());
- tapipath=pw->pw_dir;
- tapipath += "/.tapi2p";
- }
- else tapipath=custompath;
- PathManager::Setup(tapipath);
- mkdir(tapipath.c_str(), 0755);
- mkdir(PathManager::KeyPath().c_str(), 0755);
- Config conf(PathManager::ConfigPath());
- if(!conf.Size())
- {
- std::string data;
- std::cout << "Seems like this is the first time you run Tapi2P\nPlease input your nickname:" << std::endl;
- std::getline(std::cin, data);
- conf.Set("Account", "Nick", data);
- std::cout << "Which port would you like to use?\nNote: This port needs to be open and routed to your computer in order for Tapi2P to work correctly." << std::endl;
- std::getline(std::cin, data);
- // TODO: Check that the port is actually in valid port range
- conf.Set("Account", "Port", data);
- conf.Flush();
- }
- std::ifstream selfkey((PathManager::SelfKeyPath()+".pub").c_str());
- if(selfkey.is_open()) selfkey.close();
- else
- {
- generate_self_keypair(conf,PathManager::SelfKeyPath());
- }
- core_fd=setup_local_socket(PathManager::SocketPath());
- if(core_fd==-1)
- {
- exit(EXIT_FAILURE);
- }
-}
-
-void generate_self_keypair(Config& c, const std::string kp)
-{
- std::cout << "Generating RSA Keypair...This might take a while" << std::endl;
- RSA_KeyGenerator::WriteKeyPair(kp);
- RSA_PublicKey pk;
- try
- {
- pk.Load(kp);
- std::cout << "Keypair created.\nGive this public key to your peers:\n\n" << pk << std::endl;
- }
- catch(KeyException& e)
- {
- std::cout << "Keypair creation failed.\nReason: " << e.what() << std::endl;
- }
-}
-
-void Core::ParsePacket(C_Packet& p, const std::wstring& nick)
-{
- try
- {
- unsigned char* np=(unsigned char*)memmem(p.M_RawData()+AES::MAGIC_LEN, p.M_Size()-AES::MAGIC_LEN, AES::Magic(), AES::MAGIC_LEN);
- if(np)
- {
- while(np)
- {
- std::vector<unsigned char>& data=AES::Decrypt((unsigned char*)p.M_RawData(), np-p.M_RawData(), pkey);
- p.M_Pop(np-p.M_RawData());
- std::wcout << L"1" << (wchar_t*)&data[0] << std::endl;
- //tapi2p::UI::WriteLine(tapi2p::UI::Main(), L"[" + nick + L"] " + (wchar_t*)&data[0]);
- np=(unsigned char*)memmem(p.M_RawData()+AES::MAGIC_LEN, p.M_Size()-AES::MAGIC_LEN, AES::Magic(), AES::MAGIC_LEN);
- }
- std::vector<unsigned char>& data=AES::Decrypt((unsigned char*)p.M_RawData(), p.M_Size(), pkey);
- std::wcout << L"2" << (wchar_t*)&data[0] << std::endl;
- //tapi2p::UI::WriteLine(tapi2p::UI::Main(), L"[" + nick + L"] " + (wchar_t*)&data[0]);
- }
- else
- {
- std::vector<unsigned char>& data=AES::Decrypt((unsigned char*)p.M_RawData(), p.M_Size(), pkey);
-
- const std::vector<int>& fds=PipeManager::Container();
- std::vector<int> disconnected;
- PipeManager::Lock();
- for(std::vector<int>::const_iterator it=fds.begin(); it!=fds.end(); ++it)
- {
- int b=send(*it, (char*)&data[0], data.size(), 0);
- if(b<=0)
- {
- disconnected.push_back(*it);
- }
- }
- PipeManager::Unlock();
- for(std::vector<int>::const_iterator it=disconnected.begin(); it!=disconnected.end(); ++it)
- {
- PipeManager::Remove(*it);
- }
- //std::wcout << (wchar_t*)&data[0] << std::endl;
- //tapi2p::UI::WriteLine(tapi2p::UI::Main(), L"[" + nick + L"] " + (wchar_t*)&data[0]);
- }
- }
- catch(KeyException& e)
- {
- //tapi2p::UI::WriteLine(tapi2p::UI::Main(), L"Failed to decrypt incoming message from: " + nick);
- }
- p.M_Clear();
-}
-
-static void peerloop(void* arg)
-{
- Peer* p = (Peer*)arg;
- p->m_Selector.M_Add(p->Sock_In);
- p->m_Selector.M_Add(p->Sock_Out);
- Config& c = PathManager::GetConfig();
- std::wstring nick;
- if(p->Sock_In.M_Fd()>0) nick=c.Getw(p->Sock_In.M_Ip().M_ToString(), "Nick");
- else nick=c.Getw(p->Sock_Out.M_Ip().M_ToString(), "Nick");
- while(Core::Running())
- {
- p->m_Selector.M_Wait(1000);
- if(p->Sock_In.M_Fd()>0 && p->m_Selector.M_IsReady(p->Sock_In))
- {
- if(p->Sock_In.M_Receive(p->Packet, 1000))
- {
- Core::ParsePacket(p->Packet,nick);
- }
- else
- {
- PeerManager::Remove(p);
- break;
- }
- }
- else if(p->Sock_Out.M_Fd()>0 && p->m_Selector.M_IsReady(p->Sock_Out))
- {
- if(p->Sock_Out.M_Receive(p->Packet, 1000))
- {
- Core::ParsePacket(p->Packet,nick);
- }
- else
- {
- PeerManager::Remove(p);
- break;
- }
- }
- }
- return;
-}
-
-void network_startup(void* args)
-{
- Config& c = PathManager::GetConfig();
-
- unsigned short port;
- std::stringstream ss;
- ss << c.Get("Account", "Port");
- ss >> port;
- C_TcpSocket m_Incoming(port);
- m_Incoming.M_Bind();
- m_Incoming.M_Listen();
- C_Selector s;
- s.M_Add(m_Incoming);
- C_Packet p;
-
- std::vector<ConfigItem> known_peers=c.Get("Peers");
- while(Core::Running())
- {
- s.M_Wait(2000);
- if(s.M_IsReady(m_Incoming))
- {
- C_TcpSocket* sock=m_Incoming.M_Accept();
- if(sock)
- {
- Peer* p=NULL;
- for(std::vector<ConfigItem>::const_iterator it=known_peers.begin(); it!=known_peers.end(); ++it)
- {
- if(sock->M_Ip() == it->Key().c_str())
- {
- bool newconn=true;
- unsigned short port;
- std::string portstr=c.Get(it->Key(), "Port");
- std::stringstream ss;
- ss << portstr; ss >> port;
-
- std::vector<Peer*>& peers=PeerManager::Do();
- for(std::vector<Peer*>::iterator pt=peers.begin(); pt!=peers.end(); ++pt)
- {
- // If Sock_In!=NULL and ips and ports match, we have an existing connection
- if((*pt)->Sock_In.M_Fd() && (*pt)->Sock_In.M_Ip() == sock->M_Ip() && (*pt)->Sock_Out.M_Port() == port)
- {
- newconn=false;
- break;
- }
- else if((*pt)->Sock_Out.M_Ip() == sock->M_Ip() && (*pt)->Sock_Out.M_Port() == port)
- {// One-way to bi-directional
- (*pt)->Sock_In = *sock;
- (*pt)->m_Selector.M_Add((*pt)->Sock_In);
- newconn=false;
- break;
- }
- }
- PeerManager::Done();
-
- if(newconn)
- {
- p = new Peer(*sock);
- try
- {
- p->Key.Load(PathManager::KeyPath() + "/" + c.Get(it->Key(), "Key"));
- }
- catch(const KeyException& e)
- {
- std::cout << "Peer identification failed: " << e.what() << std::endl;
- delete p;
- p=NULL;
- }
- if(p)
- {
- try
- {
- p->Sock_Out=C_TcpSocket(sock->M_Ip(), port);
- p->Sock_Out.M_Connect();
- }
- catch(const std::runtime_error& e)
- {
- p->m_Connectable=false;
- p->Sock_Out.M_Disconnect();
- p->Sock_Out.M_Clear();
- }
-
- PeerManager::Add(p);
- p->Thread.M_Start(peerloop, p);
- }
- }
- break;
- }
- }
- }
- }
- }
- m_Incoming.M_Close();
- return;
-}
-void sendall(const std::wstring& msg)
-{
- C_Selector s;
- const std::vector<Peer*>& peers=PeerManager::Do();
- C_Packet p;
- for(std::vector<Peer*>::const_iterator it=peers.begin(); it!=peers.end(); ++it)
- {
- std::vector<unsigned char>& data=AES::Encrypt((unsigned char*)msg.c_str(), (msg.size()*sizeof(wchar_t))+sizeof(wchar_t), 80, (*it)->Key);
- for(int i=0; i<data.size(); ++i) p << data[i];
- if((*it)->m_Connectable)
- {
- s.M_Add((*it)->Sock_Out);
- s.M_WaitWrite(1000);
- if(s.M_IsReady((*it)->Sock_Out))
- {
- try
- {
- (*it)->Sock_Out.M_Send(p);
- }
- catch(const std::runtime_error& e)
- {
- PeerManager::Remove(*it);
- }
- }
- //else tapi2p::UI::WriteLine(tapi2p::UI::Active(), L"Failed to write sockout");
- }
- else
- {
- s.M_Add((*it)->Sock_In);
- s.M_WaitWrite(1000);
- if(s.M_IsReady((*it)->Sock_In))
- {
- try
- {
- (*it)->Sock_In.M_Send(p);
- }
- catch(const std::runtime_error& e)
- {
- PeerManager::Remove(*it);
- }
- }
- //else tapi2p::UI::WriteLine(tapi2p::UI::Active(), L"Failed to write sockin");
- }
- s.M_Clear();
- p.M_Clear();
- }
- PeerManager::Done();
-}
-
-void connect(void* arg)
-{
- Config& c = PathManager::GetConfig();
- const ConfigItem& ci=*(const ConfigItem*)arg;
- unsigned short port;
- std::wstringstream ss;
- std::string ip=ci.Key();
- std::wstring portstr=c.Getw(ci.Key(), "Port");
- ss << portstr; ss >> port;
- Peer* p=NULL;
- std::wstring ipw;
- ipw.resize(ip.length());
- std::copy(ip.begin(), ip.end(), ipw.begin());
- //tapi2p::UI::Write(tapi2p::UI::Content, L"Trying: " + ipw + L":" + portstr);
- try
- {
- C_TcpSocket sock(ci.Key().c_str(), port);
-
- // We need to set the socket to nonblocking mode to not get blocked
- // if we try to connect to a peer which is not online at the moment.
- int sockargs=fcntl(sock.M_Fd(), F_GETFL, NULL);
- sockargs |= O_NONBLOCK;
- if(fcntl(sock.M_Fd(), F_SETFL, sockargs)<0)
- {
- //tapi2p::UI::Write(tapi2p::UI::Content, L"Socket error.");
- }
- sock.M_Connect();
- sockargs &= ~O_NONBLOCK;
- if(fcntl(sock.M_Fd(), F_SETFL, sockargs)<0)
- {
- //tapi2p::UI::Write(tapi2p::UI::Content, L"Socket error.");
- }
- C_Selector s;
- s.M_Add(sock);
- s.M_WaitWrite(5000);
- if(s.M_IsReady(sock))
- {
- int err=0;
- int errlen=sizeof(err);
- if(getsockopt(sock.M_Fd(), SOL_SOCKET, SO_ERROR, (void*)&err, (socklen_t*)&errlen)<0 || err)
- {
- //tapi2p::UI::Write(tapi2p::UI::Content, L"Fail.");
- return;
- }
- //tapi2p::UI::Write(tapi2p::UI::Content, L"Connected.");
- p = new Peer();
- p->Sock_Out=sock;
- p->Key.Load(PathManager::KeyPath() + "/" + c.Get(ci.Key(), "Key"));
- PeerManager::Add(p);
- p->Thread.M_Start(peerloop, p);
- }
- }
- catch(const std::runtime_error& e)
- {
- //tapi2p::UI::Write(tapi2p::UI::Content, L"Fail");
- if(p)
- {
- p->Sock_Out.M_Disconnect();
- PeerManager::Remove(p);
- }
- }
- catch(const KeyException& e)
- {
- std::cout << e.what() << std::endl;
- if(p)
- {
- p->Sock_Out.M_Disconnect();
- PeerManager::Remove(p);
- }
- }
-}
-
-void connect_to_peers(void*)
-{
- Config& c = PathManager::GetConfig();
- std::vector<ConfigItem> peerconfs=c.Get("Peers");
- std::vector<C_Thread*> connections;
- for(std::vector<ConfigItem>::const_iterator it=peerconfs.begin(); it!=peerconfs.end(); ++it)
- {
- connections.push_back(new C_Thread(connect, (void*)&*it));
- }
- for(std::vector<C_Thread*>::const_iterator it=connections.begin(); it!=connections.end(); ++it)
- {
- (*it)->M_Join();
- delete *it;
- }
-}
-
-void pipe_accept(void*)
-{
- struct timeval to;
- fd_set orig;
- FD_SET(Core::Socket(), &orig);
- fd_set set;
- while(Core::Running())
- {
- set=orig;
- to.tv_sec=1;
- to.tv_usec=0;
- int nfds=select(Core::Socket()+1, &set, NULL, NULL, &to);
- if(nfds>0 && FD_ISSET(Core::Socket(), &set))
- {
- int fd=accept(Core::Socket(), NULL, NULL);
- if(fd>0) PipeManager::Add(fd);
- }
- }
-}
-
-Event poll_event()
-{
- Event ret;
- Event* e=&ret;
- const std::vector<int>& fds=PipeManager::Container();
- fd_set set;
- PipeManager::Lock();
- int max=0;
- for(std::vector<int>::const_iterator it=fds.begin(); it!=fds.end(); ++it)
- {
- if(*it > max) max=*it;
- FD_SET(*it, &set);
- }
- PipeManager::Unlock();
- struct timeval to;
- to.tv_sec=1;
- to.tv_usec=0;
- int nfds=select(max+1, &set, NULL, NULL, &to);
- for(std::vector<int>::const_iterator it=fds.begin(); it!=fds.end(); ++it)
- {
- if(e->Type() != Event::None)
- {
- e->next=new Event();
- e = e->next;
- }
- if(FD_ISSET(*it, &set))
- {
- int EVENT_MAX=4096;
- int EVENT_LEN=5;
- char buf[EVENT_MAX];
- bool more=false;
- do
- {
- memset(buf, 0, EVENT_MAX);
- int b=recv(*it, buf, EVENT_MAX, 0);
- if(b == EVENT_MAX) more=true;
- if(!more)
- {
- for(int i=EVENT_LEN; i<=b; ++i) *e << buf[i];
- if(strncmp(buf, "EMSG:", EVENT_LEN) == 0)
- {
- e->SetType(Event::Message);
- }
- }
- else
- {
- for(int i=0; i<b; ++i) *e << buf[i];
- }
- } while(more);
- }
- }
- return ret;
-}
View
111 ui/cli/cli.c
@@ -3,12 +3,17 @@
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
+#include <limits.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <unistd.h>
#include <event.h>
+#include <getopt.h>
+#include <ctype.h>
+
+static const int usage_add_peer=0;
int core_socket()
{
@@ -31,8 +36,112 @@ int core_socket()
return fd;
}
-int main()
+int check_ip(char* ip)
+{
+ struct in_addr dummy;
+ if(inet_aton(ip, &dummy) == 0) return -1;
+ else return 0;
+}
+
+void add_peer(char** args)
+{
+ char* peer_ip_str=args[0];
+ char* peer_port_str=args[1];
+
+ //peer_ip_str=strtok(peerstr, ":");
+ //peer_port_str=strtok(NULL, ":");
+
+ int port=strtol(peer_port_str, NULL, 10);
+ if(port==LONG_MIN || port==LONG_MAX || port<0 || port>65535)
+ {
+ fprintf(stderr, "Port argument is not a valid port.\n");
+ return;
+ }
+
+ if(check_ip(peer_ip_str))
+ {
+ fprintf(stderr, "Ip is not a valid ipv4 ip address.\n");
+ return;
+ }
+
+ struct config* c = getconfig();
+ config_add(c, peer_ip_str, "Port", peer_port_str);
+ FILE* conffile=fopen(configpath(), "w");
+ config_flush(c, conffile);
+ fclose(conffile);
+ printf("Peer %s:%s added successfully!\n", peer_ip_str, peer_port_str);
+}
+
+void usage(int usage_page)
{
+ switch(usage_page)
+ {
+ case 0:
+ {
+ printf("tapi2p -- Adding peers\n"
+ "Flags needed:\n"
+ "\t-p|--port => Port which peer uses\n"
+ "\t-k|--key => Name of the keyfile that has the peer's public key\n");
+ break;
+ }
+ }
+}
+
+int main(int argc, char** argv)
+{
+ int optind;
+ static struct option options[] =
+ {
+ {"add-peer", required_argument, 0, 'a'},
+ {"port", required_argument, 0, 'p'},
+ {"key", required_argument, 0, 'k'},
+ {0, 0, 0, 0}
+ };
+
+ char* add_peer_args[3]={0,0,0};
+
+ for(;;)
+ {
+ int c=getopt_long(argc, argv, "a:p:k:", options, &optind);
+ if(c==-1) break;
+ switch(c)
+ {
+ case 'a':
+ {
+ add_peer_args[0]=optarg;
+ break;
+ }
+ case 'p':
+ {
+ add_peer_args[1]=optarg;
+ if(add_peer_args[0] && add_peer_args[2])
+ {
+ add_peer(add_peer_args);
+ }
+ break;
+ }
+ case 'k':
+ {
+ add_peer_args[2]=optarg;
+ if(add_peer_args[0] && add_peer_args[1])
+ {
+ add_peer(add_peer_args);
+ }
+ break;
+ }
+ }
+ }
+ for(int i=0; i<3; ++i)
+ {
+ if(!add_peer_args[i])
+ {
+ usage(usage_add_peer);
+ break;
+ }
+ else printf("%s\n", add_peer_args[i]);
+ }
+ return;
+
int fd=core_socket();
struct Event e;
event_init(&e, Message, "foobar");

No commit comments for this range

Something went wrong with that request. Please try again.