forked from PaddyCahil/windows-api-function-cheatsheets
-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathsocket-cheatsheet.txt
72 lines (70 loc) · 16.3 KB
/
socket-cheatsheet.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
. .
|\/\/|
|____|
.-----------. .-----------. .------. .-----.
/ \.-------./ \.-------*-. | | |----------.
| | .--. \ | / '-' | \
'---. .---' '--' |-. .-:-' /____/_ | .-. |www.c-asm.com
| | .---.___-' | | '.____ \ | | | |www.x.com/7etsuo
| | '-___-''\ | | / | / '-' |discord.gg/c-asm
| |\ \ | |/ |-_______-' /
'-_____-' '--_______--' '-_____-'-.__________.' '-__________.'
2024 Shoutout to DeLuks
▗▖ ▗▄▄▄▖▗▖ ▗▖▗▖ ▗▖▗▖ ▗▖ ▗▄▄▖ ▗▄▖ ▗▄▄▖▗▖ ▗▖▗▄▄▄▖▗▄▄▄▖ for the Tetsuo graphics
▐▌ █ ▐▛▚▖▐▌▐▌ ▐▌ ▝▚▞▘ ▐▌ ▐▌ ▐▌▐▌ ▐▌▗▞▘▐▌ █
▐▌ █ ▐▌ ▝▜▌▐▌ ▐▌ ▐▌ ▝▀▚▖▐▌ ▐▌▐▌ ▐▛▚▖ ▐▛▀▀▘ █
▐▙▄▄▖▗▄█▄▖▐▌ ▐▌▝▚▄▞▘▗▞▘▝▚▖ ▗▄▄▞▘▝▚▄▞▘▝▚▄▄▖▐▌ ▐▌▐▙▄▄▖ █
▗▄▄▖▗▖ ▗▖▗▄▄▄▖ ▗▄▖▗▄▄▄▖▗▄▄▖▗▖ ▗▖▗▄▄▄▖▗▄▄▄▖▗▄▄▄▖
▐▌ ▐▌ ▐▌▐▌ ▐▌ ▐▌ █ ▐▌ ▐▌ ▐▌▐▌ ▐▌ █
▐▌ ▐▛▀▜▌▐▛▀▀▘▐▛▀▜▌ █ ▝▀▚▖▐▛▀▜▌▐▛▀▀▘▐▛▀▀▘ █
▝▚▄▄▖▐▌ ▐▌▐▙▄▄▖▐▌ ▐▌ █ ▗▄▄▞▘▐▌ ▐▌▐▙▄▄▖▐▙▄▄▖ █
1. Interaction
2. Port and Service Functions
TCP Server getservbyname(char *name, char *proto) // Fetch port by service name and protocol
┌───────────────────┐ getservbyport(int port, char *proto) // Fetch service name by port and protocol
│ socket() │
└─────────┬─────────┘ 3. Byte Ordering Functions
│ htons (unsigned short hostshort) // Convert 16-bit host to network byte order
┌─────────▼─────────┐ htonl (unsigned long hostlong) // Convert 32-bit host to network byte order
known port │ bind() │ ntohs (unsigned short netshort) // Convert 16-bit network to host byte order
└─────────┬─────────┘ ntohl (unsigned long netlong) // Convert 32-bit network to host byte order
│
┌─────────▼─────────┐ 4. IP Address Functions
│ listen() │ inet_aton (const char *strptr, s ruct in_addr *addrptr) // Convert string to network address
└─────────┬─────────┘ inet_addr (const char *strptr) // Convert string to IPv4 address (32-bit network order)
│ inet_ntoa (struct in_addr inaddr) // Convert IPv4 address to string
┌─────────▼─────────┐
│ accept() │ 5. Socket Core Functions
└─────────┬─────────┘ socket (int family, int type, int protocol) // Get socket descriptor
│ connect (int sockfd, struct sockaddr *serv_addr, int addrlen) // Connect to server
TCP Client ▼ bind (int sockfd, struct sockaddr *my_addr, int addrlen) // Bind socket to local address
┌───────────────────┐ blocks until connection listen (int sockfd, int backlog) // Listen for incoming connections
│ socket() │ from client accept (int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen) // Accept connection
└─────────┬─────────┘ │ send (int sockfd, const void *msg, int len, int flags) // Send data
│ │ recv (int sockfd, void *buf, int len, unsigned int flags) // Receive data
┌─────────▼─────────┐ connection │ sendto (int sockfd, const void *msg, int len, unsigned int flags, const struct sockaddr *to, int tolen) // Send to UNCONNECTED socket
│ connect() ◄─────────────────────► recvfrom (int sockfd, void *buf, int len, unsigned int flags, struct sockaddr *from, int *fromlen) // Receive from UNCONNECTED socket
└─────────┬─────────┘(TCP 3-way handshake)│ close (int sockfd) // Close socket
│ │ shutdown (int sockfd, int how) // Gracefully close socket
┌─────────▼─────────┐ │ select (int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout) // Monitor multiple sockets
┌─────► write() ┼────┐ ┌─────────▼─────────┐
│ └─────────┬─────────┘ └──────► read() │ 6. Socket Helper Functions
│ │ └─────────┬─────────┘ write (int fildes, const void *buf, int nbyte) // Write to file descriptor
│ │ │ read (int fildes, const void *buf, int nbyte) // Read from file descriptor
│ │ ▼ fork (void) // Create new process
│ │ process request bzero (void *s, int nbyte) // Set memory to 0
│ │ │ bcmp (const void *s1, const void *s2, int nbyte) // Compare byte strings
│ │ │ bcopy (const void *s1, void *s2, int nbyte) // Copy byte strings
│ │ ┌─────────▼─────────┐ memset (void *s, int c, int nbyte) // Set memory to specific value
│ │ ┌──────┼ write() │
│ ┌──────────▼────────┐ data(reply)└─────────┬─────────┘ 7. Linux Socket Structures
└────│ read() ◄─────┘ │ sockaddr
└──────────┬────────┘ │ struct sockaddr { unsigned short sa_family; char sa_data[14]; } // Generic socket address
│ ┌─────────▼─────────┐ sockaddr_in
│ │ read() │ struct sockaddr_in { short int sin_family; unsigned short int sin_port; struct in_addr sin_addr; unsigned char sin_zero[8]; } // IPv4 address
│ └─────────┬─────────┘ in_addr
┌──────────▼────────┐ │ struct in_addr { unsigned long s_addr; } // 32-bit IPv4 address
│ close() ├────┐ │ hostent
└───────────────────┘ EOF ┌─────────▼─────────┐ struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } // Host information
└───────► close() │ servent
└───────────────────┘ struct servent { char *s_name; char **s_aliases; int s_port; char *s_proto; } // Service and port information