diff options
Diffstat (limited to 'src/include/common.h')
-rw-r--r-- | src/include/common.h | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/src/include/common.h b/src/include/common.h new file mode 100644 index 000000000..7ce1e9290 --- /dev/null +++ b/src/include/common.h | |||
@@ -0,0 +1,115 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2014, 2015 netblue30 (netblue30@yahoo.com) | ||
3 | * | ||
4 | * This file is part of firejail project | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU General Public License as published by | ||
8 | * the Free Software Foundation; either version 2 of the License, or | ||
9 | * (at your option) any later version. | ||
10 | * | ||
11 | * This program is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * GNU General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU General Public License along | ||
17 | * with this program; if not, write to the Free Software Foundation, Inc., | ||
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
19 | */ | ||
20 | |||
21 | #ifndef COMMON_H | ||
22 | #define COMMON_H | ||
23 | #define _GNU_SOURCE | ||
24 | #include <stdio.h> | ||
25 | #include <sys/types.h> | ||
26 | #include <unistd.h> | ||
27 | #include <stdlib.h> | ||
28 | #include <stdint.h> | ||
29 | #include <stddef.h> | ||
30 | #include <string.h> | ||
31 | #include <ctype.h> | ||
32 | #include <assert.h> | ||
33 | |||
34 | #define errExit(msg) do { char msgout[500]; sprintf(msgout, "Error %s:%s(%d)", msg, __FUNCTION__, __LINE__); perror(msgout); exit(1);} while (0) | ||
35 | |||
36 | // macro to print ip addresses in a printf statement | ||
37 | #define PRINT_IP(A) \ | ||
38 | ((int) (((A) >> 24) & 0xFF)), ((int) (((A) >> 16) & 0xFF)), ((int) (((A) >> 8) & 0xFF)), ((int) ( (A) & 0xFF)) | ||
39 | |||
40 | // macro to print a mac addresses in a printf statement | ||
41 | #define PRINT_MAC(A) \ | ||
42 | ((unsigned) (*(A)) & 0xff), ((unsigned) (*((A) + 1) & 0xff)), ((unsigned) (*((A) + 2) & 0xff)), \ | ||
43 | ((unsigned) (*((A) + 3)) & 0xff), ((unsigned) (*((A) + 4) & 0xff)), ((unsigned) (*((A) + 5)) & 0xff) | ||
44 | |||
45 | // the number of bits in a network mask | ||
46 | static inline uint8_t mask2bits(uint32_t mask) { | ||
47 | uint32_t tmp = 0x80000000; | ||
48 | int i; | ||
49 | uint8_t rv = 0; | ||
50 | |||
51 | for (i = 0; i < 32; i++, tmp >>= 1) { | ||
52 | if (tmp & mask) | ||
53 | rv++; | ||
54 | else | ||
55 | break; | ||
56 | } | ||
57 | return rv; | ||
58 | } | ||
59 | |||
60 | // read an IPv4 address and convert it to uint32_t | ||
61 | static inline int atoip(const char *str, uint32_t *ip) { | ||
62 | unsigned a, b, c, d; | ||
63 | |||
64 | if (sscanf(str, "%u.%u.%u.%u", &a, &b, &c, &d) != 4 || a > 255 || b > 255 || c > 255 || d > 255) | ||
65 | return 1; | ||
66 | |||
67 | *ip = a * 0x1000000 + b * 0x10000 + c * 0x100 + d; | ||
68 | return 0; | ||
69 | } | ||
70 | |||
71 | // verify an ip address is in the network range given by ifip and mask | ||
72 | static inline char *in_netrange(uint32_t ip, uint32_t ifip, uint32_t ifmask) { | ||
73 | if ((ip & ifmask) != (ifip & ifmask)) | ||
74 | return "Error: the IP address is not in the interface range\n"; | ||
75 | else if ((ip & ifmask) == ip) | ||
76 | return "Error: the IP address is a network address\n"; | ||
77 | else if ((ip | ~ifmask) == ip) | ||
78 | return "Error: the IP address is a network address\n"; | ||
79 | return NULL; | ||
80 | } | ||
81 | |||
82 | // read a mac address | ||
83 | static inline int atomac(char *str, unsigned char macAddr[6]) { | ||
84 | unsigned mac[6]; | ||
85 | |||
86 | if (sscanf(str, "%2x:%2x:%2x:%2x:%2x:%2x", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]) != 6) | ||
87 | return 1; | ||
88 | |||
89 | int i; | ||
90 | for (i = 0; i < 6; i++) { | ||
91 | if (mac[i] > 0xff) | ||
92 | return 1; | ||
93 | |||
94 | macAddr[i] = (unsigned char) mac[i]; | ||
95 | } | ||
96 | |||
97 | return 0; | ||
98 | } | ||
99 | |||
100 | // check a mac address is configured | ||
101 | static inline int mac_not_zero(const unsigned char mac[6]) { | ||
102 | int i; | ||
103 | for (i = 0; i < 6; i++) { | ||
104 | if (mac[i] != 0) | ||
105 | return 1; | ||
106 | } | ||
107 | |||
108 | return 0; | ||
109 | } | ||
110 | |||
111 | int join_namespace(pid_t pid, char *type); | ||
112 | int name2pid(const char *name, pid_t *pid); | ||
113 | char *pid_proc_comm(const pid_t pid); | ||
114 | char *pid_proc_cmdline(const pid_t pid); | ||
115 | #endif | ||