bool CAllocator::SetAllocatePolicy(const CAllocatePolicy &oPolicy) { bool bForbidCustomPolicy; CAllocatePolicy oDefault; if(m_nSize || m_pArray || m_pReclaimer) return false; if(GetDefaultPolicy(oDefault, bForbidCustomPolicy) && bForbidCustomPolicy) return true; m_oAllocatePolicy = oPolicy; if(m_oAllocatePolicy.nMaxObjectNum < m_oAllocatePolicy.nInitializeObjectNum) m_oAllocatePolicy.nMaxObjectNum = m_oAllocatePolicy.nInitializeObjectNum; if(m_oAllocatePolicy.nRecycleSize < m_oAllocatePolicy.nInitializeObjectNum) m_oAllocatePolicy.nRecycleSize = m_oAllocatePolicy.nInitializeObjectNum; if(m_oAllocatePolicy.nRecycleSize > m_oAllocatePolicy.nMaxObjectNum) m_oAllocatePolicy.nRecycleSize = m_oAllocatePolicy.nMaxObjectNum; if(m_oAllocatePolicy.nRecycleSize) m_pReclaimer = new CBaseSingleList<uint8>(0); if(m_oAllocatePolicy.nInitializeObjectNum) { uint8* pShift; uint32 i, nUnitSize = m_pClass->GetInstanceSize()+sizeof(ulong); if(m_oAllocatePolicy.nMaxObjectNum == m_oAllocatePolicy.nInitializeObjectNum && m_oAllocatePolicy.nMaxObjectNum == m_oAllocatePolicy.nRecycleSize) { uint32 nSize = nUnitSize * m_oAllocatePolicy.nRecycleSize; pShift = (uint8*)CMalloc::Malloc(nSize); m_pArray = pShift; for(i=0; i<m_oAllocatePolicy.nRecycleSize; ++i) { m_pClass->ConstructInstance(pShift+sizeof(ulong)); m_pReclaimer->Push(pShift); pShift += nUnitSize; } } else { for(i=0; i<m_oAllocatePolicy.nInitializeObjectNum; ++i) { pShift = (uint8*)CMalloc::Malloc(nUnitSize); m_pClass->ConstructInstance(pShift+sizeof(ulong)); m_pReclaimer->Push(pShift); } } m_nSize = m_oAllocatePolicy.nInitializeObjectNum; } return true; }
// 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); }
// Create new local-bridge BRIDGE *BrNewBridge(HUB *h, char *name, POLICY *p, bool local, bool monitor, bool tapmode, char *tapaddr, bool limit_broadcast, LOCALBRIDGE *parent_local_bridge) { BRIDGE *b; POLICY *policy; THREAD *t; // Validate arguments if (h == NULL || name == NULL || parent_local_bridge == NULL) { return NULL; } if (p == NULL) { policy = ClonePolicy(GetDefaultPolicy()); } else { policy = ClonePolicy(p); } b = ZeroMalloc(sizeof(BRIDGE)); b->Cedar = h->Cedar; b->Hub = h; StrCpy(b->Name, sizeof(b->Name), name); b->Policy = policy; b->Local = local; b->Monitor = monitor; b->TapMode = tapmode; b->LimitBroadcast = limit_broadcast; b->ParentLocalBridge = parent_local_bridge; if (b->TapMode) { if (tapaddr != NULL && IsZero(tapaddr, 6) == false) { Copy(b->TapMacAddress, tapaddr, 6); } else { GenMacAddress(b->TapMacAddress); } } if (monitor) { // Enabling monitoring mode policy->MonitorPort = true; } if (b->LimitBroadcast == false) { // Disable broadcast limiter policy->NoBroadcastLimiter = true; } // Start thread t = NewThread(BrBridgeThread, b); WaitThreadInit(t); ReleaseThread(t); return b; }
// 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); }