Skip to content
Browse files

Added tests to check onion_request_get_sockaddr_atorage works properl…

…y. Now requests can get all available info from client.
  • Loading branch information...
1 parent 949146b commit 9fde43f3f5c6732d0ebed1621d1536bc69f3d4f0 @davidmoreno committed May 4, 2012
Showing with 80 additions and 19 deletions.
  1. +0 −16 examples/hello/hello.c
  2. +3 −2 src/onion/onion.c
  3. +3 −1 src/onion/request.c
  4. +74 −0 tests/01-internal/02-request.c
View
16 examples/hello/hello.c
@@ -11,22 +11,6 @@ int hello(void *p, onion_request *req, onion_response *res){
onion_response_printf(res, "<p>Path: %s", onion_request_get_query(req, "1"));
}
onion_response_printf(res,"<p>Client description: %s",onion_request_get_client_description(req));
-
- // Now using sockaddr_storage
- struct sockaddr_storage *client_addr;
- socklen_t client_len;
- client_addr=onion_request_get_sockadd_storage(req, &client_len);
-
- char tmp[256];
- getnameinfo((struct sockaddr *)client_addr, client_len, tmp, sizeof(tmp),
- NULL, 0, NI_NUMERICHOST);
-
- onion_response_printf(res,"<p>Client description bis: %s", tmp);
- tmp[0]=0;
- getnameinfo((struct sockaddr *)client_addr, client_len, tmp, sizeof(tmp),
- NULL, 0, NI_NAMEREQD);
- onion_response_printf(res,"<p>Client name: %s", tmp);
-
return OCS_PROCESSED;
}
View
5 src/onion/onion.c
@@ -446,7 +446,6 @@ int onion_listen(onion *o){
}
}
#endif
- char address[64];
if (sockfd==0){
struct addrinfo hints;
struct addrinfo *result, *rp;
@@ -491,11 +490,13 @@ int onion_listen(onion *o){
return errno;
}
+#ifdef __DEBUG__
+ char address[64];
getnameinfo(rp->ai_addr, rp->ai_addrlen, address, 32,
&address[32], 32, NI_NUMERICHOST | NI_NUMERICSERV);
ONION_DEBUG("Listening to %s:%s",address,&address[32]);
+#endif
freeaddrinfo(result);
-
listen(sockfd,5); // queue of only 5.
}
o->listenfd=sockfd;
View
4 src/onion/request.c
@@ -92,7 +92,7 @@ onion_request *onion_request_new(onion_server *server, void *socket, const char
onion_request* onion_request_new_from_socket(onion_server* server, void* socket, struct sockaddr_storage* cli_addr, socklen_t cli_len){
onion_request *req=onion_request_new(server, socket, NULL);
if (cli_addr){
- req->client_addr=*cli_addr;
+ memcpy(&req->client_addr,cli_addr, cli_len);
}
req->client_len=cli_len;
return req;
@@ -543,6 +543,8 @@ const char *onion_request_get_client_description(onion_request *req){
struct sockaddr_storage *onion_request_get_sockadd_storage(onion_request *req, socklen_t *client_len){
if (client_len)
*client_len=req->client_len;
+ if (req->client_len==0)
+ return NULL;
return &req->client_addr;
}
View
74 tests/01-internal/02-request.c
@@ -18,6 +18,7 @@
#include <malloc.h>
#include <string.h>
+#include <netdb.h>
#include <onion/dict.h>
#include <onion/server.h>
@@ -332,6 +333,78 @@ void t07_multiline_headers(){
END_LOCAL();
}
+void t08_sockaddr_storage(){
+ INIT_LOCAL();
+ onion_request *req;
+
+
+ {
+ struct sockaddr_storage client_addr;
+ socklen_t client_len=0;
+
+ req=onion_request_new_from_socket(NULL, NULL, &client_addr, client_len);
+ FAIL_IF_EQUAL(onion_request_get_sockadd_storage(req, NULL), &client_addr);
+ FAIL_IF_EQUAL(onion_request_get_sockadd_storage(req, &client_len), &client_addr);
+ FAIL_IF_NOT_EQUAL_INT(client_len, 0);
+ onion_request_free(req);
+ }
+
+ {
+ struct sockaddr_storage *client_addr;
+ socklen_t client_len=0;
+ struct addrinfo hints;
+ struct addrinfo *result, *rp;
+ char hostA[128], portA[16];
+ char hostB[128], portB[16];
+
+ memset(&hints,0, sizeof(struct addrinfo));
+ hints.ai_canonname=NULL;
+ hints.ai_addr=NULL;
+ hints.ai_next=NULL;
+ hints.ai_socktype=SOCK_STREAM;
+ hints.ai_family=AF_UNSPEC;
+ hints.ai_flags=AI_PASSIVE|AI_NUMERICSERV;
+
+ int err=getaddrinfo("localhost","8080", &hints, &result);
+ FAIL_IF_NOT_EQUAL_INT(err,0);
+ if (err!=0)
+ goto exit;
+ for(rp=result;rp!=NULL;rp=rp->ai_next){
+ memset(hostA,0,sizeof(hostA));
+ memset(hostB,0,sizeof(hostB));
+ memset(portA,0,sizeof(portA));
+ memset(portB,0,sizeof(portB));
+
+ getnameinfo(rp->ai_addr, rp->ai_addrlen, hostA, sizeof(hostA), portA, sizeof(portA), NI_NUMERICHOST | NI_NUMERICSERV);
+ req=onion_request_new_from_socket(NULL, NULL,(struct sockaddr_storage *)rp->ai_addr, rp->ai_addrlen);
+ client_addr=onion_request_get_sockadd_storage(req, &client_len);
+ FAIL_IF_EQUAL(client_addr, (struct sockaddr_storage *)rp->ai_addr);
+ FAIL_IF_EQUAL(client_addr, NULL);
+
+ getnameinfo((struct sockaddr *)client_addr, client_len, hostB, sizeof(hostB), portB, sizeof(portB), NI_NUMERICHOST | NI_NUMERICSERV);
+
+ FAIL_IF_NOT_EQUAL_STR(hostA, hostB);
+ FAIL_IF_NOT_EQUAL_STR(portA, portB);
+ FAIL_IF_NOT_EQUAL_STR(hostA, onion_request_get_client_description(req));
+ onion_request_free(req);
+ }
+ freeaddrinfo(result);
+
+ }
+
+ {
+ req=onion_request_new(NULL, NULL, NULL);
+ struct sockaddr_storage *client_addr;
+ socklen_t client_len;
+ client_addr=onion_request_get_sockadd_storage(req, &client_len);
+ FAIL_IF_NOT_EQUAL(client_addr, NULL);
+ onion_request_free(req);
+ }
+
+exit:
+ END_LOCAL();
+}
+
int main(int argc, char **argv){
START();
@@ -343,6 +416,7 @@ int main(int argc, char **argv){
t05_create_add_free_POST();
t06_create_add_free_POST_toobig();
t07_multiline_headers();
+ t08_sockaddr_storage();
teardown();
END();
}

0 comments on commit 9fde43f

Please sign in to comment.
Something went wrong with that request. Please try again.