我是靠谱客的博主 兴奋刺猬,最近开发中收集的这篇文章主要介绍socket的实现,安卓系统AOSP源码,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

bioniclibcincludesyssocket.h

#ifndef _SYS_SOCKET_H_
#define _SYS_SOCKET_H_
#include <sys/cdefs.h>
#include <sys/types.h>
#include <linux/socket.h>
#include <asm/fcntl.h>
#include <asm/socket.h>
#include <linux/sockios.h>
#include <linux/uio.h>
#include <linux/types.h>
#include <linux/compiler.h>
__BEGIN_DECLS
#define sockaddr_storage __kernel_sockaddr_storage
typedef unsigned short sa_family_t;
struct timespec;
#ifdef __mips__
#define SOCK_DGRAM      1
#define SOCK_STREAM     2
#define SOCK_RAW        3
#define SOCK_RDM        4
#define SOCK_SEQPACKET  5
#define SOCK_DCCP       6
#define SOCK_PACKET     10
#else
#define SOCK_STREAM      1
#define SOCK_DGRAM       2
#define SOCK_RAW         3
#define SOCK_RDM         4
#define SOCK_SEQPACKET   5
#define SOCK_PACKET      10
#endif
#define SOCK_CLOEXEC O_CLOEXEC
#define SOCK_NONBLOCK O_NONBLOCK
enum {
  SHUT_RD = 0,
#define SHUT_RD         SHUT_RD
  SHUT_WR,
#define SHUT_WR         SHUT_WR
  SHUT_RDWR
#define SHUT_RDWR       SHUT_RDWR
};
struct sockaddr {
  sa_family_t sa_family;
  char sa_data[14];
};
struct linger {
  int l_onoff;
  int l_linger;
};
struct msghdr {
  void* msg_name;
  socklen_t msg_namelen;
  struct iovec* msg_iov;
  size_t msg_iovlen;
  void* msg_control;
  size_t msg_controllen;
  int msg_flags;
};
struct mmsghdr {
  struct msghdr msg_hdr;
  unsigned int msg_len;
};
struct cmsghdr {
  size_t cmsg_len;
  int cmsg_level;
  int cmsg_type;
};
#define CMSG_NXTHDR(mhdr, cmsg) __cmsg_nxthdr((mhdr), (cmsg))
#define CMSG_ALIGN(len) ( ((len)+sizeof(long)-1) & ~(sizeof(long)-1) )
#define CMSG_DATA(cmsg) ((void*)((char*)(cmsg) + CMSG_ALIGN(sizeof(struct cmsghdr))))
#define CMSG_SPACE(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + CMSG_ALIGN(len))
#define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
#define CMSG_FIRSTHDR(msg) 
  ((msg)->msg_controllen >= sizeof(struct cmsghdr) 
   ? (struct cmsghdr*) (msg)->msg_control : (struct cmsghdr*) NULL)
