Skip to content

Instantly share code, notes, and snippets.

unsigned int index = reciprocal_scale(phash, num_socks);
return reuse->socks[index];
$ ss -tlnpe | grep :45000
LISTEN 0 128 *:45000 *:* users:(("my_server",pid=3020,fd=3),("my_server",pid=3019,fd=3)) ino:3854904087 sk:37d5a0
$ ss -tlnpe | grep :45000
LISTEN 0 128 *:45000 *:* users:(("my_server",pid=1975,fd=3)) ino:3854935788 sk:37d59c
LISTEN 0 128 *:45000 *:* users:(("my_server",pid=1974,fd=3)) ino:3854935786 sk:37d59d
struct sock *inet_csk_accept(struct sock *sk)
{
struct sock *newsk = NULL; /* client socket */
/* Make sure that this socket is listening, and that it has something pending. */
lock_sock(sk);
if (sk->sk_state == TCP_LISTEN)
if ("there are completed connections waiting to be accepted")
newsk = get_first_connection(sk);
release_sock(sk);
server.sin_family = AF_INET;
server.sin_port = htons(SERVER_PORT);
bcopy(server_ent->h_addr, &server.sin_addr.s_addr, server_ent->h_length);
/* Connect to server, and set the socket's destination IP address and port#
* based on above parameters. Also, request the kernel to automatically set
* the Source IP and port# if the application did not call bind() prior to connect().
*/
connect(fd, (struct sockaddr *)&server, sizeof server);
srv_addr.sin_family = AF_INET;
srv_addr.sin_addr.s_addr = INADDR_ANY;
srv_addr.sin_port = htons(SERVER_PORT);
bind(fd, &srv_addr, sizeof srv_addr);
const struct linger opt = { .l_onoff = 1, .l_linger = 0 };
setsockopt(fd, SOL_SOCKET, SO_LINGER, &opt, sizeof opt);
close(fd);
@kkumar-fk
kkumar-fk / text-file
Created July 24, 2019 02:56
Typical server program
1. Create a socket:
server_fd = socket(...);
2. Bind to a well known IP address and port#:
ret = bind(server_fd, ...);
3. Mark the socket as passive by changing it's state to LISTEN:
ret = listen(server_fd, ...);
4. Wait for a client to connect, and get a reference file descriptor:
client_fd = accept(server_fd, ...);
entry: | __x64_sys_sendto() {
entry: | __sys_sendto() {
entry: | sockfd_lookup_light() {
exit: 0.722 us | }
entry: | sock_sendmsg() {
entry: | security_socket_sendmsg() {
entry: | apparmor_socket_sendmsg() {
exit: 0.763 us | }
exit: 1.069 us | }
entry: | inet_sendmsg() {
@kkumar-fk
kkumar-fk / libvirt_netif_definition.xml
Created November 29, 2021 13:38
Libvirt Network Interfaces definition for a VM
<interface type='network'>
<mac address='52:54:00:00:12:53'/>
<source network='enp66s0f0_br'/>
<target dev='tap01'/>
<model type='virtio'/>
<driver name='vhost' queues='4'/>
<link state='down'/>
<teaming type='persistent'/>
<alias name='ua-backup0'/>
</interface>