From: Felix Fietkau Date: Mon, 7 Apr 2014 19:11:58 +0000 (+0200) Subject: add support for querying local/remote address X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fuclient.git;a=commitdiff_plain;h=cb7867a867e45949950e03de0ea845e483ffc6eb;hp=316bcc8c62e195a892813b050a922ebd3935eba2 add support for querying local/remote address Signed-off-by: Felix Fietkau --- diff --git a/uclient-example.c b/uclient-example.c index 2e88b28..a54aa88 100644 --- a/uclient-example.c +++ b/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 + #include #include +#include + #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)); diff --git a/uclient-http.c b/uclient-http.c index 05dd3aa..ee354c7 100644 --- a/uclient-http.c +++ b/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); diff --git a/uclient.c b/uclient.c index 82553a5..bcb7357 100644 --- a/uclient.c +++ b/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 #include #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) { diff --git a/uclient.h b/uclient.h index 65e48b2..25990c0 100644 --- a/uclient.h +++ b/uclient.h @@ -18,6 +18,8 @@ #ifndef __LIBUBOX_UCLIENT_H #define __LIBUBOX_UCLIENT_H +#include + #include #include #include @@ -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;