Created V0.3.x branch and moved the head into the trunk directory.

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@246 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
drbob 2007-11-15 03:18:48 +00:00
commit 935745a08e
1318 changed files with 348809 additions and 0 deletions

View file

@ -0,0 +1,31 @@
RS_TOP_DIR = ..
include ../make.opt
OBJ = pqistrings.o \
p3face-people.o p3face-file.o \
p3face-msgs.o p3face-server.o \
p3face-config.o \
p3face-network.o \
p3face-startup.o \
rstypes.o \
rsiface.o \
TESTS =
all : $(OBJ) librs $(TESTS)
librs: $(OBJ)
$(AR) r $(LIBRS) $(OBJ)
$(RANLIB) $(LIBRS)
.cc.o:
$(CC) $(CFLAGS) -c $<
clean:
-/bin/rm $(OBJ)
clobber: clean

View file

@ -0,0 +1,368 @@
/*
* "$Id: p3face-config.cc,v 1.4 2007-05-05 16:10:05 rmf24 Exp $"
*
* RetroShare C++ Interface.
*
* Copyright 2004-2006 by Robert Fernie.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
#include "rsserver/p3face.h"
#include <iostream>
#include <sstream>
#include "pqi/pqidebug.h"
const int p3facemsgzone = 11453;
#include <sys/time.h>
#include <time.h>
/****************************************/
/* RsIface Config */
/* Config */
int RsServer::ConfigAddSharedDir( std::string dir )
{
/* call the server... */
RsIface &iface = getIface();
/* lock Mutexes */
lockRsCore(); /* LOCK */
iface.lockData(); /* LOCK */
server -> addSearchDirectory(dir);
/* unlock Mutexes */
iface.unlockData(); /* UNLOCK */
unlockRsCore(); /* UNLOCK */
/* does its own locking */
UpdateAllConfig();
return 1;
}
int RsServer::ConfigRemoveSharedDir( std::string dir )
{
/* fill the rsiface class */
RsIface &iface = getIface();
/* lock Mutexes */
lockRsCore(); /* LOCK */
iface.lockData(); /* LOCK */
server -> removeSearchDirectory(dir);
/* unlock Mutexes */
iface.unlockData(); /* UNLOCK */
unlockRsCore(); /* UNLOCK */
/* does its own locking */
UpdateAllConfig();
return 1;
}
int RsServer::ConfigSetIncomingDir( std::string dir )
{
/* fill the rsiface class */
RsIface &iface = getIface();
/* lock Mutexes */
lockRsCore(); /* LOCK */
iface.lockData(); /* LOCK */
server -> setSaveDir(dir);
/* unlock Mutexes */
iface.unlockData(); /* UNLOCK */
unlockRsCore(); /* UNLOCK */
/* does its own locking */
UpdateAllConfig();
return 1;
}
#ifdef MOVE_TO_RS_NETWORK
int RsServer::ConfigSetLocalAddr( std::string ipAddr, int port )
{
/* check if this is all necessary */
struct in_addr inaddr_local;
if (0 == inet_aton(ipAddr.c_str(), &inaddr_local))
{
//bad address - reset.
return 0;
}
/* fill the rsiface class */
RsIface &iface = getIface();
/* lock Mutexes */
lockRsCore(); /* LOCK */
iface.lockData(); /* LOCK */
/* a little rough and ready, should be moved to the server! */
cert *c = sslr -> getOwnCert();
/* always change the address (checked by sslr->checkNetAddress()) */
c -> localaddr.sin_addr = inaddr_local;
c -> localaddr.sin_port = htons((short) port);
sslr -> checkNetAddress();
pqih -> restart_listener();
sslr -> CertsChanged();
/* unlock Mutexes */
iface.unlockData(); /* UNLOCK */
unlockRsCore(); /* UNLOCK */
/* does its own locking */
UpdateAllConfig();
return 1;
}
int RsServer::ConfigSetExtAddr( std::string ipAddr, int port )
{
/* check if this is all necessary */
struct in_addr inaddr;
if (0 == inet_aton(ipAddr.c_str(), &inaddr))
{
//bad address - reset.
return 0;
}
/* fill the rsiface class */
RsIface &iface = getIface();
/* lock Mutexes */
lockRsCore(); /* LOCK */
iface.lockData(); /* LOCK */
/* a little rough and ready, should be moved to the server! */
cert *c = sslr -> getOwnCert();
/* always change the address (checked by sslr->checkNetAddress()) */
c -> serveraddr.sin_addr = inaddr;
c -> serveraddr.sin_port = htons((short) port);
sslr -> checkNetAddress();
sslr -> CertsChanged();
/* unlock Mutexes */
iface.unlockData(); /* UNLOCK */
unlockRsCore(); /* UNLOCK */
/* does its own locking */
UpdateAllConfig();
return 1;
}
#endif /* MOVE_TO_RS_NETWORK */
int RsServer::ConfigSetLanConfig( bool firewalled, bool forwarded )
{
/* fill the rsiface class */
RsIface &iface = getIface();
/* lock Mutexes */
lockRsCore(); /* LOCK */
iface.lockData(); /* LOCK */
/* a little rough and ready, should be moved to the server! */
cert *c = sslr -> getOwnCert();
c -> Firewalled(firewalled);
c -> Forwarded(forwarded);
sslr -> checkNetAddress();
sslr -> CertsChanged();
/* unlock Mutexes */
iface.unlockData(); /* UNLOCK */
unlockRsCore(); /* UNLOCK */
/* does its own locking */
UpdateAllConfig();
return 1;
}
int RsServer::ConfigSetExtName( std::string addr )
{
/* fill the rsiface class */
RsIface &iface = getIface();
/* lock Mutexes */
lockRsCore(); /* LOCK */
iface.lockData(); /* LOCK */
/* a little rough and ready, should be moved to the server! */
//cert *c = sslr -> getOwnCert();
/* set the Name here */
sslr -> checkNetAddress();
sslr -> CertsChanged();
/* unlock Mutexes */
iface.unlockData(); /* UNLOCK */
unlockRsCore(); /* UNLOCK */
/* does its own locking */
UpdateAllConfig();
return 1;
}
int RsServer::ConfigSetDataRates( int total, int indiv ) /* in kbrates */
{
/* fill the rsiface class */
RsIface &iface = getIface();
/* lock Mutexes */
lockRsCore(); /* LOCK */
iface.lockData(); /* LOCK */
pqih -> setMaxRate(true, total);
pqih -> setMaxRate(false, total);
pqih -> setMaxIndivRate(true, indiv);
pqih -> setMaxIndivRate(false, indiv);
/* unlock Mutexes */
iface.unlockData(); /* UNLOCK */
unlockRsCore(); /* UNLOCK */
/* does its own locking */
UpdateAllConfig();
return 1;
}
int RsServer::ConfigSetBootPrompt( bool on )
{
return 1;
}
int RsServer::UpdateAllConfig()
{
/* fill the rsiface class */
RsIface &iface = getIface();
/* lock Mutexes */
lockRsCore(); /* LOCK */
iface.lockData(); /* LOCK */
RsConfig &config = iface.mConfig;
cert *own = sslr -> getOwnCert();
/* set the id. */
{
RsCertId rid = intGetCertId(own);
std::ostringstream out;
out << rid;
config.ownId = out.str();
}
config.ownName = own->Name();
/* shared dirs */
std::list<std::string> &dirs = server -> getSearchDirectories();
config.sharedDirList = dirs;
config.incomingDir = server->getSaveDir();
/* ports */
config.localAddr = inet_ntoa(own -> localaddr.sin_addr);
config.localPort = ntohs(own -> localaddr.sin_port);
config.firewalled = own -> Firewalled();
config.forwardPort = own -> Forwarded();
if (own -> Firewalled() && !own -> Forwarded())
{
config.extAddr = "0.0.0.0";
config.extPort = 0;
config.extName = "<Incoming Not Possible>";
}
else
{
config.extAddr = inet_ntoa(own -> serveraddr.sin_addr);
config.extPort = ntohs(own -> serveraddr.sin_port);
config.extName = "<Coming Soon!>";
}
/* data rates */
config.maxDataRate = (int) pqih -> getMaxRate(true); /* kb */
config.maxIndivDataRate = (int) pqih -> getMaxIndivRate(true);/* kb */
config.promptAtBoot = true; /* popup the password prompt */
/* update DHT/UPnP config */
UpdateNetworkConfig(config);
/* Notify of Changes */
iface.setChanged(RsIface::Config);
/* unlock Mutexes */
iface.unlockData(); /* UNLOCK */
unlockRsCore(); /* UNLOCK */
return 1;
}
int RsServer::ConfigSave()
{
/* fill the rsiface class */
RsIface &iface = getIface();
/* lock Mutexes */
lockRsCore(); /* LOCK */
iface.lockData(); /* LOCK */
// call save all the other parts.
server -> save_config();
ad -> save_configuration();
pqih -> save_config();
sslr -> saveCertificates();
/* unlock Mutexes */
iface.unlockData(); /* UNLOCK */
unlockRsCore(); /* UNLOCK */
return 1;
}

View file

