diff options
-rw-r--r-- | jail.go | 82 |
1 files changed, 80 insertions, 2 deletions
@@ -3,10 +3,14 @@ package jail // #include <stdio.h> // #include <stdlib.h> // #include <string.h> +// #include <errno.h> // #include <sys/param.h> // #include <sys/jail.h> // #include <sys/uio.h> -// #include <errno.h> +// #include <sys/types.h> +// #include <sys/socket.h> +// #include <netinet/in.h> +// #include <arpa/inet.h> // // void set_iov_field(struct iovec *iov, int field, void *val, size_t len) { // iov[field].iov_base = val; @@ -34,6 +38,14 @@ package jail // int get_errno() { // return errno; // } +// +// struct in_addr *get_in_addr_ptr(struct in_addr *a, int idx) { +// return &a[idx]; +// } +// +// struct in6_addr *get_in6_addr_ptr(struct in6_addr *a, int idx) { +// return &a[idx]; +// } import "C" import "errors" import "fmt" @@ -72,7 +84,73 @@ func mapToIov(params map[string]interface{}) (unsafe.Pointer, int, []unsafe.Poin C.set_iov_field((*C.struct_iovec)(iov), C.int(i), unsafe.Pointer(c_key), C.strlen(c_key)+1) i++ - if v_i, ok := v.(int); ok { + if k == "ip4.addr" { + if v_ip, ok := v.(string); ok { + ip := C.malloc(C.sizeof_struct_in_addr) + freeList = append(freeList, ip) + + c_str := C.CString(v_ip) + + if C.inet_pton(C.AF_INET, c_str, ip) == -1 { + panic("Invalid IPv4 address") + } + + C.free(unsafe.Pointer(c_str)) + + C.set_iov_field((*C.struct_iovec)(iov), C.int(i), ip, C.sizeof_struct_in_addr) + } else if v_a, ok := v.([]string); ok { + ips := C.malloc(C.ulong(C.sizeof_struct_in_addr * len(v_a))) + freeList = append(freeList, ips) + + for i, ip := range v_a { + c_str := C.CString(ip) + ptr := C.get_in_addr_ptr((*C.struct_in_addr)(ips), C.int(i)) + + if C.inet_pton(C.AF_INET, c_str, unsafe.Pointer(ptr)) == -1 { + panic("Invalid IPv4 address") + } + + C.free(unsafe.Pointer(c_str)) + } + + C.set_iov_field((*C.struct_iovec)(iov), C.int(i), ips, C.ulong(C.sizeof_struct_in_addr * len(v_a))) + } else { + panic("Unknown IPv4 type") + } + } else if k == "ip6.addr" { + if v_ip, ok := v.(string); ok { + ip := C.malloc(C.sizeof_struct_in6_addr) + freeList = append(freeList, ip) + + c_str := C.CString(v_ip) + + if C.inet_pton(C.AF_INET6, c_str, ip) == -1 { + panic("Invalid IPv6 address") + } + + C.free(unsafe.Pointer(c_str)) + + C.set_iov_field((*C.struct_iovec)(iov), C.int(i), ip, C.sizeof_struct_in6_addr) + } else if v_a, ok := v.([]string); ok { + ips := C.malloc(C.ulong(C.sizeof_struct_in6_addr * len(v_a))) + freeList = append(freeList, ips) + + for i, ip := range v_a { + c_str := C.CString(ip) + ptr := C.get_in6_addr_ptr((*C.struct_in6_addr)(ips), C.int(i)) + + if C.inet_pton(C.AF_INET6, c_str, unsafe.Pointer(ptr)) == -1 { + panic("Invalid IPv6 address") + } + + C.free(unsafe.Pointer(c_str)) + } + + C.set_iov_field((*C.struct_iovec)(iov), C.int(i), ips, C.ulong(C.sizeof_struct_in6_addr * len(v_a))) + } else { + panic("Unknown IPv6 type") + } + } else if v_i, ok := v.(int); ok { c_val := C.malloc(C.sizeof_int) freeList = append(freeList, unsafe.Pointer(c_val)) |