Компьютерные сети 2016, краткая справка
1. getaddrinfo(), struct addrinfo, sockaddr_in, in_addr
sockaddr_in6, in6_addr
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
int getaddrinfo(const char *node, const char *service,
const struct addrinfo *hints,
struct addrinfo **res);
void freeaddrinfo(struct addrinfo *res);
const char *gai_strerror(int errcode);
struct addrinfo {
int ai_flags;
int ai_family;
int ai_socktype;
int ai_protocol;
socklen_t ai_addrlen;
struct sockaddr *ai_addr;
char *ai_canonname;
struct addrinfo *ai_next;
};
man 7 ip:
struct sockaddr_in {
sa_family_t sin_family; /* address family: AF_INET */
in_port_t sin_port; /* port in network byte order */
struct in_addr sin_addr; /* internet address */
};
/* Internet address. */
struct in_addr {
uint32_t s_addr; /* address in network byte order */
};
man 8 ipv6:
struct sockaddr_in6 {
sa_family_t sin6_family; /* AF_INET6 */
in_port_t sin6_port; /* port number */
uint32_t sin6_flowinfo; /* IPv6 flow information */
struct in6_addr sin6_addr; /* IPv6 address */
uint32_t sin6_scope_id; /* Scope ID (new in 2.4) */
};
struct in6_addr {
unsigned char s6_addr[16]; /* IPv6 address */
};
2. getnameinfo()
#include <sys/socket.h>
#include <netdb.h>
int getnameinfo(const struct sockaddr *sa, socklen_t salen,
char *host, size_t hostlen,
char *serv, size_t servlen, int flags);
3. inet_ntop()
#include <arpa/inet.h>
const char *inet_ntop(int af, const void *src,
char *dst, socklen_t size);
4. inet_pton()
#include <arpa/inet.h>
int inet_pton(int af, const char *src, void *dst);
5. htonl(), htons(), ntohl(), ntohs()
#include <arpa/inet.h>
uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint16_t hostshort);
uint32_t ntohl(uint32_t netlong);
uint16_t ntohs(uint16_t netshort);
6. socket()
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
int socket(int domain, int type, int protocol);
Name Purpose Man page
AF_UNIX, AF_LOCAL Local communication unix(7)
AF_INET IPv4 Internet protocols ip(7)
AF_INET6 IPv6 Internet protocols ipv6(7)
SOCK_STREAM Provides sequenced, reliable, two-way, connection-
based byte streams. An out-of-band data transmission
mechanism may be supported.
SOCK_DGRAM Supports datagrams (connectionless, unreliable mes-
sages of a fixed maximum length).
SOCK_SEQPACKET Provides a sequenced, reliable, two-way connection-
based data transmission path for datagrams of fixed
maximum length; a consumer is required to read an
entire packet with each input system call.
7. connect()
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
int connect(int sockfd, const struct sockaddr *addr,
socklen_t addrlen);
8. getsockname()
#include <sys/socket.h>
int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
getsockname() returns the current address to which the socket sockfd
is bound, in the buffer pointed to by addr. The addrlen argument
should be initialized to indicate the amount of space (in bytes)
pointed to by addr. On return it contains the actual size of the
socket address.
9. getpeername()
#include <sys/socket.h>
int getpeername(int sockfd,
struct sockaddr *addr, socklen_t *addrlen);
getpeername() returns the address of the peer connected to the socket
sockfd, in the buffer pointed to by addr. The addrlen argument
should be initialized to indicate the amount of space pointed to by
addr. On return it contains the actual size of the name returned (in
bytes). The name is truncated if the buffer provided is too small.
10. send(), sendto()
#include <sys/types.h>
#include <sys/socket.h>
ssize_t send(int sockfd, const void *buf, size_t len, int flags);
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
const struct sockaddr *dest_addr, socklen_t addrlen);
ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags);
11. recv(), recvfrom()
#include <sys/types.h>
#include <sys/socket.h>
ssize_t recv(int sockfd, void *buf, size_t len, int flags);
ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,
struct sockaddr *src_addr, socklen_t *addrlen);
ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags);
If src_addr is not NULL, and the underlying protocol provides the
source address, this source address is filled in. When src_addr is
NULL, nothing is filled in; in this case, addrlen is not used, and
should also be NULL. The argument addrlen is a value-result argu-
ment, which the caller should initialize before the call to the size
of the buffer associated with src_addr, and modified on return to
indicate the actual size of the source address. The returned address
is truncated if the buffer provided is too small; in this case,
addrlen will return a value greater than was supplied to the call.
12. bind()
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
int bind(int sockfd, const struct sockaddr *addr,
socklen_t addrlen);
13. listen()
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
int listen(int sockfd, int backlog);
14. accept()
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
argument addr is a pointer to a sockaddr structure. This struc-
ture is filled in with the address of the peer socket, as known to
the communications layer. The exact format of the address returned
addr is determined by the socket's address family (see socket(2) and
the respective protocol man pages). When addr is NULL, nothing is
filled in; in this case, addrlen is not used, and should also be
NULL.
The addrlen argument is a value-result argument: the caller must ini-
tialize it to contain the size (in bytes) of the structure pointed to
by addr; on return it will contain the actual size of the peer
address.
The returned address is truncated if the buffer provided is too
small; in this case, addrlen will return a value greater than was
supplied to the call.