Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Data sending support, fix for peer management

  • Loading branch information...
commit fb1ce4e2b0578179958bc2275190a9b7e7f5567e 1 parent b321523
@Detegr authored
Showing with 60 additions and 10 deletions.
  1. +54 −3 core/core.c
  2. +4 −6 core/peermanager.c
  3. +2 −1  core/peermanager.h
View
57 core/core.c
@@ -22,6 +22,8 @@
#include <signal.h>
#define SOCKET_ONEWAY -2
+// Length of the password used to AES encrypt data
+#define PW_LEN 80
static volatile sig_atomic_t run_threads=1;
@@ -236,6 +238,12 @@ static int check_peer_key(struct peer* p, char* addr, int newconn, struct config
peer_free(p);
return -1;
}
+ else
+ {
+#ifndef NDEBUG
+ printf("Key %s loaded for peer %s, address %x\n", keybuf, addr, p);
+#endif
+ }
p->isock=newconn;
}
else
@@ -318,9 +326,12 @@ void* connection_thread(void* args)
{
printf("%s:%u\no:%d\ni:%d\n", pp->addr, pp->port, pp->osock, pp->isock);
}
- if(peer_exists(p))
+ if((pp=peer_exists(p)))
{
- if(check_peer_key(p, hbuf, newconn, conf) == 0)
+ peer_remove(p);
+ // Remove the newly allocated peer and check if
+ // we have oneway connection with the old one
+ if(check_peer_key(pp, hbuf, newconn, conf) == 0)
{
#ifndef NDEBUG
printf("Oneway connection to bidirectional for %s:%u\n", hbuf, port);
@@ -332,7 +343,6 @@ void* connection_thread(void* args)
printf("Peer exists\n");
#endif
}
- peer_remove(p);
continue;
}
}
@@ -403,6 +413,9 @@ void* read_thread(void* args)
size_t datalen;
unsigned char* data=aes_decrypt_with_key(readbuf, b, &deckey, &datalen);
evt_t* e=new_event_fromstr(data);
+#ifndef NDEBUG
+ printf("Got %s\n", data);
+#endif
if(e)
{
send_event(e);
@@ -462,12 +475,50 @@ static int connect_to_peers()
return 0;
}
+void send_to_all(unsigned char* data_to_enc, int len)
+{
+ struct peer* p;
+ fd_set wset;
+ peer_writeset(&wset);
+ int nfds=select(write_max()+1, NULL, &wset, NULL, NULL);
+ if(nfds>0)
+ {
+ while(p=peer_next())
+ {
+ int fd;
+ if(p->osock==SOCKET_ONEWAY) fd=p->isock;
+ else fd=p->osock;
+ assert(fd >= 0);
+ assert(fd != SOCKET_ONEWAY);
+ if(FD_ISSET(fd, &wset))
+ {
+ int enclen=0;
+ unsigned char* data=aes_encrypt_random_pass(
+ data_to_enc,
+ len,
+ PW_LEN,
+ &p->key,
+ &enclen);
+ printf("Sending %d bytes to %d\n", enclen, fd);
+ if(send(fd, data, enclen, 0) != enclen)
+ {
+ // TODO: Remove peer on error
+ perror("send");
+ }
+ }
+ }
+ }
+}
+
void process_event(evt_t* e)
{
switch(e->type)
{
case Message:
+ {
+ send_to_all(e->data, strnlen(e->data, EVENT_MAX-EVENT_LEN));
break;
+ }
case ListPeers:
{
char data[EVENT_MAX-EVENT_LEN];
View
10 core/peermanager.c
@@ -104,7 +104,7 @@ int peer_addtoset(struct peer* p)
return 0;
}
-int peer_exists(struct peer* p)
+struct peer* peer_exists(struct peer* p)
{
pthread_mutex_lock(&m_lock);
for(int i=0; i<m_peercount; ++i)
@@ -113,16 +113,14 @@ int peer_exists(struct peer* p)
m_peers[i].port == p->port &&
&m_peers[i] != p)
{
- // Return value is a shortcut for tapi2p to use
- // Basically any nonzero value will do
- int ret=m_peers[i].isock;
- assert(ret!=0);
+ struct peer* ret=&m_peers[i];
+ assert(ret);
pthread_mutex_unlock(&m_lock);
return ret;
}
}
pthread_mutex_unlock(&m_lock);
- return 0;
+ return NULL;
}
struct peer* peer_next()
View
3  core/peermanager.h
@@ -15,7 +15,8 @@ void peer_removefromset(struct peer* p);
// address and the port of p matches to another peer.
// Also, it assumes that p is allocated with peer_new()
// and thus it doesn't check if p itself exists.
-int peer_exists(struct peer* p);
+// Returns the existing peer if peer exists, NULL otherwise
+struct peer* peer_exists(struct peer* p);
int peer_remove(struct peer* p);
void peers_free();
Please sign in to comment.
Something went wrong with that request. Please try again.