unp #2 (reading notes) (fundamental functions & declarations)

#include <netinet/in.h>

/* Internet address */
sruct in_addr {
  uint32_t            s_addr;       /* address in network byte order */
};

struct sockaddr_in {
  sa_familt_t         sin_family;   /* address family: AF_INET */
  in_port_t           sin_port;     /* port in network byte order */
  struct in_addr      sin_addr;     /* internet address */
};


#include <sys/socket.h>

struct sockaddr {
  sa_family_t         sa_family;        /* address family, AF_xxx       */
  char                sa_data[14];      /* 14 bytes of protocol address */ 
};


#include <sys/types.h>
#include <sys/socket.h>
/* application to kernel */
int bind(int sockfd, const struct sockaddr* addr, socklen_t addrlen);
int connect(int sockfd, const struct sockaddr* addr, socklen_t addrlen);
ssize_t sendto(int sockfd, const void* buf, size_t len, int flags,
               const struct sockaddr* dest_addr, socklen_t addrlen);

/* kernel to application */
int accept(int sockfd, struct sockaddr* addr, socklen_t* addrlen);
ssize_t recvfrom(int sockfd, void* buf, size_t len, int flags,
                 struct sockaddr* src_addr, socklen_t* addrlen);
int getsockname(int sockfd, struct sockaddr* addr, socklen_t* addrlen);
int getpeername(int sockfd, struct sockaddr* addr, socklen_t* addrlen);


/* Check if little endian or big endian. */
#include <stdio.h>
int main() {
  union {
    short s;
    char c[sizeof(short)];
  } un;
  un.s = 0x0102;
  if (sizeof(short) == 2) {
    if (un.c[0] == 1 && un.c[1] == 2)
      printf("big-endian\n");
    else if (un.c[0] == 2 && un.c[1] == 1)
      printf("little-endian\n");
    else
      printf("unknown\n");
  } else 
    printf("sizeof(short) = %d\n", sizeof(short));
  return 0;
}


#include <arpa/inet.h>
uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint16_t hostshort);
uint32_t ntohl(uint32_t netlong);
uint16_t htons(uint16_t netshort);


#include <string.h>
void bzero(void* s, size_t n);
void bcopy(const void* src, void* dest, size_t n);
int bcmp(const void* s1, const void* s2, size_t n);
void* memset(void* s, int c, size_t n);
int memcmp(const void* s1, const void* s2, size_t n);
void* memcpy(void* dest, const void* src, size_t n);


#include <arpa/inet.h>
int inet_aton(const char* cp, struct in_addr *inp);
in_addr_t inet_addr(const char* cp);
char* inet_ntoa(struct in_addr in);

int inet_pton(int af, const char* src, void* dst);
const char* inet_ntop(int af, const void* src, char* dst, socklen_t size);


#include <unistd.h>
ssize_t write(int fd, const void* buf, size_t count);
ssize_t read(int fd, void* buf, size_t count);


#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int fstat(int fd, struct stat* buf);

#include <unistd.h>
int pid_t fork(void);
#include <unistd.h>
extern char** environ;
int execl(const char* path, const char* arg, ...);
int execlp(const char* file, const char* arg, ...);
int execle(const char* path, const char* arg, ..., char* const envp[]);
int execv(const char* path, char* const argv[]);       /* system call */
int execvp(const char* file, char* const argv[]);
int execvpe(const char* file, char* const argv[], char* const envp[]);

Figure 1
Figure 2

posted @ 2013-07-28 21:30  srk  阅读(115)  评论(0编辑  收藏  举报