Soft'N'Design Software
/
projects
/
webserver.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
best makefile
[webserver.git]
/
server.c
diff --git
a/server.c
b/server.c
index 1535ef39e5157480fdd73f5cf5beaa3fa1524d20..b2a564d9767a23cb73921fa3c51c204125c24ecb 100644
(file)
--- a/
server.c
+++ b/
server.c
@@
-1,8
+1,5
@@
-/* depend: */
-/* cflags: */
-/* linker: color.o debug.o */
-
#include <assert.h>
#include <assert.h>
+#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@
-15,28
+12,22
@@
#include <fcntl.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <fcntl.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
-//#include <sys/types.h>
-//#include <sys/socket.h>
#include <sys/select.h>
#endif
#include <sys/select.h>
#endif
+#include <unistd.h>
#include "debug.h"
#include "server.h"
#include "debug.h"
#include "server.h"
-/*
compat
*/
+/*
types
*/
#ifdef _WIN32 /* Windows */
#ifdef _WIN32 /* Windows */
-#define PF_INET AF_INET
-#define ERRNO (WSAGetLastError ())
+typedef SOCKET socket_t;
#else /* Posix */
#else /* Posix */
-#define closesocket close
-#define ERRNO errno
-#define SOCKET_ERROR -1
+typedef int socket_t;
#endif
#endif
-/* types */
-
/* constants */
#define BACKLOG 5
/* constants */
#define BACKLOG 5
@@
-45,8
+36,30
@@
/* macros */
/* macros */
+#ifdef _WIN32 /* Windows */
+#define PF_INET AF_INET
+#define ERRNO (WSAGetLastError ())
+#else /* Posix */
+#define closesocket close
+#define ERRNO errno
+#define SOCKET_ERROR -1
+#define INVALID_SOCKET -1
+#endif
+
/* gobal variables */
/* gobal variables */
+socket_t sock = INVALID_SOCKET;
+socket_t conn = INVALID_SOCKET;
+
+/* stop server */
+
+void stop_server (int sig)
+{
+ VERBOSE (INFO, PRINT ("Signal: %d\n", sig));
+ terminate_network_context ();
+ exit (0);
+}
+
/* init network context */
void init_network_context (void)
/* init network context */
void init_network_context (void)
@@
-54,15
+67,35
@@
void init_network_context (void)
#ifdef _WIN32 /* Windows */
WSADATA WSAData;
WSAStartup (MAKEWORD(2,0), &WSAData);
#ifdef _WIN32 /* Windows */
WSADATA WSAData;
WSAStartup (MAKEWORD(2,0), &WSAData);
+
assert (INVALID_SOCKET == (socket_t)-1);
assert (SOCKET_ERROR == -1);
#endif
assert (INVALID_SOCKET == (socket_t)-1);
assert (SOCKET_ERROR == -1);
#endif
+
+#ifndef _WIN32 /* Posix */
+ signal (SIGINT, &stop_server);
+#endif
+ signal (SIGABRT, &stop_server);
+ signal (SIGSEGV, &stop_server);
+ signal (SIGTERM, &stop_server);
}
/* terminate network context */
}
/* terminate network context */
+void _closesocket (socket_t sock)
+{
+ if (sock != INVALID_SOCKET) {
+ int rc = closesocket (sock);
+ if (rc == SOCKET_ERROR) {
+ VERBOSE (ERROR, PERROR ("error: close %d\n", ERRNO));
+ }
+ }
+}
+
void terminate_network_context (void)
{
void terminate_network_context (void)
{
+ _closesocket (sock);
+ _closesocket (conn);
#ifdef _WIN32 /* Windows */
WSACleanup ();
#endif
#ifdef _WIN32 /* Windows */
WSACleanup ();
#endif
@@
-70,132
+103,135
@@
void terminate_network_context (void)
/* open listening socket */
/* open listening socket */
-
socket_
t open_listening_socket (int port)
+
in
t open_listening_socket (int port)
{
{
- VERBOSE (DEBUG, fprintf (stdout, "Opening socket\n"));
- socket_t sock = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (sock == INVALID_SOCKET) {
- return -1;
+ int val = 1;
+
+ VERBOSE (DEBUG, PRINT ("Opening socket\n"));
+ //socket_t sock = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
+ _closesocket (sock);
+ socket_t _sock = socket (AF_INET, SOCK_STREAM, 0);
+ if (_sock == INVALID_SOCKET) {
+ return 0;
}
struct sockaddr_in addr = {0};
}
struct sockaddr_in addr = {0};
- addr.sin_family = PF_INET;
+ //bzero (&addr, sizeof (addr));
+ //addr.sin_family = PF_INET;
+ addr.sin_family = AF_INET;
addr.sin_port = htons (port);
addr.sin_addr.s_addr = htonl (INADDR_ANY);
addr.sin_port = htons (port);
addr.sin_addr.s_addr = htonl (INADDR_ANY);
- VERBOSE (DEBUG, fprintf (stdout, "Binding socket\n"));
- int rc = bind (sock, (struct sockaddr *)&addr, sizeof (addr));
+ VERBOSE (DEBUG, PRINT ("Binding socket\n"));
+ //setsockopt (_sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof (val));
+ int rc = bind (_sock, (struct sockaddr *)&addr, sizeof (addr));
if (rc == SOCKET_ERROR) {
if (rc == SOCKET_ERROR) {
- VERBOSE (ERROR, fprintf (stderr, "error: bind %d\n", ERRNO));
- rc = closesocket (sock);
- if (rc == SOCKET_ERROR) {
- VERBOSE (ERROR, fprintf (stderr, "error: close %d\n", ERRNO));
- }
- return -1;
+ VERBOSE (ERROR, PERROR ("error: bind %d\n", ERRNO));
+ _closesocket (_sock);
+ return 0;
}
}
- VERBOSE (DEBUG,
fprintf (stdout,
"Configuring socket\n"));
+ VERBOSE (DEBUG,
PRINT (
"Configuring socket\n"));
#ifndef _WIN32 /* Posix */
#ifndef _WIN32 /* Posix */
- fcntl (sock, F_SETFL, O_NONBLOCK);
+ fcntl (
_
sock, F_SETFL, O_NONBLOCK);
#endif
#endif
- int val = 1;
- rc = setsockopt (sock, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof (val));
+ rc = setsockopt (_sock, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof (val));
if (rc < 0) {
if (rc < 0) {
- VERBOSE (ERROR, fprintf (stderr, "error: %s\n", "setsockopt/TCP_NODELAY"));
- closesocket (sock);
- if (rc == SOCKET_ERROR) {
- VERBOSE (ERROR, fprintf (stderr, "error: close %d\n", ERRNO));
- }
- return -1;
+ VERBOSE (ERROR, PERROR ("error: %s\n", "setsockopt/TCP_NODELAY"));
+ _closesocket (_sock);
+ return 0;
}
}
- rc = listen (sock, BACKLOG);
+ rc = listen (
_
sock, BACKLOG);
if (rc < 0) {
if (rc < 0) {
- VERBOSE (ERROR, fprintf (stderr, "error: %s\n", "listen"));
- closesocket (sock);
- if (rc == SOCKET_ERROR) {
- VERBOSE (ERROR, fprintf (stderr, "error: close %d\n", ERRNO));
- }
- return -1;
+ VERBOSE (ERROR, PERROR ("error: %s\n", "listen"));
+ _closesocket (_sock);
+ return 0;
}
}
- return sock;
+ sock = _sock;
+
+ return 1;
}
/* accept incomming connection */
}
/* accept incomming connection */
-
socket_t accept_incoming_connection (socket_t sock
)
+
int accept_incoming_connection (void
)
{
{
- socket_t connection = accept (sock, NULL, 0);
- if (connection < 0) {
- return INVALID_SOCKET;
+ if (sock == INVALID_SOCKET) {
+ VERBOSE (ERROR, PERROR ("Can't accept connection from closed socket\n"));
+ return 0;
+ }
+
+ if (conn != INVALID_SOCKET) {
+ _closesocket (conn);
+ conn = INVALID_SOCKET;
+ }
+ socket_t _conn = accept (sock, NULL, 0);
+ if (_conn == INVALID_SOCKET) {
+ return 0;
}
#ifndef _WIN32 /* POSIX */
}
#ifndef _WIN32 /* POSIX */
- fcntl (
connectio
n, F_SETFL, O_NONBLOCK);
+ fcntl (
_con
n, F_SETFL, O_NONBLOCK);
#endif
int val = 1;
#endif
int val = 1;
- int rc = setsockopt (
connectio
n, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof (val));
+ int rc = setsockopt (
_con
n, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof (val));
if (rc < 0) {
if (rc < 0) {
- VERBOSE (ERROR, fprintf (stderr, "error: %s\n", "setsockopt/TCP_NODELAY"));
- closesocket (connection);
- if (rc == SOCKET_ERROR) {
- VERBOSE (ERROR, fprintf (stderr, "error: close %d\n", ERRNO));
- }
- return INVALID_SOCKET;
+ VERBOSE (ERROR, PERROR ("error: %s\n", "setsockopt/TCP_NODELAY"));
+ _closesocket (_conn);
+ return 0;
}
}
- return connection;
+ conn = _conn;
+
+ return 1;
}
}
-/* close
listening
socket */
+/* close
connection
socket */
-void close_
socket (socket_t sock
)
+void close_
connection (void
)
{
{
- int rc = closesocket (sock);
- if (rc == SOCKET_ERROR) {
- VERBOSE (ERROR, fprintf (stderr, "error: %d\n", ERRNO));
- }
+ _closesocket (conn);
+ conn = INVALID_SOCKET;
}
/* receive data from socket */
}
/* receive data from socket */
-int receive_data (
socket_t sock, unsigned
char **pdata)
+int receive_data (char **pdata)
{
{
-
unsigned
char buffer[BUFFER_SIZE] = {0};
-
unsigned
char *data = NULL;
+ char buffer[BUFFER_SIZE] = {0};
+ char *data = NULL;
int len = 0;
int len = 0;
+ if (conn == INVALID_SOCKET) {
+ VERBOSE (ERROR, PERROR ("Can't read data from closed socket\n"));
+ return 0;
+ }
+
while (1) {
/* timeout management */
fd_set rfds;
struct timeval tv = { 0, TIMEOUT };
FD_ZERO (&rfds);
while (1) {
/* timeout management */
fd_set rfds;
struct timeval tv = { 0, TIMEOUT };
FD_ZERO (&rfds);
- FD_SET (
sock
, &rfds);
+ FD_SET (
conn
, &rfds);
- int retval = select (
sock
+ 1, &rfds, NULL, NULL, &tv);
+ int retval = select (
conn
+ 1, &rfds, NULL, NULL, &tv);
if (retval != 1) { /* 0 or SOCKET_ERROR */
break;
}
/* read from socket */
if (retval != 1) { /* 0 or SOCKET_ERROR */
break;
}
/* read from socket */
- int rc = read (sock, buffer, BUFFER_SIZE);
-
- if (rc == 0) { /* sock closed */
- if (data) {
- free (data);
- data = NULL;
- len = 0;
- }
- break;
+ int rc = recv (conn, buffer, BUFFER_SIZE, 0);
+ VERBOSE (DEBUG, PRINT ("rc: %d\nerrno: %d\n", rc, ERRNO));
-
} else if ((rc < 0) && (ERRNO != EAGAIN)) { /*
error */
+
if (rc == 0) { /* sock closed or
error */
if (data) {
free (data);
data = NULL;
}
if (data) {
free (data);
data = NULL;
}
- len =
-1
;
+ len =
(rc < 0) ? -1 : 0
;
break;
} else if (rc > 0) {
break;
} else if (rc > 0) {
@@
-213,18
+249,20
@@
int receive_data (socket_t sock, unsigned char **pdata)
/* send data onto socket */
/* send data onto socket */
-int send_data (
socket_t sock, unsigned
char *data, int len)
+int send_data (char *data, int len)
{
int index = 0;
{
int index = 0;
+ if (conn == INVALID_SOCKET) {
+ VERBOSE (ERROR, PERROR ("Can't read data from closed socket\n"));
+ return 0;
+ }
+
while (index < len) {
while (index < len) {
- int rc =
write (sock, data + index, len - index
);
+ int rc =
send (conn, data + index, len - index, 0
);
- if (rc == 0) { /* sock closed */
- index = 0;
- break;
- } else if ((rc < 0) && (ERRNO != EAGAIN)) { /* error */
- index = -1;
+ if (rc <= 0) { /* sock closed or error */
+ index = (rc < 0) ? -1 : 0;
break;
} else if (rc > 0) {
index += rc;
break;
} else if (rc > 0) {
index += rc;