@@ -28,10 +28,79 @@ extern "C" {
28
28
#include " linux/wireguard.h"
29
29
}
30
30
31
- #define SETCONFIG 1
32
- #define GETCONFIG 1
33
- #define LISTDEV 1
34
- #define DELIFACE 1
31
+ #define EPERM 1 /* Operation not permitted */
32
+ #define ENOENT 2 /* No such file or directory */
33
+ #define ESRCH 3 /* No such process */
34
+ #define EINTR 4 /* Interrupted system call */
35
+ #define EIO 5 /* I/O error */
36
+ #define ENXIO 6 /* No such device or address */
37
+ #define E2BIG 7 /* Argument list too long */
38
+ #define ENOEXEC 8 /* Exec format error */
39
+ #define EBADF 9 /* Bad file number */
40
+ #define ECHILD 10 /* No child processes */
41
+ #define EAGAIN 11 /* Try again */
42
+ #define ENOMEM 12 /* Out of memory */
43
+ #define EACCES 13 /* Permission denied */
44
+ #define EFAULT 14 /* Bad address */
45
+ #define ENOTBLK 15 /* Block device required */
46
+ #define EBUSY 16 /* Device or resource busy */
47
+ #define EEXIST 17 /* File exists */
48
+ #define EXDEV 18 /* Cross-device link */
49
+ #define ENODEV 19 /* No such device */
50
+ #define ENOTDIR 20 /* Not a directory */
51
+ #define EISDIR 21 /* Is a directory */
52
+ #define EINVAL 22 /* Invalid argument */
53
+ #define ENFILE 23 /* File table overflow */
54
+ #define EMFILE 24 /* Too many open files */
55
+ #define ENOTTY 25 /* Not a typewriter */
56
+ #define ETXTBSY 26 /* Text file busy */
57
+ #define EFBIG 27 /* File too large */
58
+ #define ENOSPC 28 /* No space left on device */
59
+ #define ESPIPE 29 /* Illegal seek */
60
+ #define EROFS 30 /* Read-only file system */
61
+ #define EMLINK 31 /* Too many links */
62
+ #define EPIPE 32 /* Broken pipe */
63
+ #define EDOM 33 /* Math argument out of domain of func */
64
+ #define ERANGE 34 /* Math result not representable */
65
+
66
+ std::string getKernelMesage (int errStatus) {
67
+ std::string message = std::string (" Error code: " ).append (std::to_string (errStatus));
68
+ if (errStatus == -EPERM) message = std::string (" Operation not permitted, code: " ).append (std::to_string (errStatus));
69
+ else if (errStatus == -ENOENT) message = std::string (" No such file or directory, code: " ).append (std::to_string (errStatus));
70
+ else if (errStatus == -ESRCH) message = std::string (" No such process, code: " ).append (std::to_string (errStatus));
71
+ else if (errStatus == -EINTR) message = std::string (" Interrupted system call, code: " ).append (std::to_string (errStatus));
72
+ else if (errStatus == -EIO) message = std::string (" I/O error, code: " ).append (std::to_string (errStatus));
73
+ else if (errStatus == -ENXIO) message = std::string (" No such device or address, code: " ).append (std::to_string (errStatus));
74
+ else if (errStatus == -E2BIG) message = std::string (" Argument list too long, code: " ).append (std::to_string (errStatus));
75
+ else if (errStatus == -ENOEXEC) message = std::string (" Exec format error, code: " ).append (std::to_string (errStatus));
76
+ else if (errStatus == -EBADF) message = std::string (" Bad file number, code: " ).append (std::to_string (errStatus));
77
+ else if (errStatus == -ECHILD) message = std::string (" No child processes, code: " ).append (std::to_string (errStatus));
78
+ else if (errStatus == -EAGAIN) message = std::string (" Try again, code: " ).append (std::to_string (errStatus));
79
+ else if (errStatus == -ENOMEM) message = std::string (" Out of memory, code: " ).append (std::to_string (errStatus));
80
+ else if (errStatus == -EACCES) message = std::string (" Permission denied, code: " ).append (std::to_string (errStatus));
81
+ else if (errStatus == -EFAULT) message = std::string (" Bad address, code: " ).append (std::to_string (errStatus));
82
+ else if (errStatus == -ENOTBLK) message = std::string (" Block device required, code: " ).append (std::to_string (errStatus));
83
+ else if (errStatus == -EBUSY) message = std::string (" Device or resource busy, code: " ).append (std::to_string (errStatus));
84
+ else if (errStatus == -EEXIST) message = std::string (" File exists, code: " ).append (std::to_string (errStatus));
85
+ else if (errStatus == -EXDEV) message = std::string (" Cross-device link, code: " ).append (std::to_string (errStatus));
86
+ else if (errStatus == -ENODEV) message = std::string (" No such device, code: " ).append (std::to_string (errStatus));
87
+ else if (errStatus == -ENOTDIR) message = std::string (" Not a directory, code: " ).append (std::to_string (errStatus));
88
+ else if (errStatus == -EISDIR) message = std::string (" Is a directory, code: " ).append (std::to_string (errStatus));
89
+ else if (errStatus == -EINVAL) message = std::string (" Invalid argument, code: " ).append (std::to_string (errStatus));
90
+ else if (errStatus == -ENFILE) message = std::string (" File table overflow, code: " ).append (std::to_string (errStatus));
91
+ else if (errStatus == -EMFILE) message = std::string (" Too many open files, code: " ).append (std::to_string (errStatus));
92
+ else if (errStatus == -ENOTTY) message = std::string (" Not a typewriter, code: " ).append (std::to_string (errStatus));
93
+ else if (errStatus == -ETXTBSY) message = std::string (" Text file busy, code: " ).append (std::to_string (errStatus));
94
+ else if (errStatus == -EFBIG) message = std::string (" File too large, code: " ).append (std::to_string (errStatus));
95
+ else if (errStatus == -ENOSPC) message = std::string (" No space left on device, code: " ).append (std::to_string (errStatus));
96
+ else if (errStatus == -ESPIPE) message = std::string (" Illegal seek, code: " ).append (std::to_string (errStatus));
97
+ else if (errStatus == -EROFS) message = std::string (" Read-only file system, code: " ).append (std::to_string (errStatus));
98
+ else if (errStatus == -EMLINK) message = std::string (" Too many links, code: " ).append (std::to_string (errStatus));
99
+ else if (errStatus == -EPIPE) message = std::string (" Broken pipe, code: " ).append (std::to_string (errStatus));
100
+ else if (errStatus == -EDOM) message = std::string (" Math argument out of domain of func, code: " ).append (std::to_string (errStatus));
101
+ else if (errStatus == -ERANGE) message = std::string (" Math result not representable, code: " ).append (std::to_string (errStatus));
102
+ return message;
103
+ }
35
104
36
105
unsigned long maxName () {
37
106
return IFNAMSIZ;
@@ -78,13 +147,7 @@ void deleteInterface::Execute() {
78
147
if (!!devicesList) {
79
148
for ((device_name) = (devicesList), (len) = 0 ; ((len) = strlen (device_name)); (device_name) += (len) + 1 ) {
80
149
if (device_name == wgName.c_str ()) {
81
- if ((len = wg_add_device (wgName.c_str ())) < 0 ) {
82
- std::string err = " Error code: " ;
83
- err = err.append (std::to_string (len));
84
- if (len == -ENOMEM) err = " Out of memory" ;
85
- else if (len == -errno) err = ((std::string)" Cannot add device, code: " ).append (std::to_string (len));
86
- SetError (err);
87
- }
150
+ if ((len = wg_add_device (wgName.c_str ())) < 0 ) SetError (getKernelMesage (len));
88
151
break ;
89
152
}
90
153
}
@@ -94,12 +157,8 @@ void deleteInterface::Execute() {
94
157
95
158
void setConfig::Execute () {
96
159
int res = setInterface (wgName);
97
- if (res < 0 ) {
98
- std::string err = " Error code: " ;
99
- err = err.append (std::to_string (res));
100
- if (res == -ENOMEM) err = " Out of memory" ;
101
- else if (res == -errno) err = ((std::string)" Cannot add device, code: " ).append (std::to_string (res));
102
- SetError (err);
160
+ if (res < 0 && res != EEXIST) {
161
+ SetError (getKernelMesage (res));
103
162
return ;
104
163
}
105
164
@@ -270,12 +329,7 @@ void setConfig::Execute() {
270
329
271
330
// Set interface config
272
331
if ((res = wg_set_device (deviceStruct)) < 0 ) {
273
- std::string err = " Set wireguard config Error code: " ;
274
- err = err.append (std::to_string (res));
275
- if (res == -ENODEV) err = " No such device" ;
276
- else if (res == -EINVAL) err = " Invalid argument" ;
277
- else if (res == -ENOSPC) err = " No space left on device" ;
278
- SetError (err);
332
+ SetError (getKernelMesage (res));
279
333
}
280
334
281
335
if (res >= 0 ) {
@@ -313,9 +367,7 @@ std::string keyTo64(const uint8_t *key) {
313
367
void getConfig::Execute () {
314
368
int res; wg_device *device;
315
369
if ((res = wg_get_device (&device, strdup (wgName.c_str ()))) < 0 ) {
316
- std::string err = " Cannot get wireguard device, Error code " ;
317
- err = err.append (std::to_string (res));
318
- SetError (err);
370
+ SetError (getKernelMesage (res));
319
371
return ;
320
372
}
321
373
0 commit comments