-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinet_sockets.c
More file actions
54 lines (44 loc) · 1.67 KB
/
inet_sockets.c
File metadata and controls
54 lines (44 loc) · 1.67 KB
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
/*************************************************************************\
* Copyright (C) Michael Kerrisk, 2017. *
* *
* This program is free software. You may use, modify, and redistribute it *
* under the terms of the GNU Lesser General Public License as published *
* by the Free Software Foundation, either version 3 or (at your option) *
* any later version. This program is distributed without any warranty. *
* See the files COPYING.lgpl-v3 and COPYING.gpl-v3 for details. *
\*************************************************************************/
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <unistd.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <string.h>
int openTcpServerSocket(int backlog, int port)
{
struct sockaddr_in6 sockaddr;
int sock;
int rc;
/*
* Create the socket address structure
*/
memset((char *) &sockaddr, '\0', sizeof(struct sockaddr_in6));
sockaddr.sin6_family = AF_INET6;
sockaddr.sin6_port = htons((short) (port & 0xFFFF));
memset(&sockaddr.sin6_addr.s6_addr[0], 0, sizeof(sockaddr.sin6_addr.s6_addr));
sock = socket(AF_INET6, SOCK_STREAM, 0);
if (sock < 0) {
return -1;
}
fcntl(sock, F_SETFD, FD_CLOEXEC);
rc = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &rc, sizeof(rc));
if (bind(sock, (struct sockaddr *) &sockaddr, sizeof(sockaddr)) < 0) {
close(sock);
return -1;
}
if (listen(sock, backlog) < 0) {
close(sock);
return -1;
}
return sock;
}