/* * Funzione che inizializza la socket di ascolto tcp. * Ritorna 0 in caso successo e -1 in caso di errore. */ int set_listen_socket(unsigned short port) { struct sockaddr_in my_addr; int on = 1; if ((tcp_listen = tcp_socket()) < 0) { perror("set_listen_socket error - can't initialize tcp socket"); return -1; } if (setsockopt(tcp_listen, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) { perror("set_listen_socket error - setsockopt failed"); return -1; } printf("PORT: %d\n", ntohs(port)); set_addr_any(&my_addr, ntohs(port)); if (inet_bind(tcp_listen, &my_addr)){ perror ("set_listen_socket error - bind failed on tcp socket"); return -1; } if (listen(tcp_listen, BACKLOG) < 0) { perror ("set_listen_socket error - listen failed on tcp socket"); return -1; } fd_add(tcp_listen); return 0; }
static int restore_tcp_conn_state(int sk, struct inet_sk_info *ii) { int ifd, aux; TcpStreamEntry *tse; pr_info("Restoring TCP connection id %x ino %x\n", ii->ie->id, ii->ie->ino); ifd = open_image(CR_FD_TCP_STREAM, O_RSTR, ii->ie->ino); if (ifd < 0) goto err; if (pb_read_one(ifd, &tse, PB_TCP_STREAM) < 0) goto err_c; if (restore_tcp_seqs(sk, tse)) goto err_c; if (inet_bind(sk, ii)) goto err_c; if (inet_connect(sk, ii)) goto err_c; if (restore_tcp_opts(sk, tse)) goto err_c; if (restore_tcp_queues(sk, tse, ifd)) goto err_c; if (tse->has_nodelay && tse->nodelay) { aux = 1; if (restore_opt(sk, SOL_TCP, TCP_NODELAY, &aux)) goto err_c; } if (tse->has_cork && tse->cork) { aux = 1; if (restore_opt(sk, SOL_TCP, TCP_CORK, &aux)) goto err_c; } tcp_stream_entry__free_unpacked(tse, NULL); close(ifd); return 0; err_c: tcp_stream_entry__free_unpacked(tse, NULL); close(ifd); err: return -1; }
#include "inet_sockets.h" #define BUF_SIZE 1024 int main(int argc __attribute__((unused)), char *argv[] __attribute__((unused))) { int retc; int sfda, sfdb, sfdc; struct sockaddr_in addr; char buf[BUF_SIZE]; sfda = inet_bind("10001", SOCK_DGRAM, NULL); if (sfda == -1) { perror("inet_bind"); exit(EXIT_FAILURE); } sfdb = inet_bind("10002", SOCK_DGRAM, NULL); if (sfdb == -1) { perror("inet_bind"); exit(EXIT_FAILURE); } sfdc = inet_bind("10003", SOCK_DGRAM, NULL); if (sfdc == -1) { perror("inet_bind"); exit(EXIT_FAILURE);