Esempio n. 1
0
// Thread that processes the accepted TCP connection
void TCPAcceptedThread(THREAD *t, void *param)
{
	TCP_ACCEPTED_PARAM *data;
	LISTENER *r;
	SOCK *s;
	CONNECTION *c;
	bool flag1;
	char tmp[128];
	// Validate arguments
	if (t == NULL || param == NULL)
	{
		return;
	}

	// Initialize
	data = (TCP_ACCEPTED_PARAM *)param;
	r = data->r;
	s = data->s;
	AddRef(r->ref);
	AddRef(s->ref);

	// Create a connection
	c = NewServerConnection(r->Cedar, s, t);

	// Register to Cedar as a transient connection
	AddConnection(c->Cedar, c);

	NoticeThreadInit(t);

	AcceptInit(s);
	StrCpy(c->ClientHostname, sizeof(c->ClientHostname), s->RemoteHostname);
	IPToStr(tmp, sizeof(tmp), &s->RemoteIP);
	if (IS_SPECIAL_PORT(s->RemotePort) == false)
	{
		SLog(r->Cedar, "LS_LISTENER_ACCEPT", r->Port, tmp, s->RemoteHostname, s->RemotePort);
	}

	// Reception
	ConnectionAccept(c);
	flag1 = c->flag1;

	// Release
	SLog(r->Cedar, "LS_CONNECTION_END_1", c->Name);
	ReleaseConnection(c);

	// Release
	if (flag1 == false)
	{
		Debug("%s %u flag1 == false\n", __FILE__, __LINE__);
		IPToStr(tmp, sizeof(tmp), &s->RemoteIP);

		if (IS_SPECIAL_PORT(s->RemotePort) == false)
		{
			SLog(r->Cedar, "LS_LISTENER_DISCONNECT", tmp, s->RemotePort);
		}
		Disconnect(s);
	}
	ReleaseSock(s);
	ReleaseListener(r);
}
Esempio n. 2
0
// Stop all connections
void StopAllConnection(CEDAR *c)
{
	UINT num;
	UINT i;
	CONNECTION **connections;
	// Validate arguments
	if (c == NULL)
	{
		return;
	}

	LockList(c->ConnectionList);
	{
		connections = ToArray(c->ConnectionList);
		num = LIST_NUM(c->ConnectionList);
		DeleteAll(c->ConnectionList);
	}
	UnlockList(c->ConnectionList);

	for (i = 0;i < num;i++)
	{
		StopConnection(connections[i], false);
		ReleaseConnection(connections[i]);
	}
	Free(connections);
}
Esempio n. 3
0
int DBSpool::ExecuteSQL(const string& strSQL, RESDataList* res, int &iRelt, unsigned long long* iInsertId)
{
    MYSQL_RES* SQLRes = NULL;
    short shIdt = 0;
    int iRet = ExecuteSQL(strSQL, &SQLRes, shIdt, iRelt, iInsertId);
    if (iRet == SQL_TYPE_SELECT){
        //MYSQL_FIELD* fields;
        MYSQL_ROW row;
        int irows, ifields;
        string strValue;
        irows = mysql_num_rows(SQLRes);
        if(irows == 0) {
            ReleaseConnection(shIdt);
            return iRet;
        }
        ifields = mysql_num_fields(SQLRes);
        if(ifields == 0){
            ReleaseConnection(shIdt);
            return iRet;
        }
        //fields = mysql_fetch_fields(SQLRes);
        for(int i=0; i<irows; i++){
            if((row = mysql_fetch_row(SQLRes)) == NULL){
                break;
            }
            for(int j=0; j<ifields; j++) {
                if (row[j]){
                    strValue = row[j];
                }else{
                    strValue = "";
                }
                res->push_back(strValue);
            }
        }
        ReleaseConnection(shIdt);
    }
    return iRet;
}
Esempio n. 4
0
// Bridging thread
void BrBridgeThread(THREAD *thread, void *param)
{
	BRIDGE *b;
	CONNECTION *c;
	SESSION *s;
	HUB *h;
	char name[MAX_SIZE];
	// Validate arguments
	if (thread == NULL || param == NULL)
	{
		return;
	}

	b = (BRIDGE *)param;

	// Create a connection object
	c = NewServerConnection(b->Cedar, NULL, thread);
	c->Protocol = CONNECTION_HUB_BRIDGE;

	// Create a session object
	s = NewServerSession(b->Cedar, c, b->Hub, BRIDGE_USER_NAME, b->Policy);
	HLog(b->Hub, "LH_START_BRIDGE", b->Name, s->Name);
	StrCpy(name, sizeof(name), b->Name);
	h = b->Hub;
	AddRef(h->ref);
	s->BridgeMode = true;
	s->Bridge = b;
	c->Session = s;
	ReleaseConnection(c);

	// Dummy user name for local-bridge
	s->Username = CopyStr(BRIDGE_USER_NAME_PRINT);

	b->Session = s;
	AddRef(s->ref);

	// Notify completion
	NoticeThreadInit(thread);

	// Main procedure of the session
	Debug("Bridge %s Start.\n", b->Name);
	SessionMain(s);
	Debug("Bridge %s Stop.\n", b->Name);

	HLog(h, "LH_STOP_BRIDGE", name);

	ReleaseHub(h);

	ReleaseSession(s);
}
Esempio n. 5
0
// Link server thread
void LinkServerSessionThread(THREAD *t, void *param)
{
	LINK *k = (LINK *)param;
	CONNECTION *c;
	SESSION *s;
	POLICY *policy;
	wchar_t name[MAX_SIZE];
	// Validate arguments
	if (t == NULL || param == NULL)
	{
		return;
	}

	// Create a server connection
	c = NewServerConnection(k->Cedar, NULL, t);
	c->Protocol = CONNECTION_HUB_LINK_SERVER;

	// Create a policy
	policy = ZeroMalloc(sizeof(POLICY));
	Copy(policy, k->Policy, sizeof(POLICY));

	// Create a server session
	s = NewServerSession(k->Cedar, c, k->Hub, LINK_USER_NAME, policy);
	s->LinkModeServer = true;
	s->Link = k;
	c->Session = s;
	ReleaseConnection(c);

	// User name
	s->Username = CopyStr(LINK_USER_NAME_PRINT);

	k->ServerSession = s;
	AddRef(k->ServerSession->ref);

	// Notify the initialization completion
	NoticeThreadInit(t);

	UniStrCpy(name, sizeof(name), k->Option->AccountName);
	HLog(s->Hub, "LH_LINK_START", name, s->Name);

	// Main function of session
	SessionMain(s);

	HLog(s->Hub, "LH_LINK_STOP", name);

	ReleaseSession(s);
}
Esempio n. 6
0
// リンクサーバースレッド
void LinkServerSessionThread(THREAD *t, void *param)
{
	LINK *k = (LINK *)param;
	CONNECTION *c;
	SESSION *s;
	POLICY *policy;
	wchar_t name[MAX_SIZE];
	// 引数チェック
	if (t == NULL || param == NULL)
	{
		return;
	}

	// サーバーコネクションの作成
	c = NewServerConnection(k->Cedar, NULL, t);
	c->Protocol = CONNECTION_HUB_LINK_SERVER;

	// ポリシーの作成
	policy = ZeroMalloc(sizeof(POLICY));
	Copy(policy, k->Policy, sizeof(POLICY));

	// サーバーセッションの作成
	s = NewServerSession(k->Cedar, c, k->Hub, LINK_USER_NAME, policy);
	s->LinkModeServer = true;
	s->Link = k;
	c->Session = s;
	ReleaseConnection(c);

	// ユーザー名
	s->Username = CopyStr(LINK_USER_NAME_PRINT);

	k->ServerSession = s;
	AddRef(k->ServerSession->ref);

	// 初期化完了を通知
	NoticeThreadInit(t);

	UniStrCpy(name, sizeof(name), k->Option->AccountName);
	HLog(s->Hub, "LH_LINK_START", name, s->Name);

	// セッションのメイン関数
	SessionMain(s);

	HLog(s->Hub, "LH_LINK_STOP", name);

	ReleaseSession(s);
}
Esempio n. 7
0
//============================================================================
//		NDBHandlePool::Execute : Execute a query.
//----------------------------------------------------------------------------
NStatus NDBHandlePool::Execute(const NDBQuery &theQuery, const NDBResultFunctor &theResult, NTime waitFor)
{	NDBHandle		*dbHandle;
	NStatus			theErr;



	// Execute the query
	theErr = kNErrMemory;
	
	if (AcquireConnection(dbHandle))
		{
		theErr = dbHandle->Execute(theQuery, theResult, waitFor);
		ReleaseConnection(dbHandle);
		}
	
	return(theErr);
}
Esempio n. 8
0
// Delete connection from Cedar
void DelConnection(CEDAR *cedar, CONNECTION *c)
{
	// Validate arguments
	if (cedar == NULL || c == NULL)
	{
		return;
	}

	LockList(cedar->ConnectionList);
	{
		Debug("Connection %s Deleted from Cedar.\n", c->Name);
		if (Delete(cedar->ConnectionList, c))
		{
			ReleaseConnection(c);
		}
	}
	UnlockList(cedar->ConnectionList);
}
Esempio n. 9
0
// Layer-3 interface thread
void L3IfThread(THREAD *t, void *param)
{
	L3IF *f;
	CONNECTION *c;
	SESSION *s;
	POLICY *policy;
	char tmp[MAX_SIZE];
	char name[MAX_SIZE];
	char username[MAX_SIZE];
	// Validate arguments
	if (t == NULL || param == NULL)
	{
		return;
	}

	f = (L3IF *)param;

	StrCpy(username, sizeof(username), L3_USERNAME);
	if (f->Switch != NULL)
	{
		StrCat(username, sizeof(username), f->Switch->Name);
	}

	// Create a connection
	c = NewServerConnection(f->Switch->Cedar, NULL, t);
	c->Protocol = CONNECTION_HUB_LAYER3;

	// Create a Session
	policy = ClonePolicy(GetDefaultPolicy());
	// Not to limit the number of broadcast by policy
	policy->NoBroadcastLimiter = true;
	s = NewServerSession(f->Switch->Cedar, c, f->Hub, username, policy);
	c->Session = s;

	ReleaseConnection(c);

	// Determine the name of the session
	GetMachineHostName(tmp, sizeof(tmp));
	if (f->Switch->Cedar->Server->ServerType == SERVER_TYPE_STANDALONE)
	{
		Format(name, sizeof(name), "SID-L3-%s-%u", f->Switch->Name, Inc(f->Hub->SessionCounter));
	}
	else
	{
		Format(name, sizeof(name), "SID-L3-%s-%s-%u", tmp, f->Switch->Name, Inc(f->Hub->SessionCounter));
	}
	ConvertSafeFileName(name, sizeof(name), name);
	StrUpper(name);

	Free(s->Name);
	s->Name = CopyStr(name);

	s->L3SwitchMode = true;
	s->L3If = f;

	if (s->Username != NULL)
	{
		Free(s->Username);
	}
	s->Username = CopyStr(username);

	StrCpy(s->UserNameReal, sizeof(s->UserNameReal), username);

	f->Session = s;
	AddRef(s->ref);

	// Notify the initialization completion
	NoticeThreadInit(t);

	// Session main process
	SessionMain(s);

	// Release the session
	ReleaseSession(s);
}
Esempio n. 10
0
int ConnectionServer::Svc()
{
	int fd,new_fd;

	_info("ConnectionServer::Svc() begin !\n");
	while (!_stop_task)
	{
		pthread_mutex_lock(&_epoll_mutex);
		if (_stop_task) 
		{
			pthread_mutex_unlock(&_epoll_mutex);
			
			_debug("ConnectionServer::Svc() get exit pipe signal !\n");
			break;
		}

		if (_epoll_ready_event_num <= 0){
			//fprintf(stderr,"ConnectionServer::Svc() #### wait !\n");
			_epoll_ready_event_num = epoll_wait(_epoll_fd, _epoll_ready_event, MAX_EPOLL_EVENT_NUM, -1);
		}
		if (_epoll_ready_event_num-- < 0)
		{
			pthread_mutex_unlock(&_epoll_mutex);
			if (errno == EINTR){
				
				_debug("ConnectionServer::Svc() errno == EINTR !\n");
				continue;
			}else{
				//_debug("ConnectionServer::Svc() errno == EINTR else !\n");
				break;
			}
		}

		//fprintf(stderr,"ConnectionServer::Svc() #### 1 !\n");
		fd = _epoll_ready_event[_epoll_ready_event_num].data.fd;
		if (fd == _socket_server_listen)
		{
			while ((new_fd = accept(fd, NULL, NULL)) >= 0)
			{
                                pthread_mutex_lock(&_connections_mutex);
				if(_now_connections>=MAX_FD_NUM)
				{
					_debug("new connection error,because the max connection(%d) is achieved\n",MAX_FD_NUM);
					::close(new_fd);
					//pthread_mutex_unlock(&_connections_mutex);
				}
				else if(new_fd >= MAX_FD_NUM) {
					_debug("new connection error,because the max fd(%d) is achieved\n",new_fd);
					::close(new_fd);
					pthread_mutex_unlock(&_connections_mutex);
				}
				else
				{                                                       
                                	_now_connections++;
					pthread_mutex_unlock(&_connections_mutex);
					set_socket(new_fd, O_NONBLOCK);
					//create_handle(new_fd,LISTEN_PORT_1);
					CreateConnection(new_fd,READABLE_SOCKET);
					add_input_fd(new_fd);
					//WEBSEARCH_DEBUG((LM_DEBUG,"[new connection on socket(%d)]\n", new_fd));
                  		}
			}
			pthread_mutex_unlock(&_epoll_mutex);
			//fprintf(stderr,"ConnectionServer::Svc() fd == _socket_server_listen continue !\n");
			continue;
		}
		if (fd == _pipe_read)
		{
			pthread_mutex_unlock(&_epoll_mutex);
			continue;
		}

		del_input_fd(fd);
		pthread_mutex_unlock(&_epoll_mutex);
		
		Connection* conn = GetConnection(fd);
		

		if (conn && impl_ && !(impl_->Run((void*)conn)<0)){
			add_input_fd(fd);
		}else {
			if (conn){
          _err("Connection error!!\n");
				ReleaseConnection(conn);
			}
		}
			
	}
	_info("ConnectionServer::Svc() exit !\n");
	return 0;
}
Esempio n. 11
0
void ConnectionServer::ErrorHandle(class Connection* conn)
{
	if (conn)
		ReleaseConnection(conn);
};
Esempio n. 12
0
// SecureNAT server-side thread
void SnSecureNATThread(THREAD *t, void *param)
{
	SNAT *s;
	CONNECTION *c;
	SESSION *se;
	POLICY *policy;
	HUB *h;
	// Validate arguments
	if (t == NULL || param == NULL)
	{
		return;
	}

	s = (SNAT *)param;
	// Create a server connection
	c = NewServerConnection(s->Cedar, NULL, t);
	c->Protocol = CONNECTION_HUB_SECURE_NAT;

	// Apply the default policy
	policy = ClonePolicy(GetDefaultPolicy());

	// Not to limit the number of broadcast
	policy->NoBroadcastLimiter = true;

	h = s->Hub;
	AddRef(h->ref);

	// create a server session
	se = NewServerSession(s->Cedar, c, s->Hub, SNAT_USER_NAME, policy);
	se->SecureNATMode = true;
	se->SecureNAT = s;
	c->Session = se;
	ReleaseConnection(c);

	HLog(se->Hub, "LH_NAT_START", se->Name);

	// User name
	se->Username = CopyStr(SNAT_USER_NAME_PRINT);

	s->Session = se;
	AddRef(se->ref);

	// Notification initialization completion
	NoticeThreadInit(t);

	ReleaseCancel(s->Nat->Virtual->Cancel);
	s->Nat->Virtual->Cancel = se->Cancel1;
	AddRef(se->Cancel1->ref);

	if (s->Nat->Virtual->NativeNat != NULL)
	{
		CANCEL *old_cancel = NULL;

		Lock(s->Nat->Virtual->NativeNat->CancelLock);
		{
			if (s->Nat->Virtual->NativeNat->Cancel != NULL)
			{
				old_cancel = s->Nat->Virtual->NativeNat->Cancel;

				s->Nat->Virtual->NativeNat->Cancel = se->Cancel1;

				AddRef(se->Cancel1->ref);
			}
		}
		Unlock(s->Nat->Virtual->NativeNat->CancelLock);

		if (old_cancel != NULL)
		{
			ReleaseCancel(old_cancel);
		}
	}

	// Main function of the session
	Debug("SecureNAT Start.\n");
	SessionMain(se);
	Debug("SecureNAT Stop.\n");

	HLog(se->Hub, "LH_NAT_STOP");

	ReleaseHub(h);

	ReleaseSession(se);
}