Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GCO and C symbols defined out of go code #58

Open
ingvagabund opened this issue May 21, 2017 · 1 comment
Open

GCO and C symbols defined out of go code #58

ingvagabund opened this issue May 21, 2017 · 1 comment

Comments

@ingvagabund
Copy link
Contributor

ingvagabund commented May 21, 2017

Given /usr/lib/golang/src/net/cgo_resnew.go from stdlib:

package net

/*
#include <sys/types.h>
#include <sys/socket.h>

#include <netdb.h>
*/
import "C"

import "unsafe"

func cgoNameinfoPTR(b []byte, sa *C.struct_sockaddr, salen C.socklen_t) (int, error) {
        gerrno, err := C.getnameinfo(sa, salen, (*C.char)(unsafe.Pointer(&b[0])), C.socklen_t(len(b)), nil, 0, C.NI_NAMEREQD)
        return int(gerrno), err

definition of some C.name symbols are not known in advance from the go code itself:

  • C.struct_sockaddr
  • C.socklen_t
  • C.getnameinfo
  • C.char
  • C.socklen_t
  • C.NI_NAMEREQD

Type of some of the symbols can be determined based on they context:

  • C.struct_sockaddr and C.socklen_t are data types as they are part of a function signature definition
  • C.getnameinfo is function as it has used in a call expression and has more than one argument
  • C.char is a data type as char is C language keyword for char type

Some can not:

  • C.socklen_t: is it a function call or a data type casting? Given it is used as an parameter type, it is a type
  • C.NI_NAMEREQD: is it a variable or a pointer to a function?

Man page for getnameinfo says:

NAME
       getnameinfo - address-to-name translation in protocol-independent manner

SYNOPSIS
       #include <sys/socket.h>
       #include <netdb.h>

       int getnameinfo(const struct sockaddr *addr, socklen_t addrlen,
                       char *host, socklen_t hostlen,
                       char *serv, socklen_t servlen, int flags);

So one can deduce:

  • C.NI_NAMEREQD is expected to be a variable of type in as there is no call expression

One can process header files sys/types.h, sys/socket.h and netdb.h to get a list of types, variables/constants and functions. E.g. for each C header that is used in a go file provide a special file (e.g. sys/types.g.yaml) with C symbol definitions (just for illustration, types and functions are defined across multiple header file):

header_file: "header/file/name.h"
symbols:
types:
  - type: struct_sockaddr
  - type: socklen_t
functions:
- function: getnameinfo

Or, the yaml file can be defined per cgo go file as well as cgo_resnew.go.yaml or per multiple cgo files as cgo.yaml.

Additional links

@ingvagabund
Copy link
Contributor Author

@pirat89

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant