Commit c6635e3904476c1ccd7b2e045840a73791a97d6b

Authored by Georg Hopp
1 parent d2848c0b

more work on socket handling stuff... @TODO think about renaming it to connectio…

…n as it only handles TCP sockets
@@ -14,7 +14,8 @@ CLASS(SOCK) { @@ -14,7 +14,8 @@ CLASS(SOCK) {
14 int handle; /* socket handle for server */ 14 int handle; /* socket handle for server */
15 }; 15 };
16 16
17 -void sock_initServer(SOCK this, int backlog); 17 +void sock_connect(SOCK this, const char * addr);
  18 +void sock_listen(SOCK this, int backlog);
18 SOCK sock_accept(SOCK this, char remoteAddr[16]); 19 SOCK sock_accept(SOCK this, char remoteAddr[16]);
19 20
20 #endif /* __SOCKET_H__ */ 21 #endif /* __SOCKET_H__ */
@@ -23,7 +23,7 @@ __construct(SERVER) @@ -23,7 +23,7 @@ __construct(SERVER)
23 FD_ZERO(&(this->fdset)); 23 FD_ZERO(&(this->fdset));
24 24
25 this->sock = new(SOCK, port); 25 this->sock = new(SOCK, port);
26 - sock_initServer(this->sock, backlog); 26 + sock_listen(this->sock, backlog);
27 27
28 this->max_fd = this->sock->handle; 28 this->max_fd = this->sock->handle;
29 FD_SET(this->sock->handle, &(this->fdset)); 29 FD_SET(this->sock->handle, &(this->fdset));
@@ -17,28 +17,10 @@ INIT_CLASS(SOCK); @@ -17,28 +17,10 @@ INIT_CLASS(SOCK);
17 17
18 __construct(SOCK) 18 __construct(SOCK)
19 { 19 {
20 - this->logger = va_arg(* params, struct _logger *);  
21 - this->port = va_arg(* params, int);  
22 -}  
23 -  
24 -__destruct(SOCK)  
25 -{  
26 - if (0 != this->handle) {  
27 - shutdown(this->handle, SHUT_RDWR);  
28 - close(this->handle);  
29 - }  
30 -}  
31 -  
32 -__jsonConst(SOCK) {}  
33 -__toJson(SOCK) {}  
34 -__clear(SOCK) {} 20 + int reUse = 1; /* TODO: make this configurable */
35 21
36 -  
37 -void  
38 -sock_initServer(SOCK this, int backlog)  
39 -{  
40 - struct sockaddr_in addr; /* Local address */  
41 - int reUse = 1; /* TODO: make this configurable */ 22 + this->logger = va_arg(* params, LOGGER);
  23 + this->port = va_arg(* params, int);
42 24
43 /* Create socket for incoming connections */ 25 /* Create socket for incoming connections */
44 if (-1 == (this->handle = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))) { 26 if (-1 == (this->handle = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))) {
@@ -48,66 +30,20 @@ sock_initServer(SOCK this, int backlog) @@ -48,66 +30,20 @@ sock_initServer(SOCK this, int backlog)
48 exit(EXIT_FAILURE); 30 exit(EXIT_FAILURE);
49 } 31 }
50 32
51 - /* Make the socket REUSE a TIME_WAT socket */ 33 + /* Make the socket REUSE a TIME_WAIT socket */
52 setsockopt(this->handle, SOL_SOCKET, SO_REUSEADDR, &reUse, sizeof (reUse)); 34 setsockopt(this->handle, SOL_SOCKET, SO_REUSEADDR, &reUse, sizeof (reUse));
53 -  
54 - /* Construct local address structure */  
55 - memset(&addr, 0, sizeof(addr)); /* Zero out structure */  
56 -  
57 - addr.sin_family = AF_INET; /* Internet address family */  
58 - addr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */  
59 - addr.sin_port = htons(this->port); /* Local port */  
60 -  
61 - /* Bind to the local address */  
62 - if (-1 == bind(this->handle, (struct sockaddr *) &addr, sizeof(addr))) {  
63 - logger_log(this->logger, LOGGER_CRIT,  
64 - "error binding socket: %s - service terminated",  
65 - strerror(errno));  
66 - exit(EXIT_FAILURE);  
67 - }  
68 -  
69 - /* Mark the socket so it will listen for incoming connections */  
70 - if (-1 == listen(this->handle, backlog)) {  
71 - logger_log(this->logger, LOGGER_CRIT,  
72 - "error binding socket: %s - service terminated",  
73 - strerror(errno));  
74 - exit(EXIT_FAILURE);  
75 - }  
76 } 35 }
77 36
78 -SOCK  
79 -sock_accept(SOCK this, char remoteAddr[16]) 37 +__destruct(SOCK)
80 { 38 {
81 - SOCK sock; /* Socket for client */  
82 - struct sockaddr_in addr; /* Client address */  
83 - unsigned int len; /* Length of client address data structure */  
84 -  
85 - /* Set the size of the in-out parameter */  
86 - len = sizeof(addr);  
87 -  
88 - sock = new(SOCK, this->logger, this->port);  
89 - /**  
90 - * @TODO: change port to remote port on success  
91 - */  
92 -  
93 - /* Wait for a client to connect */  
94 - if (-1 == (sock->handle = accept(this->handle, (struct sockaddr *) &addr, &len))) {  
95 - logger_log(this->logger, LOGGER_WARNING,  
96 - "error acception connection: %s", strerror(errno));  
97 - } else {  
98 - strncpy (remoteAddr, inet_ntoa(addr.sin_addr), sizeof(remoteAddr)-1);  
99 - }  
100 -  
101 - /* clntSock is connected to a client! */  
102 - /**  
103 - * @TODO add verbosity level to logger  
104 - */  
105 -// if (0 != this->logger->verbose) {  
106 - logger_log(this->logger, LOGGER_INFO,  
107 - "handling client %s\n", inet_ntoa(addr.sin_addr));  
108 -// }  
109 -  
110 - return sock; 39 + if (0 != this->handle) {
  40 + shutdown(this->handle, SHUT_RDWR);
  41 + close(this->handle);
  42 + }
111 } 43 }
112 44
  45 +__jsonConst(SOCK) {}
  46 +__toJson(SOCK) {}
  47 +__clear(SOCK) {}
  48 +