#define CMSG_OK(mhdr, cmsg) ((cmsg)->cmsg_len >= sizeof(struct cmsghdr) &&   (cmsg)->cmsg_len <= (unsigned long)   ((mhdr)->msg_controllen -   ((char*)(cmsg) - (char*)(mhdr)->msg_control)))
struct cmsghdr* __cmsg_nxthdr(struct msghdr*, struct cmsghdr*);
#define SCM_RIGHTS 0x01
#define SCM_CREDENTIALS 0x02
#define SCM_SECURITY 0x03
struct ucred {
  pid_t pid;
  uid_t uid;
  gid_t gid;
};
#define AF_UNSPEC 0
#define AF_UNIX 1
#define AF_LOCAL 1
#define AF_INET 2
#define AF_AX25 3
#define AF_IPX 4
#define AF_APPLETALK 5
#define AF_NETROM 6
#define AF_BRIDGE 7
#define AF_ATMPVC 8
#define AF_X25 9
#define AF_INET6 10
#define AF_ROSE 11
#define AF_DECnet 12
#define AF_NETBEUI 13
#define AF_SECURITY 14
#define AF_KEY 15
#define AF_NETLINK 16
#define AF_ROUTE AF_NETLINK
#define AF_PACKET 17
#define AF_ASH 18
#define AF_ECONET 19
#define AF_ATMSVC 20
#define AF_RDS 21
#define AF_SNA 22
#define AF_IRDA 23
#define AF_PPPOX 24
#define AF_WANPIPE 25
#define AF_LLC 26
#define AF_CAN 29
#define AF_TIPC 30
#define AF_BLUETOOTH 31
#define AF_IUCV 32
#define AF_RXRPC 33
#define AF_ISDN 34
#define AF_PHONET 35
#define AF_IEEE802154 36
#define AF_CAIF 37
#define AF_ALG 38
#define AF_MAX 39
#define PF_UNSPEC AF_UNSPEC
#define PF_UNIX AF_UNIX
#define PF_LOCAL AF_LOCAL
#define PF_INET AF_INET
#define PF_AX25 AF_AX25
#define PF_IPX AF_IPX
#define PF_APPLETALK AF_APPLETALK
#define PF_NETROM AF_NETROM
#define PF_BRIDGE AF_BRIDGE
#define PF_ATMPVC AF_ATMPVC
#define PF_X25 AF_X25
#define PF_INET6 AF_INET6
#define PF_ROSE AF_ROSE
#define PF_DECnet AF_DECnet
#define PF_NETBEUI AF_NETBEUI
#define PF_SECURITY AF_SECURITY
#define PF_KEY AF_KEY
#define PF_NETLINK AF_NETLINK
#define PF_ROUTE AF_ROUTE
#define PF_PACKET AF_PACKET
#define PF_ASH AF_ASH
#define PF_ECONET AF_ECONET
#define PF_ATMSVC AF_ATMSVC
#define PF_RDS AF_RDS
#define PF_SNA AF_SNA
#define PF_IRDA AF_IRDA
#define PF_PPPOX AF_PPPOX
#define PF_WANPIPE AF_WANPIPE
#define PF_LLC AF_LLC
#define PF_CAN AF_CAN
#define PF_TIPC AF_TIPC
#define PF_BLUETOOTH AF_BLUETOOTH
#define PF_IUCV AF_IUCV
#define PF_RXRPC AF_RXRPC
#define PF_ISDN AF_ISDN
#define PF_PHONET AF_PHONET
#define PF_IEEE802154 AF_IEEE802154
#define PF_CAIF AF_CAIF
#define PF_ALG AF_ALG
#define PF_MAX AF_MAX
#define SOMAXCONN 128
#define MSG_OOB 1
#define MSG_PEEK 2
#define MSG_DONTROUTE 4
#define MSG_TRYHARD 4
#define MSG_CTRUNC 8
#define MSG_PROBE 0x10
#define MSG_TRUNC 0x20
#define MSG_DONTWAIT 0x40
#define MSG_EOR 0x80
#define MSG_WAITALL 0x100
#define MSG_FIN 0x200
#define MSG_SYN 0x400
#define MSG_CONFIRM 0x800
#define MSG_RST 0x1000
#define MSG_ERRQUEUE 0x2000
#define MSG_NOSIGNAL 0x4000
#define MSG_MORE 0x8000
#define MSG_WAITFORONE 0x10000
#define MSG_FASTOPEN 0x20000000
#define MSG_CMSG_CLOEXEC 0x40000000
#define MSG_EOF MSG_FIN
#define MSG_CMSG_COMPAT 0
#define SOL_IP 0
#define SOL_TCP 6
#define SOL_UDP 17
#define SOL_IPV6 41
#define SOL_ICMPV6 58
#define SOL_SCTP 132
#define SOL_RAW 255
#define SOL_IPX 256
#define SOL_AX25 257
#define SOL_ATALK 258
#define SOL_NETROM 259
#define SOL_ROSE 260
#define SOL_DECNET 261
#define SOL_X25 262
#define SOL_PACKET 263
#define SOL_ATM 264
#define SOL_AAL 265
#define SOL_IRDA 266
#define SOL_NETBEUI 267
#define SOL_LLC 268
#define SOL_DCCP 269
#define SOL_NETLINK 270
#define SOL_TIPC 271
#define IPX_TYPE 1
#ifdef __i386__
# define __socketcall extern __attribute__((__cdecl__))
#else
# define __socketcall extern
#endif
__socketcall int accept(int, struct sockaddr*, socklen_t*);
__socketcall int accept4(int, struct sockaddr*, socklen_t*, int);
__socketcall int bind(int, const struct sockaddr*, int);
__socketcall int connect(int, const struct sockaddr*, socklen_t);
__socketcall int getpeername(int, struct sockaddr*, socklen_t*);
__socketcall int getsockname(int, struct sockaddr*, socklen_t*);
__socketcall int getsockopt(int, int, int, void*, socklen_t*);
__socketcall int listen(int, int);
__socketcall int recvmmsg(int, struct mmsghdr*, unsigned int, int, const struct timespec*);
__socketcall int recvmsg(int, struct msghdr*, int);
__socketcall int sendmmsg(int, const struct mmsghdr*, unsigned int, int);
__socketcall int sendmsg(int, const struct msghdr*, int);
__socketcall int setsockopt(int, int, int, const void*, socklen_t);
__socketcall int shutdown(int, int);
__socketcall int socket(int, int, int);
__socketcall int socketpair(int, int, int, int*);
extern ssize_t send(int, const void*, size_t, int);
extern ssize_t recv(int, void*, size_t, int);
__socketcall ssize_t sendto(int, const void*, size_t, int, const struct sockaddr*, socklen_t);
__socketcall ssize_t recvfrom(int, void*, size_t, int, const struct sockaddr*, socklen_t*);
__errordecl(__recvfrom_error, "recvfrom called with size bigger than buffer");
extern ssize_t __recvfrom_chk(int, void*, size_t, size_t, int, const struct sockaddr*, socklen_t*);
extern ssize_t __recvfrom_real(int, void*, size_t, int, const struct sockaddr*, socklen_t*) __RENAME(recvfrom);
#if defined(__BIONIC_FORTIFY)
__BIONIC_FORTIFY_INLINE
ssize_t recvfrom(int fd, void* buf, size_t len, int flags, const struct sockaddr* src_addr, socklen_t* addr_len) {
  size_t bos = __bos0(buf);
#if !defined(__clang__)
  if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
    return __recvfrom_real(fd, buf, len, flags, src_addr, addr_len);
  }
  if (__builtin_constant_p(len) && (len <= bos)) {
    return __recvfrom_real(fd, buf, len, flags, src_addr, addr_len);
  }
  if (__builtin_constant_p(len) && (len > bos)) {
    __recvfrom_error();
  }