@ -0,0 +1,365 @@
/*
* "$Id: p3face-file.cc,v 1.6 2007-04-15 18:45:23 rmf24 Exp $"
*
* RetroShare C++ Interface.
*
* Copyright 2004-2006 by Robert Fernie.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
#include "rsiface/rsiface.h"
#include "rsserver/p3face.h"
#include "util/rsdir.h"
#include <iostream>
#include <sstream>
#include "pqi/pqidebug.h"
const int p3facefilezone = 11452;
#include <sys/time.h>
#include <time.h>
static const int FileMaxAge = 1800; /* Reload Data after 30 minutes */
/* add in an extension if necessary */
int ensureExtension(std::string &name, std::string def_ext)
{
return 1;
}
/****************************************/
/****************************************/
int RsServer::UpdateRemotePeople()
{
RsIface &iface = getIface();
/* lock Mutexes */
lockRsCore(); /* LOCK */
iface.lockData(); /* LOCK */
/* unlock Mutexes */
iface.unlockData(); /* UNLOCK */
unlockRsCore(); /* UNLOCK */
return 1;
}
/****************************************/
/****************************************/
int RsServer::UpdateAllTransfers()
{
NotifyBase &cb = getNotify();
cb.notifyListPreChange(NOTIFY_LIST_TRANSFERLIST, NOTIFY_TYPE_MOD);
RsIface &iface = getIface();
/* lock Mutexes */
lockRsCore(); /* LOCK */
iface.lockData(); /* LOCK */
/* clear the old transfer list() */
std::list<FileTransferInfo> &transfers = iface.mTransferList;
transfers.clear();
std::list<FileTransferItem *> nTransList = server -> getTransfers();
std::list<FileTransferItem *>::iterator it;
for(it = nTransList.begin(); it != nTransList.end(); it++)
{
FileTransferInfo ti;
if ((*it) -> p)
{
ti.source = (*it) -> p -> Name();
}
else
{
ti.source = "Unknown";
}
ti.id = intGetCertId((cert *) (*it) -> p);
ti.fname = (*it) -> name;
ti.hash = (*it) -> hash;
ti.path = (*it) -> path;
ti.size = (*it) -> size;
ti.transfered = (*it) -> transferred;
/* other ones!!! */
ti.tfRate = (*it) -> crate;
ti.downloadStatus = (*it) -> state;
transfers.push_back(ti);
}
iface.setChanged(RsIface::Transfer);
/* unlock Mutexes */
iface.unlockData(); /* UNLOCK */
unlockRsCore(); /* UNLOCK */
/* Now Notify of the Change */
cb.notifyListChange(NOTIFY_LIST_TRANSFERLIST, NOTIFY_TYPE_MOD);
return 1;
}
int RsServer::FileRequest(std::string fname, std::string hash,
uint32_t size, std::string dest)
{
lockRsCore(); /* LOCKED */
std::cerr << "RsServer::FileRequest(" << fname << ", ";
std::cerr << hash << ", " << size << ", " << dest << ")" << std::endl;
int ret = server -> getFile(fname, hash, size, dest);
unlockRsCore(); /* UNLOCKED */
return ret;
}
/* Actions For Upload/Download */
int RsServer::FileRecommend(std::string fname, std::string hash, int size)
{
/* check for new data */
RsIface &iface = getIface();
/* lock Mutexes */
lockRsCore(); /* LOCK */
iface.lockData(); /* LOCK */
/* add the entry to FileRecommends.
*/
std::list<FileInfo> &recList = iface.mRecommendList;
std::list<FileInfo>::iterator it;
FileInfo fi;
fi.path = ""; /* this is not needed / wanted anymore */
fi.hash = hash;
fi.fname = fname;
fi.size = size;
fi.rank = 1;
fi.inRecommend = false;
/* check if it exists already! */
bool found = false;
for(it = recList.begin(); (!found) && (it != recList.end()); it++)
{
if ((it->hash == fi.hash) && (it -> fname == fi.fname))
{
found = true;
}
}
if (!found)
{
recList.push_back(fi);
}
/* Notify of change */
iface.setChanged(RsIface::Recommend);
/* lock Mutexes */
iface.unlockData(); /* UNLOCK */
unlockRsCore(); /* UNLOCK */
return true;
}
int RsServer::FileCancel(std::string fname, std::string hash, uint32_t size)
{
lockRsCore(); /* LOCKED */
server -> cancelTransfer(fname, hash, size);
unlockRsCore(); /* UNLOCKED */
return 1;
}
int RsServer::FileClearCompleted()
{
std::cerr << "RsServer::FileClearCompleted()" << std::endl;
/* check for new data */
RsIface &iface = getIface();
/* lock Mutexes */
lockRsCore(); /* LOCK */
iface.lockData(); /* LOCK */
server -> clear_old_transfers();
/* lock Mutexes */
iface.unlockData(); /* UNLOCK */
unlockRsCore(); /* UNLOCK */
/* update data */
UpdateAllTransfers();
return 1;
}
int RsServer::FileSetBandwidthTotals(float outkB, float inkB)
{
int ret = 0;
return ret;
}
/**************************************************************************/
#if 0
int RsServer::FileBroadcast(std::string id, std::string src, int size)
{
lockRsCore(); /* LOCKED */
RsCertId uId(id);
int ret = 1;
cert *c = intFindCert(uId);
if ((c == NULL) || (c == sslr -> getOwnCert()))
{
ret = 0;
}
if (ret)
{
/* TO DO */
}
unlockRsCore(); /* UNLOCKED */
return ret;
}
int RsServer::FileDelete(std::string id, std::string fname)
{
lockRsCore(); /* LOCKED */
RsCertId uId(id);
int ret = 1;
cert *c = intFindCert(uId);
if ((c == NULL) || (c == sslr -> getOwnCert()))
{
ret = 0;
}
if (ret)
{
/* TO DO */
}
unlockRsCore(); /* UNLOCKED */
return ret;
}
#endif
/**************************************************************************/
int RsServer::RequestDirDetails(std::string uid, std::string path,
DirDetails &details)
{
/* lock Mutexes */
RsIface &iface = getIface();
lockRsCore(); /* LOCK */
iface.lockData(); /* LOCK */
/* call to filedexserver */
int val = server->RequestDirDetails(uid, path, details);
/* done! */
/* unlock Mutexes */
iface.unlockData(); /* UNLOCK */
unlockRsCore(); /* UNLOCK */
return val;
}
int RsServer::RequestDirDetails(void *ref, DirDetails &details, uint32_t flags)
{
/* lock Mutexes */
RsIface &iface = getIface();
lockRsCore(); /* LOCK */
iface.lockData(); /* LOCK */
/* call to filedexserver */
int val = server->RequestDirDetails(ref, details, flags);
/* done! */
/* unlock Mutexes */
iface.unlockData(); /* UNLOCK */
unlockRsCore(); /* UNLOCK */
return val;
}
int RsServer::SearchKeywords(std::list<std::string> keywords, std::list<FileDetail> &results)
{
/* lock Mutexes */
RsIface &iface = getIface();
lockRsCore(); /* LOCK */
iface.lockData(); /* LOCK */
/* call to filedexserver */
int val = server->SearchKeywords(keywords, results);
/* done! */
/* unlock Mutexes */
iface.unlockData(); /* UNLOCK */
unlockRsCore(); /* UNLOCK */
return val;
}
int RsServer::SearchBoolExp(Expression *exp, std::list<FileDetail> &results)
{
/* lock Mutexes */
RsIface &iface = getIface();
lockRsCore(); /* LOCK */
iface.lockData(); /* LOCK */
/* call to filedexserver */
int val = server->SearchBoolExp(exp, results);
/* done! */
/* unlock Mutexes */
iface.unlockData(); /* UNLOCK */
unlockRsCore(); /* UNLOCK */
return val;
}

View file

