telize

High performance JSON IP and GeoIP REST API (IP Geolocation)
Log | Files | Refs | README | LICENSE

commit 2bd72dfe1fbd08d31f124e3f92f1a3771a2b1da6
parent 400cf28dd0b51a5e6b93c13a2bd28108c56d8a0a
Author: Frederic Cambus <fred@statdns.com>
Date:   Thu,  4 Oct 2018 22:55:20 +0200

Return the client IP address in the 'ip' endpoint

Diffstat:
Msrc/ip.c | 27++++++++++++++++++++++++++-
1 file changed, 26 insertions(+), 1 deletion(-)

diff --git a/src/ip.c b/src/ip.c @@ -1,3 +1,19 @@ +/*****************************************************************************/ +/* */ +/* Telize 2.0.0 */ +/* Copyright (c) 2013-2018, Frederic Cambus */ +/* https://www.telize.com */ +/* */ +/* Created: 2013-08-15 */ +/* Last Updated: 2018-10-04 */ +/* */ +/* Telize is released under the BSD 2-Clause license. */ +/* See LICENSE file for details. */ +/* */ +/*****************************************************************************/ + +#include <sys/socket.h> + #include <kore/kore.h> #include <kore/http.h> @@ -6,6 +22,15 @@ int ip(struct http_request *); int ip(struct http_request *req) { - http_response(req, 200, NULL, 0); + char addr[INET6_ADDRSTRLEN]; + + if (req->owner->addrtype == AF_INET) { + inet_ntop(req->owner->addrtype, &(req->owner->addr.ipv4.sin_addr), addr, sizeof(addr)); + } else { + inet_ntop(req->owner->addrtype, &(req->owner->addr.ipv6.sin6_addr), addr, sizeof(addr)); + } + + http_response(req, 200, addr, strlen(addr)); + return (KORE_RESULT_OK); }