#endif
  return __recvfrom_chk(fd, buf, len, bos, flags, src_addr, addr_len);
}
__BIONIC_FORTIFY_INLINE
ssize_t recv(int socket, void* buf, size_t len, int flags) {
  return recvfrom(socket, buf, len, flags, NULL, 0);
}
#endif /* __BIONIC_FORTIFY */
#undef __socketcall
__END_DECLS
#endif /* _SYS_SOCKET_H */

bioniclibcbionicsocket.cpp

#include "private/NetdClientDispatch.h"
#include <sys/socket.h>
int socket(int domain, int type, int protocol) {
    return __netdClientDispatch.socket(domain, type, protocol);
}

bioniclibcbionicsend.cpp

#include <sys/socket.h>
ssize_t send(int socket, const void* buf, size_t len, int flags) {
  return sendto(socket, buf, len, flags, NULL, 0);//注释1
}

bioniclibcbionicrecv.cpp

#undef _FORTIFY_SOURCE
#include <sys/socket.h>
ssize_t recv(int socket, void *buf, size_t len, int flags) {
  return recvfrom(socket, buf, len, flags, NULL, 0);
}

bioniclibcbionicconnect.cpp

#include "private/NetdClientDispatch.h"
#include <sys/socket.h>
int connect(int sockfd, const sockaddr* addr, socklen_t addrlen) {
    return __netdClientDispatch.connect(sockfd, addr, addrlen);
}

bioniclibcbionicbindresvport.c

#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#define START_PORT	600
#define END_PORT	IPPORT_RESERVED
#define NUM_PORTS	(END_PORT - START_PORT)
int bindresvport(int sd, struct sockaddr_in *sin)
{
    static short        port;
    struct sockaddr_in  sin0;
    int                 nn, ret;
    if (sin == NULL) {
        sin = &sin0;
        memset( sin, 0, sizeof *sin );
        sin->sin_family = AF_INET;
    } else if (sin->sin_family != AF_INET) {
        errno = EPFNOSUPPORT;
        return -1;
    }
    if (port == 0) {
        port = START_PORT + (getpid() % NUM_PORTS);
    }
    for (nn = NUM_PORTS; nn > 0; nn--, port++) 
    {
        if (port == END_PORT)
            port = START_PORT;
        sin->sin_port = htons(port);
        do {
            ret = bind(sd, (struct sockaddr*)sin, sizeof(*sin));
        } while (ret < 0 && errno == EINTR);
        if (!ret)
            break;
    }
    return ret;
}

