3 /* linker: color.o debug.o */
9 #ifdef _WIN32 /* Windows */
16 #include <netinet/ip.h>
17 #include <netinet/tcp.h>
18 //#include <sys/types.h>
19 //#include <sys/socket.h>
20 #include <sys/select.h>
29 #ifdef _WIN32 /* Windows */
30 #define PF_INET AF_INET
31 #define ERRNO (WSAGetLastError ())
33 #define closesocket close
35 #define SOCKET_ERROR -1
43 #define BUFFER_SIZE 4096
44 #define TIMEOUT 100000
50 /* init network context */
52 void init_network_context (void)
54 #ifdef _WIN32 /* Windows */
56 WSAStartup (MAKEWORD(2,0), &WSAData
);
57 assert (INVALID_SOCKET
== (socket_t
)-1);
58 assert (SOCKET_ERROR
== -1);
62 /* terminate network context */
64 void terminate_network_context (void)
66 #ifdef _WIN32 /* Windows */
71 /* open listening socket */
73 socket_t
open_listening_socket (int port
)
77 VERBOSE (DEBUG
, fprintf (stdout
, "Opening socket\n"));
78 //socket_t sock = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
79 socket_t sock
= socket (AF_INET
, SOCK_STREAM
, 0);
80 if (sock
== INVALID_SOCKET
) {
84 struct sockaddr_in addr
= {0};
85 bzero (&addr
, sizeof (addr
));
86 //addr.sin_family = PF_INET;
87 addr
.sin_family
= AF_INET
;
88 addr
.sin_port
= htons (port
);
89 addr
.sin_addr
.s_addr
= htonl (INADDR_ANY
);
91 VERBOSE (DEBUG
, fprintf (stdout
, "Binding socket\n"));
92 //setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof (val));
93 int rc
= bind (sock
, (struct sockaddr
*)&addr
, sizeof (addr
));
94 if (rc
== SOCKET_ERROR
) {
95 VERBOSE (ERROR
, fprintf (stderr
, "error: bind %d\n", ERRNO
));
96 rc
= closesocket (sock
);
97 if (rc
== SOCKET_ERROR
) {
98 VERBOSE (ERROR
, fprintf (stderr
, "error: close %d\n", ERRNO
));
103 VERBOSE (DEBUG
, fprintf (stdout
, "Configuring socket\n"));
104 #ifndef _WIN32 /* Posix */
105 fcntl (sock
, F_SETFL
, O_NONBLOCK
);
107 rc
= setsockopt (sock
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof (val
));
109 VERBOSE (ERROR
, fprintf (stderr
, "error: %s\n", "setsockopt/TCP_NODELAY"));
111 if (rc
== SOCKET_ERROR
) {
112 VERBOSE (ERROR
, fprintf (stderr
, "error: close %d\n", ERRNO
));
117 rc
= listen (sock
, BACKLOG
);
119 VERBOSE (ERROR
, fprintf (stderr
, "error: %s\n", "listen"));
121 if (rc
== SOCKET_ERROR
) {
122 VERBOSE (ERROR
, fprintf (stderr
, "error: close %d\n", ERRNO
));
130 /* accept incomming connection */
132 socket_t
accept_incoming_connection (socket_t sock
)
134 socket_t connection
= accept (sock
, NULL
, 0);
135 if (connection
< 0) {
136 return INVALID_SOCKET
;
139 #ifndef _WIN32 /* POSIX */
140 fcntl (connection
, F_SETFL
, O_NONBLOCK
);
143 int rc
= setsockopt (connection
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof (val
));
145 VERBOSE (ERROR
, fprintf (stderr
, "error: %s\n", "setsockopt/TCP_NODELAY"));
146 closesocket (connection
);
147 if (rc
== SOCKET_ERROR
) {
148 VERBOSE (ERROR
, fprintf (stderr
, "error: close %d\n", ERRNO
));
150 return INVALID_SOCKET
;
156 /* close listening socket */
158 void close_socket (socket_t sock
)
160 int rc
= closesocket (sock
);
161 if (rc
== SOCKET_ERROR
) {
162 VERBOSE (ERROR
, fprintf (stderr
, "error: %d\n", ERRNO
));
166 /* receive data from socket */
168 int receive_data (socket_t sock
, unsigned char **pdata
)
170 unsigned char buffer
[BUFFER_SIZE
] = {0};
171 unsigned char *data
= NULL
;
176 /* timeout management */
178 struct timeval tv
= { 0, TIMEOUT
};
180 FD_SET (sock
, &rfds
);
182 int retval
= select (sock
+ 1, &rfds
, NULL
, NULL
, &tv
);
183 if (retval
!= 1) { /* 0 or SOCKET_ERROR */
187 /* read from socket */
188 int rc
= read (sock
, buffer
, BUFFER_SIZE
);
190 if (rc
== 0) { /* sock closed */
198 } else if ((rc
< 0) && (ERRNO
!= EAGAIN
)) { /* error */
207 data
= realloc (data
, len
+ rc
);
208 memcpy (data
+ len
, buffer
, rc
);
219 /* send data onto socket */
221 int send_data (socket_t sock
, unsigned char *data
, int len
)
225 while (index
< len
) {
226 int rc
= write (sock
, data
+ index
, len
- index
);
228 if (rc
== 0) { /* sock closed */
231 } else if ((rc
< 0) && (ERRNO
!= EAGAIN
)) { /* error */
242 /* vim: set ts=4 sw=4 et: */