113 // vim: set ts=4 sw=4: 49 // vim: set ts=4 sw=4:
  1 +#include <stdio.h> /* for printf() and fprintf() */
  2 +#include <sys/types.h> /* SO_REUSEADDR */
  3 +#include <sys/socket.h> /* for socket(), bind(), and connect() */
  4 +#include <arpa/inet.h> /* for sockaddr_in and inet_ntoa() */
  5 +#include <stdlib.h> /* for atoi() and exit() */
  6 +#include <string.h> /* for memset() */
  7 +#include <unistd.h> /* for close() */
  8 +#include <errno.h> /* for errno */
  9 +#include <stdarg.h>
  10 +
  11 +#include "logger.h"
  12 +#include "cclass.h"
  13 +#include "socket.h"
  14 +
  15 +SOCK
  16 +sock_accept(SOCK this, char remoteAddr[16])
  17 +{
  18 + SOCK sock; /* Socket for client */
  19 + unsigned int len; /* Length of client address data structure */
  20 +
  21 + /* Set the size of the in-out parameter */
  22 + len = sizeof(this->addr);
  23 +
  24 + sock = new(SOCK, this->logger, this->port);
  25 + /**
  26 + * @TODO: change port to remote port on success
  27 + */
  28 +
  29 + /* Wait for a client to connect */
  30 + sock->handle = accept(this->handle, (struct sockaddr *) &(sock->addr), &len);
  31 + if (-1 == sock->handle) {
  32 + logger_log(this->logger, LOGGER_WARNING,
  33 + "error acception connection: %s", strerror(errno));
  34 + } else {
  35 + strncpy (remoteAddr, inet_ntoa((sock->addr).sin_addr), sizeof(remoteAddr)-1);
  36 + }
  37 +
  38 + /* clntSock is connected to a client! */
  39 + /**
  40 + * @TODO add verbosity level to logger
  41 + */
  42 +// if (0 != this->logger->verbose) {
  43 + logger_log(this->logger, LOGGER_INFO,
  44 + "handling client %s\n", inet_ntoa((this->addr).sin_addr));
  45 +// }
  46 +
  47 + return sock;
  48 +}
  49 +
  50 +// vim: set ts=4 sw=4:
  1 +#include <stdio.h> /* for printf() and fprintf() */
  2 +#include <sys/types.h> /* SO_REUSEADDR */
  3 +#include <sys/socket.h> /* for socket(), bind(), and connect() */
  4 +#include <arpa/inet.h> /* for sockaddr_in and inet_ntoa() */
  5 +#include <stdlib.h> /* for atoi() and exit() */
  6 +#include <string.h> /* for memset() */
  7 +#include <unistd.h> /* for close() */
  8 +#include <errno.h> /* for errno */
  9 +#include <stdarg.h>
  10 +
  11 +#include "logger.h"
  12 +#include "cclass.h"
  13 +#include "socket.h"
  14 +
  15 +
  16 +void
  17 +sock_connect(SOCK this, const char * addr)
  18 +{
  19 + inet_pton(AF_INET, addr, &((this->addr).sin_addr));
  20 + (this->addr).sin_family = AF_INET; /* Internet address family */
  21 + (this->addr).sin_port = htons(this->port); /* Local port */
  22 +
  23 + if (-1 == connect(this->handle, (struct sockaddr*) &(this->addr), sizeof(this->addr))) {
  24 + logger_log(this->logger, LOGGER_CRIT,
  25 + "error connection socket: %s - service terminated",
  26 + strerror(errno));
  27 + exit(EXIT_FAILURE);
  28 + }
  29 +}
  30 +
  31 +// vim: set ts=4 sw=4:
  1 +#include <stdio.h> /* for printf() and fprintf() */
  2 +#include <sys/types.h> /* SO_REUSEADDR */
  3 +#include <sys/socket.h> /* for socket(), bind(), and connect() */
  4 +#include <arpa/inet.h> /* for sockaddr_in and inet_ntoa() */
  5 +#include <stdlib.h> /* for atoi() and exit() */
  6 +#include <string.h> /* for memset() */
  7 +#include <unistd.h> /* for close() */
  8 +#include <errno.h> /* for errno */
  9 +#include <stdarg.h>
  10 +
  11 +#include "logger.h"
  12 +#include "cclass.h"
  13 +#include "socket.h"
  14 +
  15 +
  16 +void
  17 +sock_listen(SOCK this, int backlog)
  18 +{
  19 + (this->addr).sin_family = AF_INET; /* Internet address family */
  20 + (this->addr).sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
  21 + (this->addr).sin_port = htons(this->port); /* Local port */
  22 +
  23 + /* Bind to the local address */
  24 + if (-1 == bind(this->handle, (struct sockaddr *) &(this->addr), sizeof(this->addr))) {
  25 + logger_log(this->logger, LOGGER_CRIT,
  26 + "error binding socket: %s - service terminated",
  27 + strerror(errno));
  28 + exit(EXIT_FAILURE);
  29 + }
  30 +
  31 + /* Mark the socket so it will listen for incoming connections */
  32 + if (-1 == listen(this->handle, backlog)) {
  33 + logger_log(this->logger, LOGGER_CRIT,
  34 + "error binding socket: %s - service terminated",
  35 + strerror(errno));
  36 + exit(EXIT_FAILURE);
  37 + }
  38 +}
  39 +
  40 +// vim: set ts=4 sw=4:
@@ -4,20 +4,18 @@ TESTS_ENVIRONMENT = valgrind --error-exitcode=123 --leak-check=full --quiet @@ -4,20 +4,18 @@ TESTS_ENVIRONMENT = valgrind --error-exitcode=123 --leak-check=full --quiet
4 TESTS = cclassTest loggerTest socketTest serverTest 4 TESTS = cclassTest loggerTest socketTest serverTest
5 check_PROGRAMS = cclassTest loggerTest socketTest serverTest 5 check_PROGRAMS = cclassTest loggerTest socketTest serverTest
6 6
7 -cclassTest_SOURCES = runtest.c cclassTest.c mock/class.c ../src/cclass.c  
8 -cclassTest_LDADD = $(LIBOBJS) 7 +SOURCES = runtest.c ../src/cclass.c
  8 +
  9 +cclassTest_SOURCES = $(SOURCES) cclassTest.c mock/class.c
9 cclassTest_CFLAGS = -Wall -ggdb -O0 -finline-functions -I ../include -I .. -I . 10 cclassTest_CFLAGS = -Wall -ggdb -O0 -finline-functions -I ../include -I .. -I .
10 11
11 -loggerTest_SOURCES = runtest.c loggerTest.c ../src/cclass.c ../src/logger.c  
12 -loggerTest_LDADD = $(LIBOBJS) 12 +loggerTest_SOURCES = $(SOURCES) loggerTest.c ../src/logger.c
13 loggerTest_CFLAGS = -Wall -ggdb -O0 -I ../include -I .. -I . 13 loggerTest_CFLAGS = -Wall -ggdb -O0 -I ../include -I .. -I .
14 14
15 -socketTest_SOURCES = runtest.c socketTest.c ../src/cclass.c ../src/logger.c ../src/socket.c  
16 -socketTest_LDADD = $(LIBOBJS) 15 +socketTest_SOURCES = $(SOURCES) socketTest.c ../src/logger.c ../src/socket.c ../src/socket_listen.c ../src/socket_accept.c ../src/socket_connect.c
17 socketTest_CFLAGS = -Wall -ggdb -O0 -I ../include -I .. -I . 16 socketTest_CFLAGS = -Wall -ggdb -O0 -I ../include -I .. -I .
18 17
19 -serverTest_SOURCES = runtest.c serverTest.c ../src/cclass.c ../src/logger.c ../src/socket.c ../src/server.c  
20 -serverTest_LDADD = $(LIBOBJS) 18 +serverTest_SOURCES = $(SOURCES) serverTest.c ../src/logger.c ../src/socket.c ../src/socket_listen.c ../src/socket_accept.c ../src/server.c
21 serverTest_CFLAGS = -Wall -ggdb -O0 -I ../include -I .. -I . 19 serverTest_CFLAGS = -Wall -ggdb -O0 -I ../include -I .. -I .
22 20
23 EXTRA_DIST = runtest.h mock/class.h 21 EXTRA_DIST = runtest.h mock/class.h
@@ -67,7 +67,7 @@ main(int argc, char * argv[]) @@ -67,7 +67,7 @@ main(int argc, char * argv[])
67 { 67 {
68 size_t errors = 0; 68 size_t errors = 0;
69 size_t failures = 0; 69 size_t failures = 0;
70 - size_t assertions = 0; 70 + // size_t assertions = 0; // @TODO find a way to count assertions
71 71
72 size_t index; 72 size_t index;
73 73
1 #include <stdio.h> 1 #include <stdio.h>
2 #include <stdlib.h> 2 #include <stdlib.h>
  3 +#include <unistd.h>
3 4
4 #include "runtest.h" 5 #include "runtest.h"
5 #include "logger.h" 6 #include "logger.h"
@@ -37,6 +38,7 @@ __setUp() @@ -37,6 +38,7 @@ __setUp()
37 ASSERT_INSTANCE_OF(SOCK, sock); 38 ASSERT_INSTANCE_OF(SOCK, sock);
38 ASSERT_INSTANCE_OF(LOGGER, sock->logger); 39 ASSERT_INSTANCE_OF(LOGGER, sock->logger);
39 ASSERT_EQUAL(TEST_PORT, sock->port); 40 ASSERT_EQUAL(TEST_PORT, sock->port);
  41 + ASSERT_NOT_EQUAL(0, sock->handle);
40 42
41 return TEST_OK; 43 return TEST_OK;
42 } 44 }
@@ -69,11 +71,9 @@ int (* const tearDown)() = __tearDown; @@ -69,11 +71,9 @@ int (* const tearDown)() = __tearDown;
69 71
70 static 72 static
71 int 73 int
72 -testInitServer() 74 +testListen()
73 { 75 {
74 - sock_initServer(sock, 10);  
75 -  
76 - ASSERT_NOT_NULL(sock->handle); 76 + sock_listen(sock, 10);
77 77
78 return TEST_OK; 78 return TEST_OK;
79 } 79 }
@@ -82,13 +82,42 @@ static @@ -82,13 +82,42 @@ static
82 int 82 int
83 testAccept() 83 testAccept()
84 { 84 {
85 - /*  
86 - * @TODO ...  
87 - */ 85 + SOCK acc, con;
  86 + char addr[16];
  87 + pid_t pid;
  88 +
  89 + sock_listen(sock, 10);
  90 +
  91 + pid = fork();
  92 +
  93 + switch(pid) {
  94 + case 0:
  95 + con = new(SOCK, logger, TEST_PORT);
  96 + sleep(1);
  97 + sock_connect(con, "127.0.0.1");
  98 + delete(&con);
  99 + __tearDown();
  100 + exit(EXIT_SUCCESS);
  101 + break;
  102 +
  103 + case -1:
  104 + return TEST_FAILED;
  105 +
  106 + default:
  107 + acc = sock_accept(sock, addr);
  108 + }
  109 +
  110 + if (NULL != acc) {
  111 + ASSERT_OBJECT(acc);
  112 + delete(&acc);
  113 + }
  114 +
  115 + return TEST_OK;
88 } 116 }
89 117
90 const testfunc tests[] = { 118 const testfunc tests[] = {
91 - testInitServer 119 + testListen,
  120 + testAccept
92 }; 121 };
93 const size_t count = FUNCS_COUNT(tests); 122 const size_t count = FUNCS_COUNT(tests);
94 123
Please register or login to post a comment