bioniclibcbionicaccept.cpp

#include <sys/socket.h>
int accept(int sockfd, sockaddr* addr, socklen_t* addrlen) {
    return accept4(sockfd, addr, addrlen, 0);
}

bioniclibcbionicaccept4.cpp

#include "private/NetdClientDispatch.h"
#include <sys/socket.h>
int accept4(int sockfd, sockaddr* addr, socklen_t* addrlen, int flags) {
    return __netdClientDispatch.accept4(sockfd, addr, addrlen, flags);
}



注释1:sendto方法

Externalchromium_orgnative_client_sdksrclibrariesnacl_iosyscallssocketsendto.c

		#include "nacl_io/kernel_intercept.h"
		#include "nacl_io/kernel_wrap.h"
		#if defined(PROVIDES_SOCKET_API) && !defined(__GLIBC__)
		ssize_t sendto(int fd, const void* buf, size_t len, int flags,
		               const struct sockaddr* addr, socklen_t addrlen) {
		  return ki_sendto(fd, buf, len, flags, addr, addrlen);
		}
		#endif

系统调用
Externalchromium_orgnative_client_sdksrclibrariesnacl_iokernel_intercept.cc 483行

#include "nacl_io/kernel_intercept.h"
#include <assert.h>
#include <errno.h>
#include <string.h>
#include "nacl_io/kernel_proxy.h"
#include "nacl_io/kernel_wrap.h"
#include "nacl_io/kernel_wrap_real.h"
#include "nacl_io/log.h"
#include "nacl_io/osmman.h"
#include "nacl_io/ossocket.h"
#include "nacl_io/ostime.h"
#include "nacl_io/pepper_interface.h"
#include "nacl_io/real_pepper_interface.h"
using namespace nacl_io;
#define ON_NOSYS_RETURN(x)    
  if (!ki_is_initialized()) { 
    errno = ENOSYS;           
    return x;                 
  }