@ -0,0 +1,740 @@
/*
* "$Id: p3face-msgs.cc,v 1.7 2007-05-05 16:10:06 rmf24 Exp $"
*
* RetroShare C++ Interface.
*
* Copyright 2004-2006 by Robert Fernie.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
#include "rsserver/p3face.h"
#include "util/rsdir.h"
#include <iostream>
#include <sstream>
#include "pqi/pqidebug.h"
const int p3facemsgzone = 11453;
#include <sys/time.h>
#include <time.h>
unsigned long getMsgId(RsMsgId &id)
{
unsigned long mid = 0;
mid = (id.data[0] << 24);
mid |= (id.data[1] << 16);
mid |= (id.data[2] << 8);
mid |= id.data[3];
return mid;
}
void getRsMsgId(RsMsgId &rsmid, unsigned int mid)
{
/* version that uses the uniqueMsgId stored in sid */
/* 16 Bytes XXX Must be equal! */
for(int i = 0; i < CHAN_SIGN_SIZE; i++)
rsmid.data[i] = 0;
rsmid.data[0] = (0xff & (mid >> 24));
rsmid.data[1] = (0xff & (mid >> 16));
rsmid.data[2] = (0xff & (mid >> 8));
rsmid.data[3] = (0xff & (mid >> 0));
return;
}
/****************************************/
/****************************************/
/* Message Items */
int RsServer::MessageSend(MessageInfo &info)
{
/* so we send this.... */
lockRsCore(); /* LOCK */
MsgItem *msg = new MsgItem();
/* id who it is to */
msg -> p = intFindCert(info.id);
msg -> cid = msg -> p -> cid;
msg -> title = info.title;
msg -> header = info.header;
msg -> msg = info.msg;
msg -> sendTime = time(NULL);
std::list<FileInfo>::iterator it;
for(it = info.files.begin(); it != info.files.end(); it++)
{
MsgFileItem mfi;
mfi.hash = it -> hash;
mfi.name = it -> fname;
mfi.size = it -> size;
msg -> files.push_back(mfi);
}
std::cerr << "RsServer::MessageSend()" << std::endl;
msg->print(std::cerr);
server -> sendMessage(msg);
unlockRsCore(); /* UNLOCK */
UpdateAllMsgs();
return 1;
}
/****************************************/
/****************************************/
int RsServer::MessageDelete(std::string id)
{
lockRsCore(); /* LOCK */
RsMsgId uid(id);
unsigned long mid = getMsgId(uid);
std::cerr << "RsServer::MessageDelete()" << std::endl;
std::cerr << "str: " << id << std::endl;
std::cerr << "uid: " << uid << std::endl;
std::cerr << "mid: " << mid << std::endl;
server -> removeMsgId(mid);
unlockRsCore(); /* UNLOCK */
UpdateAllMsgs();
return 1;
}
int RsServer::MessageRead(std::string id)
{
lockRsCore(); /* LOCK */
RsMsgId uid(id);
unsigned long mid = getMsgId(uid);
std::cerr << "RsServer::MessageRead()" << std::endl;
std::cerr << "str: " << id << std::endl;
std::cerr << "uid: " << uid << std::endl;
std::cerr << "mid: " << mid << std::endl;
server -> markMsgIdRead(mid);
unlockRsCore(); /* UNLOCK */
// (needed?) UpdateAllMsgs();
return 1;
}
/****************************************/
/****************************************/
int RsServer::ChannelCreateNew(ChannelInfo &info)
{
UpdateAllChannels();
return 1;
}
/****************************************/
/****************************************/
int RsServer::ChannelSendMsg(ChannelInfo &info)
{
UpdateAllChannels();
return 1;
}
/****************************************/
/****************************************/
int RsServer::ChatSend(ChatInfo &ci)
{
lockRsCore(); /* LOCK */
/* send a message to all for now */
if (ci.chatflags & RS_CHAT_PRIVATE)
{
/* to only one person */
RsCertId id(ci.rsid);
cert *c = intFindCert(id);
ChatItem *item = new ChatItem();
item -> sid = getPQIsearchId();
item -> p = c;
item -> cid = c -> cid;
item -> msg = ci.msg;
item -> flags = PQI_ITEM_FLAG_PRIVATE;
server -> sendPrivateChat(item);
}
else
{
/* global */
server -> sendChat(ci.msg);
}
unlockRsCore(); /* UNLOCK */
UpdateAllChat();
return 1;
}
int RsServer::UpdateAllChat()
{
RsIface &iface = getIface();
/* lock Mutexes */
lockRsCore(); /* LOCK */
iface.lockData(); /* LOCK */
/* get any messages and push them to iface */
#if 1
// at the end here, we handle chats.
if (server -> chatChanged.Changed(0))
{
// get the items from the list.
std::list<ChatItem *> clist = server -> getChatQueue();
std::list<ChatItem *>::iterator it;
for(it = clist.begin(); it != clist.end(); it++)
{
ChatInfo ci;
initRsChatInfo((*it), ci);
iface.mChatList.push_back(ci);
delete (*it);
}
}
#endif
iface.setChanged(RsIface::Chat);
/* unlock Mutexes */
iface.unlockData(); /* UNLOCK */
unlockRsCore(); /* UNLOCK */
return 1;
}
/****************************************/
/****************************************/
int RsServer::UpdateAllMsgs()
{
NotifyBase &cb = getNotify();
cb.notifyListPreChange(NOTIFY_LIST_MESSAGELIST, NOTIFY_TYPE_MOD);
RsIface &iface = getIface();
/* lock Mutexes */
lockRsCore(); /* LOCK */
iface.lockData(); /* LOCK */
/* do stuff */
std::list<MsgItem *> &msglist = server -> getMsgList();
std::list<MsgItem *> &msgOutlist = server -> getMsgOutList();
std::list<MsgItem *>::iterator mit;
std::list<MessageInfo> &msgs = iface.mMessageList;
msgs.clear();
for(mit = msglist.begin(); mit != msglist.end(); mit++)
{
MessageInfo mi;
initRsMI(*mit, mi);
msgs.push_back(mi);
}
for(mit = msgOutlist.begin(); mit != msgOutlist.end(); mit++)
{
MessageInfo mi;
initRsMI(*mit, mi);
msgs.push_back(mi);
}
iface.setChanged(RsIface::Message);
/* unlock Mutexes */
iface.unlockData(); /* UNLOCK */
unlockRsCore(); /* UNLOCK */
cb.notifyListChange(NOTIFY_LIST_MESSAGELIST, NOTIFY_TYPE_MOD);
return 1;
}
/****************************************/
/****************************************/
int RsServer::UpdateAllChannels()
{
#ifdef PQI_USE_CHANNELS
std::list<pqichannel *> chanlist;
std::list<pqichannel *>::iterator cit;
NotifyBase &cb = getNotify();
cb.notifyListPreChange(NOTIFY_LIST_CHANNELLIST, NOTIFY_TYPE_MOD);
RsIface &iface = getIface();
/* lock Mutexes */
lockRsCore(); /* LOCK */
iface.lockData(); /* LOCK */
std::map<RsChanId, ChannelInfo> &chs = iface.mChannelMap;
server->getAvailableChannels(chanlist);
/* empty the old list */
chs.clear();
for(cit = chanlist.begin(); cit != chanlist.end(); ++cit)
{
ChannelInfo ci;
initRsCI(*cit, ci);
intAddChannel(ci);
{
std::ostringstream out;
out << "fltkserver::UpdateAllChannels() Added: ";
out << ci;
pqioutput(PQL_DEBUG_BASIC, p3facemsgzone, out.str());
}
/* then add msgs */
std::list<chanMsgSummary> summarylist;
std::list<chanMsgSummary>::iterator mit;
channelSign sign = (*cit)->getSign();
server->getChannelMsgList(sign, summarylist);
for(mit = summarylist.begin(); mit != summarylist.end(); mit++)
{
channelMsg *cm = NULL;
cm = server->getChannelMsg(sign, mit->mh);
MessageInfo msg;
initRsCMI(*cit, cm, msg);
// the files....
PQChanItem::FileList::const_iterator it;
for(it = cm->msg->files.begin(); it != cm->msg->files.end(); ++it)
{
FileInfo file;
/* add to the message */
//ChanFileDisItem *cfdi = new ChanFileDisItem(it->name, it->size);
initRsCMFI(*cit, &(*mit), &(*it), file);
msg.files.push_back(file);
msg.size += file.size;
msg.count++;
} // files loop.
ci.size += msg.size;
ci.count += ci.count;
/* add the Msg? */
intAddChannelMsg(ci.chanId, msg);
} // msg loop
} // channel loop.
/* Notify of Changes */
iface.setChanged(RsIface::Channel);
/* release Mutexes */
iface.unlockData(); /* UNLOCK */
unlockRsCore(); /* UNLOCK */
cb.notifyListChange(NOTIFY_LIST_CHANNELLIST, NOTIFY_TYPE_MOD);
#endif
return 1;
}
/**** HELPER FNS For Chat/Msg/Channel Lists ************
*
* The iface->Mutex is required to be locked
* for intAddChannel / intAddChannelMsg.
*/
void RsServer::initRsChatInfo(ChatItem *c, ChatInfo &i)
{
RsCertId id = intGetCertId((cert *) c->p);
std::ostringstream out;
out << id;
i.name = c -> p -> Name();
i.rsid = out.str();
i.msg = c -> msg;
if (c -> flags & PQI_ITEM_FLAG_PRIVATE)
{
std::cerr << "RsServer::initRsChatInfo() Chat Private!!!";
i.chatflags = RS_CHAT_PRIVATE;
}
else
{
i.chatflags = RS_CHAT_PUBLIC;
std::cerr << "RsServer::initRsChatInfo() Chat Public!!!";
}
std::cerr << std::endl;
}
#ifdef PQI_USE_CHANNELS
int RsServer::intAddChannel(ChannelInfo &info)
{
RsIface &iface = getIface();
std::map<RsChanId, ChannelInfo> &chs = iface.mChannelMap;
chs[info.chanId] = info;
return 1;
}
int RsServer::intAddChannelMsg(RsChanId id, MessageInfo &msg)
{
RsIface &iface = getIface();
std::map<RsChanId, ChannelInfo> &chs = iface.mChannelMap;
std::map<RsChanId, ChannelInfo>::iterator it = chs.find(id);
if (it != chs.end())
{
/* add the message */
/*
std::map<MsgId, MessageInfo> &msgs =
it -> second.msglist;
msgs[MsgId] = msg;
*/
std::list<MessageInfo> &msgs =
it -> second.msglist;
msgs.push_back(msg);
}
return 1;
}
RsChanId RsServer::signToChanId(const channelSign &cs) const
{
/* hackish here */
RsChanId id;
int i;
for(i = 0; i < CHAN_SIGN_SIZE; i++) /* 16 Bytes XXX Must be equal! */
id.data[i] = cs.sign[i];
return id;
}
void RsServer::initRsCI(pqichannel *in, ChannelInfo &out)
{
out.chanId = signToChanId(in -> getSign());
out.mode = in -> getMode();
out.rank = in -> getRanking();
out.chanName = in -> getName();
out.count = in -> getMsgCount();
/*
out.size = in -> getMsgSize();
*/
}
void RsServer::initRsCMI(pqichannel *chan, channelMsg *cm, MessageInfo &msg)
{
msg.title = cm->msg->title;
msg.msg = cm->msg->msg;
int i;
MsgHash h = getMsgHash(cm->msg); /* call from p3channel.h */
/* Copy MsgId over */
for(i = 0; i < CHAN_SIGN_SIZE; i++) /* 16 Bytes XXX Must be equal! */
msg.msgId.data[i] = h.sign[i];
/* init size to zero */
msg.size = 0;
msg.count = 0;
}
void RsServer::initRsCMFI(pqichannel *chan, chanMsgSummary *msg,
const PQChanItem::FileItem *cfdi,
FileInfo &file)
{
file.searchId = 0;
file.path = "";
file.fname = cfdi -> name;
file.hash = cfdi -> hash;
file.ext = "";
file.size = cfdi -> size;
file.inRecommend = false;
/* check the status */
file.status = FileInfo::kRsFiStatusNone;
/* cfdi -> status; */
if (file.status > FileInfo::kRsFiStatusNone)
{
intCheckFileStatus(file);
}
else
{
file.avail = 0;
file.rank = 0;
}
}
#endif
void RsServer::intCheckFileStatus(FileInfo &file)
{
/* see if its being transfered */
file.avail = file.size / 2;
file.rank = 0;
}
void RsServer::initRsMI(MsgItem *msg, MessageInfo &mi)
{
mi.id = intGetCertId((cert *) msg->p);
mi.msgflags = 0;
/* translate flags, if we sent it... outgoing */
if ((msg->msgflags & PQI_MI_FLAGS_OUTGOING)
|| (msg->p == sslr->getOwnCert()))
{
mi.msgflags |= RS_MSG_OUTGOING;
}
/* if it has a pending flag, then its in the outbox */
if (msg->msgflags & PQI_MI_FLAGS_PENDING)
{
mi.msgflags |= RS_MSG_PENDING;
}
if (msg->msgflags & PQI_MI_FLAGS_NEW)
{
mi.msgflags |= RS_MSG_NEW;
}
mi.srcname = msg->p->Name();
mi.title = msg->title;
mi.header = msg->header;
mi.msg = msg->msg;
mi.count = 0;
mi.size = 0;
std::list<MsgFileItem>::iterator it;
for(it = msg->files.begin(); it != msg->files.end(); it++)
{
FileInfo fi;
fi.fname = RsDirUtil::getTopDir(it->name);
fi.size = it->size;
fi.hash = it->hash;
fi.path = it->name;
mi.files.push_back(fi);
mi.count++;
mi.size += fi.size;
}
mi.ts = msg->sendTime;
#if 0
/* hash the message (nasty to put here!) */
std::ostringstream out;
msg->print(out);
char *data = strdup(out.str().c_str());
unsigned int dlen = strlen(data);
unsigned int hashsize = 1024;
unsigned char hash[hashsize];
int hsize = sslr -> hashDigest(data, dlen, hash, hashsize);
if (hsize >= CHAN_SIGN_SIZE)
{
for(int i = 0; i < CHAN_SIGN_SIZE; i++) /* 16 Bytes XXX Must be equal! */
mi.msgId.data[i] = hash[i];
}
free(data);
#else
getRsMsgId(mi.msgId, msg->sid);
#endif
}
/* Flagging Persons / Channels / Files in or out of a set (CheckLists) */
int RsServer::ClearInChat()
{
lockRsCore(); /* LOCK */
std::list<cert *>::iterator it;
std::list<cert *> &certs = sslr -> getCertList();
for(it = certs.begin(); it != certs.end(); it++)
{
(*it)->InChat(false);
}
sslr->IndicateCertsChanged();
unlockRsCore(); /* UNLOCK */
return 1;
}
/* Flagging Persons / Channels / Files in or out of a set (CheckLists) */
int RsServer::SetInChat(std::string id, bool in) /* friend : chat msgs */
{
/* so we send this.... */
lockRsCore(); /* LOCK */
RsCertId rsid(id);
cert *c = intFindCert(rsid);
if (c)
{
c->InChat(in);
sslr->IndicateCertsChanged();
std::cerr << "Set InChat(" << id << ") to " << (in ? "True" : "False") << std::endl;
}
else
{
std::cerr << "FAILED TO Set InChat(" << id << ") to " << (in ? "True" : "False") << std::endl;
}
unlockRsCore(); /* UNLOCK */
UpdateAllCerts();
return 1;
}
int RsServer::ClearInMsg()
{
lockRsCore(); /* LOCK */
std::list<cert *>::iterator it;
std::list<cert *> &certs = sslr -> getCertList();
for(it = certs.begin(); it != certs.end(); it++)
{
(*it)->InMessage(false);
}
sslr->IndicateCertsChanged();
unlockRsCore(); /* UNLOCK */
return 1;
}
int RsServer::SetInMsg(std::string id, bool in) /* friend : msgs */
{
/* so we send this.... */
lockRsCore(); /* LOCK */
RsCertId rsid(id);
cert *c = intFindCert(rsid);
if (c)
{
c->InMessage(in);
sslr->IndicateCertsChanged();
std::cerr << "Set InMsg(" << id << ") to " << (in ? "True" : "False") << std::endl;
}
else
{
std::cerr << "FAILED to Set InMsg(" << id << ") to " << (in ? "True" : "False") << std::endl;
}
unlockRsCore(); /* UNLOCK */
UpdateAllCerts();
return 1;
}
int RsServer::ClearInBroadcast()
{
return 1;
}
int RsServer::ClearInSubscribe()
{
return 1;
}
int RsServer::SetInBroadcast(std::string id, bool in) /* channel : channel broadcast */
{
return 1;
}
int RsServer::SetInSubscribe(std::string id, bool in) /* channel : subscribed channels */
{
return 1;
}
int RsServer::ClearInRecommend()
{
/* find in people ... set chat flag */
RsIface &iface = getIface();
iface.lockData(); /* LOCK IFACE */
std::list<FileInfo> &recs = iface.mRecommendList;
std::list<FileInfo>::iterator it;
for(it = recs.begin(); it != recs.end(); it++)
{
it -> inRecommend = false;
}
iface.unlockData(); /* UNLOCK IFACE */
return 1;
}
int RsServer::SetInRecommend(std::string id, bool in) /* file : recommended file */
{
/* find in people ... set chat flag */
RsIface &iface = getIface();
iface.lockData(); /* LOCK IFACE */
std::list<FileInfo> &recs = iface.mRecommendList;
std::list<FileInfo>::iterator it;
for(it = recs.begin(); it != recs.end(); it++)
{
if (it -> fname == id)
{
/* set flag */
it -> inRecommend = in;
std::cerr << "Set InRecommend (" << id << ") to " << (in ? "True" : "False") << std::endl;
}
}
iface.unlockData(); /* UNLOCK IFACE */
return 1;
}

