Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ BreakBeforeBraces: Custom
BraceWrapping:
SplitEmptyFunction: false
AfterCaseLabel: true
InsertBraces: true
...
48 changes: 48 additions & 0 deletions Common++/src/IpUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,9 @@ static const char* inet_ntop6(const uint8_t* src, char* dst, size_t size)
*/
memset(words, '\0', sizeof words);
for (i = 0; i < NS_IN6ADDRSZ; i++)
{
words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
}
best.base = -1;
best.len = 0;
cur.base = -1;
Expand All @@ -199,27 +201,37 @@ static const char* inet_ntop6(const uint8_t* src, char* dst, size_t size)
if (words[i] == 0)
{
if (cur.base == -1)
{
cur.base = i, cur.len = 1;
}
else
{
cur.len++;
}
}
else
{
if (cur.base != -1)
{
if (best.base == -1 || cur.len > best.len)
{
best = cur;
}
cur.base = -1;
}
}
}
if (cur.base != -1)
{
if (best.base == -1 || cur.len > best.len)
{
best = cur;
}
}
if (best.base != -1 && best.len < 2)
{
best.base = -1;
}

/*
* Format the result.
Expand All @@ -231,25 +243,33 @@ static const char* inet_ntop6(const uint8_t* src, char* dst, size_t size)
if (best.base != -1 && i >= best.base && i < (best.base + best.len))
{
if (i == best.base)
{
*tp++ = ':';
}
continue;
}
/* Are we following an initial run of 0x00s or any real hex? */
if (i != 0)
{
*tp++ = ':';
}
/* Is this address an encapsulated IPv4? */
if (i == 6 && best.base == 0 && (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
{
if (!inet_ntop4(src + 12, tp, sizeof tmp - (tp - tmp)))
{
return (nullptr);
}
tp += strlen(tp);
break;
}
tp += snprintf(tp, (unsigned long)(sizeof tmp - (tp - tmp)), "%x", words[i]);
}
/* Was it a trailing run of 0x00's? */
if (best.base != -1 && (best.base + best.len) == (NS_IN6ADDRSZ / NS_INT16SZ))
{
*tp++ = ':';
}
*tp++ = '\0';

/*
Expand Down Expand Up @@ -291,27 +311,37 @@ static int inet_pton4(const char* src, uint8_t* dst)
size_t newSize = *tp * 10 + (pch - digits);

if (newSize > 255)
{
return (0);
}
*tp = (u_char)newSize;
if (!saw_digit)
{
if (++octets > 4)
{
return (0);
}
saw_digit = 1;
}
}
else if (ch == '.' && saw_digit)
{
if (octets == 4)
{
return (0);
}
*++tp = 0;
saw_digit = 0;
}
else
{
return (0);
}
}
if (octets < 4)
{
return (0);
}
memcpy(dst, tmp, NS_INADDRSZ);
return (1);
}
Expand Down Expand Up @@ -342,8 +372,12 @@ static int inet_pton6(const char* src, uint8_t* dst)
colonp = nullptr;
/* Leading :: requires some special handling. */
if (*src == ':')
{
if (*++src != ':')
{
return (0);
}
}
curtok = src;
saw_xdigit = 0;
val = 0;
Expand All @@ -352,13 +386,17 @@ static int inet_pton6(const char* src, uint8_t* dst)
const char *pch, *xdigits;

if ((pch = strchr((xdigits = xdigits_l), ch)) == nullptr)
{
pch = strchr((xdigits = xdigits_u), ch);
}
if (pch != nullptr)
{
val <<= 4;
val |= (pch - xdigits);
if (val > 0xffff)
{
return (0);
}
saw_xdigit = 1;
continue;
}
Expand All @@ -368,7 +406,9 @@ static int inet_pton6(const char* src, uint8_t* dst)
if (!saw_xdigit)
{
if (colonp)
{
return (0);
}
colonp = tp;
continue;
}
Expand All @@ -377,7 +417,9 @@ static int inet_pton6(const char* src, uint8_t* dst)
return (0);
}
if (tp + NS_INT16SZ > endp)
{
return (0);
}
*tp++ = (u_char)(val >> 8) & 0xff;
*tp++ = (u_char)val & 0xff;
saw_xdigit = 0;
Expand All @@ -395,7 +437,9 @@ static int inet_pton6(const char* src, uint8_t* dst)
if (saw_xdigit)
{
if (tp + NS_INT16SZ > endp)
{
return (0);
}
*tp++ = (u_char)(val >> 8) & 0xff;
*tp++ = (u_char)val & 0xff;
}
Expand All @@ -409,7 +453,9 @@ static int inet_pton6(const char* src, uint8_t* dst)
int i;

