Commit | Line | Data |
---|---|---|
00afbb65 LM |
1 | /* depend: */ |
2 | /* cflags: */ | |
3 | /* linker: color.o debug.o */ | |
4 | ||
5 | #include <assert.h> | |
6 | #include <stdio.h> | |
7 | #include <stdlib.h> | |
8 | #ifdef _WIN32 /* Windows */ | |
9 | #include <winsock2.h> | |
10 | #include <ws2tcpip.h> | |
11 | #else /* Posix */ | |
12 | #include <errno.h> | |
13 | #include <unistd.h> | |
14 | #include <fcntl.h> | |
15 | #include <netinet/ip.h> | |
16 | #include <netinet/tcp.h> | |
17 | //#include <sys/types.h> | |
18 | //#include <sys/socket.h> | |
19 | #endif | |
20 | ||
21 | #include "debug.h" | |
22 | ||
23 | #include "server.h" | |
24 | ||
25 | /* types */ | |
26 | ||
27 | #ifdef _WIN32 /* Windows */ | |
28 | typedef SOCKET socket_t; | |
29 | #define PF_INET AF_INET | |
30 | #define ERRNO (WSAGetLastError ()) | |
31 | #else /* Posix */ | |
32 | typedef int socket_t; | |
33 | #define closesocket close | |
34 | #define ERRNO errno | |
35 | #define INVALID_SOCKET -1 | |
36 | #define SOCKET_ERROR -1 | |
37 | #endif | |
38 | ||
39 | /* constants */ | |
40 | ||
41 | #define BUFFER_SIZE 4096 | |
42 | ||
43 | /* macros */ | |
44 | ||
45 | /* gobal variables */ | |
46 | ||
47 | /* open listening socket */ | |
48 | ||
49 | socket_t open_listening_socket (int port) | |
50 | { | |
51 | #ifdef _WIN32 /* Windows */ | |
52 | WSADATA WSAData; | |
53 | WSAStartup (MAKEWORD(2,0), &WSAData); | |
54 | assert (INVALID_SOCKET == (socket_t)-1); | |
55 | assert (SOCKET_ERROR == (socket_t)-1); | |
56 | #endif | |
57 | ||
58 | VERBOSE (DEBUG, fprintf (stdout, "Opening socket\n")); | |
59 | socket_t sock = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); | |
60 | if ((sock == INVALID_SOCKET) || (sock < 0)) | |
61 | return -1; | |
62 | ||
63 | struct sockaddr_in addr = {0}; | |
64 | addr.sin_family = PF_INET; | |
65 | addr.sin_port = htons (port); | |
66 | addr.sin_addr.s_addr = htonl (INADDR_ANY); | |
67 | ||
68 | VERBOSE (DEBUG, fprintf (stdout, "Binding socket\n")); | |
69 | int rc = bind (sock, (struct sockaddr *)&addr, sizeof (addr)); | |
70 | if ((rc == SOCKET_ERROR) || (rc < 0)) { | |
71 | VERBOSE (ERROR, fprintf (stderr, "error: %d\n", ERRNO)); | |
72 | rc = closesocket (sock); | |
73 | if ((rc == SOCKET_ERROR) || (rc < 0)) { | |
74 | VERBOSE (ERROR, fprintf (stderr, "error: %d\n", ERRNO)); | |
75 | } | |
76 | return -1; | |
77 | } | |
78 | ||
79 | VERBOSE (DEBUG, fprintf (stdout, "Configuring socket\n")); | |
80 | #ifdef _WIN32 /* Windows */ | |
81 | #else /* Posix */ | |
82 | fcntl (sock, F_SETFL, O_NONBLOCK); | |
83 | #endif | |
84 | int val = 1; | |
85 | rc = setsockopt (sock, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof (val)); | |
86 | if (rc < 0) { | |
87 | VERBOSE (ERROR, fprintf (stderr, "%s\n", "setsockopt/TCP_NODELAY")); | |
88 | closesocket (sock); | |
89 | return -1; | |
90 | } | |
91 | ||
92 | return sock; | |
93 | } | |
94 | ||
95 | /* close listening socket */ | |
96 | void close_listening_socket (socket_t sock) | |
97 | { | |
98 | closesocket (sock); | |
99 | #ifdef _WIN32 /* Windows */ | |
100 | WSACleanup (); | |
101 | #endif | |
102 | } | |
103 | ||
104 | /* vim: set ts=4 sw=4 et: */ |