Browse Source
more work on socket handling stuff... @TODO think about renaming it to connection as it only handles TCP sockets
master
more work on socket handling stuff... @TODO think about renaming it to connection as it only handles TCP sockets
master
9 changed files with 181 additions and 96 deletions
-
3include/socket.h
-
2src/server.c
-
90src/socket.c
-
50src/socket_accept.c
-
31src/socket_connect.c
-
40src/socket_listen.c
-
14tests/Makefile.am
-
2tests/runtest.c
-
45tests/socketTest.c
@ -0,0 +1,50 @@ |
|||
#include <stdio.h> /* for printf() and fprintf() */ |
|||
#include <sys/types.h> /* SO_REUSEADDR */ |
|||
#include <sys/socket.h> /* for socket(), bind(), and connect() */ |
|||
#include <arpa/inet.h> /* for sockaddr_in and inet_ntoa() */ |
|||
#include <stdlib.h> /* for atoi() and exit() */ |
|||
#include <string.h> /* for memset() */ |
|||
#include <unistd.h> /* for close() */ |
|||
#include <errno.h> /* for errno */ |
|||
#include <stdarg.h> |
|||
|
|||
#include "logger.h" |
|||
#include "cclass.h" |
|||
#include "socket.h" |
|||
|
|||
SOCK |
|||
sock_accept(SOCK this, char remoteAddr[16]) |
|||
{ |
|||
SOCK sock; /* Socket for client */ |
|||
unsigned int len; /* Length of client address data structure */ |
|||
|
|||
/* Set the size of the in-out parameter */ |
|||
len = sizeof(this->addr); |
|||
|
|||
sock = new(SOCK, this->logger, this->port); |
|||
/** |
|||
* @TODO: change port to remote port on success |
|||
*/ |
|||
|
|||
/* Wait for a client to connect */ |
|||
sock->handle = accept(this->handle, (struct sockaddr *) &(sock->addr), &len); |
|||
if (-1 == sock->handle) { |
|||
logger_log(this->logger, LOGGER_WARNING, |
|||
"error acception connection: %s", strerror(errno)); |
|||
} else { |
|||
strncpy (remoteAddr, inet_ntoa((sock->addr).sin_addr), sizeof(remoteAddr)-1); |
|||
} |
|||
|
|||
/* clntSock is connected to a client! */ |
|||
/** |
|||
* @TODO add verbosity level to logger |
|||
*/ |
|||
// if (0 != this->logger->verbose) { |
|||
logger_log(this->logger, LOGGER_INFO, |
|||
"handling client %s\n", inet_ntoa((this->addr).sin_addr)); |
|||
// } |
|||
|
|||
return sock; |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,31 @@ |
|||
#include <stdio.h> /* for printf() and fprintf() */ |
|||
#include <sys/types.h> /* SO_REUSEADDR */ |
|||
#include <sys/socket.h> /* for socket(), bind(), and connect() */ |
|||
#include <arpa/inet.h> /* for sockaddr_in and inet_ntoa() */ |
|||
#include <stdlib.h> /* for atoi() and exit() */ |
|||
#include <string.h> /* for memset() */ |
|||
#include <unistd.h> /* for close() */ |
|||
#include <errno.h> /* for errno */ |
|||
#include <stdarg.h> |
|||
|
|||
#include "logger.h" |
|||
#include "cclass.h" |
|||
#include "socket.h" |
|||
|
|||
|
|||
void |
|||
sock_connect(SOCK this, const char * addr) |
|||
{ |
|||
inet_pton(AF_INET, addr, &((this->addr).sin_addr)); |
|||
(this->addr).sin_family = AF_INET; /* Internet address family */ |
|||
(this->addr).sin_port = htons(this->port); /* Local port */ |
|||
|
|||
if (-1 == connect(this->handle, (struct sockaddr*) &(this->addr), sizeof(this->addr))) { |
|||
logger_log(this->logger, LOGGER_CRIT, |
|||
"error connection socket: %s - service terminated", |
|||
strerror(errno)); |
|||
exit(EXIT_FAILURE); |
|||
} |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,40 @@ |
|||
#include <stdio.h> /* for printf() and fprintf() */ |
|||
#include <sys/types.h> /* SO_REUSEADDR */ |
|||
#include <sys/socket.h> /* for socket(), bind(), and connect() */ |
|||
#include <arpa/inet.h> /* for sockaddr_in and inet_ntoa() */ |
|||
#include <stdlib.h> /* for atoi() and exit() */ |
|||
#include <string.h> /* for memset() */ |
|||
#include <unistd.h> /* for close() */ |
|||
#include <errno.h> /* for errno */ |
|||
#include <stdarg.h> |
|||
|
|||
#include "logger.h" |
|||
#include "cclass.h" |
|||
#include "socket.h" |
|||
|
|||
|
|||
void |
|||
sock_listen(SOCK this, int backlog) |
|||
{ |
|||
(this->addr).sin_family = AF_INET; /* Internet address family */ |
|||
(this->addr).sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */ |
|||
(this->addr).sin_port = htons(this->port); /* Local port */ |
|||
|
|||
/* Bind to the local address */ |
|||
if (-1 == bind(this->handle, (struct sockaddr *) &(this->addr), sizeof(this->addr))) { |
|||
logger_log(this->logger, LOGGER_CRIT, |
|||
"error binding socket: %s - service terminated", |
|||
strerror(errno)); |
|||
exit(EXIT_FAILURE); |
|||
} |
|||
|
|||
/* Mark the socket so it will listen for incoming connections */ |
|||
if (-1 == listen(this->handle, backlog)) { |
|||
logger_log(this->logger, LOGGER_CRIT, |
|||
"error binding socket: %s - service terminated", |
|||
strerror(errno)); |
|||
exit(EXIT_FAILURE); |
|||
} |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue