📄 opendhcpd.cpp
字号:
tm *ttm = gmtime(&currTime); strftime(tempbuff, sizeof(tempbuff), "%a, %d %b %Y %H:%M:%S GMT", ttm); WORD hs = sprintf(req->buffer, send200, tempbuff, tempbuff); req->bytes = send(req->sock, req->buffer, hs, 0); char *fp = req->buffer + 8; fp += sprintf(fp, "<html>\n<head>\n<META HTTP-EQUIV=\"Refresh\" CONTENT=\"60\">\n<META HTTP-EQUIV=\"Cache-Control\" CONTENT=\"no-cache\">\n</head>\n"); fp += sprintf(fp, "<body bgcolor=\"#cccccc\">\n<h2>%s</h2>\n", sVersion); fp += sprintf(fp, "<table border=\"1\" width=\"600\" bgcolor=\"#b8b8b8\">\n"); fp += sprintf(fp, "<tr><th colspan=\"5\"><font size=\"5\"><i>Static Leases</i></font></th></tr>\n"); fp += sprintf(fp, "<tr><th>IP</th><th>Mac Address</th><th>Lease Expiry</th><th>Hostname</th></tr>\n"); for (p = dhcpCache.begin(); p != dhcpCache.end(); p++) { if(fp > maxChunk) { req->bytes = fp - req->buffer; sprintf(size, "\r\n%04x\r\n", req->bytes - 8); memcpy(req->buffer, size, 8); errno = 0; if (req->bytes != send(req->sock, req->buffer, req->bytes, 0) || errno) { close(req->sock); return; } fp = req->buffer + 8; } if ((dhcpEntry = p->second) && dhcpEntry->fixed) { fp += sprintf(fp, "<tr>"); fp += sprintf(fp, line200, IP2String(tempbuff, dhcpEntry->ip)); BYTE bp_hlen = fromBase64(bp_chaddr, dhcpEntry->mapname); fp += sprintf(fp, line200, hex2String(tempbuff, bp_chaddr, bp_hlen, ':')); if (dhcpEntry->expiry >= LONG_MAX) fp += sprintf(fp, line200, "Infinity"); else if (dhcpEntry->expiry >= currTime) { tm *ttm = localtime(&dhcpEntry->expiry); strftime(tempbuff, sizeof(tempbuff), "%d-%b-%y %X", ttm); fp += sprintf(fp, line200, tempbuff); } else { fp += sprintf(fp, line200, " "); } if (dhcpEntry->hostname) { fp += sprintf(fp, line200, dhcpEntry->hostname); } else fp += sprintf(fp, line200, " "); fp += sprintf(fp, "</tr>\n"); } } fp += sprintf(fp, "</table>\n<br>\n<table border=\"1\" width=\"600\" bgcolor=\"#b8b8b8\">\n"); fp += sprintf(fp, "<tr><th colspan=\"4\"><font size=\"5\"><i>Active Dynamic Leases</i></font></th></tr>\n"); fp += sprintf(fp, "<tr><th>IP</th><th>Mac Address</th><th>Lease Expiry</th><th>Hostname</th></tr>\n"); for (char rangeInd = 0; rangeInd < 32 && cfig.dhcpRanges[rangeInd].rangeStart; rangeInd++) { for (ind = 0, iip = cfig.dhcpRanges[rangeInd].rangeStart; iip <= cfig.dhcpRanges[rangeInd].rangeEnd; iip++, ind++) { if(fp > maxChunk) { req->bytes = fp - req->buffer; sprintf(size, "\r\n%04x\r\n", req->bytes - 8); memcpy(req->buffer, size, 8); errno = 0; if (req->bytes != send(req->sock, req->buffer, req->bytes, 0) || errno) { close(req->sock); return; } fp = req->buffer + 8; } if ((dhcpEntry = cfig.dhcpRanges[rangeInd].dhcpEntry[ind]) && dhcpEntry->local && !dhcpEntry->fixed && dhcpEntry->expiry > currTime && iip == htonl(dhcpEntry->ip)) { fp += sprintf(fp, "<tr>"); fp += sprintf(fp, line200, IP2String(tempbuff, dhcpEntry->ip)); BYTE bp_hlen = fromBase64(bp_chaddr, dhcpEntry->mapname); fp += sprintf(fp, line200, hex2String(tempbuff, bp_chaddr, bp_hlen, ':')); if (dhcpEntry->expiry >= LONG_MAX) fp += sprintf(fp, line200, "Infinity"); else { tm *ttm = localtime(&dhcpEntry->expiry); strftime(tempbuff, sizeof(tempbuff), "%d-%b-%y %X", ttm); fp += sprintf(fp, line200, tempbuff); } if (dhcpEntry->hostname) fp += sprintf(fp, line200, dhcpEntry->hostname); else fp += sprintf(fp, line200, " "); fp += sprintf(fp, "</tr>\n"); } } } fp += sprintf(fp, "</table>\n<br>\n<table border=\"1\" width=\"600\" bgcolor=\"#b8b8b8\">\n"); fp += sprintf(fp, "<tr><th colspan=\"5\"><font size=\"5\"><i>Free Dynamic Leases</i></font></th></tr>\n"); BYTE colNum = 0; for (char rangeInd = 0; rangeInd < 32 && cfig.dhcpRanges[rangeInd].rangeStart; rangeInd++) { for (ind = 0, iip = cfig.dhcpRanges[rangeInd].rangeStart; iip <= cfig.dhcpRanges[rangeInd].rangeEnd; iip++, ind++) { if (cfig.dhcpRanges[rangeInd].expiry[ind] <= currTime) { colNum++; if (colNum >= 6) { if(fp > maxChunk) { req->bytes = fp - req->buffer; sprintf(size, "\r\n%04x\r\n", req->bytes - 8); memcpy(req->buffer, size, 8); errno = 0; if (req->bytes != send(req->sock, req->buffer, req->bytes, 0) || errno) { close(req->sock); return; } fp = req->buffer + 8; } fp += sprintf(fp, "</tr>\n<tr>"); colNum = 1; } fp += sprintf(fp, line200, IP2String(tempbuff, htonl(iip))); } } } fp += sprintf(fp, "</tr>\n</table>\n</body>\n</html>"); sprintf(size, "\r\n%04x\r\n", (fp - req->buffer) - 8); memcpy(req->buffer, size, 8); fp += sprintf(fp, "\r\n%04x\r\n\r\n", 0); req->bytes = fp - req->buffer; errno = 0; if (req->bytes != send(req->sock, req->buffer, req->bytes, 0) || errno) { closesocket(req->sock); return; } pthread_t threadId; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); int errcode = pthread_create(&threadId, &attr, myCloseSocket, (void*)req->sock); pthread_attr_destroy(&attr); if(errcode) { if (cfig.logLevel) { sprintf(logBuff, "Thread Creation Failed"); logMess(logBuff, 1); } shutdown(req->sock, 2); close(req->sock); } return;}DWORD resad(data1 *req){ time_t t = time(NULL); DWORD MinRange = 0; DWORD MaxRange = 0; //DWORD minRange; //DWORD maxRange; //calcRangeLimits(cfig.dhcpConn[req->sockInd].server, cfig.dhcpConn[req->sockInd].mask, &minRange, &maxRange); if (req->dhcpp.header.bp_giaddr) { setLeaseExpiry(req->dhcpp.header.bp_giaddr, LONG_MAX); setLeaseExpiry(req->addr.sin_addr.s_addr, LONG_MAX); } //printf("range=%s-", IP2String(tempbuff, htonl(minRange))); //printf("%s ", IP2String(tempbuff, htonl(maxRange))); //printf("mask=%s", IP2String(tempbuff, cfig.dhcpConn[req->sockInd].mask)); //printf("server=%s", IP2String(tempbuff, cfig.dhcpConn[req->sockInd].server)); //printf("ip=%s\n\n", IP2String(tempbuff, req->dhcpEntry->ip)); //printf("%s\n",toBase64(req->dhcpp.header.bp_chaddr, req->dhcpp.header.bp_hlen)); //printf("%s\n",IP2String(tempbuff, req->dhcpEntry->ip)); if (chad(req)) { DWORD mask = INADDR_NONE; if (req->dhcpEntry->bitmask) mask = mask >> (32 - req->dhcpEntry->bitmask); else if (req->dhcpEntry->rangeInd >= 0) mask = cfig.dhcpRanges[req->dhcpEntry->rangeInd].mask; else mask = cfig.mask; //printf("%s\n", IP2String(tempbuff,mask)); if (req->dhcpp.header.bp_giaddr) calcRangeLimits(req->dhcpp.header.bp_giaddr, mask, &MinRange, &MaxRange); else if (htonl(cfig.dhcpConn[req->sockInd].mask) > htonl(mask)) calcRangeLimits(cfig.dhcpConn[req->sockInd].server, cfig.dhcpConn[req->sockInd].mask, &MinRange, &MaxRange); else calcRangeLimits(cfig.dhcpConn[req->sockInd].server, mask, &MinRange, &MaxRange); if (htonl(req->dhcpEntry->ip) >= MinRange && htonl(req->dhcpEntry->ip) <= MaxRange) { setLeaseExpiry(req->dhcpEntry, 20, false); return req->dhcpEntry->ip; } else if (req->dhcpEntry->fixed) { req->dhcpEntry->no_route = 1; setLeaseExpiry(req->dhcpEntry, 20, false); return req->dhcpEntry->ip; } } if (req->dhcpEntry && req->dhcpEntry->rangeInd >= 0 && req->dhcpEntry->rangeInd < 32) { DWORD iip = htonl(req->dhcpEntry->ip); if (iip >= cfig.dhcpRanges[req->dhcpEntry->rangeInd].rangeStart && iip <= cfig.dhcpRanges[req->dhcpEntry->rangeInd].rangeEnd) { int ind = iip - cfig.dhcpRanges[req->dhcpEntry->rangeInd].rangeStart; cfig.dhcpRanges[req->dhcpEntry->rangeInd].dhcpEntry[ind] = 0; } } DWORD iipNew = 0; DWORD iipExp = 0; DWORD rangeStart = 0; DWORD rangeEnd = 0; BOOL rangeFound = false; char k = -1; char rangeInd = -1; int ind = -1; bool macFound = false; if (cfig.hasMac) { for (k = 0; k < 32 && cfig.dhcpRanges[k].rangeStart; k++) { data13 *range = &cfig.dhcpRanges[k]; if(range->macStart && range->macEnd && memcmp(req->dhcpp.header.bp_chaddr, range->macStart, req->dhcpp.header.bp_hlen) >= 0 && memcmp(req->dhcpp.header.bp_chaddr, range->macEnd, req->dhcpp.header.bp_hlen) <= 0) { macFound = true; break; } } } if (!iipNew && req->reqIP) { k = getRangeInd(req->reqIP); if (k >= 0) { data13 *range = &cfig.dhcpRanges[k]; if((!macFound && !range->macStart && !range->macEnd) || (range->macStart && range->macEnd && memcmp(req->dhcpp.header.bp_chaddr, range->macStart, req->dhcpp.header.bp_hlen) >= 0 && memcmp(req->dhcpp.header.bp_chaddr, range->macEnd, req->dhcpp.header.bp_hlen) <= 0)) { ind = getIndex(k, req->reqIP); if (ind >= 0 && range->expiry[ind] <= t && !range->dhcpEntry[ind]) { if (req->dhcpp.header.bp_giaddr) calcRangeLimits(req->dhcpp.header.bp_giaddr, range->mask, &MinRange, &MaxRange); else if (htonl(cfig.dhcpConn[req->sockInd].mask) > htonl(range->mask)) calcRangeLimits(cfig.dhcpConn[req->sockInd].server, cfig.dhcpConn[req->sockInd].mask, &MinRange, &MaxRange); else calcRangeLimits(cfig.dhcpConn[req->sockInd].server, range->mask, &MinRange, &MaxRange); DWORD iip = htonl(req->reqIP); if (iip >= MinRange && iip <= MaxRange) { iipNew = iip; rangeInd = k; } } } } } for (k = 0; !iipNew && k < 32 && cfig.dhcpRanges[k].rangeStart; k++) { data13 *range = &cfig.dhcpRanges[k]; if((!macFound && !range->macStart && !range->macEnd) || (range->macStart && range->macEnd && memcmp(req->dhcpp.header.bp_chaddr, range->macStart, req->dhcpp.header.bp_hlen) >= 0 && memcmp(req->dhcpp.header.bp_chaddr, range->macEnd, req->dhcpp.header.bp_hlen) <= 0)) { rangeStart = range->rangeStart; rangeEnd = range->rangeEnd; if (req->dhcpp.header.bp_giaddr) calcRangeLimits(req->dhcpp.header.bp_giaddr, range->mask, &MinRange, &MaxRange); else if (htonl(cfig.dhcpConn[req->sockInd].mask) > htonl(range->mask)) calcRangeLimits(cfig.dhcpConn[req->sockInd].server, cfig.dhcpConn[req->sockInd].mask, &MinRange, &MaxRange); else calcRangeLimits(cfig.dhcpConn[req->sockInd].server, range->mask, &MinRange, &MaxRange); if (rangeStart < MinRange) rangeStart = MinRange; if (rangeEnd > MaxRange) rangeEnd = MaxRange; if (rangeStart <= rangeEnd) { rangeFound = true; for (DWORD m = rangeStart; m <= rangeEnd; m++) { if (!range->expiry[m - range->rangeStart]) { iipNew = m; rangeInd = k; break; } else if (!iipExp && range->expiry[m - range->rangeStart] < t) { iipExp = m; rangeInd = k; } } } } } if (!iipNew && iipExp) { ind = iipExp - cfig.dhcpRanges[rangeInd].rangeStart; req->dhcpEntry = cfig.dhcpRanges[rangeInd].dhcpEntry[ind]; if (req->dhcpEntry) { dhcpCache.erase(req->dhcpEntry->mapname); free(req->dhcpEntry->mapname); req->dhcpEntry->mapname = cloneString(toBase64(req->dhcpp.header.bp_chaddr, req->dhcpp.header.bp_hlen)); if (!req->dhcpEntry->mapname) { sprintf(logBuff, "Memory Allocation Error"); logMess(logBuff, 1); return 0; } dhcpCache[req->dhcpEntry->mapname] = req->dhcpEntry; req->dhcpEntry->ip = htonl(iipExp); req->dhcpEntry->rangeInd = rangeInd; setLeaseExpiry(req->dhcpEntry, 20, false); if (!req->dhcpEntry->fixed) { if (req->dhcpp.header.bp_giaddr) req->dhcpEntry->source = req->dhcpp.header.bp_giaddr; else req->dhcpEntry->source = cfig.dhcpConn[req->sockInd].server; } return req->dhcpEntry->ip; } else iipNew = iipExp; } if (iipNew) { if (!req->dhcpEntry) { req->dhcpEntry = (data7*)calloc(1, sizeof(data7)); if (!req->dhcpEntry) { sprintf(logBuff, "Memory Allocation Error"); logMess(logBuff, 1); return 0; } req->dhcpEntry->mapname = cloneString(toBase64(req->dhcpp.header.bp_chaddr, req->dhcpp.header.bp_hlen)); if (!req->dhcpEntry->mapname) { sprintf(logBuff, "Memory Allocation Error"); free(req->dhcpEntry); logMess(logBuff, 1); return 0; } dhcpCache[req->dhcpEntry->mapname] = req->dhcpEntry; } req->dhcpEntry->ip = htonl(iipNew); req->dhcpEntry->rangeInd = rangeInd; setLeaseExpiry(req->dhcpEntry, 20, false); if (!req->dhcpEntry->fixed) { if (req->dhcpp.header.bp_giaddr) req->dhcpEntry->source = req->dhcpp.header.bp_giaddr; else req->dhcpEntry->source = cfig.dhcpConn[req->sockInd].server; } return req->dhcpEntry->ip; } if (!iipNew) { if (cfig.logLevel) { if (rangeFound) { if (req->dhcpp.header.bp_giaddr) sprintf(logBuff, "No free leases available for DHCP discover from RelayAgent %s", IP2String(tempbuff, req->dhcpp.header.bp_giaddr)); else sprintf(logBuff, "No free leases available for DHCP discover from interface %s", IP2String(tempbuff, cfig.dhcpConn[req->sockInd].server)); } else { if (req->dhcpp.header.bp_giaddr) sprintf(logBuff, "No Matching DHCP Range available for DHCP discover from RelayAgent %s", IP2String(tempbuff, req->dhcpp.header.bp_giaddr)); else sprintf(logBuff, "No Matching DHCP Range available for DHCP discover from interface %s", IP2String(tempbuff, cfig.dhcpConn[req->sockInd].server)); } logMess(logBuff, 1); } } return 0;}DWORD chad(data1 *req){ req->dhcpEntry = dhcpCache[toBase64(req->dhcpp.header.bp_chaddr, req->dhcpp.header.bp_hlen)]; if (req->dhcpEntry && req->dhcpEntry->ip && (req->dhcpEntry->fixed || (req->dhcpp.header.bp_giaddr && req->dhcpEntry->source == req->dhcpp.header.bp_giaddr) || req->dhcpEntry->source == cfig.dhcpConn[req->sockInd].server)) return req->dhcpEntry->ip; else return 0;}DWORD sdmess(data1 *req){ time_t t = time(NULL); //printf("Request=%u Req IP=%s\n",req->req_type, IP2String(tempbuff, req->dhcpp.header.bp_ciaddr)); if (req->req_type == DHCP_MESS_NONE) { req->dhcpp.header.bp_yiaddr = chad(req); if (!req->dhcpp.header.bp_yiaddr) { if (cfig.logLevel) { sprintf(logBuff, "No Static Entry found for BOOTP request from client %s", hex2String(tempbuff, req->dhcpp.header.bp_chaddr, req->dhcpp.header.bp_hlen, ':')); logMess(logBuff, 1); } return 0; } } else if (req->req_type == DHCP_MESS_DECLINE) { if (chad(req) == req->dhcpp.header.bp_ciaddr) { setLeaseExpiry(req->dhcpp.header.bp_ciaddr, LONG_MAX); req->dhcpEntry->ip = 0; if (cfig.logLevel) { sprintf(logBuff, "IP Address %s declind by Client %s", IP2String(tempbuff, req->dhcpp.header.bp_ciaddr), req->hostname); logMess(logBuff, 1); } } return 0; } else if (req->req_type == DHCP_MESS_RELEASE) { if (chad(req) == req->dhcpp.header.bp_ciaddr) { updateDHCP(req); if (req->dhcpEntry->local && cfig.replication) sendRepl(req); if (cfig.logLevel == 2) { sprintf(logBuff, "IP Address %s released by Client %s", IP2String(tempbuff, req->dhcpp.header.bp_ciaddr), req->hostname); logMess(logBuff, 2); } } return 0; } else if (req->req_type == DHCP_MESS_INFORM) { recvRepl(req); return 0; } else if (req->req_type == DHCP_MESS_DISCOVER && strcasecmp(req->hostname, cfig.servername)) { req->dhcpp.header.bp_yiaddr = resad(req); if (!req->dhcpp.header.bp_yiaddr) return 0; req->resp_type = DHCP_MESS_OFFER; } else if (req->req_type == DHCP_MESS_REQUEST) { //printf("%s\n", IP2String(tempbuff, req->dhcpp.header.bp_ciaddr));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -