Skip to content

Commit

Permalink
fix ip address conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Downs authored and Brian Downs committed Jun 11, 2018
1 parent 21a4ff2 commit 8ae1d23
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 46 deletions.
4 changes: 3 additions & 1 deletion _examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io/ioutil"
"os"
"time"

"github.com/briandowns/jail"
)
Expand All @@ -13,7 +14,7 @@ func main() {
Path: "/zroot/jails/build", //Make sure this directory exists
Name: "jailname",
Hostname: "hostname",
IP4: "192.168.0.200/24",
IP4: "192.168.0.200",
Chdir: true,
}
jid, err := jail.Jail(o)
Expand All @@ -22,6 +23,7 @@ func main() {
os.Exit(1)
}
fmt.Printf("JID: %d - / director listing in jail", jid)
time.Sleep(30 * time.Second)
files, err := ioutil.ReadDir(".")
if err != nil {
fmt.Println(err)
Expand Down
30 changes: 11 additions & 19 deletions jail.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,13 @@ func Jail(o *Opts) (int, error) {
}
var uint32ip uint32
if o.IP4 != "" {
uip, err := ipToUint32(o.IP4)
if err != nil {
return 0, err
}
uint32ip = uip
uint32ip = ip2int(net.ParseIP(o.IP4))
}
ia := &inAddr{
sAddr: inAddrT(uint32ip),
}
ia := &inAddr{sAddr: inAddrT(uint32ip)}
j := &jail{
Version: uint32(0),
Version: uint32(2),
Path: uintptr(unsafe.Pointer(jp)),
Hostname: uintptr(unsafe.Pointer(hn)),
Name: uintptr(unsafe.Pointer(jn)),
Expand All @@ -144,8 +142,8 @@ func Jail(o *Opts) (int, error) {
return 0, fmt.Errorf("invalid version: %d", e1)
case ErrjailNoFreeJIDFound:
return 0, fmt.Errorf("no free JID found: %d", e1)
case ErrJailNoSuchFileDirectory:
return 0, fmt.Errorf("No such file or directory: %s\n", o.Path)
case ErrJailNoSuchFileDirectory:
return 0, fmt.Errorf("No such file or directory: %s\n", o.Path)
}
return 0, fmt.Errorf("%d", e1)
}
Expand Down Expand Up @@ -324,18 +322,12 @@ func attachRemove(call, jailID int) error {
return nil
}

// ipToUint32 converts a string representation of an IP address
// into an uint32
func ipToUint32(sip string) (uint32, error) {
var ip net.IP
ip, _, err := net.ParseCIDR(sip)
if err != nil {
return 0, err
}
// ip2int converts the given IP address to an uint32
func ip2int(ip net.IP) uint32 {
if len(ip) == 16 {
return binary.BigEndian.Uint32(ip[12:16]), nil
return binary.LittleEndian.Uint32(ip[12:16])
}
return binary.LittleEndian.Uint32(ip), nil
return binary.LittleEndian.Uint32(ip)
}

// uint32ip converts an uint32 representation of a string into an IP
Expand Down
26 changes: 0 additions & 26 deletions jail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,29 +204,3 @@ func Test_uint32ip(t *testing.T) {
})
}
}

func Test_ipToUint32(t *testing.T) {
type args struct {
sip string
}
tests := []struct {
name string
args args
want uint32
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ipToUint32(tt.args.sip)
if (err != nil) != tt.wantErr {
t.Errorf("ipToUint32() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("ipToUint32() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 8ae1d23

Please sign in to comment.