-
-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add IPv4 CIDR support and clean up code.
- Loading branch information
Showing
10 changed files
with
184 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#pragma once | ||
|
||
#include <linux/types.h> | ||
#include <string.h> | ||
#include <arpa/inet.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
struct ip | ||
{ | ||
__u32 ip; | ||
__u32 cidr; | ||
}; | ||
|
||
/** | ||
* Parses an IP string with CIDR support. Stores IP in network byte order in ip.ip and CIDR in ip.cidr. | ||
* | ||
* @param ip The IP string. | ||
* | ||
* @return Returns an IP structure with IP and CIDR. | ||
*/ | ||
struct ip ParseIp(const char *ip) | ||
{ | ||
struct ip ret = {0}; | ||
ret.cidr = 32; | ||
|
||
char *token = strtok((char *) ip, "/"); | ||
|
||
if (token) | ||
{ | ||
ret.ip = inet_addr(token); | ||
|
||
token = strtok(NULL, "/"); | ||
|
||
if (token) | ||
{ | ||
ret.cidr = (unsigned int) strtoul(token, NULL, 10); | ||
} | ||
} | ||
|
||
return ret; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#pragma once | ||
|
||
#include "xdpfw.h" | ||
|
||
/** | ||
* Checks if an IP is within a specific CIDR range. | ||
* | ||
* @param src_ip The source/main IP to check against. | ||
* @param net_ip The network IP. | ||
* @param cidr The CIDR range. | ||
* | ||
* @return 1 on yes, 0 on no. | ||
*/ | ||
static __always_inline __u8 IsIpInRange(__u32 src_ip, __u32 net_ip, __u8 cidr) | ||
{ | ||
return !((src_ip ^ net_ip) & htonl(0xFFFFFFFFu << (32 - cidr))); | ||
} |
Oops, something went wrong.