if (tp == endp)
{
return (0);
}
for (i = 1; i <= n; i++)
{
endp[-i] = colonp[n - i];
Expand All @@ -418,7 +464,9 @@ static int inet_pton6(const char* src, uint8_t* dst)
tp = endp;
}
if (tp != endp)
{
return (0);
}
memcpy(dst, tmp, NS_IN6ADDRSZ);
return (1);
}
Expand Down
2 changes: 2 additions & 0 deletions Common++/src/SystemUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,10 @@ namespace pcpp
case CTRL_BREAK_EVENT:
{
if (ApplicationEventHandler::getInstance().m_ApplicationInterruptedHandler != nullptr)
{
ApplicationEventHandler::getInstance().m_ApplicationInterruptedHandler(
ApplicationEventHandler::getInstance().m_ApplicationInterruptedCookie);
}
return TRUE;
}

Expand Down
18 changes: 18 additions & 0 deletions Examples/Arping/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,15 @@ int main(int argc, char* argv[])

// verify that interface name or IP were provided
if (!ifaceNameOrIpProvided)
{
EXIT_WITH_ERROR("You must provide at least interface name or interface IP (-i switch)");
}

// verify target IP was provided
if (!targetIpProvided)
{
EXIT_WITH_ERROR("You must provide target IP (-T switch)");
}

pcpp::PcapLiveDevice* dev = nullptr;

Expand All @@ -200,28 +204,42 @@ int main(int argc, char* argv[])
{
dev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIpOrName(ifaceNameOrIP);
if (dev == nullptr)
{
EXIT_WITH_ERROR("Couldn't find interface by provided IP address or name");
}
}
else
{
EXIT_WITH_ERROR("Interface name or IP empty");
}

// open device in promiscuous mode
if (!dev->open())
{
EXIT_WITH_ERROR("Couldn't open interface device '" << dev->getName() << "'");
}

// if source MAC not provided - use the interface MAC address
if (sourceMac == pcpp::MacAddress::Zero)
{
sourceMac = dev->getMacAddress();
}

// if source MAC is still invalid, it means it couldn't be extracted from interface
if (sourceMac == pcpp::MacAddress::Zero)
{
EXIT_WITH_ERROR("MAC address couldn't be extracted from interface");
}

if (sourceIP == pcpp::IPv4Address::Zero)
{
sourceIP = dev->getIPv4Address();
}

if (sourceIP == pcpp::IPv4Address::Zero)
{
EXIT_WITH_ERROR("Source IPv4 address wasn't supplied and couldn't be retrieved from interface");
}

// let's go
double arpResponseTimeMS = 0;
Expand Down
6 changes: 6 additions & 0 deletions Examples/DNSResolver/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ int main(int argc, char* argv[])

// make sure that hostname is provided
if (!hostnameProvided)
{
EXIT_WITH_ERROR("Hostname not provided");
}

// find the interface to send the DNS request from
pcpp::PcapLiveDevice* dev = nullptr;
Expand All @@ -184,7 +186,9 @@ int main(int argc, char* argv[])
{
dev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIpOrName(interfaceNameOrIP);
if (dev == nullptr)
{
EXIT_WITH_ERROR("Couldn't find interface by provided IP address or name");
}
}
// if interface name or IP was not provided - find a device that has a default gateway
else
Expand All @@ -201,7 +205,9 @@ int main(int argc, char* argv[])
}

if (dev == nullptr)
{
EXIT_WITH_ERROR("Couldn't find an interface with a default gateway");
}
}

std::cout << "Using interface '" << dev->getIPv4Address() << "'" << std::endl;
Expand Down
Loading
Loading