Skip to content

cmd/cgo: macros for C field names not recognized by cgo #9601

Open
@jeeger

Description

@jeeger

The linux manpage ipv6(7) states that in6_addr is a C struct containing an unsigned char array[16] with name s6_addr. When compiling a C program, this works:

#include <stdio.h>
#include <netinet/in.h>

int main() {
  struct in6_addr addr;
  printf("%s\n", addr.s6_addr);
}

However, the following Go program does not compile with the error addr.s6_addr undefined (type C.struct_in6_addr has no field or method s6_addr):

package main

// #include <netinet/in.h>
// #include <sys/socket.h>
import "C"

import "fmt"

func main() {
    // See man 7 ipv6
    var addr C.struct_in6_addr
    fmt.Printf("%v", addr.s6_addr)
}

Testing with go tool cgo reveals that the struct used is defined in in.h, and thus using addr.__in6_u works with go.

When the include file linux/in6.h is included, and go tool cgo -- -D__USE_KERNEL_IPV6_DEFS test.go is run, the correct (man page) definition of struct in6_addr shows up in _obj/_cgo_gotypes.go. I do not know how to get go to compile this correctly however.

Go version used is go version go1.3.3 linux/amd64. Contact me if you need any more information.

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions