diff options
Diffstat (limited to 'src/include/common.h')
-rw-r--r-- | src/include/common.h | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/src/include/common.h b/src/include/common.h index 5bcbaad88..f72ec9738 100644 --- a/src/include/common.h +++ b/src/include/common.h | |||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | * Copyright (C) 2014-2021 Firejail Authors | 2 | * Copyright (C) 2014-2022 Firejail Authors |
3 | * | 3 | * |
4 | * This file is part of firejail project | 4 | * This file is part of firejail project |
5 | * | 5 | * |
@@ -73,6 +73,25 @@ static inline int atoip(const char *str, uint32_t *ip) { | |||
73 | return 0; | 73 | return 0; |
74 | } | 74 | } |
75 | 75 | ||
76 | // read an IPv4 address in CIDR format, for example 192.168.1.0/24 | ||
77 | static inline int atocidr(const char *str, uint32_t *ip, uint32_t *mask) { | ||
78 | unsigned a, b, c, d, e; | ||
79 | |||
80 | // extract ip | ||
81 | int rv = sscanf(str, "%u.%u.%u.%u/%u", &a, &b, &c, &d, &e); | ||
82 | if (rv != 5 || a > 255 || b > 255 || c > 255 || d > 255 || e > 32) | ||
83 | return 1; | ||
84 | *ip = a * 0x1000000 + b * 0x10000 + c * 0x100 + d; | ||
85 | |||
86 | // extract mask | ||
87 | uint32_t tmp; | ||
88 | unsigned i; | ||
89 | for (i = 0, *mask = 0, tmp = 0x80000000; i < e; i++, tmp >>= 1) { | ||
90 | *mask |= tmp; | ||
91 | } | ||
92 | return 0; | ||
93 | } | ||
94 | |||
76 | // verify an ip address is in the network range given by ifip and mask | 95 | // verify an ip address is in the network range given by ifip and mask |
77 | static inline char *in_netrange(uint32_t ip, uint32_t ifip, uint32_t ifmask) { | 96 | static inline char *in_netrange(uint32_t ip, uint32_t ifip, uint32_t ifmask) { |
78 | if ((ip & ifmask) != (ifip & ifmask)) | 97 | if ((ip & ifmask) != (ifip & ifmask)) |
@@ -123,4 +142,5 @@ int pid_proc_cmdline_x11_xpra_xephyr(const pid_t pid); | |||
123 | int pid_hidepid(void); | 142 | int pid_hidepid(void); |
124 | void warn_dumpable(void); | 143 | void warn_dumpable(void); |
125 | const char *gnu_basename(const char *path); | 144 | const char *gnu_basename(const char *path); |
145 | int *str_to_int_array(const char *str, size_t *sz); | ||
126 | #endif | 146 | #endif |