function_name
stringlengths
1
80
function
stringlengths
14
10.6k
comment
stringlengths
12
8.02k
normalized_comment
stringlengths
6
6.55k
DeleteAllUserListCache
void DeleteAllUserListCache(LIST *o) { UINT i; // Validate arguments if (o == NULL) { return; } LockList(o); { for (i = 0;i < LIST_NUM(o);i++) { USERLIST *u = LIST_DATA(o, i); FreeUserListEntry(u); } DeleteAll(o); } UnlockList(o); }
/* Delete the cache of the all user list*/
Delete the cache of the all user list
FreeUserList
void FreeUserList(LIST *o) { UINT i; // Validate arguments if (o == NULL) { return; } for (i = 0;i < LIST_NUM(o);i++) { USERLIST *u = LIST_DATA(o, i); FreeUserListEntry(u); } ReleaseList(o); }
/* Release the user list*/
Release the user list
GetHubAdminOptionData
UINT GetHubAdminOptionData(RPC_ADMIN_OPTION *ao, char *name) { UINT i; // Validate arguments if (ao == NULL || name == NULL) { return INFINITE; } for (i = 0;i < ao->NumItem;i++) { ADMIN_OPTION *a = &ao->Items[i]; if (StrCmpi(a->Name, name) == 0) { return a->Value; } } return INFINITE; }
/* Get data from RPC_ADMIN_OPTION*/
Get data from RPC_ADMIN_OPTION
NewAdminOption
ADMIN_OPTION *NewAdminOption(char *name, UINT value) { ADMIN_OPTION *a; // Validate arguments if (name == NULL) { return NULL; } a = ZeroMalloc(sizeof(ADMIN_OPTION)); StrCpy(a->Name, sizeof(a->Name), name); a->Value = value; return a; }
/* Create a new ADMIN OPTION*/
Create a new ADMIN OPTION
CloneAcList
LIST *CloneAcList(LIST *o) { LIST *ret; // Validate arguments if (o == NULL) { return NULL; } ret = NewAcList(); SetAcList(ret, o); return ret; }
/* Clone the AC list*/
Clone the AC list
SetAcList
void SetAcList(LIST *o, LIST *src) { UINT i; // Validate arguments if (o == NULL || src == NULL) { return; } DelAllAc(o); for (i = 0;i < LIST_NUM(src);i++) { AC *ac = LIST_DATA(src, i); AddAc(o, ac); } }
/* Set all the AC list*/
Set all the AC list
DelAllAc
void DelAllAc(LIST *o) { UINT i; // Validate arguments if (o == NULL) { return; } for (i = 0;i < LIST_NUM(o);i++) { AC *ac = LIST_DATA(o, i); Free(ac); } DeleteAll(o); }
/* Remove all AC from the AC list*/
Remove all AC from the AC list
FreeAcList
void FreeAcList(LIST *o) { UINT i; // Validate arguments if (o == NULL) { return; } for (i = 0;i < LIST_NUM(o);i++) { AC *ac = LIST_DATA(o, i); Free(ac); } ReleaseList(o); }
/* Release the AC list*/
Release the AC list
GenerateAcStr
char *GenerateAcStr(AC *ac) { char tmp[MAX_SIZE]; char ip[64], mask[64]; if (ac == NULL) { return NULL; } IPToStr(ip, sizeof(ip), &ac->IpAddress); MaskToStr(mask, sizeof(mask), &ac->SubnetMask); if (ac->Masked == false) { Format(tmp, sizeof(tmp), "%s", ip); } else { Format(tmp, sizeof(tmp), "%s/%s", ip, mask); } return CopyStr(tmp); }
/* Generate a string that indicates the contents of the AC*/
Generate a string that indicates the contents of the AC
IsIpDeniedByAcList
bool IsIpDeniedByAcList(IP *ip, LIST *o) { UINT i; // Validate arguments if (ip == NULL || o == NULL) { return false; } if (GetGlobalServerFlag(GSF_DISABLE_AC) != 0) { return false; } for (i = 0;i < LIST_NUM(o);i++) { AC *ac = LIST_DATA(o, i); if (IsIpMaskedByAc(ip, ac)) { if (ac->Deny == false) { return false; } else { return true; } } } return false; }
/* Calculate whether the specified IP address is rejected by the access list*/
Calculate whether the specified IP address is rejected by the access list
SetAc
void SetAc(LIST *o, UINT id, AC *ac) { // Validate arguments if (o == NULL || id == 0 || ac == NULL) { return; } if (DelAc(o, id)) { AddAc(o, ac); } }
/* Set the AC*/
Set the AC
GetAc
AC *GetAc(LIST *o, UINT id) { UINT i; // Validate arguments if (o == NULL || id == 0) { return NULL; } for (i = 0;i < LIST_NUM(o);i++) { AC *ac = LIST_DATA(o, i); if (ac->Id == id) { return Clone(ac, sizeof(AC)); } } return NULL; }
/* Get the AC*/
Get the AC
DelAc
bool DelAc(LIST *o, UINT id) { UINT i; // Validate arguments if (o == NULL || id == 0) { return false; } for (i = 0;i < LIST_NUM(o);i++) { AC *ac = LIST_DATA(o, i); if (ac->Id == id) { if (Delete(o, ac)) { Free(ac); NormalizeAcList(o); return true; } } } return false; }
/* Delete the AC*/
Delete the AC
AddAc
void AddAc(LIST *o, AC *ac) { // Validate arguments if (o == NULL || ac == NULL) { return; } if (LIST_NUM(o) < MAX_HUB_ACS) { Insert(o, Clone(ac, sizeof(AC))); NormalizeAcList(o); } }
/* Add an AC to the list*/
Add an AC to the list
NormalizeAcList
void NormalizeAcList(LIST *o) { UINT i; // Validate arguments if (o == NULL) { return; } for (i = 0;i < LIST_NUM(o);i++) { AC *ac = LIST_DATA(o, i); if (IsIP6(&ac->IpAddress)) { ac->IpAddress.ipv6_scope_id = 0; } ac->Id = (i + 1); } }
/* Normalize the AC list*/
Normalize the AC list
NewAcList
LIST *NewAcList() { return NewList(CmpAc); }
/* Create a new AC list*/
Create a new AC list
CopyCrl
CRL *CopyCrl(CRL *crl) { CRL *ret; // Validate arguments if (crl == NULL) { return NULL; } ret = ZeroMalloc(sizeof(CRL)); if (crl->Serial != NULL) { ret->Serial = NewXSerial(crl->Serial->data, crl->Serial->size); } ret->Name = CopyName(crl->Name); Copy(ret->DigestMD5, crl->DigestMD5, MD5_SIZE); Copy(ret->DigestSHA1, crl->DigestSHA1, SHA1_SIZE); return ret; }
/* Copy the CRL*/
Copy the CRL
FreeCrl
void FreeCrl(CRL *crl) { // Validate arguments if (crl == NULL) { return; } if (crl->Serial != NULL) { FreeXSerial(crl->Serial); } if (crl->Name != NULL) { FreeName(crl->Name); } Free(crl); }
/* Release the CRL*/
Release the CRL
IsValidCertInHub
bool IsValidCertInHub(HUB *h, X *x) { bool ret; // Validate arguments if (h == NULL || x == NULL) { return false; } if (h->HubDb == NULL) { return false; } LockList(h->HubDb->CrlList); { ret = IsCertMatchCrlList(x, h->HubDb->CrlList); } UnlockList(h->HubDb->CrlList); if (ret) { // This is invalid because it was matched return false; } // This is valid because it wasn't matched return true; }
/* Check whether the certificate has been disabled by searching the CRL list of Virtual HUB*/
Check whether the certificate has been disabled by searching the CRL list of Virtual HUB
IsCertMatchCrlList
bool IsCertMatchCrlList(X *x, LIST *o) { UINT i; // Validate arguments if (x == NULL || o == NULL) { return false; } for (i = 0;i < LIST_NUM(o);i++) { CRL *crl = LIST_DATA(o, i); if (IsCertMatchCrl(x, crl)) { return true; } } return false; }
/* Search whether the certificate matches the CRL list*/
Search whether the certificate matches the CRL list
GetHubAdminOptionHelpString
wchar_t *GetHubAdminOptionHelpString(char *name) { char tmp[MAX_SIZE]; wchar_t *ret; // Validate arguments if (name == NULL) { return L""; } Format(tmp, sizeof(tmp), "HUB_AO_%s", name); ret = _UU(tmp); if (UniIsEmptyStr(ret)) { ret = _UU("HUB_AO_UNKNOWN"); } return ret; }
/* Get the help string of administration options*/
Get the help string of administration options
DeleteAllHubAdminOption
void DeleteAllHubAdminOption(HUB *h, bool lock) { UINT i; // Validate arguments if (h == NULL) { return; } if (lock) { LockList(h->AdminOptionList); } for (i = 0;i < LIST_NUM(h->AdminOptionList);i++) { Free(LIST_DATA(h->AdminOptionList, i)); } DeleteAll(h->AdminOptionList); if (lock) { UnlockList(h->AdminOptionList); } }
/* Delete all administration options of Virtual HUB*/
Delete all administration options of Virtual HUB
GetHubAdminOptionEx
UINT GetHubAdminOptionEx(HUB *h, char *name, UINT default_value) { UINT ret = default_value; // Validate arguments if (h == NULL || name == NULL) { return 0; } LockList(h->AdminOptionList); { ADMIN_OPTION *a, t; Zero(&t, sizeof(t)); StrCpy(t.Name, sizeof(t.Name), name); Trim(t.Name); a = Search(h->AdminOptionList, &t); if (a != NULL) { ret = a->Value; } } UnlockList(h->AdminOptionList); return ret; }
/* Get the administration options for the virtual HUB*/
Get the administration options for the virtual HUB
StartHubWatchDog
void StartHubWatchDog(HUB *h) { THREAD *t; // Validate arguments if (h == NULL) { return; } h->HaltWatchDog = false; h->WatchDogEvent = NewEvent(); t = NewThread(HubWatchDogThread, h); WaitThreadInit(t); ReleaseThread(t); }
/* Start the watchdog*/
Start the watchdog
StopHubWatchDog
void StopHubWatchDog(HUB *h) { // Validate arguments if (h == NULL) { return; } h->HaltWatchDog = true; Set(h->WatchDogEvent); WaitThread(h->WatchDogThread, INFINITE); ReleaseThread(h->WatchDogThread); h->WatchDogThread = NULL; h->HaltWatchDog = false; ReleaseEvent(h->WatchDogEvent); h->WatchDogEvent = NULL; }
/* Stop the watchdog*/
Stop the watchdog
EnableSecureNAT
void EnableSecureNAT(HUB *h, bool enable) { EnableSecureNATEx(h, enable, false); }
/* Enable / disable the SecureNAT*/
Enable / disable the SecureNAT
AddAccessList
void AddAccessList(HUB *hub, ACCESS *a) { AddAccessListEx(hub, a, false, false); }
/* Adding Access List*/
Adding Access List
InitAccessList
void InitAccessList(HUB *hub) { // Validate arguments if (hub == NULL) { return; } hub->AccessList = NewList(CmpAccessList); }
/* Initialize the access list*/
Initialize the access list
FreeAccessList
void FreeAccessList(HUB *hub) { UINT i; // Validate arguments if (hub == NULL) { return; } for (i = 0;i < LIST_NUM(hub->AccessList);i++) { ACCESS *a = LIST_DATA(hub->AccessList, i); Free(a); } ReleaseList(hub->AccessList); hub->AccessList = NULL; }
/* Release the access list*/
Release the access list
MakeSimpleUsernameRemoveNtDomain
void MakeSimpleUsernameRemoveNtDomain(char *dst, UINT dst_size, char *src) { char tmp1[MAX_SIZE]; char tmp2[MAX_SIZE]; // Validate arguments if (dst == NULL || src == NULL) { return; } ParseNtUsername(src, tmp1, sizeof(tmp1), tmp2, sizeof(tmp2), false); StrCpy(dst, dst_size, tmp1); }
/* Generate a user name without domain name of the Windows NT*/
Generate a user name without domain name of the Windows NT
UsernameToInt64
UINT64 UsernameToInt64(char *name) { UCHAR hash[SHA1_SIZE]; UINT64 ret; char tmp[MAX_USERNAME_LEN + 1]; // Validate arguments if (name == 0 || IsEmptyStr(name)) { return 0; } if (StartWith(name, ACCESS_LIST_INCLUDED_PREFIX) || StartWith(name, ACCESS_LIST_EXCLUDED_PREFIX)) { return Rand64(); } MakeSimpleUsernameRemoveNtDomain(tmp, sizeof(tmp), name); Trim(tmp); StrUpper(tmp); if (StrLen(tmp) == 0) { return 0; } Sha0(hash, tmp, StrLen(tmp)); Copy(&ret, hash, sizeof(ret)); return ret; }
/* Convert the user name to UINT*/
Convert the user name to UINT
GetSessionByName
SESSION *GetSessionByName(HUB *hub, char *name) { // Validate arguments if (hub == NULL || name == NULL) { return NULL; } LockList(hub->SessionList); { UINT i; for (i = 0;i < LIST_NUM(hub->SessionList);i++) { SESSION *s = LIST_DATA(hub->SessionList, i); if (StrCmpi(s->Name, name) == 0) { // Found AddRef(s->ref); UnlockList(hub->SessionList); return s; } } } UnlockList(hub->SessionList); return NULL; }
/* Search the session from the session name*/
Search the session from the session name
HubPaGetCancel
CANCEL *HubPaGetCancel(SESSION *s) { HUB_PA *pa = (HUB_PA *)s->PacketAdapter->Param; AddRef(pa->Cancel->ref); return pa->Cancel; }
/* Get the cancel object*/
Get the cancel object
HubPaGetNextPacket
UINT HubPaGetNextPacket(SESSION *s, void **data) { UINT ret = 0; HUB_PA *pa = (HUB_PA *)s->PacketAdapter->Param; // Get one from the head of the queue LockQueue(pa->PacketQueue); { BLOCK *block = GetNext(pa->PacketQueue); if (block == NULL) { // No queue ret = 0; } else { if (block->IsFlooding) { CedarAddCurrentTcpQueueSize(s->Cedar, -((int)block->Size)); } // Found *data = block->Buf; ret = block->Size; // Release the memory of the structure of the block Free(block); } } UnlockQueue(pa->PacketQueue); return ret; }
/* Get the packet to be transmitted next*/
Get the packet to be transmitted next
IsIPManagementTargetForHUB
bool IsIPManagementTargetForHUB(IP *ip, HUB *hub) { // Validate arguments if (ip == NULL || hub == NULL) { return false; } if (hub->Option == NULL) { return true; } if (IsIP4(ip)) { if (hub->Option->ManageOnlyPrivateIP) { if (IsIPPrivate(ip) == false) { return false; } } } else { if (hub->Option->ManageOnlyLocalUnicastIPv6) { UINT ip_type = GetIPAddrType6(ip); if (!(ip_type & IPV6_ADDR_LOCAL_UNICAST)) { return false; } } } return true; }
/* Confirm whether the specified IP address is managed by Virtual HUB*/
Confirm whether the specified IP address is managed by Virtual HUB
DeleteOldIpTableEntry
void DeleteOldIpTableEntry(LIST *o) { UINT i; IP_TABLE_ENTRY *old = NULL; // Validate arguments if (o == NULL) { return; } for (i = 0;i < LIST_NUM(o);i++) { IP_TABLE_ENTRY *e = LIST_DATA(o, i); old = e; } if (old != NULL) { Delete(o, old); Free(old); } }
/* Delete old IP table entries*/
Delete old IP table entries
AddStormList
STORM *AddStormList(HUB_PA *pa, UCHAR *mac_address, IP *src_ip, IP *dest_ip, bool strict) { STORM *s; // Validate arguments if (pa == NULL || mac_address == NULL) { return NULL; } s = ZeroMalloc(sizeof(STORM)); if (src_ip != NULL) { Copy(&s->SrcIp, src_ip, sizeof(IP)); } if (dest_ip != NULL) { Copy(&s->DestIp, dest_ip, sizeof(IP)); } Copy(s->MacAddress, mac_address, 6); s->StrictMode = strict; Insert(pa->StormList, s); return s; }
/* Add to Storm list*/
Add to Storm list
IntoTrafficLimiter
void IntoTrafficLimiter(TRAFFIC_LIMITER *tr, PKT *p) { UINT64 now = Tick64(); // Validate arguments if (tr == NULL || p == NULL) { return; } if (tr->LastTime == 0 || tr->LastTime > now || (tr->LastTime + LIMITER_SAMPLING_SPAN) < now) { // Sampling initialization tr->Value = 0; tr->LastTime = now; } // Value increase tr->Value += (UINT64)p->PacketSize * (UINT64)8; }
/* Add a packet to traffic limiter*/
Add a packet to traffic limiter
StorePacketFilterByTrafficLimiter
bool StorePacketFilterByTrafficLimiter(SESSION *s, PKT *p) { HUB_PA *pa; TRAFFIC_LIMITER *tr; // Validate arguments if (s == NULL || p == NULL) { return false; } if (s->Policy->MaxUpload == 0) { // Unlimited return true; } pa = (HUB_PA *)s->PacketAdapter->Param; tr = &pa->UploadLimiter; // Restrictions are not applied for priority packets if (IsMostHighestPriorityPacket(s, p)) { return true; } // Input packets to the limiter IntoTrafficLimiter(tr, p); // Compare the current bandwidth and limit value if ((tr->Value * (UINT64)1000 / (UINT64)LIMITER_SAMPLING_SPAN) > s->Policy->MaxUpload) { // Discard the packet return false; } return true; }
/* The bandwidth reduction by traffic limiter*/
The bandwidth reduction by traffic limiter
StorePacketFilter
bool StorePacketFilter(SESSION *s, PKT *packet) { // Validate arguments if (s == NULL || packet == NULL) { return false; } // The bandwidth reduction by traffic limiter if (StorePacketFilterByTrafficLimiter(s, packet) == false) { return false; } // Packet filter by policy if (StorePacketFilterByPolicy(s, packet) == false) { return false; } // The packet filter with Access Lists if (ApplyAccessListToStoredPacket(s->Hub, s, packet) == false) { return false; } return true; }
/* Filtering of packets to store*/
Filtering of packets to store
GetHubPacketAdapter
PACKET_ADAPTER *GetHubPacketAdapter() { // Hand over by creating a function list PACKET_ADAPTER *pa = NewPacketAdapter(HubPaInit, HubPaGetCancel, HubPaGetNextPacket, HubPaPutPacket, HubPaFree); return pa; }
/* Get the packet adapter for the HUB*/
Get the packet adapter for the HUB
StopAllSession
void StopAllSession(HUB *h) { SESSION **s; UINT i, num; // Validate arguments if (h == NULL) { return; } LockList(h->SessionList); { num = LIST_NUM(h->SessionList); s = ToArray(h->SessionList); DeleteAll(h->SessionList); } UnlockList(h->SessionList); for (i = 0;i < num;i++) { StopSession(s[i]); ReleaseSession(s[i]); } Free(s); }
/* Stop all the SESSION of the HUB*/
Stop all the SESSION of the HUB
DelSession
void DelSession(HUB *h, SESSION *s) { // Validate arguments if (h == NULL || s == NULL) { return; } LockList(h->SessionList); { if (Delete(h->SessionList, s)) { Debug("Session %s was Deleted from %s.\n", s->Name, h->Name); ReleaseSession(s); } } UnlockList(h->SessionList); }
/* Remove the SESSION from HUB*/
Remove the SESSION from HUB
AddSession
void AddSession(HUB *h, SESSION *s) { // Validate arguments if (h == NULL || s == NULL) { return; } LockList(h->SessionList); { Insert(h->SessionList, s); AddRef(s->ref); Debug("Session %s Inserted to %s.\n", s->Name, h->Name); if (s->InProcMode) { s->UniqueId = GetNewUniqueId(h); } } UnlockList(h->SessionList); }
/* Add a SESSION to the HUB*/
Add a SESSION to the HUB
GetNewUniqueId
UINT GetNewUniqueId(HUB *h) { UINT id; // Validate arguments if (h == NULL) { return 0; } for (id = 1;;id++) { if (SearchSessionByUniqueId(h, id) == NULL) { return id; } } }
/* Create a new unique ID of the HUB*/
Create a new unique ID of the HUB
SearchSessionByUniqueId
SESSION *SearchSessionByUniqueId(HUB *h, UINT id) { UINT i; // Validate arguments if (h == NULL) { return NULL; } for (i = 0;i < LIST_NUM(h->SessionList);i++) { SESSION *s = LIST_DATA(h->SessionList, i); if (s->UniqueId == id) { return s; } } return NULL; }
/* Search for a session by the unique session ID*/
Search for a session by the unique session ID
StopHub
void StopHub(HUB *h) { bool old_status = false; // Validate arguments if (h == NULL) { return; } old_status = h->Offline; h->HubIsOnlineButHalting = true; SetHubOffline(h); if (h->Halt == false) { SLog(h->Cedar, "LS_HUB_STOP", h->Name); h->Halt = true; } h->Offline = old_status; h->HubIsOnlineButHalting = false; }
/* Stop the operation of the HUB*/
Stop the operation of the HUB
IsHub
bool IsHub(CEDAR *cedar, char *name) { HUB *h; // Validate arguments if (cedar == NULL || name == NULL) { return false; } h = GetHub(cedar, name); if (h == NULL) { return false; } ReleaseHub(h); return true; }
/* Get whether a HUB which have the specified name exists*/
Get whether a HUB which have the specified name exists
GetHub
HUB *GetHub(CEDAR *cedar, char *name) { HUB *h, t; // Validate arguments if (cedar == NULL || name == NULL) { return NULL; } LockHubList(cedar); t.Name = name; h = Search(cedar->HubList, &t); if (h == NULL) { UnlockHubList(cedar); return NULL; } AddRef(h->ref); UnlockHubList(cedar); return h; }
/* Get the HUB*/
Get the HUB
LockHubList
void LockHubList(CEDAR *cedar) { // Validate arguments if (cedar == NULL) { return; } LockList(cedar->HubList); }
/* Lock the HUB list*/
Lock the HUB list
UnlockHubList
void UnlockHubList(CEDAR *cedar) { // Validate arguments if (cedar == NULL) { return; } UnlockList(cedar->HubList); }
/* Unlock the HUB list*/
Unlock the HUB list
ReleaseHub
void ReleaseHub(HUB *h) { // Validate arguments if (h == NULL) { return; } if (Release(h->ref) == 0) { CleanupHub(h); } }
/* Release the HUB*/
Release the HUB
GetRadiusServer
bool GetRadiusServer(HUB *hub, char *name, UINT size, UINT *port, char *secret, UINT secret_size) { UINT interval; return GetRadiusServerEx(hub, name, size, port, secret, secret_size, &interval); }
/* Get the Radius server information*/
Get the Radius server information
SetRadiusServer
void SetRadiusServer(HUB *hub, char *name, UINT port, char *secret) { SetRadiusServerEx(hub, name, port, secret, RADIUS_RETRY_INTERVAL); }
/* Set the Radius server information*/
Set the Radius server information
CompareIpTable
int CompareIpTable(void *p1, void *p2) { IP_TABLE_ENTRY *e1, *e2; if (p1 == NULL || p2 == NULL) { return 0; } e1 = *(IP_TABLE_ENTRY **)p1; e2 = *(IP_TABLE_ENTRY **)p2; if (e1 == NULL || e2 == NULL) { return 0; } return CmpIpAddr(&e1->Ip, &e2->Ip); }
/* Comparison function of IP table entries*/
Comparison function of IP table entries
GetHashOfMacTable
UINT GetHashOfMacTable(void *p) { UINT v; MAC_TABLE_ENTRY *e = p; if (e == NULL) { return 0; } v = e->MacAddress[0] + e->MacAddress[1] + e->MacAddress[2] + e->MacAddress[3] + e->MacAddress[4] + e->MacAddress[5] + e->VlanId; return v; }
/* Get hash of MAC table entry*/
Get hash of MAC table entry
CompareHub
int CompareHub(void *p1, void *p2) { HUB *h1, *h2; if (p1 == NULL || p2 == NULL) { return 0; } h1 = *(HUB **)p1; h2 = *(HUB **)p2; if (h1 == NULL || h2 == NULL) { return 0; } return StrCmpi(h1->Name, h2->Name); }
/* Comparison function of HUB*/
Comparison function of HUB
IsHubMacAddress
bool IsHubMacAddress(UCHAR *mac) { // Validate arguments if (mac == NULL) { return false; } if (mac[0] == 0x00 && mac[1] == SE_HUB_MAC_ADDR_SIGN) { return true; } return false; }
/* Examine whether the MAC address is for the ARP polling of the Virtual HUB*/
Examine whether the MAC address is for the ARP polling of the Virtual HUB
IsHubIpAddress32
bool IsHubIpAddress32(UINT ip32) { IP ip; UINTToIP(&ip, ip32); return IsHubIpAddress(&ip); }
/* Examine whether the IP address is for the ARP polling of the Virtual HUB*/
Examine whether the IP address is for the ARP polling of the Virtual HUB
GetHubMsg
wchar_t *GetHubMsg(HUB *h) { wchar_t *ret = NULL; // Validate arguments if (h == NULL) { return NULL; } Lock(h->lock); { if (h->Msg != NULL) { ret = CopyUniStr(h->Msg); } } Unlock(h->lock); return ret; }
/* Get a message from HUB*/
Get a message from HUB
SetHubMsg
void SetHubMsg(HUB *h, wchar_t *msg) { // Validate arguments if (h == NULL) { return; } Lock(h->lock); { if (h->Msg != NULL) { Free(h->Msg); h->Msg = NULL; } if (UniIsEmptyStr(msg) == false) { h->Msg = UniCopyStr(msg); } } Unlock(h->lock); }
/* Set a message to the HUB*/
Set a message to the HUB
GetHubLogSetting
void GetHubLogSetting(HUB *h, HUB_LOG *setting) { // Validate arguments if (setting == NULL || h == NULL) { return; } Copy(setting, &h->LogSetting, sizeof(HUB_LOG)); }
/* Get a log setting of the HUB*/
Get a log setting of the HUB
SetHubLogSettingEx
void SetHubLogSettingEx(HUB *h, HUB_LOG *setting, bool no_change_switch_type) { UINT i1, i2; // Validate arguments if (setting == NULL || h == NULL) { return; } i1 = h->LogSetting.PacketLogSwitchType; i2 = h->LogSetting.SecurityLogSwitchType; Copy(&h->LogSetting, setting, sizeof(HUB_LOG)); if (no_change_switch_type) { h->LogSetting.PacketLogSwitchType = i1; h->LogSetting.SecurityLogSwitchType = i2; } // Packet logger configuration SetLogSwitchType(h->PacketLogger, setting->PacketLogSwitchType); SetLogSwitchType(h->SecurityLogger, setting->SecurityLogSwitchType); }
/* Update the log settings of the HUB*/
Update the log settings of the HUB
CompareCert
int CompareCert(void *p1, void *p2) { X *x1, *x2; wchar_t tmp1[MAX_SIZE]; wchar_t tmp2[MAX_SIZE]; if (p1 == NULL || p2 == NULL) { return 0; } x1 = *(X **)p1; x2 = *(X **)p2; if (x1 == NULL || x2 == NULL) { return 0; } GetPrintNameFromX(tmp1, sizeof(tmp1), x1); GetPrintNameFromX(tmp2, sizeof(tmp2), x2); return UniStrCmpi(tmp1, tmp2); }
/* Compare the list of certificates*/
Compare the list of certificates
NewHubDb
HUBDB *NewHubDb() { HUBDB *d = ZeroMalloc(sizeof(HUBDB)); d->GroupList = NewList(CompareGroupName); d->UserList = NewList(CompareUserName); d->RootCertList = NewList(CompareCert); d->CrlList = NewList(NULL); d->AcList = NewAcList(); return d; }
/* Creating a new HUBDB*/
Creating a new HUBDB
IsIPsecSaTunnelMode
bool IsIPsecSaTunnelMode(IPSECSA *sa) { // Validate arguments if (sa == NULL) { return false; } if (sa->TransformSetting.CapsuleMode == IKE_P2_CAPSULE_TUNNEL || sa->TransformSetting.CapsuleMode == IKE_P2_CAPSULE_NAT_TUNNEL_1 || sa->TransformSetting.CapsuleMode == IKE_P2_CAPSULE_NAT_TUNNEL_2) { return true; } return false; }
/* Get whether the specified IPsec SA is in tunnel mode*/
Get whether the specified IPsec SA is in tunnel mode
ProcIPsecEtherIPPacketRecv
void ProcIPsecEtherIPPacketRecv(IKE_SERVER *ike, IKE_CLIENT *c, UCHAR *data, UINT data_size, bool is_tunnel_mode) { BLOCK *b; // Validate arguments if (ike == NULL || c == NULL || data == NULL || data_size == 0) { return; } c->IsEtherIPOnIPsecTunnelMode = is_tunnel_mode; IPsecIkeClientManageEtherIPServer(ike, c); b = NewBlock(data, data_size, 0); EtherIPProcRecvPackets(c->EtherIP, b); Free(b); }
/* An EtherIP packet has been received via an IPsec tunnel*/
An EtherIP packet has been received via an IPsec tunnel
IPsecIkeSendUdpForDebug
void IPsecIkeSendUdpForDebug(UINT dst_port, UINT dst_ip, void *data, UINT size) { SOCK *s = NewUDP(0); IP d; SetIP(&d, dst_ip, dst_ip, dst_ip, dst_ip); SendTo(s, &d, dst_port, data, size); ReleaseSock(s); }
/* Send a raw packet for debugging*/
Send a raw packet for debugging
IPsecIkeClientSendEtherIPPackets
void IPsecIkeClientSendEtherIPPackets(IKE_SERVER *ike, IKE_CLIENT *c, ETHERIP_SERVER *s) { UINT i; // Validate arguments if (ike == NULL || c == NULL || s == NULL) { return; } for (i = 0;i < LIST_NUM(s->SendPacketList);i++) { BLOCK *b = LIST_DATA(s->SendPacketList, i); // Packet transmission IPsecSendPacketByIkeClient(ike, c, b->Buf, b->Size, IPSEC_IP_PROTO_ETHERIP); FreeBlock(b); } DeleteAll(s->SendPacketList); }
/* EtherIP packet transmission (via IPsec SA tunnel)*/
EtherIP packet transmission (via IPsec SA tunnel)
MarkIkeSaAsDeleted
void MarkIkeSaAsDeleted(IKE_SERVER *ike, IKE_SA *sa) { // Validate arguments if (ike == NULL || sa == NULL) { return; } if (sa->Deleting) { return; } ike->StateHasChanged = true; sa->Deleting = true; Debug("IKE SA %I64u - %I64u has been marked as being deleted.\n", sa->InitiatorCookie, sa->ResponderCookie); SendDeleteIkeSaPacket(ike, sa->IkeClient, sa->InitiatorCookie, sa->ResponderCookie); IPsecLog(ike, NULL, sa, NULL, "LI_DELETE_IKE_SA"); }
/* Mark the IKE SA for deletion*/
Mark the IKE SA for deletion
MarkIPsecSaAsDeleted
void MarkIPsecSaAsDeleted(IKE_SERVER *ike, IPSECSA *sa) { // Validate arguments if (ike == NULL || sa == NULL) { return; } if (sa->Deleting) { return; } ike->StateHasChanged = true; sa->Deleting = true; Debug("IPsec SA 0x%X has been marked as being deleted.\n", sa->Spi); SendDeleteIPsecSaPacket(ike, sa->IkeClient, sa->Spi); IPsecLog(ike, NULL, NULL, sa, "LI_DELETE_IPSEC_SA"); }
/* Mark the IPsec SA for deletion*/
Mark the IPsec SA for deletion
SendDeleteIPsecSaPacket
void SendDeleteIPsecSaPacket(IKE_SERVER *ike, IKE_CLIENT *c, UINT spi) { IKE_PACKET_PAYLOAD *payload; BUF *buf; // Validate arguments if (ike == NULL || c == NULL || spi == 0) { return; } buf = NewBuf(); WriteBufInt(buf, spi); payload = IkeNewDeletePayload(IKE_PROTOCOL_ID_IPSEC_ESP, NewListSingle(buf)); SendInformationalExchangePacket(ike, c, payload); }
/* IPsec SA Deletion packet transmission process*/
IPsec SA Deletion packet transmission process
SendDeleteIkeSaPacket
void SendDeleteIkeSaPacket(IKE_SERVER *ike, IKE_CLIENT *c, UINT64 init_cookie, UINT64 resp_cookie) { IKE_PACKET_PAYLOAD *payload; BUF *buf; // Validate arguments if (ike == NULL || c == NULL) { return; } buf = NewBuf(); WriteBufInt64(buf, init_cookie); WriteBufInt64(buf, resp_cookie); payload = IkeNewDeletePayload(IKE_PROTOCOL_ID_IKE, NewListSingle(buf)); SendInformationalExchangePacket(ike, c, payload); }
/* IKE SA deletion packet transmission process*/
IKE SA deletion packet transmission process
SendInformationalExchangePacket
void SendInformationalExchangePacket(IKE_SERVER *ike, IKE_CLIENT *c, IKE_PACKET_PAYLOAD *payload) { SendInformationalExchangePacketEx(ike, c, payload, false, 0, 0); }
/* Information exchange packet transmission process*/
Information exchange packet transmission process
GenerateNewMessageId
UINT GenerateNewMessageId(IKE_SERVER *ike) { UINT ret; // Validate arguments if (ike == NULL) { return 0; } while (true) { ret = Rand32(); if (ret != 0 && ret != 0xffffffff) { UINT i; bool ok = true; for (i = 0;i < LIST_NUM(ike->IPsecSaList);i++) { IPSECSA *sa = LIST_DATA(ike->IPsecSaList, i); if (sa->MessageId == ret) { ok = false; break; } } if (ok) { return ret; } } } }
/* Create a new message ID*/
Create a new message ID
SearchIPsecSaByMessageId
IPSECSA *SearchIPsecSaByMessageId(IKE_SERVER *ike, IKE_CLIENT *c, UINT message_id) { UINT i; // Validate arguments if (ike == NULL || c == NULL || message_id == 0) { return NULL; } for (i = 0;i < LIST_NUM(ike->IPsecSaList);i++) { IPSECSA *sa = LIST_DATA(ike->IPsecSaList, i); if (sa->IkeClient == c) { if (sa->MessageId == message_id) { if (sa->ServerToClient == false) { if (sa->Established == false) { return sa; } } } } } return NULL; }
/* Search for IPsec SA from Message ID*/
Search for IPsec SA from Message ID
SearchClientToServerIPsecSaBySpi
IPSECSA *SearchClientToServerIPsecSaBySpi(IKE_SERVER *ike, UINT spi) { IPSECSA t; // Validate arguments if (ike == NULL || spi == 0) { return NULL; } t.ServerToClient = false; t.Spi = spi; return Search(ike->IPsecSaList, &t); }
/* Search for IPsec SA from SPI value*/
Search for IPsec SA from SPI value
SearchIkeSaByCookie
IKE_SA *SearchIkeSaByCookie(IKE_SERVER *ike, UINT64 init_cookie, UINT64 resp_cookie) { UINT i; // Validate arguments if (ike == NULL) { return NULL; } for (i = 0;i < LIST_NUM(ike->IkeSaList);i++) { IKE_SA *sa = LIST_DATA(ike->IkeSaList, i); if (sa->InitiatorCookie == init_cookie && sa->ResponderCookie == resp_cookie) { return sa; } } return NULL; }
/* Search an IKE SA from the value of the Cookie*/
Search an IKE SA from the value of the Cookie
GenerateNewIPsecSaSpi
UINT GenerateNewIPsecSaSpi(IKE_SERVER *ike, UINT counterpart_spi) { UINT ret; // Validate arguments if (ike == NULL) { return 0; } while (true) { ret = Rand32(); if (ret != counterpart_spi) { if (ret >= 4096 && ret != INFINITE) { if (SearchClientToServerIPsecSaBySpi(ike, ret) == NULL) { return ret; } } } } }
/* Generate the SPI value of new IPsec SA*/
Generate the SPI value of new IPsec SA
IkeCalcPhase2InitialIv
void IkeCalcPhase2InitialIv(void *iv, IKE_SA *sa, UINT message_id) { BUF *b; UCHAR hash[IKE_MAX_HASH_SIZE]; // Validate arguments if (iv == NULL || sa == NULL) { return; } message_id = Endian32(message_id); b = NewBuf(); WriteBuf(b, sa->Iv, sa->BlockSize); WriteBuf(b, &message_id, sizeof(UINT)); IkeHash(sa->TransformSetting.Hash, hash, b->Buf, b->Size); Copy(iv, hash, sa->TransformSetting.Crypto->BlockSize); FreeBuf(b); }
/* Calculate the initial IV for Phase 2*/
Calculate the initial IV for Phase 2
IPsecSaUpdateIv
void IPsecSaUpdateIv(IPSECSA *sa, void *iv, UINT iv_size) { // Validate arguments if (sa == NULL || iv == NULL) { return; } Copy(sa->Iv, iv, MIN(sa->IkeSa->BlockSize, iv_size)); if (iv_size < sa->IkeSa->BlockSize) { Zero(sa->Iv + sa->IkeSa->BlockSize, sa->IkeSa->BlockSize - iv_size); } sa->IsIvExisting = true; }
/* Update the IV of IPsec SA*/
Update the IV of IPsec SA
IkeSaUpdateIv
void IkeSaUpdateIv(IKE_SA *sa, void *iv, UINT iv_size) { // Validate arguments if (sa == NULL || iv == NULL) { return; } Copy(sa->Iv, iv, MIN(sa->BlockSize, iv_size)); if (iv_size < sa->BlockSize) { Zero(sa->Iv + sa->BlockSize, sa->BlockSize - iv_size); } sa->IsIvExisting = true; }
/* Update the IV of the IKE SA*/
Update the IV of the IKE SA
IkeNewCryptoKeyFromK
IKE_CRYPTO_KEY *IkeNewCryptoKeyFromK(IKE_SERVER *ike, void *k, UINT k_size, IKE_HASH *h, IKE_CRYPTO *c, UINT crypto_key_size) { BUF *key_buf; IKE_CRYPTO_KEY *ret; // Validate arguments if (ike == NULL || k == NULL || k_size == 0 || h == NULL || c == NULL || crypto_key_size == 0) { return NULL; } key_buf = IkeExpandKeySize(h, k, k_size, crypto_key_size); if (key_buf == NULL) { return NULL; } ret = IkeNewKey(c, key_buf->Buf, key_buf->Size); FreeBuf(key_buf); return ret; }
/* Generate a key from K*/
Generate a key from K
IkeAddVendorIdPayloads
void IkeAddVendorIdPayloads(IKE_PACKET *p) { // Validate arguments if (p == NULL) { return; } IkeAddVendorId(p, IKE_VENDOR_ID_RFC3947_NAT_T); IkeAddVendorId(p, IKE_VENDOR_ID_IPSEC_NAT_T_IKE_03); IkeAddVendorId(p, IKE_VENDOR_ID_IPSEC_NAT_T_IKE_02); IkeAddVendorId(p, IKE_VENDOR_ID_IPSEC_NAT_T_IKE_02_2); IkeAddVendorId(p, IKE_VENDOR_ID_IPSEC_NAT_T_IKE_00); IkeAddVendorId(p, IKE_VENDOR_ID_RFC3706_DPD); }
/* Add the vendor ID payload list*/
Add the vendor ID payload list
IkeAddVendorId
void IkeAddVendorId(IKE_PACKET *p, char *str) { BUF *buf; IKE_PACKET_PAYLOAD *payload; // Validate arguments if (p == NULL || str == NULL) { return; } buf = IkeStrToVendorId(str); if (buf == NULL) { return; } payload = IkeNewDataPayload(IKE_PAYLOAD_VENDOR_ID, buf->Buf, buf->Size); Add(p->PayloadList, payload); FreeBuf(buf); }
/* Add the vendor ID payload*/
Add the vendor ID payload
IkeStrToVendorId
BUF *IkeStrToVendorId(char *str) { // Validate arguments if (IsEmptyStr(str)) { return NULL; } if (StartWith(str, "0x")) { BUF *buf = StrToBin(str + 2); if (buf == NULL || buf->Size == 0) { FreeBuf(buf); return NULL; } return buf; } else { BUF *buf; UCHAR hash[MD5_SIZE]; Md5(hash, str, StrLen(str)); buf = MemToBuf(hash, sizeof(hash)); return buf; } }
/* Convert string to the vendor ID*/
Convert string to the vendor ID
IkeSendUdpPacket
void IkeSendUdpPacket(IKE_SERVER *ike, UINT type, IP *server_ip, UINT server_port, IP *client_ip, UINT client_port, void *data, UINT size) { UDPPACKET *p; // Validate arguments if (ike == NULL || server_ip == NULL || client_ip == NULL || server_port == 0 || client_port == 0 || data == NULL || size == 0) { return; } p = NewUdpPacket(server_ip, server_port, client_ip, client_port, data, size); p->Type = type; Add(ike->SendPacketList, p); }
/* Send an UDP packet*/
Send an UDP packet
FindIkeSaByResponderCookie
IKE_SA *FindIkeSaByResponderCookie(IKE_SERVER *ike, UINT64 responder_cookie) { IKE_SA t; // Validate arguments if (ike == NULL || responder_cookie == 0) { return NULL; } t.ResponderCookie = responder_cookie; return Search(ike->IkeSaList, &t); }
/* Search an IKE SA from the Responder Cookie*/
Search an IKE SA from the Responder Cookie
FindIkeSaByResponderCookieAndClient
IKE_SA *FindIkeSaByResponderCookieAndClient(IKE_SERVER *ike, UINT64 responder_cookie, IKE_CLIENT *c) { IKE_SA *sa; // Validate arguments if (ike == NULL || responder_cookie == 0 || c == NULL) { return NULL; } sa = FindIkeSaByResponderCookie(ike, responder_cookie); if (sa == NULL) { return NULL; } if (sa->IkeClient != c) { return NULL; } return sa; }
/* Search an IKE SA from the Responder Cookie and the IKE_CLIENT*/
Search an IKE SA from the Responder Cookie and the IKE_CLIENT
GetNumberOfIPsecSaOfIkeClient
UINT GetNumberOfIPsecSaOfIkeClient(IKE_SERVER *ike, IKE_CLIENT *c) { UINT num = 0, i; // Validate arguments if (ike == NULL || c == NULL) { return 0; } for (i = 0;i < LIST_NUM(ike->IPsecSaList);i++) { IPSECSA *sa = LIST_DATA(ike->IPsecSaList, i); if (sa->IkeClient == c) { num++; } } return num; }
/* Get the number of IPsec SA that is associated with the IKE_CLIENT*/
Get the number of IPsec SA that is associated with the IKE_CLIENT
GetNumberOfIkeSaOfIkeClient
UINT GetNumberOfIkeSaOfIkeClient(IKE_SERVER *ike, IKE_CLIENT *c) { UINT num = 0, i; // Validate arguments if (ike == NULL || c == NULL) { return 0; } for (i = 0;i < LIST_NUM(ike->IkeSaList);i++) { IKE_SA *sa = LIST_DATA(ike->IkeSaList, i); if (sa->IkeClient == c) { num++; } } return num; }
/* Get the number of IKE SA that is associated with the IKE_CLIENT*/
Get the number of IKE SA that is associated with the IKE_CLIENT
GetNumberOfIkeClientsFromIP
UINT GetNumberOfIkeClientsFromIP(IKE_SERVER *ike, IP *client_ip) { UINT i, num; // Validate arguments if (ike == NULL || client_ip == NULL) { return 0; } num = 0; for (i = 0;i < LIST_NUM(ike->ClientList);i++) { IKE_CLIENT *c = LIST_DATA(ike->ClientList, i); if (CmpIpAddr(&c->ClientIP, client_ip) == 0) { num++; } } return num; }
/* Get the number of clients that are connected from the specified IP address*/
Get the number of clients that are connected from the specified IP address
CmpIPsecSa
int CmpIPsecSa(void *p1, void *p2) { IPSECSA *sa1, *sa2; int r; // Validate arguments if (p1 == NULL || p2 == NULL) { return 0; } sa1 = *(IPSECSA **)p1; sa2 = *(IPSECSA **)p2; if (sa1 == NULL || sa2 == NULL) { return 0; } r = COMPARE_RET(sa1->ServerToClient, sa2->ServerToClient); if (r != 0) { return r; } r = COMPARE_RET(sa1->Spi, sa2->Spi); return r; }
/* Comparison of IPsec SA*/
Comparison of IPsec SA
CmpIkeSa
int CmpIkeSa(void *p1, void *p2) { IKE_SA *sa1, *sa2; int r; // Validate arguments if (p1 == NULL || p2 == NULL) { return 0; } sa1 = *(IKE_SA **)p1; sa2 = *(IKE_SA **)p2; if (sa1 == NULL || sa2 == NULL) { return 0; } r = COMPARE_RET(sa1->ResponderCookie, sa2->ResponderCookie); return r; }
/* Comparison of IKE_SA*/
Comparison of IKE_SA
GenerateNewResponserCookie
UINT64 GenerateNewResponserCookie(IKE_SERVER *ike) { UINT64 c; // Validate arguments if (ike == NULL) { return 0; } while (true) { bool b = false; UINT i; c = Rand64(); for (i = 0;i < LIST_NUM(ike->IkeSaList);i++) { IKE_SA *sa = LIST_DATA(ike->IkeSaList, i); if (sa->ResponderCookie == c) { b = true; break; } } if (b == false) { return c; } } }
/* Creating a new Responder Cookie*/
Creating a new Responder Cookie
ParseIKEPacketHeader
IKE_PACKET *ParseIKEPacketHeader(UDPPACKET *p) { // Validate arguments if (p == NULL) { return NULL; } return IkeParseHeader(p->Data, p->Size, NULL); }
/* Parse the IKE packet header*/
Parse the IKE packet header
StopIKEServer
void StopIKEServer(IKE_SERVER *ike) { // Validate arguments if (ike == NULL) { return; } }
/* Stop the IKE server*/
Stop the IKE server
SetIKEServerSockEvent
void SetIKEServerSockEvent(IKE_SERVER *ike, SOCK_EVENT *e) { // Validate arguments if (ike == NULL) { return; } if (e != NULL) { AddRef(e->ref); } if (ike->SockEvent != NULL) { ReleaseSockEvent(ike->SockEvent); } ike->SockEvent = e; }
/* Set the socket events in IKE server*/
Set the socket events in IKE server
FreeIkeClient
void FreeIkeClient(IKE_SERVER *ike, IKE_CLIENT *c) { // Validate arguments if (c == NULL || ike == NULL) { return; } if (c->L2TP != NULL) { StopL2TPServer(c->L2TP, true); FreeL2TPServer(c->L2TP); } if (c->EtherIP != NULL) { ReleaseEtherIPServer(c->EtherIP); } FreeBuf(c->SendID1_Buf); FreeBuf(c->SendID2_Buf); Free(c); }
/* Release the IKE client*/
Release the IKE client
FreeIPsecSa
void FreeIPsecSa(IPSECSA *sa) { // Validate arguments if (sa == NULL) { return; } IkeFreeKey(sa->CryptoKey); FreeBuf(sa->SendBuffer); FreeBuf(sa->InitiatorRand); FreeBuf(sa->ResponderRand); FreeBuf(sa->SharedKey); IkeDhFreeCtx(sa->Dh); Free(sa); }
/* Release the IPsec SA*/
Release the IPsec SA