Browse Source

add support for querying local/remote address

Signed-off-by: Felix Fietkau <nbd@openwrt.org>
Felix Fietkau 10 years ago
parent
commit
cb7867a867
4 changed files with 59 additions and 1 deletions
  1. 12 1
      uclient-example.c
  2. 8 0
      uclient-http.c
  3. 27 0
      uclient.c
  4. 12 0
      uclient.h

+ 12 - 1
uclient-example.c

@@ -15,17 +15,28 @@
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
-#include <libubox/blobmsg.h>
+
 #include <unistd.h>
 #include <stdio.h>
 
+#include <libubox/blobmsg.h>
+
 #include "uclient.h"
 
+
 static void example_header_done(struct uclient *cl)
 {
 	struct blob_attr *cur;
+	char local[INET6_ADDRSTRLEN], remote[INET6_ADDRSTRLEN];
+	int local_port, remote_port;
 	int rem;
 
+	uclient_get_addr(local, &local_port, &cl->local_addr);
+	uclient_get_addr(remote, &remote_port, &cl->remote_addr);
+
+	fprintf(stderr, "Connected: %s:%d -> %s:%d\n",
+		local, local_port, remote, remote_port);
+
 	printf("Headers (%d): \n", cl->status_code);
 	blobmsg_for_each_attr(cur, cl->meta, rem) {
 		printf("%s=%s\n", blobmsg_name(cur), (char *) blobmsg_data(cur));

+ 8 - 0
uclient-http.c

@@ -503,6 +503,7 @@ uclient_http_send_headers(struct uclient_http *uh)
 static void uclient_http_headers_complete(struct uclient_http *uh)
 {
 	enum auth_type auth_type = uh->auth_type;
+	socklen_t sl;
 
 	uh->state = HTTP_STATE_RECV_DATA;
 	uh->uc.meta = uh->meta.head;
@@ -515,6 +516,13 @@ static void uclient_http_headers_complete(struct uclient_http *uh)
 		return;
 	}
 
+	memset(&uh->uc.local_addr, 0, sizeof(uh->uc.local_addr));
+	memset(&uh->uc.remote_addr, 0, sizeof(uh->uc.remote_addr));
+
+	sl = sizeof(uh->uc.local_addr);
+	getsockname(uh->ufd.fd.fd, &uh->uc.local_addr.sa, &sl);
+	getpeername(uh->ufd.fd.fd, &uh->uc.remote_addr.sa, &sl);
+
 	if (uh->uc.cb->header_done)
 		uh->uc.cb->header_done(&uh->uc);
 

+ 27 - 0
uclient.c

@@ -15,11 +15,38 @@
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
+#include <arpa/inet.h>
 #include <libubox/ustream-ssl.h>
 #include "uclient.h"
 #include "uclient-utils.h"
 #include "uclient-backend.h"
 
+char *uclient_get_addr(char *dest, int *port, union uclient_addr *a)
+{
+	int portval;
+	void *ptr;
+
+	switch(a->sa.sa_family) {
+	case AF_INET:
+		ptr = &a->sin.sin_addr;
+		portval = a->sin.sin_port;
+		break;
+	case AF_INET6:
+		ptr = &a->sin6.sin6_addr;
+		portval = a->sin6.sin6_port;
+		break;
+	default:
+		return strcpy(dest, "Unknown");
+	}
+
+	inet_ntop(a->sa.sa_family, ptr, dest, INET6_ADDRSTRLEN);
+	if (port)
+		*port = ntohs(portval);
+
+	return dest;
+}
+
+
 struct uclient_url __hidden *
 uclient_get_url(const char *url_str, const char *auth_str)
 {

+ 12 - 0
uclient.h

@@ -18,6 +18,8 @@
 #ifndef __LIBUBOX_UCLIENT_H
 #define __LIBUBOX_UCLIENT_H
 
+#include <netinet/in.h>
+
 #include <libubox/blob.h>
 #include <libubox/ustream.h>
 #include <libubox/ustream-ssl.h>
@@ -32,10 +34,18 @@ enum uclient_error_code {
 	UCLIENT_ERROR_SSL_CN_MISMATCH,
 };
 
+union uclient_addr {
+	struct sockaddr sa;
+	struct sockaddr_in sin;
+	struct sockaddr_in6 sin6;
+};
+
 struct uclient {
 	const struct uclient_backend *backend;
 	const struct uclient_cb *cb;
 
+	union uclient_addr local_addr, remote_addr;
+
 	struct uclient_url *url;
 	void *priv;
 
@@ -65,6 +75,8 @@ int uclient_read(struct uclient *cl, char *buf, int len);
 int uclient_write(struct uclient *cl, char *buf, int len);
 int uclient_request(struct uclient *cl);
 
+char *uclient_get_addr(char *dest, int *port, union uclient_addr *a);
+
 /* HTTP */
 extern const struct uclient_backend uclient_backend_http;