struct KernelInterceptState {
  KernelProxy* kp;
  PepperInterface* ppapi;
  bool kp_owned;
};
static KernelInterceptState s_state;
// The the test code we want to be able to save the previous kernel
// proxy when intialising and restore it on uninit.
static KernelInterceptState s_saved_state;
int ki_push_state_for_testing() {
  assert(s_saved_state.kp == NULL);
  if (s_saved_state.kp != NULL)
    return 1;
  s_saved_state = s_state;
  s_state.kp = NULL;
  s_state.ppapi = NULL;
  s_state.kp_owned = false;
  return 0;
}
static void ki_pop_state() {
  // Swap out the KernelProxy. This will normally reset the
  // proxy to NULL, aside from in test code that has called
  // ki_push_state_for_testing().
  s_state = s_saved_state;
  s_saved_state.kp = NULL;
  s_saved_state.ppapi = NULL;
  s_saved_state.kp_owned = false;
}
int ki_pop_state_for_testing() {
  ki_pop_state();
  return 0;
}
int ki_init(void* kp) {
  LOG_TRACE("ki_init: %p", kp);
  return ki_init_ppapi(kp, 0, NULL);
}
int ki_init_ppapi(void* kp,
                  PP_Instance instance,
                  PPB_GetInterface get_browser_interface) {
  assert(!s_state.kp);
  if (s_state.kp != NULL)
    return 1;
  PepperInterface* ppapi = NULL;
  if (instance && get_browser_interface) {
    ppapi = new RealPepperInterface(instance, get_browser_interface);
    s_state.ppapi = ppapi;
  }
  int rtn = ki_init_interface(kp, ppapi);
  return rtn;
}
int ki_init_interface(void* kp, void* pepper_interface) {
  LOG_TRACE("ki_init_interface: %p %p", kp, pepper_interface);
  assert(!s_state.kp);
  if (s_state.kp != NULL)
    return 1;
  PepperInterface* ppapi = static_cast<PepperInterface*>(pepper_interface);
  kernel_wrap_init();
  if (kp == NULL) {
    s_state.kp = new KernelProxy();
    s_state.kp_owned = true;
  } else {
    s_state.kp = static_cast<KernelProxy*>(kp);
    s_state.kp_owned = false;
  }
  if (s_state.kp->Init(ppapi) != 0)
    return 1;
  return 0;
}
int ki_is_initialized() {
  return s_state.kp != NULL;
}
int ki_uninit() {
  LOG_TRACE("ki_uninit");
  assert(s_state.kp);
  if (s_state.kp == NULL)
    return 1;
  if (s_saved_state.kp == NULL)
    kernel_wrap_uninit();
  // If we are going to delete the KernelProxy don't do it
  // until we've swapped it out.
  KernelInterceptState state_to_delete = s_state;
  ki_pop_state();
  if (state_to_delete.kp_owned)
    delete state_to_delete.kp;
  delete state_to_delete.ppapi;
  return 0;
}
nacl_io::KernelProxy* ki_get_proxy() {
  return s_state.kp;
}
int ki_chdir(const char* path) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->chdir(path);
}
void ki_exit(int status) {
  if (ki_is_initialized())
    s_state.kp->exit(status);
  _real_exit(status);
}
char* ki_getcwd(char* buf, size_t size) {
  // gtest uses getcwd in a static initializer and expects it to always
  // succeed.  If we haven't initialized kernel-intercept yet, then try
  // the IRT's getcwd, and fall back to just returning ".".
  if (!ki_is_initialized()) {
    int rtn = _real_getcwd(buf, size);
    if (rtn != 0) {
      if (rtn == ENOSYS) {
        buf[0] = '.';
        buf[1] = 0;
      } else {
        errno = rtn;
        return NULL;
      }
    }
    return buf;
  }
  return s_state.kp->getcwd(buf, size);
}
char* ki_getwd(char* buf) {
  ON_NOSYS_RETURN(NULL);
  return s_state.kp->getwd(buf);
}
int ki_dup(int oldfd) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->dup(oldfd);
}
int ki_dup2(int oldfd, int newfd) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->dup2(oldfd, newfd);
}
int ki_chmod(const char* path, mode_t mode) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->chmod(path, mode);
}
int ki_fchdir(int fd) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->fchdir(fd);
}
int ki_fchmod(int fd, mode_t mode) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->fchmod(fd, mode);
}
int ki_stat(const char* path, struct stat* buf) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->stat(path, buf);
}
int ki_mkdir(const char* path, mode_t mode) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->mkdir(path, mode);
}
int ki_rmdir(const char* path) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->rmdir(path);
}
int ki_mount(const char* source,
             const char* target,
             const char* filesystemtype,
             unsigned long mountflags,
             const void* data) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->mount(source, target, filesystemtype, mountflags, data);
}
int ki_umount(const char* path) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->umount(path);
}
int ki_open(const char* path, int oflag, mode_t mode) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->open(path, oflag, mode);
}
int ki_pipe(int pipefds[2]) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->pipe(pipefds);
}
ssize_t ki_read(int fd, void* buf, size_t nbyte) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->read(fd, buf, nbyte);
}
ssize_t ki_write(int fd, const void* buf, size_t nbyte) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->write(fd, buf, nbyte);
}
int ki_fstat(int fd, struct stat* buf) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->fstat(fd, buf);
}
int ki_getdents(int fd, void* buf, unsigned int count) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->getdents(fd, buf, count);
}
int ki_ftruncate(int fd, off_t length) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->ftruncate(fd, length);
}
int ki_fsync(int fd) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->fsync(fd);
}
int ki_fdatasync(int fd) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->fdatasync(fd);
}
int ki_isatty(int fd) {
  ON_NOSYS_RETURN(0);
  return s_state.kp->isatty(fd);
}
int ki_close(int fd) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->close(fd);
}
off_t ki_lseek(int fd, off_t offset, int whence) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->lseek(fd, offset, whence);
}
int ki_remove(const char* path) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->remove(path);
}
int ki_unlink(const char* path) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->unlink(path);
}
int ki_truncate(const char* path, off_t length) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->truncate(path, length);
}
int ki_lstat(const char* path, struct stat* buf) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->lstat(path, buf);
}
int ki_link(const char* oldpath, const char* newpath) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->link(oldpath, newpath);
}
int ki_rename(const char* path, const char* newpath) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->rename(path, newpath);
}
int ki_symlink(const char* oldpath, const char* newpath) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->symlink(oldpath, newpath);
}
int ki_access(const char* path, int amode) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->access(path, amode);
}
int ki_readlink(const char* path, char* buf, size_t count) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->readlink(path, buf, count);
}
int ki_utimes(const char* path, const struct timeval times[2]) {
  ON_NOSYS_RETURN(-1);
  // Implement in terms of utimens.
  struct timespec ts[2];
  ts[0].tv_sec = times[0].tv_sec;
  ts[0].tv_nsec = times[0].tv_usec * 1000;
  ts[1].tv_sec = times[1].tv_sec;
  ts[1].tv_nsec = times[1].tv_usec * 1000;
  return s_state.kp->utimens(path, ts);
}
int ki_futimes(int fd, const struct timeval times[2]) {
  ON_NOSYS_RETURN(-1);
  // Implement in terms of futimens.
  struct timespec ts[2];
  ts[0].tv_sec = times[0].tv_sec;
  ts[0].tv_nsec = times[0].tv_usec * 1000;
  ts[1].tv_sec = times[1].tv_sec;
  ts[1].tv_nsec = times[1].tv_usec * 1000;
  return s_state.kp->futimens(fd, ts);
}
void* ki_mmap(void* addr,
              size_t length,
              int prot,
              int flags,
              int fd,
              off_t offset) {
  ON_NOSYS_RETURN(MAP_FAILED);
  return s_state.kp->mmap(addr, length, prot, flags, fd, offset);
}
int ki_munmap(void* addr, size_t length) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->munmap(addr, length);
}
int ki_open_resource(const char* file) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->open_resource(file);
}
int ki_fcntl(int d, int request, va_list args) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->fcntl(d, request, args);
}
int ki_ioctl(int d, int request, va_list args) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->ioctl(d, request, args);
}
int ki_chown(const char* path, uid_t owner, gid_t group) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->chown(path, owner, group);
}
int ki_fchown(int fd, uid_t owner, gid_t group) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->fchown(fd, owner, group);
}
int ki_lchown(const char* path, uid_t owner, gid_t group) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->lchown(path, owner, group);
}
int ki_utime(const char* filename, const struct utimbuf* times) {
  ON_NOSYS_RETURN(-1);
  // Implement in terms of utimens.
  struct timespec ts[2];
  ts[0].tv_sec = times->actime;
  ts[0].tv_nsec = 0;
  ts[1].tv_sec = times->modtime;
  ts[1].tv_nsec = 0;
  return s_state.kp->utimens(filename, ts);
}
int ki_futimens(int fd, const struct timespec times[2]) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->futimens(fd, times);
}
int ki_poll(struct pollfd* fds, nfds_t nfds, int timeout) {
  return s_state.kp->poll(fds, nfds, timeout);
}
int ki_select(int nfds,
              fd_set* readfds,
              fd_set* writefds,
              fd_set* exceptfds,
              struct timeval* timeout) {
  return s_state.kp->select(nfds, readfds, writefds, exceptfds, timeout);
}
int ki_tcflush(int fd, int queue_selector) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->tcflush(fd, queue_selector);
}
int ki_tcgetattr(int fd, struct termios* termios_p) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->tcgetattr(fd, termios_p);
}
int ki_tcsetattr(int fd,
                 int optional_actions,
                 const struct termios* termios_p) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->tcsetattr(fd, optional_actions, termios_p);
}
int ki_kill(pid_t pid, int sig) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->kill(pid, sig);
}
int ki_killpg(pid_t pid, int sig) {
  errno = ENOSYS;
  return -1;
}
int ki_sigaction(int signum,
                 const struct sigaction* action,
                 struct sigaction* oaction) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->sigaction(signum, action, oaction);
}
int ki_sigpause(int sigmask) {
  errno = ENOSYS;
  return -1;
}
int ki_sigpending(sigset_t* set) {
  errno = ENOSYS;
  return -1;
}
int ki_sigsuspend(const sigset_t* set) {
  errno = ENOSYS;
  return -1;
}
sighandler_t ki_signal(int signum, sighandler_t handler) {
  return ki_sigset(signum, handler);
}
sighandler_t ki_sigset(int signum, sighandler_t handler) {
  ON_NOSYS_RETURN(SIG_ERR);
  // Implement sigset(2) in terms of sigaction(2).
  struct sigaction action;
  struct sigaction oaction;
  memset(&action, 0, sizeof(action));
  memset(&oaction, 0, sizeof(oaction));
  action.sa_handler = handler;
  int rtn = s_state.kp->sigaction(signum, &action, &oaction);
  if (rtn)
    return SIG_ERR;
  return oaction.sa_handler;
}
#ifdef PROVIDES_SOCKET_API
// Socket Functions
int ki_accept(int fd, struct sockaddr* addr, socklen_t* len) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->accept(fd, addr, len);
}
int ki_bind(int fd, const struct sockaddr* addr, socklen_t len) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->bind(fd, addr, len);
}
int ki_connect(int fd, const struct sockaddr* addr, socklen_t len) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->connect(fd, addr, len);
}
struct hostent* ki_gethostbyname(const char* name) {
  ON_NOSYS_RETURN(NULL);
  return s_state.kp->gethostbyname(name);
}
int ki_getnameinfo(const struct sockaddr *sa,
                   socklen_t salen,
                   char *host,
                   size_t hostlen,
                   char *serv,
                   size_t servlen,
                   unsigned int flags) {
  ON_NOSYS_RETURN(EAI_SYSTEM);
  return s_state.kp->getnameinfo(sa, salen, host, hostlen, serv, servlen,
                                 flags);
}
int ki_getaddrinfo(const char* node,
                   const char* service,
                   const struct addrinfo* hints,
                   struct addrinfo** res) {
  ON_NOSYS_RETURN(EAI_SYSTEM);
  return s_state.kp->getaddrinfo(node, service, hints, res);
}
void ki_freeaddrinfo(struct addrinfo* res) {
  s_state.kp->freeaddrinfo(res);
}
int ki_getpeername(int fd, struct sockaddr* addr, socklen_t* len) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->getpeername(fd, addr, len);
}
int ki_getsockname(int fd, struct sockaddr* addr, socklen_t* len) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->getsockname(fd, addr, len);
}
int ki_getsockopt(int fd, int lvl, int optname, void* optval, socklen_t* len) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->getsockopt(fd, lvl, optname, optval, len);
}
int ki_listen(int fd, int backlog) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->listen(fd, backlog);
}
ssize_t ki_recv(int fd, void* buf, size_t len, int flags) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->recv(fd, buf, len, flags);
}
ssize_t ki_recvfrom(int fd,
                    void* buf,
                    size_t len,
                    int flags,
                    struct sockaddr* addr,
                    socklen_t* addrlen) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->recvfrom(fd, buf, len, flags, addr, addrlen);
}
ssize_t ki_recvmsg(int fd, struct msghdr* msg, int flags) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->recvmsg(fd, msg, flags);
}
ssize_t ki_send(int fd, const void* buf, size_t len, int flags) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->send(fd, buf, len, flags);
}
ssize_t ki_sendto(int fd,
                  const void* buf,
                  size_t len,
                  int flags,
                  const struct sockaddr* addr,
                  socklen_t addrlen) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->sendto(fd, buf, len, flags, addr, addrlen);
}
ssize_t ki_sendmsg(int fd, const struct msghdr* msg, int flags) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->sendmsg(fd, msg, flags);
}
int ki_setsockopt(int fd,
                  int lvl,
                  int optname,
                  const void* optval,
                  socklen_t len) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->setsockopt(fd, lvl, optname, optval, len);
}
int ki_shutdown(int fd, int how) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->shutdown(fd, how);
}
int ki_socket(int domain, int type, int protocol) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->socket(domain, type, protocol);
}
int ki_socketpair(int domain, int type, int protocol, int* sv) {
  ON_NOSYS_RETURN(-1);
  return s_state.kp->socketpair(domain, type, protocol, sv);
}
#endif  // PROVIDES_SOCKET_API





最后

以上就是兴奋刺猬为你收集整理的socket的实现,安卓系统AOSP源码的全部内容,希望文章能够帮你解决socket的实现,安卓系统AOSP源码所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(38)

评论列表共有 0 条评论

立即
投稿
返回
顶部