View file

@ -0,0 +1,435 @@
/*
* "$Id: p3face-people.cc,v 1.8 2007-04-15 18:45:23 rmf24 Exp $"
*
* RetroShare C++ Interface.
*
* Copyright 2004-2006 by Robert Fernie.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
#include "dht/dhthandler.h"
#include "upnp/upnphandler.h"
#include "rsserver/p3face.h"
#include "rsserver/pqistrings.h"
#include <iostream>
#include <sstream>
#include "pqi/pqidebug.h"
#include <sys/time.h>
#include <time.h>
const int p3facenetworkzone = 4219;
/*****
int RsServer::NetworkDHTActive(bool active);
int RsServer::NetworkUPnPActive(bool active);
int RsServer::NetworkDHTStatus();
int RsServer::NetworkUPnPStatus();
********/
/* internal */
/*****
int RsServer::CheckNetworking();
int RsServer::InitNetworking();
int RsServer::InitDHT();
int RsServer::CheckDHT();
int RsServer::InitUPnP();
int RsServer::CheckUPnP();
********/
int RsServer::NetworkDHTActive(bool active)
{
lockRsCore(); /* LOCK */
server->setDHTEnabled(active);
unlockRsCore(); /* UNLOCK */
return 1;
}
int RsServer::NetworkUPnPActive(bool active)
{
lockRsCore(); /* LOCK */
server->setUPnPEnabled(active);
unlockRsCore(); /* UNLOCK */
return 1;
}
int RsServer::NetworkDHTStatus()
{
lockRsCore(); /* LOCK */
server->getDHTEnabled();
unlockRsCore(); /* UNLOCK */
return 1;
}
int RsServer::NetworkUPnPStatus()
{
lockRsCore(); /* LOCK */
server->getUPnPEnabled();
unlockRsCore(); /* UNLOCK */
return 1;
}
int RsServer::InitNetworking(std::string dhtfile)
{
InitDHT(dhtfile);
InitUPnP();
return 1;
}
int RsServer::CheckNetworking()
{
CheckDHT();
CheckUPnP();
return 1;
}
dhthandler *dhtp = NULL;
upnphandler *upnpp = NULL;
pqiAddrStore *getDHTServer()
{
return dhtp;
}
int RsServer::InitDHT(std::string file)
{
/* only startup if it is supposed to be started! */
if (server -> getDHTEnabled())
{
dhtp = new dhthandler(file);
}
else
{
dhtp = new dhthandler("");
}
/*
*
*/
dhtp -> start();
cert *c = sslr -> getOwnCert();
SetExternalPorts();
/* give it our port, and hash */
dhtp -> setOwnHash(c->Signature());
return 1;
}
int RsServer::SetExternalPorts()
{
cert *c = sslr -> getOwnCert();
/* decide on the most sensible port */
unsigned short port = ntohs(c -> serveraddr.sin_port);
if (port < 100)
{
port = ntohs(c -> localaddr.sin_port);
}
/* set for both DHT and UPnP -> so they are the same! */
if (upnpp)
upnpp -> setExternalPort(port);
if (dhtp)
dhtp -> setOwnPort(port);
return 1;
}
int RsServer::CheckDHT()
{
lockRsCore(); /* LOCK */
int i;
int ret = 1;
if (server -> getDHTEnabled())
{
/* startup if necessary */
}
else
{
/* shutdown if necessary */
}
/* for each friend */
/* add, and then check */
std::list<cert *>::iterator it;
std::list<cert *> &certs = sslr -> getCertList();
std::string emptystr("");
//int online = 0;
for(it = certs.begin(), i = 0; it != certs.end(); it++, i++)
{
cert *c = (*it);
/* skip own cert */
if (c == sslr -> getOwnCert())
{
continue;
}
if (c -> hasDHT())
{
/* ignore */
}
else
{
std::string id = c -> Signature();
dhtp -> addFriend(id);
struct sockaddr_in addr;
unsigned int flags;
if (dhtp -> addrFriend(id, addr, flags))
{
c -> setDHT(addr, flags);
/* connect attempt! */
c -> nc_timestamp = 0;
c -> WillConnect(true);
}
}
}
unlockRsCore(); /* UNLOCK */
return ret;
}
int RsServer::InitUPnP()
{
upnpp = new upnphandler();
/*
*
*/
/* set our internal address to it */
cert *c = sslr -> getOwnCert();
upnpp -> setInternalAddress(c -> localaddr);
SetExternalPorts();
upnpp -> start();
return 1;
}
int RsServer::CheckUPnP()
{
lockRsCore(); /* LOCK */
int ret = 1;
/* set our internal address to it */
cert *c = sslr -> getOwnCert();
upnpp -> setInternalAddress(c -> localaddr);
/* get the state */
upnpentry ent;
int state = upnpp -> getUPnPStatus(ent);
if (server -> getUPnPEnabled())
{
std::cerr << "UPnP ENABLED: ";
switch(state)
{
case RS_UPNP_S_ACTIVE:
std::cerr << "UPnP Forwarding already up";
break;
case RS_UPNP_S_UDP_FAILED:
std::cerr << "UPnP TCP Forwarding Ok / UDP Failed";
break;
case RS_UPNP_S_TCP_FAILED:
std::cerr << "UPnP Forwarding Failed";
break;
case RS_UPNP_S_READY:
std::cerr << "Setting up UPnP Forwarding";
upnpp -> setupUPnPForwarding();
break;
case RS_UPNP_S_UNAVAILABLE:
case RS_UPNP_S_UNINITIALISED:
std::cerr << "Error UPNP not working";
break;
}
}
else
{
std::cerr << "UPnP DISABLED: ";
/* shutdown a forward */
switch(state)
{
case RS_UPNP_S_ACTIVE:
case RS_UPNP_S_UDP_FAILED:
case RS_UPNP_S_TCP_FAILED:
std::cerr << "Shutting down UPnP Forwarding";
upnpp->shutdownUPnPForwarding();
break;
case RS_UPNP_S_READY:
std::cerr << "UPnP Forwarding already down";
break;
case RS_UPNP_S_UNAVAILABLE:
case RS_UPNP_S_UNINITIALISED:
std::cerr << "Error UPNP not working";
break;
}
}
std::cerr << std::endl;
unlockRsCore(); /* UNLOCK */
return ret;
}
/* called from update Config (inside locks) */
int RsServer::UpdateNetworkConfig(RsConfig &config)
{
upnpentry ent;
int state = upnpp -> getUPnPStatus(ent);
config.DHTActive = server -> getDHTEnabled();
config.DHTPeers = dhtp -> dhtPeers();
config.uPnPActive = server -> getUPnPEnabled();
config.uPnPState = state;
return 1;
}
/************************/
int RsServer::ConfigSetLocalAddr( std::string ipAddr, int port )
{
/* check if this is all necessary */
struct in_addr inaddr_local;
if (0 == inet_aton(ipAddr.c_str(), &inaddr_local))
{
//bad address - reset.
return 0;
}
/* fill the rsiface class */
RsIface &iface = getIface();
/* lock Mutexes */
lockRsCore(); /* LOCK */
iface.lockData(); /* LOCK */
/* a little rough and ready, should be moved to the server! */
cert *c = sslr -> getOwnCert();
/* always change the address (checked by sslr->checkNetAddress()) */
c -> localaddr.sin_addr = inaddr_local;
c -> localaddr.sin_port = htons((short) port);
sslr -> checkNetAddress();
pqih -> restart_listener();
sslr -> CertsChanged();
/* update local port address on uPnP */
upnpp -> setInternalAddress(c -> localaddr);
/* unlock Mutexes */
iface.unlockData(); /* UNLOCK */
unlockRsCore(); /* UNLOCK */
/* does its own locking */
UpdateAllConfig();
return 1;
}
int RsServer::ConfigSetExtAddr( std::string ipAddr, int port )
{
/* check if this is all necessary */
struct in_addr inaddr;
if (0 == inet_aton(ipAddr.c_str(), &inaddr))
{
//bad address - reset.
return 0;
}
/* fill the rsiface class */
RsIface &iface = getIface();
/* lock Mutexes */
lockRsCore(); /* LOCK */
iface.lockData(); /* LOCK */
/* a little rough and ready, should be moved to the server! */
cert *c = sslr -> getOwnCert();
/* always change the address (checked by sslr->checkNetAddress()) */
c -> serveraddr.sin_addr = inaddr;
c -> serveraddr.sin_port = htons((short) port);
sslr -> checkNetAddress();
sslr -> CertsChanged();
/* update the DHT/UPnP port (in_addr is auto found ) */
SetExternalPorts();
/* unlock Mutexes */
iface.unlockData(); /* UNLOCK */
unlockRsCore(); /* UNLOCK */
/* does its own locking */
UpdateAllConfig();
return 1;
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,270 @@
/*
* "$Id: p3face-server.cc,v 1.5 2007-04-15 18:45:23 rmf24 Exp $"
*
* RetroShare C++ Interface.
*
* Copyright 2004-2006 by Robert Fernie.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
#include "rsserver/p3face.h"
#include <sys/time.h>
#include <time.h>
RsServer::RsServer(RsIface &i, NotifyBase &callback)
:RsControl(i, callback)
{
return;
}
RsServer::~RsServer()
{
return;
}
/* General Internal Helper Functions
----> MUST BE LOCKED!
*/
cert *RsServer::intFindCert(RsCertId &id)
{
certsign cs;
for(int i = 0; i < RSCERTIDLEN; i++)
{
cs.data[i] = id.data[i];
}
return sslr -> findcertsign(cs);
}
RsChanId RsServer::intGetCertId(cert *c)
{
certsign cs;
RsChanId id;
sslr -> getcertsign(c, cs);
for(int i = 0; i < RSCERTIDLEN; i++)
{
id.data[i] = cs.data[i];
}
return id;
}
#ifdef WINDOWS_SYS
#include <time.h>
#include <sys/timeb.h>
#endif
static double getCurrentTS()
{
#ifndef WINDOWS_SYS
struct timeval cts_tmp;
gettimeofday(&cts_tmp, NULL);
double cts = (cts_tmp.tv_sec) + ((double) cts_tmp.tv_usec) / 1000000.0;
#else
struct _timeb timebuf;
_ftime( &timebuf);
double cts = (timebuf.time) + ((double) timebuf.millitm) / 1000.0;
#endif
return cts;
}
/* Thread Fn: Run the Core */
void RsServer::run()
{
double timeDelta = 0.25;
double minTimeDelta = 0.1; // 25;
double maxTimeDelta = 2.0;
double kickLimit = 0.5;
double avgTickRate = timeDelta;
double lastts, ts;
lastts = ts = getCurrentTS();
long lastSec = 0; /* for the slower ticked stuff */
int min = 0;
int loop = 0;
while(1)
{
#ifndef WINDOWS_SYS
usleep((int) (timeDelta * 1000000));
#else
Sleep((int) (timeDelta * 1000));
#endif
ts = getCurrentTS();
double delta = ts - lastts;
/* for the fast ticked stuff */
if (delta > timeDelta)
{
//std::cerr << "Delta: " << delta << std::endl;
//std::cerr << "Time Delta: " << timeDelta << std::endl;
//std::cerr << "Avg Tick Rate: " << avgTickRate << std::endl;
lastts = ts;
/******************************** RUN SERVER *****************/
lockRsCore();
// std::cerr << "RsServer::run() Lock() -> Run()" << std::endl;
int moreToTick = server -> tick();
unlockRsCore();
/******************************** RUN SERVER *****************/
/* adjust tick rate depending on whether there is more.
*/
avgTickRate = 0.2 * timeDelta + 0.8 * avgTickRate;
if (1 == moreToTick)
{
timeDelta = 0.9 * avgTickRate;
if (timeDelta > kickLimit)
{
/* force next tick in one sec
* if we are reading data.
*/
timeDelta = kickLimit;
avgTickRate = kickLimit;
}
}
else
{
timeDelta = 1.1 * avgTickRate;
}
/* limiter */
if (timeDelta < minTimeDelta)
{
timeDelta = minTimeDelta;
}
else if (timeDelta > maxTimeDelta)
{
timeDelta = maxTimeDelta;
}
/* Fast Updates */
/* now we have the slow ticking stuff */
/* stuff ticked once a second (but can be slowed down) */
if ((int) ts > lastSec)
{
lastSec = (int) ts;
/* Chat needs to be quick too! */
//std::cerr << "RsServer::run() UpdateAllChat()" << std::endl;
UpdateAllChat();
// every five loops (> 5 secs)
if (loop % 5 == 0)
{
// update_quick_stats();
// Update All Every 5 Seconds.
// These Update Functions do the locking themselves.
//std::cerr << "RsServer::run() Updates()" << std::endl;
// These two have been completed!
//std::cerr << "RsServer::run() UpdateAllCerts()" << std::endl;
UpdateAllCerts();
//std::cerr << "RsServer::run() UpdateAllNetwork()" << std::endl;
UpdateAllNetwork();
// currently Dummy Functions.
//std::cerr << "RsServer::run() UpdateAllTransfers()" << std::endl;
UpdateAllTransfers();
//std::cerr << "RsServer::run() UpdateAllMsgs()" << std::endl;
UpdateAllMsgs();
//std::cerr << "RsServer::run() UpdateAllChannels()" << std::endl;
UpdateAllChannels();
//std::cerr << "RsServer::run() ";
//std::cerr << "UpdateRemotePeople()"<<std::endl;
//UpdateRemotePeople();
//std::cerr << "RsServer::run() UpdateAllFiles()" << std::endl;
//UpdateAllFiles();
//std::cerr << "RsServer::run() UpdateAllConfig()" << std::endl;
UpdateAllConfig();
//std::cerr << "RsServer::run() CheckDHT()" << std::endl;
CheckNetworking();
}
// every 60 loops (> 1 min)
if (++loop >= 60)
{
loop = 0;
// save the config every 5 minutes.
if (min % 5 == 1)
{
ConfigSave();
#ifdef PQI_USE_CHANNELS
/* hack to update for now
* Only occassionally - cos disabled
*/
// channel_list_ok = false;
// update_channels();
#endif
//std::cerr << "RsServer::run() UpdateAllFiles()" << std::endl;
//UpdateAllFiles();
}
/* hour loop */
if (++min >= 60)
{
min = 0;
}
// update_dirlist();
}
// slow update tick as well.
// update();
} // end of slow tick.
} // end of only once a second.
// update graphics.. ( but only if gui is visible )
// This is also triggered in slow tick...
//if ((ui->main_win->shown()) || (ui->chatter_window->shown()))
{
// update();
}
}
return;
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,362 @@
#ifndef MRK_P3RS_INTERFACE_H
#define MRK_P3RS_INTERFACE_H
/*
* "$Id: p3face.h,v 1.9 2007-05-05 16:10:06 rmf24 Exp $"
*
* RetroShare C++ Interface.
*
* Copyright 2004-2006 by Robert Fernie.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
#include "server/filedexserver.h"
#include "pqi/pqipersongrp.h"
#include "pqi/pqissl.h"
#include "pqi/p3disc.h"
#include "rsiface/rsiface.h"
#include "rsiface/rstypes.h"
#include "util/rsthreads.h"
/* The Main Interface Class - for controlling the server */
/* The init functions are actually Defined in p3face-startup.cc
*/
RsInit *InitRsConfig();
void CleanupRsConfig(RsInit *);
int InitRetroShare(int argc, char **argv, RsInit *config);
int LoadCertificates(RsInit *config);
RsControl *createRsControl(RsIface &iface, NotifyBase &notify);
class PendingDirectory
{
public:
PendingDirectory(RsCertId in_id, const DirInfo *req_dir, int depth);
void addEntry(PQFileItem *item);
RsCertId id;
int reqDepth;
int reqTime;
DirInfo data;
};
class RsServer: public RsControl, public RsThread
{
public:
/****************************************/
/* p3face-startup.cc: init... */
virtual int StartupRetroShare(RsInit *config);
public:
/****************************************/
/* p3face.cc: main loop / util fns / locking. */
RsServer(RsIface &i, NotifyBase &callback);
virtual ~RsServer();
/* Thread Fn: Run the Core */
virtual void run();
private:
/* locking stuff */
void lockRsCore()
{
// std::cerr << "RsServer::lockRsCore()" << std::endl;
coreMutex.lock();
}
void unlockRsCore()
{
// std::cerr << "RsServer::unlockRsCore()" << std::endl;
coreMutex.unlock();
}
/* mutex */
RsMutex coreMutex;
/* General Internal Helper Functions
(Must be Locked)
*/
cert *intFindCert(RsCertId &id);
RsCertId intGetCertId(cert *c);
/****************************************/
/* p3face-people Operations */
public:
// All Public Fns, must use td::string instead of RsCertId.
// cos of windows interfacing errors...
virtual std::string NeighGetInvite();
virtual int NeighLoadPEMString(std::string pem, std::string &id);
virtual int NeighLoadCertificate(std::string fname, std::string &id);
virtual int NeighAuthFriend(std::string id, RsAuthId code);
virtual int NeighAddFriend(std::string id);
virtual int NeighGetSigners(std::string uid, char *out, int len);
/* Friend Operations */
virtual int FriendStatus(std::string id, bool accept);
virtual int FriendRemove(std::string id);
virtual int FriendConnectAttempt(std::string id);
virtual int FriendSignCert(std::string id);
virtual int FriendTrustSignature(std::string id, bool trust);
//virtual int FriendSetAddress(std::string, std::string &addr, unsigned short port);
virtual int FriendSaveCertificate(std::string id, std::string fname);
virtual int FriendSetBandwidth(std::string id, float outkB, float inkB);
virtual int FriendSetLocalAddress(std::string id, std::string addr, unsigned short port);
virtual int FriendSetExtAddress(std::string id, std::string addr, unsigned short port);
virtual int FriendSetDNSAddress(std::string id, std::string addr);
virtual int FriendSetFirewall(std::string id, bool firewalled, bool forwarded);
private:
void intNotifyCertError(RsCertId &id, std::string errstr);
void intNotifyChangeCert(RsCertId &id);
/* Internal Update Iface Fns */
int UpdateAllCerts();
int UpdateAllNetwork();
void initRsNI(cert *c, NeighbourInfo &ni); /* translate to Ext */
int ensureExtension(std::string &name, std::string def_ext);
/****************************************/
/****************************************/
/* p3face-file Operations */
public:
/* Directory Actions */
virtual int RequestDirDetails(std::string uid, std::string path,
DirDetails &details);
virtual int RequestDirDetails(void *ref, DirDetails &details, uint32_t flags);
virtual int SearchKeywords(std::list<std::string> keywords, std::list<FileDetail> &results);
virtual int SearchBoolExp(Expression *exp, std::list<FileDetail> &results);
/* Actions For Upload/Download */
// REDO these three TODO XXX .
//virtual int FileBroadcast(std::string uId, std::string src, int size);
//virtual int FileDelete(std::string, std::string);
virtual int FileRecommend(std::string fname, std::string hash, int size);
virtual int FileRequest(std::string fname, std::string hash, uint32_t size, std::string dest);
virtual int FileCancel(std::string fname, std::string hash, uint32_t size);
virtual int FileClearCompleted();
virtual int FileSetBandwidthTotals(float outkB, float inkB);
private:
int UpdateAllTransfers();
/* send requests to people */
int UpdateRemotePeople();
/****************************************/
/****************************************/
/* p3face-msg Operations */
public:
/* Message Items */
virtual int MessageSend(MessageInfo &info);
virtual int MessageDelete(std::string id);
virtual int MessageRead(std::string id);
/* Channel Items */
virtual int ChannelCreateNew(ChannelInfo &info);
virtual int ChannelSendMsg(ChannelInfo &info);
/* Chat */
virtual int ChatSend(ChatInfo &ci);
/* Flagging Persons / Channels / Files in or out of a set (CheckLists) */
virtual int SetInChat(std::string id, bool in); /* friend : chat msgs */
virtual int SetInMsg(std::string id, bool in); /* friend : msg receipients */
virtual int SetInBroadcast(std::string id, bool in); /* channel : channel broadcast */
virtual int SetInSubscribe(std::string id, bool in); /* channel : subscribed channels */
virtual int SetInRecommend(std::string id, bool in); /* file : recommended file */
virtual int ClearInChat();
virtual int ClearInMsg();
virtual int ClearInBroadcast();
virtual int ClearInSubscribe();
virtual int ClearInRecommend();
private:
/* Internal Update Iface Fns */
int UpdateAllChat();
int UpdateAllMsgs();
int UpdateAllChannels();
void initRsChatInfo(ChatItem *c, ChatInfo &i);
#ifdef PQI_USE_CHANNELS
/* Internal Helper Fns */
RsChanId signToChanId(const channelSign &cs) const;
int intAddChannel(ChannelInfo &info);
int intAddChannelMsg(RsChanId id, MessageInfo &msg);
void initRsCI(pqichannel *in, ChannelInfo &out);
void initRsCMI(pqichannel *chan, channelMsg *cm, MessageInfo &msg);
void initRsCMFI(pqichannel *chan, chanMsgSummary *msg,
const PQChanItem::FileItem *cfdi, FileInfo &file);
#endif
void intCheckFileStatus(FileInfo &file);
void initRsMI(MsgItem *msg, MessageInfo &mi);
/****************************************/
/****************************************/
public:
/****************************************/
/* RsIface Networking */
virtual int NetworkDHTActive(bool active);
virtual int NetworkUPnPActive(bool active);
virtual int NetworkDHTStatus();
virtual int NetworkUPnPStatus();
private:
/* internal */
int InitNetworking(std::string);
int CheckNetworking();
int InitDHT(std::string);
int CheckDHT();
int InitUPnP();
int CheckUPnP();
int UpdateNetworkConfig(RsConfig &config);
int SetExternalPorts();
public:
/****************************************/
/* RsIface Config */
/* Config */
virtual int ConfigAddSharedDir( std::string dir );
virtual int ConfigRemoveSharedDir( std::string dir );
virtual int ConfigSetIncomingDir( std::string dir );
virtual int ConfigSetLocalAddr( std::string ipAddr, int port );
virtual int ConfigSetExtAddr( std::string ipAddr, int port );
virtual int ConfigSetExtName( std::string addr );
virtual int ConfigSetLanConfig( bool fire, bool forw );
virtual int ConfigSetDataRates( int total, int indiv );
virtual int ConfigSetBootPrompt( bool on );
virtual int ConfigSave( );
private:
int UpdateAllConfig();
/****************************************/
private:
// The real Server Parts.
filedexserver *server;
pqipersongrp *pqih;
sslroot *sslr;
p3disc *ad;
// Worker Data.....
};
/* Helper function to convert windows paths
* into unix (ie switch \ to /) for FLTK's file chooser
*/
std::string make_path_unix(std::string winpath);
/* Initialisation Class (not publicly disclosed to RsIFace) */
class RsInit
{
public:
/* Commandline/Directory options */
/* Key Parameters that must be set before
* RetroShare will start up:
*/
std::string load_cert;
std::string load_key;
std::string passwd;
bool havePasswd; /* for Commandline password */
bool autoLogin; /* autoLogin allowed */
/* Win/Unix Differences */
char dirSeperator;
/* Directories */
std::string basedir;
std::string homePath;
/* Listening Port */
bool forceLocalAddr;
unsigned short port;
char inet[256];
/* Logging */
bool haveLogFile;
bool outStderr;
bool haveDebugLevel;
int debugLevel;
char logfname[1024];
bool firsttime_run;
bool load_trustedpeer;
std::string load_trustedpeer_file;
bool udpListenerOnly;
};
#endif

View file

@ -0,0 +1,744 @@
/*
* "$Id: pqistrings.cc,v 1.1 2007-02-19 20:08:30 rmf24 Exp $"
*
* RetroShare C++ Interface.
*
* Copyright 2004-2006 by Robert Fernie.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
#include "pqi/pqi.h"
#include "pqi/pqinetwork.h"
/**************** PQI_USE_XPGP ******************/
#if defined(PQI_USE_XPGP)
#include "pqi/xpgpcert.h"
#else /* X509 Certificates */
/**************** PQI_USE_XPGP ******************/
#include "pqi/sslcert.h"
#endif /* X509 Certificates */
/**************** PQI_USE_XPGP ******************/
#include "rsserver/pqistrings.h"
#include <iostream>
#include <iomanip>
#include <sstream>
#include <time.h>
/**************** PQI_USE_XPGP ******************/
#if defined(PQI_USE_XPGP)
std::string getXPGPInfo(XPGP *cert);
#endif /* XPGP Certificates */
/**************** PQI_USE_XPGP ******************/
std::string get_status_string(int status)
{
std::string sstr("");
if (status & PERSON_STATUS_CONNECTED)
{
sstr += "Online";
return sstr;
}
if (!(status & PERSON_STATUS_ACCEPTED))
{
sstr += "Denied Access";
return sstr;
}
if (status & PERSON_STATUS_INUSE)
{
sstr += "Connecting";
/*
if (status & PERSON_STATUS_WILL_LISTEN)
{
sstr += "Listening";
}
sstr += "/";
if (status & PERSON_STATUS_WILL_CONNECT)
{
sstr += "Connecting";
}
sstr += "]";
*/
return sstr;
}
sstr += "Unknown";
return sstr;
}
std::string get_neighbourstatus_string(Person *p)
{
// if connected - show how long
// if !autoconnected - tick to connect.
//
// if connecting.
// show time to next connect attempt.
// else show the last connect time.
std::ostringstream connstr;
connstr << "Last Conn/Recv: ";
int lct = time(NULL) - p -> lc_timestamp;
int lrt = time(NULL) - p -> lr_timestamp;
if (lct < 100000000)
{
connstr << get_timeperiod_string(lct);
}
else
{
connstr << "Never";
}
connstr << "/";
if (lrt < 100000000)
{
connstr << get_timeperiod_string(lrt);
}
else
{
connstr << "Never";
}
return connstr.str();
}
int get_lastconnecttime(Person *p)
{
std::ostringstream connstr;
int lct = time(NULL) - p -> lc_timestamp;
int lrt = time(NULL) - p -> lr_timestamp;
if (lrt < lct)
{
lct = lrt;
}
return lct;
}
std::string get_lastconnecttime_string(Person *p)
{
int lct = get_lastconnecttime(p);
if (lct < 32000000)
{
return get_timeperiod_string(lct);
}
else
{
return std::string("Never");
}
}
std::string get_autoconnect_string(Person *p)
{
// if connected - show how long
// if !autoconnected - tick to connect.
//
// if connecting.
// show time to next connect attempt.
// else show the last connect time.
std::ostringstream connstr;
Person *own = getSSLRoot() -> getOwnCert();
if (p == own)
{
connstr << "Yourself";
return connstr.str();
}
if (p -> Connected())
{
/*
long ct = p->lc_timestamp;
if (ct < p->lr_timestamp)
ct = p->lr_timestamp;
connstr << "Online: " << get_timeperiod_string(time(NULL) - ct);
*/
connstr << "Online";
}
else if (p -> Manual())
{
if (p->trustLvl < TRUST_SIGN_AUTHEN)
{
connstr << "Please Authenticate";
}
else
{
connstr << "Tick to Connect";
}
}
else
{
connstr << "Offline";
}
/*
else if (p -> WillConnect())
{
connstr << "Connect in:";
connstr << get_timeperiod_string(p->nc_timestamp - time(NULL));
}
else
{
connstr << "Last Conn:";
long ct = p->lc_timestamp;
if (ct < p->lr_timestamp)
{
ct = p->lr_timestamp;
connstr << "(I):";
}
else
{
connstr << "(O):";
}
connstr << get_timeperiod_string(time(NULL) - ct);
}
*/
return connstr.str();
}
std::string get_trust_string(Person *p)
{
std::ostringstream srvstr;
/* This is now changing to display 2 things.
*
* (1) - Proxy
* (2) - Auth Level.
*/
Person *own = getSSLRoot() -> getOwnCert();
if (p == own)
{
srvstr << "Yourself"; // Certificate";
return srvstr.str();
}
switch(p -> trustLvl)
{
case TRUST_SIGN_OWN:
srvstr << "Auth (S)"; //Good: Own Signature";
break;
case TRUST_SIGN_TRSTED:
srvstr << "Trusted (ST)"; //Good: Trusted Signer";
break;
case TRUST_SIGN_AUTHEN:
srvstr << "Auth"; //Good: Authenticated";
break;
case TRUST_SIGN_BASIC:
srvstr << "Untrusted"; // : Acquaintance ";
break;
case TRUST_SIGN_UNTRUSTED:
srvstr << "Unknown (2)";
break;
case TRUST_SIGN_UNKNOWN:
srvstr << "Unknown (1)";
break;
case TRUST_SIGN_NONE:
srvstr << "Unknown (0)";
break;
case TRUST_SIGN_BAD: /* not checked yet */
srvstr << "Not Avail";
break;
default:
srvstr << "UNKNOWN";
break;
}
return srvstr.str();
}
std::string get_server_string(Person *p)
{
std::ostringstream srvstr;
if ((0 == inaddr_cmp(p -> serveraddr, 0)) || (p -> Local()))
{
if (0 == inaddr_cmp(p -> localaddr, 0))
{
srvstr << "Unknown Addr";
}
else
{
srvstr << "L: " << inet_ntoa(p -> localaddr.sin_addr);
srvstr << ":" << ntohs(p -> localaddr.sin_port);
}
}
else
{
srvstr << "S: " << inet_ntoa(p -> serveraddr.sin_addr);
srvstr << ":" << ntohs(p -> serveraddr.sin_port);
}
// need own cert!
Person *own = getSSLRoot() -> getOwnCert();
if ((isValidNet(&(p->serveraddr.sin_addr))) &&
(!isPrivateNet(&(p->serveraddr.sin_addr))) &&
(!sameNet(&(own->localaddr.sin_addr), &(p->serveraddr.sin_addr))))
{
srvstr << " (Proxy-S) ";
}
else if ((!p->Firewalled()) && (isValidNet(&(p->localaddr.sin_addr))) &&
(!isPrivateNet(&(p->localaddr.sin_addr))) &&
(!sameNet(&(own->localaddr.sin_addr), &(p->localaddr.sin_addr))))
{
srvstr << " (Proxy-L) ";
}
return srvstr.str();
}
const int sec_per_min = 60;
const int sec_per_hour = 3600;
const int sec_per_day = 3600 * 24;
std::string get_timeperiod_string(int secs)
{
int days = secs / sec_per_day;
secs -= days * sec_per_day;
int hours = secs / sec_per_hour;
secs -= hours * sec_per_hour;
int mins = secs / sec_per_min;
secs -= mins * sec_per_min;
std::ostringstream srvstr;
if (days > 0)
{
srvstr << days << " days ";
}
else if (hours > 0)
{
srvstr << hours << ":" << mins << " hours";
}
else
{
srvstr << mins << ":" << secs << " min";
}
return srvstr.str();
}
std::string get_neighbour_info(cert *c)
{
std::ostringstream ostr;
/**************** PQI_USE_XPGP ******************/
#if defined(PQI_USE_XPGP)
ostr << getX509CNString(c -> certificate -> subject -> subject);
#else /* X509 Certificates */
/**************** PQI_USE_XPGP ******************/
ostr << getX509CNString(c -> certificate -> cert_info -> subject);
#endif /* X509 Certificates */
/**************** PQI_USE_XPGP ******************/
ostr << "\t" << get_neighbourstatus_string(c);
return ostr.str();
}
std::string get_cert_info(cert *c)
{
std::ostringstream ostr;
ostr << "************ Certificate **************" << std::endl;
/**************** PQI_USE_XPGP ******************/
#if defined(PQI_USE_XPGP)
ostr << getXPGPInfo(c -> certificate);
#else /* X509 Certificates */
/**************** PQI_USE_XPGP ******************/
ostr << getX509Info(c -> certificate);
#endif /* X509 Certificates */
/**************** PQI_USE_XPGP ******************/
ostr << "********** Connection Info ************" << std::endl;
ostr << "Local Addr : " << inet_ntoa(c -> localaddr.sin_addr);
ostr << ":" << ntohs(c -> localaddr.sin_port) << std::endl;
ostr << "Server Addr : " << inet_ntoa(c -> serveraddr.sin_addr);
ostr << ":" << ntohs(c -> serveraddr.sin_port) << std::endl;
ostr << "FLAGS: ";
if (!c -> Firewalled())
{
ostr << "Not ";
}
ostr << "Firewalled, ";
if (!c -> Forwarded())
{
ostr << "Not ";
}
ostr << "Forwarded" << std::endl;
ostr << std::endl;
ostr << "Last Connect Addr: " << inet_ntoa(c -> lastaddr.sin_addr);
ostr << ":" << ntohs(c -> lastaddr.sin_port) << std::endl;
ostr << "Last Connect Time: ";
ostr << get_timeperiod_string(time(NULL) - c -> lc_timestamp);
ostr << std::endl;
ostr << "Last Receive Time: ";
ostr << get_timeperiod_string(time(NULL) - c -> lr_timestamp);
ostr << std::endl;
ostr << std::endl;
ostr << "Next Connect in : ";
ostr << get_timeperiod_string(c->nc_timestamp - time(NULL));
ostr << std::endl;
ostr << "AutoConnect: " << get_autoconnect_string(c);
ostr << std::endl;
ostr << "***************************************" << std::endl;
return ostr.str();
}
std::string getX509Info(X509 *cert)
{
// Details from the structure
// cert_info (X509_CINF *)
// sig_alg (X509_ALGOR *)
// signature (ASN1_BIT_STRING *)
// valid (int)
// references (int)
// name (char *)
//
// random flags
//
// skid (ASN1_OCTET_STRING *)
// akid (AUTHORITY_KEYID *)
// aux (X509_CERT_AUX *)
std::string certstr;
char numstr[1000];
sprintf(numstr, "%ld", ASN1_INTEGER_get(cert -> cert_info -> version));
certstr += "Version: ";
certstr += numstr;
sprintf(numstr, "%ld", ASN1_INTEGER_get(cert ->
cert_info -> serialNumber));
certstr += "\nSerial Number: ";
certstr += numstr;
// switch(cert -> cert_info -> signature)
// {
// case STANDRD:
// certstr += "\nSig Algorithm: Standard";
// break;
// default:
// certstr += "\nSig Algorithm: Unknown";
// break;
// }
//
certstr += "\nSubject:\n";
certstr += getX509NameString(cert -> cert_info -> subject);
// Validity in Here. cert -> cert_info -> validity;
certstr += "\nIssuer:\n";
certstr += getX509NameString(cert -> cert_info -> issuer);
// Key cert -> cert_info -> key;
//
// IDS + extensions. cert -> cert_info -> issuerUID/subjectUID;
// cert -> cert_info -> extensions;
// END OF INFO.
// Next sigalg again?
// next sig...
certstr += "\nSignature:";
for(int i = 0; i < cert -> signature -> length;)
{
if (i % 128 == 0)
{
certstr += "\n\t";
}
char hbyte = 0;
for(int j = 0; (j < 4) && (i < cert -> signature -> length);
j++, i++)
{
hbyte = hbyte << 1;
if (ASN1_BIT_STRING_get_bit(cert -> signature, i) == 1)
{
hbyte++;
//std::cerr << "1";
}
else
{
//std::cerr << "0";
}
}
if (hbyte > 9)
{
certstr += ('a' + (hbyte - 10));
}
else
{
certstr += ('0' + hbyte);
}
//std::cerr << " " << i << " " << (char) ('0' + hbyte);
//std::cerr << " " << (int) hbyte << std::endl;
}
sprintf(numstr, "%d/%d", cert -> valid, cert -> references);
certstr += "\nValid/References: ";
certstr += numstr;
certstr += "\n";
// That will do for now.
return certstr;
}
/* Helper function to convert a Epoch Timestamp
* into a string.
*/
static char *TIME_FORMAT_STR_BRIEF = "%H:%M:%S";
static char *TIME_FORMAT_STR_OLDVAGUE_NOW = "%H:%M:%S";
static char *TIME_FORMAT_STR_OLDVAGUE_WEEK = "%a %H:%M";
static char *TIME_FORMAT_STR_OLDVAGUE_OLD = "%a, %d %b";
static char *TIME_FORMAT_STR_LONG = "%c";
static char *TIME_FORMAT_STR_NORMAL = "%a, %H:%M:%S";
std::string timeFormat(int epoch, int format)
{
time_t ctime = epoch;
struct tm *stime = localtime(&ctime);
size_t msize = 1024;
char space[msize];
char *fmtstr = NULL;
if (format == TIME_FORMAT_OLDVAGUE)
{
int itime = time(NULL);
int delta = abs(itime - ctime);
if (delta < 12 * 3600)
{
format = TIME_FORMAT_OLDVAGUE_NOW;
}
else if (delta < 3 * 24 * 3600)
{
format = TIME_FORMAT_OLDVAGUE_WEEK;
}
else
{
format = TIME_FORMAT_OLDVAGUE_OLD;
}
}
switch(format)
{
case TIME_FORMAT_BRIEF:
fmtstr = TIME_FORMAT_STR_BRIEF;
break;
case TIME_FORMAT_OLDVAGUE_NOW:
fmtstr = TIME_FORMAT_STR_OLDVAGUE_NOW;
break;
case TIME_FORMAT_OLDVAGUE_WEEK:
fmtstr = TIME_FORMAT_STR_OLDVAGUE_WEEK;
break;
case TIME_FORMAT_OLDVAGUE_OLD:
fmtstr = TIME_FORMAT_STR_OLDVAGUE_OLD;
break;
case TIME_FORMAT_LONG:
fmtstr = TIME_FORMAT_STR_LONG;
break;
case TIME_FORMAT_NORMAL:
default:
fmtstr = TIME_FORMAT_STR_NORMAL;
break;
}
if (fmtstr != NULL) // Short -> Only Time.
{
int usize = strftime(space, msize, fmtstr, stime);
if (usize > 0)
return std::string(space);
}
return std::string("");
}
/**************** PQI_USE_XPGP ******************/
#if defined(PQI_USE_XPGP)
std::string getXPGPInfo(XPGP *cert)
{
std::stringstream out;
long l;
int i,j;
out << "XPGP Certificate:" << std::endl;
l=XPGP_get_version(cert);
out << " Version: " << l+1 << "(0x" << l << ")" << std::endl;
out << " Subject: " << std::endl;
out << " " << getX509NameString(cert -> subject -> subject);
out << std::endl;
out << std::endl;
out << " Signatures:" << std::endl;
for(i = 0; i < sk_XPGP_SIGNATURE_num(cert->signs); i++)
{
out << "Sign[" << i << "] -> [";
XPGP_SIGNATURE *sig = sk_XPGP_SIGNATURE_value(cert->signs,i);
ASN1_BIT_STRING *signature = sig->signature;
int signlen = ASN1_STRING_length(signature);
unsigned char *signdata = ASN1_STRING_data(signature);
/* only show the first 8 bytes */
if (signlen > 8)
signlen = 8;
for(j=0;j<signlen;j++)
{
out << std::hex << std::setw(2) << (int) (signdata[j]);
if ((j+1)%16==0)
{
out << std::endl;
}
else
{
out << ":";
}
}
out << "] by:";
out << std::endl;
out << getX509NameString(sig->issuer);
out << std::endl;
out << std::endl;
}
return out.str();
}
std::string getXPGPAuthCode(XPGP *xpgp)
{
/* get the self signature -> the first signature */
std::stringstream out;
if (1 > sk_XPGP_SIGNATURE_num(xpgp->signs))
{
out.str();
}
XPGP_SIGNATURE *sig = sk_XPGP_SIGNATURE_value(xpgp->signs,0);
ASN1_BIT_STRING *signature = sig->signature;
int signlen = ASN1_STRING_length(signature);
unsigned char *signdata = ASN1_STRING_data(signature);
/* extract the authcode from the signature */
/* convert it to a string, inverse of 2 bytes of signdata */
if (signlen > 2)
signlen = 2;
int j;
for(j=0;j<signlen;j++)
{
out << std::hex << std::setprecision(2) << std::setw(2)
<< std::setfill('0') << (unsigned int) (signdata[j]);
}
return out.str();
}
std::list<std::string> getXPGPsigners(XPGP *cert)
{
std::list<std::string> signers;
int i;
for(i = 0; i < sk_XPGP_SIGNATURE_num(cert->signs); i++)
{
XPGP_SIGNATURE *sig = sk_XPGP_SIGNATURE_value(cert->signs,i);
std::string str = getX509CNString(sig->issuer);
signers.push_back(str);
std::cerr << "XPGPsigners(" << i << ")" << str << std::endl;
}
return signers;
}
#endif /* XPGP Certificates */
/**************** PQI_USE_XPGP ******************/
std::string get_cert_name(cert *c)
{
/**************** PQI_USE_XPGP ******************/
#if defined(PQI_USE_XPGP)
return getX509CNString(c->certificate->subject -> subject);
#else
return getX509CNString(c->certificate->cert_info -> subject);
#endif /* XPGP Certificates */
/**************** PQI_USE_XPGP ******************/
}
std::string get_cert_org(cert *c)
{
/**************** PQI_USE_XPGP ******************/
#if defined(PQI_USE_XPGP)
return getX509OrgString(c->certificate->subject -> subject);
#else
return getX509OrgString(c->certificate->cert_info -> subject);
#endif /* XPGP Certificates */
/**************** PQI_USE_XPGP ******************/
}
std::string get_cert_loc(cert *c)
{
/**************** PQI_USE_XPGP ******************/
#if defined(PQI_USE_XPGP)
return getX509LocString(c->certificate->subject -> subject);
#else
return getX509LocString(c->certificate->cert_info -> subject);
#endif /* XPGP Certificates */
/**************** PQI_USE_XPGP ******************/
}
std::string get_cert_country(cert *c)
{
/**************** PQI_USE_XPGP ******************/
#if defined(PQI_USE_XPGP)
return getX509CountryString(c->certificate->subject -> subject);
#else
return getX509CountryString(c->certificate->cert_info -> subject);
#endif /* XPGP Certificates */
/**************** PQI_USE_XPGP ******************/
}

View file

@ -0,0 +1,78 @@
#ifndef PQI_STRINGS_H
#define PQI_STRINGS_H
/*
* "$Id: pqistrings.h,v 1.1 2007-02-19 20:08:30 rmf24 Exp $"
*
* RetroShare C++ Interface.
*
* Copyright 2004-2006 by Robert Fernie.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
#include <string>
#include <list>
class Person;
class cert;
#include <openssl/ssl.h>
std::string get_cert_country(cert *c);
std::string get_cert_loc(cert *c);
std::string get_cert_org(cert *c);
std::string get_cert_name(cert *c);
std::string get_status_string(int status);
std::string get_autoconnect_string(Person *p);
std::string get_server_string(Person *p);
std::string get_trust_string(Person *p);
std::string get_timeperiod_string(int secs);
std::string get_cert_info(cert *c);
std::string get_neighbour_info(cert *c);
int get_lastconnecttime(Person *p);
std::string get_lastconnecttime_string(Person *p);
std::string getX509NameString(X509_NAME *name);
std::string getX509Info(X509 *cert);
std::string getX509CNString(X509_NAME *name);
#define TIME_FORMAT_BRIEF 0x001
#define TIME_FORMAT_LONG 0x002
#define TIME_FORMAT_NORMAL 0x003
#define TIME_FORMAT_OLDVAGUE 0x004
#define TIME_FORMAT_OLDVAGUE_NOW 0x005
#define TIME_FORMAT_OLDVAGUE_WEEK 0x006
#define TIME_FORMAT_OLDVAGUE_OLD 0x007
std::string timeFormat(int epoch, int format);
#if defined(PQI_USE_XPGP)
std::string getXPGPInfo(XPGP *cert);
std::string getXPGPAuthCode(XPGP *xpgp);
std::list<std::string> getXPGPsigners(XPGP *cert);
#endif /* XPGP Certificates */
#endif

View file

@ -0,0 +1,259 @@
/*
* "$Id: rsiface.cc,v 1.6 2007-04-15 18:45:23 rmf24 Exp $"
*
* RetroShare C++ Interface.
*
* Copyright 2004-2007 by Robert Fernie.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
#include "rsiface/rsiface.h"
#include "util/rsdir.h"
const NeighbourInfo *RsIface::getFriend(std::string id)
{
RsCertId cid(id);
std::map<RsCertId,NeighbourInfo>::const_iterator it;
it = mFriendMap.find(cid);
if (it == mFriendMap.end())
{
return NULL;
}
return &(it -> second);
}
const DirInfo *RsIface::getDirectory(std::string id, std::string path)
{
const DirInfo *dir = getDirectoryMod(id, path);
return dir;
}
const PersonInfo *RsIface::getPerson(std::string id)
{
const PersonInfo *pi = getPersonMod(id);
return pi;
}
PersonInfo *RsIface::getPersonMod(std::string uid)
{
RsCertId id(uid);
/* get the Root of the Directories */
std::list<PersonInfo>::iterator pit;
/* check if local */
for(pit = mLocalDirList.begin();
(pit != mLocalDirList.end()) && (pit->id != id); pit++);
if (pit == mLocalDirList.end())
{
/* check the remote ones */
for(pit = mRemoteDirList.begin();
(pit != mRemoteDirList.end()) && (pit->id != id); pit++);
/* bailout ...? */
if (pit == mRemoteDirList.end())
{
return NULL;
}
}
return &(*pit);
}
DirInfo *RsIface::getDirectoryMod(std::string uid, std::string path)
{
RsCertId id(uid);
/* get the Root of the Directories */
std::list<DirInfo>::iterator dit;
std::list<std::string> subdirs;
std::list<std::string>::iterator sit;
PersonInfo *pi = getPersonMod(uid);
if (!pi)
{
return NULL;
}
/* have the correct person now */
RsDirUtil::breakupDirList(path, subdirs);
DirInfo *node = &(pi -> rootdir);
for(sit = subdirs.begin(); sit != subdirs.end(); sit++)
{
for(dit = node->subdirs.begin();
(dit != node->subdirs.end()) &&
(dit->dirname != *sit); dit++);
if (dit == node->subdirs.end())
{
/* Directory don't exist..... */
return NULL;
}
else
{
node = &(*dit);
}
}
return node;
}
const MessageInfo *RsIface::getMessage(std::string cid_in, std::string mid_in)
{
/* check for this message */
std::list<MessageInfo>::iterator it;
RsCertId cId(cid_in);
RsMsgId mId(mid_in);
std::cerr << "RsIface::getMessage()" << std::endl;
std::cerr << "cid: " << cid_in << " -> cId " << cId << std::endl;
std::cerr << "mid: " << mid_in << " -> mId " << mId << std::endl;
for(it = mMessageList.begin(); it != mMessageList.end(); it++)
{
std::cerr << "VS: cid: " << it->id << std::endl;
std::cerr << "VS: mid: " << it->msgId << std::endl;
if ((it->id == cId) && (mId == it->msgId))
{
std::cerr << "MATCH!" << std::endl;
return &(*it);
}
}
std::cerr << "NO MATCH :(" << std::endl;
return NULL;
}
const MessageInfo *RsIface::getChannelMsg(std::string chid_in, std::string mid_in)
{
RsChanId cId(chid_in);
RsMsgId mId(mid_in);
std::map<RsChanId, ChannelInfo>::iterator it;
it = mChannelMap.find(cId);
if (it == mChannelMap.end())
{
it = mChannelOwnMap.find(cId);
if (it == mChannelOwnMap.end())
{
return NULL;
}
}
/* else */
std::list<MessageInfo>::iterator it2;
std::list<MessageInfo> &msgs = (it->second).msglist;
for(it2 = msgs.begin(); it2 != msgs.end(); it2++)
{
if (mId == it2->msgId)
{
return &(*it2);
}
}
return NULL;
}
/* set to true */
bool RsIface::setChanged(DataFlags set)
{
if ((int) set < (int) NumOfFlags)
{
/* go for it */
mChanged[(int) set ] = true;
return true;
}
return false;
}
/* leaves it */
bool RsIface::getChanged(DataFlags set)
{
if ((int) set < (int) NumOfFlags)
{
/* go for it */
return mChanged[(int) set ];
}
return false;
}
/* resets it */
bool RsIface::hasChanged(DataFlags set)
{
if ((int) set < (int) NumOfFlags)
{
/* go for it */
if (mChanged[(int) set ])
{
mChanged[(int) set ] = false;
return true;
}
}
return false;
}
/*************************** THE REAL RSIFACE (with MUTEXES) *******/
#include "util/rsthreads.h"
class RsIfaceReal: public RsIface
{
public:
RsIfaceReal(NotifyBase &callback)
:RsIface(callback)
{ return; }
virtual void lockData()
{
// std::cerr << "RsIfaceReal::lockData()" << std::endl;
return rsIfaceMutex.lock();
}
virtual void unlockData()
{
// std::cerr << "RsIfaceReal::unlockData()" << std::endl;
return rsIfaceMutex.unlock();
}
private:
RsMutex rsIfaceMutex;
};
RsIface *createRsIface(NotifyBase &cb)
{
return new RsIfaceReal(cb);
}

View file

@ -0,0 +1,320 @@
/*
* "$Id: rstypes.cc,v 1.2 2007-04-07 08:41:00 rmf24 Exp $"
*
* RetroShare C++ Interface.
*
* Copyright 2004-2006 by Robert Fernie.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
/* Insides of RetroShare interface.
* only prints stuff out at the moment
*/
#include "rsiface/rstypes.h"
#include <iostream>
#include <sstream>
#include <iomanip>
/****************************************/
RsCertId::RsCertId()
{
for(int i = 0; i < RSCERTIDLEN; i++)
{
data[i] = 0;
}
}
/**********************************************************************
* NOTE NOTE NOTE ...... XXX
* BUG in MinGW .... %hhx in sscanf overwrites 32bits, instead of 8bits.
* this means that scanf(.... &(data[15])) is running off the
* end of the buffer, and hitting data[15-18]...
* To work around this bug we are reading into proper int32s
* and then copying the data over...
*
**********************************************************************/
RsCertId::RsCertId(std::string idstr)
{
unsigned int a, b, c, d;
/* scan the string and create an id */
if (16 != sscanf(idstr.c_str(),
"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%x:%x:%x:%x",
&(data[0]), &(data[1]), &(data[2]), &(data[3]),
&(data[4]), &(data[5]), &(data[6]), &(data[7]),
&(data[8]), &(data[9]), &(data[10]), &(data[11]),
&a, &b, &c, &d))
{
std::cerr << "RsCertId() Invalid Parsing!" << std::endl;
/* clear it */
for(int i = 0; i < RSCERTIDLEN; i++)
{
data[i] = 0;
}
}
((unsigned char *) data)[12] = a;
((unsigned char *) data)[13] = b;
((unsigned char *) data)[14] = c;
((unsigned char *) data)[15] = d;
}
bool RsCertId::operator<(const RsCertId &ref) const
{
//compare the signature.
if (0 > memcmp(data, ref.data, RSCERTIDLEN))
return true;
return false;
}
bool RsCertId::operator==(const RsCertId &ref) const
{
//compare the signature.
return (0 == memcmp(data, ref.data, RSCERTIDLEN));
}
bool RsCertId::operator!=(const RsCertId &ref) const
{
//compare the signature.
return !(*this == ref);
}
std::ostream &operator<<(std::ostream &out, const RsCertId &id)
{
std::ostringstream str;
for(int i = 0; i < RSCERTIDLEN; i++)
{
if (i != 0)
{
str << ":";
}
str << std::hex << std::setw(2) << std::setfill('0')
<< std::setprecision(2);
str << (unsigned int) (((unsigned char *) (id.data))[i]);
}
out << str.str();
return out;
}
/****************************************/
/* Print Functions for Info Classes */
std::ostream &operator<<(std::ostream &out, const NeighbourInfo &info)
{
out << "Neighbour Name: " << info.name;
out << std::endl;
out << "TrustLvl: " << info.trustLvl;
out << std::endl;
out << "Status: " << info.status;
out << std::endl;
out << "Auth Code: " << info.authCode;
out << std::endl;
return out;
}
std::ostream &operator<<(std::ostream &out, const PersonInfo &info)
{
out << "Directory Listing for: " << info.name;
out << std::endl;
print(out, info.rootdir, 0);
return out;
}
std::ostream &print(std::ostream &out, const DirInfo &info, int indentLvl)
{
int i;
std::ostringstream indents;
for(i = 0; i < indentLvl; i++)
{
indents << " ";
}
out << indents.str() << "Dir: " << info.dirname << std::endl;
if (info.subdirs.size() > 0)
{
out << indents.str() << "subdirs:" << std::endl;
std::list<DirInfo>::const_iterator it;
for(it = info.subdirs.begin(); it != info.subdirs.end(); it++)
{
print(out, *it, indentLvl + 1);
}
}
if (info.files.size() > 0)
{
out << indents.str() << "files:" << std::endl;
std::list<FileInfo>::const_iterator it2;
for(it2 = info.files.begin(); it2 != info.files.end(); it2++)
{
out << indents.str() << " " << it2->fname;
out << " " << it2->size << std::endl;
}
}
return out;
}
std::ostream &operator<<(std::ostream &out, const MessageInfo &info)
{
out << "MessageInfo(TODO)";
out << std::endl;
return out;
}
std::ostream &operator<<(std::ostream &out, const ChannelInfo &info)
{
out << "ChannelInfo(TODO)";
out << std::endl;
return out;
}
std::ostream &operator<<(std::ostream &out, const ChatInfo &info)
{
out << "ChatInfo(TODO)";
out << std::endl;
return out;
}
int DirInfo::merge(const DirInfo &udir)
{
/* add in the data from the udir */
if (infoAge > udir.infoAge)
{
return -1;
}
std::list<DirInfo>::const_iterator it;
for(it = udir.subdirs.begin(); it != udir.subdirs.end(); it++)
{
update(*it);
}
std::list<FileInfo>::const_iterator it2;
for(it2 = udir.files.begin(); it2 != udir.files.end(); it2++)
{
add(*it2);
}
infoAge = udir.infoAge;
nobytes = udir.nobytes;
//nofiles = udir.nofiles;
nofiles = subdirs.size() + files.size();
return 1;
}
int DirInfo::update(const DirInfo &dir)
{
/* add in the data from the udir */
DirInfo *odir = existsPv(dir);
if (odir)
{
// leave.. dirup -> subdirs = pd.data.subdirs;
// leave.. dirup -> files = pd.data.files;
odir->infoAge = dir.infoAge;
odir->nofiles = dir.nofiles;
odir->nobytes = dir.nobytes;
}
else
{
subdirs.push_back(dir);
}
return 1;
}
bool DirInfo::exists(const DirInfo &sdir)
{
return (existsPv(sdir) != NULL);
}
DirInfo *DirInfo::existsPv(const DirInfo &sdir)
{
std::list<DirInfo>::iterator it;
for(it = subdirs.begin(); it != subdirs.end(); it++)
{
if (sdir.dirname == it->dirname)
{
return &(*it);
}
}
return NULL;
}
bool DirInfo::exists(const FileInfo &file)
{
return (existsPv(file) != NULL);
}
FileInfo *DirInfo::existsPv(const FileInfo &file)
{
std::list<FileInfo>::iterator it;
for(it = files.begin(); it != files.end(); it++)
{
if (file.fname == it->fname)
{
return &(*it);
}
}
return NULL;
}
bool DirInfo::add(const DirInfo & sdir)
{
DirInfo *entry = existsPv(sdir);
if (entry)
{
*entry = sdir;
return false;
}
else
{
subdirs.push_back(sdir);
return true;
}
}
bool DirInfo::add(const FileInfo & file)
{
FileInfo *entry = existsPv(file);
if (entry)
{
*entry = file;
return false;
}
else
{
files.push_back(file);
return true;
}
}

View file

@ -0,0 +1,59 @@
/*
* "$Id: testRsChanId.cc,v 1.1 2007-02-19 20:08:30 rmf24 Exp $"
*
* RetroShare C++ Interface.
*
* Copyright 2004-2007 by Robert Fernie.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
#include "rstypes.h"
#include <iostream>
#include <sstream>
#include <string>
int main()
{
RsCertId id;
int i;
for(i = 0; i < 16; i++)
{
id.data[i] = i+121;
}
std::cerr << "Cert Id: " << id << std::endl;
std::ostringstream out;
out << id;
std::string idstr = out.str();
std::cerr << "Cert Id (str): " << idstr << std::endl;
RsCertId id2(idstr);
std::cerr << "Cert Id2 (from str): " << id2 << std::endl;
return 1;
}