terom@7: """ terom@7: Libc stuff terom@7: """ terom@7: terom@7: cdef extern from "stdint.h" : terom@7: # yes, these are "wrong" terom@7: ctypedef unsigned char uint8_t terom@7: ctypedef unsigned short uint16_t terom@7: ctypedef unsigned int uint32_t terom@7: terom@7: ctypedef signed char int8_t terom@7: ctypedef signed short int16_t terom@7: ctypedef signed int int32_t terom@7: terom@7: cdef extern from "sys/types.h" : terom@7: # potentially wrong... terom@7: ctypedef signed long ssize_t terom@7: terom@7: # terom@7: cdef extern from "linux/types.h" : terom@7: ctypedef uint8_t __u8 terom@7: ctypedef uint16_t __u16 terom@7: ctypedef uint32_t __u32 terom@7: terom@7: ctypedef int8_t __s8 terom@7: ctypedef int16_t __s16 terom@7: ctypedef int32_t __s32 terom@7: terom@7: cdef extern from "errno.h" : terom@7: int errno terom@7: terom@7: cdef extern from "string.h" : terom@7: void* memcpy (void *dest, void *src, size_t n) terom@7: void* memset (void *s, int c, size_t n) terom@7: terom@7: char* strerror (int errno) terom@7: terom@7: cdef extern from "alloca.h" : terom@7: void* alloca (size_t size) terom@7: terom@7: cdef extern from "sys/uio.h" : terom@7: struct iovec : terom@7: void *iov_base terom@7: size_t iov_len terom@7: terom@7: cdef extern from "arpa/inet.h" : terom@7: # XXX: correct?! terom@7: ctypedef uint32_t socklen_t terom@7: terom@7: char* c_inet_ntop "inet_ntop" (int af, void *sockaddr, char *buf, socklen_t len) terom@7: int c_inet_pton "inet_pton" (int af, char *src, void *dst) terom@7: terom@7: # terom@7: cdef extern from "sys/socket.h" : terom@7: # socket types terom@7: enum : terom@7: SOCK_STREAM terom@7: SOCK_DGRAM terom@7: SOCK_RAW terom@7: SOCK_RDM terom@7: SOCK_SEQPACKET terom@7: SOCK_PACKET terom@7: terom@7: # packet/address families terom@7: enum : terom@7: PF_UNSPEC terom@7: PF_LOCAL terom@7: PF_UNIX # same as PF_LOCAL terom@7: PF_FILE # same as PF_LOCAL terom@7: PF_INET terom@7: PF_INET6 terom@7: PF_NETLINK terom@7: PF_ROUTE # same as PF_NETLINK terom@7: terom@7: terom@7: # aliases for PF_* terom@7: enum : terom@7: AF_UNSPEC terom@7: AF_LOCAL terom@7: AF_UNIX terom@7: AF_FILE terom@7: AF_INET terom@7: AF_INET6 terom@7: AF_NETLINK terom@7: AF_ROUTE terom@7: terom@7: # base sockaddr stuff terom@7: ctypedef uint16_t sa_family_t terom@7: terom@7: struct sockaddr : terom@7: sa_family_t sa_family terom@7: terom@7: struct sockaddr_storage : terom@7: sa_family_t ss_family terom@7: terom@7: # flag values terom@7: enum : terom@7: MSG_OOB terom@7: MSG_PEEK terom@7: MSG_DONTROUTE terom@7: MSG_CTRUNC terom@7: MSG_PROXY terom@7: MSG_TRUNC terom@7: MSG_DONTWAIT terom@7: MSG_EOR terom@7: MSG_WAITALL terom@7: MSG_FIN terom@7: MSG_SYN terom@7: MSG_CONFIRM terom@7: MSG_RST terom@7: MSG_ERRQUEUE terom@7: MSG_NOSIGNAL terom@7: MSG_MORE terom@7: MSG_CMSG_CLOEXEC terom@7: terom@7: ## send/recv-msg terom@7: struct msghdr : terom@7: void *msg_name # sockaddr terom@7: socklen_t msg_namelen terom@7: terom@7: iovec *msg_iov # message data terom@7: size_t msg_iovlen terom@7: terom@7: void *msg_control # aux data terom@7: size_t msg_controllen terom@7: terom@7: int msg_flags # flags terom@7: terom@7: struct cmsghdr : terom@7: size_t cmsg_len # length including this cmsghdr struct terom@7: terom@7: int cmsg_level # originating protocol (IPPROTO_* ???) terom@7: int cmsg_type # protocol-specific type terom@7: terom@7: ## socket-level cmsghdr types terom@7: enum : terom@7: SCM_RIGHTS terom@7: SCM_CREDENTIALS terom@7: terom@7: #- ucred terom@7: terom@7: # SO_LINGER parameters terom@7: struct linger : terom@7: int l_onoff terom@7: int l_linger terom@7: terom@7: terom@7: ## API terom@7: int socket (int domain, int socktype, int protocol) terom@7: int bind (int fd, sockaddr *addr, socklen_t len) terom@7: int connect (int fd, sockaddr *addr, socklen_t len) terom@7: int listen (int fd, int n) terom@7: int accept (int fd, sockaddr *addr, socklen_t *len) terom@7: int shutdown (int fd, int how) terom@7: terom@7: int getsockname (int fd, sockaddr *addr, socklen_t *len) terom@7: int getpeername (int fd, sockaddr *addr, socklen_t *len) terom@7: terom@7: ssize_t send (int fd, void *buf, size_t n, int flags) terom@7: ssize_t recv (int fd, void *buf, size_t n, int flags) terom@7: ssize_t sendto (int fd, void *buf, size_t n, int flags, sockaddr *addr, socklen_t addr_len) terom@7: ssize_t recvfrom (int fd, void *buf, size_t n, int flags, sockaddr *addr, socklen_t addr_len) terom@7: ssize_t sendmsg (int fd, msghdr *msg, int flags) terom@7: ssize_t recvmsg (int fd, msghdr *msg, int flags) terom@7: terom@7: int getsockopt (int fd, int level, int optname, void *optval, socklen_t optlen) terom@7: int setsockopt (int fd, int level, int optname, void *optval, socklen_t optlen) terom@7: terom@7: enum : terom@7: SHUT_RD terom@7: SHUT_WR terom@7: SHUT_RDWR terom@7: terom@7: cdef extern from "netinet/in.h" : terom@7: ## socket protocol types terom@7: enum : terom@7: IPPROTO_IP terom@7: IPPROTO_ICMP terom@7: IPPROTO_IGMP terom@7: IPPROTO_TCP terom@7: IPPROTO_UDP terom@7: IPPROTO_IPV6 terom@7: IPPROTO_ICMPV6 terom@7: IPPROTO_SCTP terom@7: IPPROTO_RAW terom@7: terom@7: terom@7: ## ports terom@7: ctypedef uint16_t in_port_t terom@7: terom@7: ## AF_INET terom@7: ctypedef uint32_t in_addr_t terom@7: struct in_addr : terom@7: in_addr_t s_addr terom@7: terom@7: # XXX: should these be in another cdef? terom@7: in_addr_t INADDR_ANY terom@7: terom@7: ## AF_INET6 terom@7: struct in6_addr : terom@7: # XXX: check POSIX... terom@7: uint8_t s6_addr[16] terom@7: uint16_t s6_addr16[8] terom@7: uint32_t s6_addr32[4] terom@7: terom@7: # common in6_addr's terom@7: in6_addr in6addr_any terom@7: in6_addr in6addr_loopback terom@7: terom@7: ## constants terom@7: enum : terom@7: INET_ADDRSTRLEN terom@7: INET6_ADDRSTRLEN terom@7: terom@7: ## sockaddrs terom@7: struct sockaddr_in : terom@7: sa_family_t sin_family terom@7: in_port_t sin_port terom@7: in_addr sin_addr terom@7: terom@7: struct sockaddr_in6 : terom@7: sa_family_t sin6_family terom@7: in_port_t sin6_port terom@7: uint32_t sin6_flowinfo terom@7: in6_addr sin6_addr terom@7: uint32_t sin6_scope_id terom@7: terom@7: uint16_t htons(uint16_t) terom@7: uint32_t htonl(uint32_t) terom@7: uint16_t ntohs(uint16_t) terom@7: uint32_t ntohl(uint32_t) terom@7: terom@7: cdef extern from "netdb.h" : terom@7: ## getaddrinfo terom@7: struct addrinfo : terom@7: int ai_flags terom@7: int ai_family terom@7: int ai_socktype terom@7: int ai_protocol terom@7: int ai_addrlen terom@7: sockaddr *ai_addr terom@7: char *ai_canonname terom@7: addrinfo *ai_next terom@7: terom@7: enum : terom@7: AI_PASSIVE terom@7: AI_CANONNAME terom@7: AI_NUMERICHOST terom@7: AI_V4MAPPED terom@7: AI_ALL terom@7: AI_ADDRCONFIG terom@7: # AI_*IDN* terom@7: AI_NUMERICSERV terom@7: terom@7: int c_getaddrinfo "getaddrinfo" ( terom@7: char *node, char *service, terom@7: addrinfo *hints, addrinfo **res terom@7: ) terom@7: terom@7: void c_freeaddrinfo "freeaddrinfo" (addrinfo *res) terom@7: terom@7: ## getnameinfo terom@7: int c_getnameinfo "getnameinfo" ( terom@7: sockaddr *sa, socklen_t salen, terom@7: char *host, size_t hostlen, terom@7: char *serv, size_t servlen, terom@7: int flags terom@7: ) terom@7: terom@7: enum : terom@7: NI_NOFQDN terom@7: NI_NUMERICHOST terom@7: NI_NAMEREQD terom@7: NI_NUMERICSERV terom@7: NI_DGRAM terom@7: terom@7: NI_MAXHOST terom@7: NI_MAXSERV terom@7: terom@7: char* gai_strerror (int err) terom@7: terom@7: # python-friendly wrapper around inet_ntop terom@7: cdef object inet_ntop (int af, void *sockaddr) terom@7: cdef object inet_pton (int af, char *addr, void *sockaddr_out) terom@7: terom@7: # sockaddr, flags -> (host, service) terom@7: cdef object getnameinfo (sockaddr *sa, socklen_t salen, int flags) terom@7: terom@7: ## general errno-based errors terom@7: #cdef class Errno (py.OSError) : terom@7: # """ terom@7: # Some libc function returned an error code: terom@7: # terom@7: # func - the name of the function called terom@7: # err - the system error code terom@7: # strerror - human-readable error code -> message terom@7: # """ terom@7: # terom@7: # cdef readonly char *func terom@7: # cdef readonly int err terom@7: # cdef readonly object strerror terom@7: # terom@7: #cdef class GAIError (py.OSError) : terom@7: # """ terom@7: # Some libc GAI function returnd an error code: terom@7: # terom@7: # func - the name of the function called terom@7: # err - the GAI_* error code terom@7: # strerror - human-readable error code -> message terom@7: # """ terom@7: # terom@7: # cdef readonly char *func terom@7: # cdef readonly int err terom@7: # cdef readonly object strerror terom@7: #