mirror of
https://github.com/sigmasternchen/CFloor
synced 2025-03-15 20:28:56 +00:00
get peer addr and name
This commit is contained in:
parent
4162b78397
commit
7fc0da16f4
3 changed files with 51 additions and 4 deletions
|
@ -5,6 +5,8 @@
|
||||||
|
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
enum method {
|
enum method {
|
||||||
GET, POST, PUT
|
GET, POST, PUT
|
||||||
};
|
};
|
||||||
|
@ -46,8 +48,15 @@ struct bind {
|
||||||
struct bind_private _private;
|
struct bind_private _private;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct peer {
|
||||||
|
// INET6_ADDRSTRLEN should be enough
|
||||||
|
char addr[INET6_ADDRSTRLEN + 1];
|
||||||
|
char* name;
|
||||||
|
};
|
||||||
|
|
||||||
struct request {
|
struct request {
|
||||||
struct metaData metaData;
|
struct metaData metaData;
|
||||||
|
struct peer peer;
|
||||||
struct headers* headers;
|
struct headers* headers;
|
||||||
struct bind bind;
|
struct bind bind;
|
||||||
int fd;
|
int fd;
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
#include "networking.h"
|
#include "networking.h"
|
||||||
#include "linked.h"
|
#include "linked.h"
|
||||||
|
@ -101,6 +102,9 @@ void cleanup() {
|
||||||
|
|
||||||
close(connection->fd);
|
close(connection->fd);
|
||||||
|
|
||||||
|
if (connection->peer.name != NULL)
|
||||||
|
free(connection->peer.name);
|
||||||
|
|
||||||
free(connection);
|
free(connection);
|
||||||
|
|
||||||
linked_unlink(link);
|
linked_unlink(link);
|
||||||
|
@ -226,6 +230,7 @@ void* responseThread(void* data) {
|
||||||
.metaData = connection->metaData,
|
.metaData = connection->metaData,
|
||||||
.headers = &(connection->headers),
|
.headers = &(connection->headers),
|
||||||
.fd = connection->threads.requestFd,
|
.fd = connection->threads.requestFd,
|
||||||
|
.peer = connection->peer,
|
||||||
.userData = connection->threads.handler.data,
|
.userData = connection->threads.handler.data,
|
||||||
._private = connection
|
._private = connection
|
||||||
}, (struct response) {
|
}, (struct response) {
|
||||||
|
@ -552,8 +557,8 @@ void* listenThread(void* _bind) {
|
||||||
freeaddrinfo(result);
|
freeaddrinfo(result);
|
||||||
|
|
||||||
while(true) {
|
while(true) {
|
||||||
struct sockaddr_in client;
|
struct sockaddr_storage client;
|
||||||
socklen_t clientSize = sizeof (struct sockaddr_in);
|
socklen_t clientSize = sizeof (client);
|
||||||
|
|
||||||
tmp = accept(bindObj->_private.socketFd, (struct sockaddr *) &client, &clientSize);
|
tmp = accept(bindObj->_private.socketFd, (struct sockaddr *) &client, &clientSize);
|
||||||
if (tmp < 0) {
|
if (tmp < 0) {
|
||||||
|
@ -588,8 +593,41 @@ void* listenThread(void* _bind) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
struct peer peer;
|
||||||
|
int family = client.ss_family;
|
||||||
|
void* addrPtr;
|
||||||
|
|
||||||
|
if (family == AF_INET)
|
||||||
|
addrPtr = &(((struct sockaddr_in*) &(client))->sin_addr);
|
||||||
|
else if (family == AF_INET6)
|
||||||
|
addrPtr = &(((struct sockaddr_in*) &(client))->sin_addr);
|
||||||
|
|
||||||
|
if (inet_ntop(family, addrPtr, &(peer.addr[0]), INET6_ADDRSTRLEN + 1) == NULL) {
|
||||||
|
error("networking: Couldn't set peer addr string: %s", strerror(errno));
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct hostent entry;
|
||||||
|
struct hostent* result;
|
||||||
|
int h_errno;
|
||||||
|
|
||||||
|
#define LOCAL_BUFFER_SIZE (128)
|
||||||
|
char buffer[LOCAL_BUFFER_SIZE];
|
||||||
|
|
||||||
|
gethostbyaddr_r(&(client), sizeof(client), family, &entry, &(buffer[0]), LOCAL_BUFFER_SIZE, &result, &h_errno);
|
||||||
|
if (result == NULL) {
|
||||||
|
peer.name = strclone("");
|
||||||
|
} else {
|
||||||
|
peer.name = strclone(entry.h_name);
|
||||||
|
if (peer.name == NULL) {
|
||||||
|
error("networking: Couldn't strclone hostname: %s", strerror(errno));
|
||||||
|
peer.name = strclone("");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
connection->state = OPENED;
|
connection->state = OPENED;
|
||||||
connection->client = client;
|
connection->peer = peer;
|
||||||
connection->bind = bindObj;
|
connection->bind = bindObj;
|
||||||
connection->fd = tmp;
|
connection->fd = tmp;
|
||||||
connection->metaData = (struct metaData) {
|
connection->metaData = (struct metaData) {
|
||||||
|
|
|
@ -41,7 +41,7 @@ struct threads {
|
||||||
|
|
||||||
struct connection {
|
struct connection {
|
||||||
enum connectionState state;
|
enum connectionState state;
|
||||||
struct sockaddr_in client;
|
struct peer peer;
|
||||||
struct bind* bind;
|
struct bind* bind;
|
||||||
volatile sig_atomic_t inUse;
|
volatile sig_atomic_t inUse;
|
||||||
int fd;
|
int fd;
|
||||||
|
|
Loading…
Reference in a new issue