From ab3ce016435beac3f6bca5b836941fc401372d32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20=C3=96man?= Date: Wed, 18 Nov 2009 22:24:06 +0000 Subject: [PATCH] tcp server: keep track of the local (ie. our) IP + port --- src/htsp.c | 3 +- src/http.c | 6 ++-- src/http.h | 1 + src/tcp.c | 15 ++++++++-- src/tcp.h | 85 ++---------------------------------------------------- 5 files changed, 21 insertions(+), 89 deletions(-) diff --git a/src/htsp.c b/src/htsp.c index 134cea91..0d05776a 100644 --- a/src/htsp.c +++ b/src/htsp.c @@ -998,7 +998,8 @@ htsp_write_scheduler(void *aux) * */ static void -htsp_serve(int fd, void *opaque, struct sockaddr_in *source) +htsp_serve(int fd, void *opaque, struct sockaddr_in *source, + struct sockaddr_in *self) { htsp_connection_t htsp; char buf[30]; diff --git a/src/http.c b/src/http.c index 7ba118a5..9de8ab79 100644 --- a/src/http.c +++ b/src/http.c @@ -748,7 +748,8 @@ http_serve_requests(http_connection_t *hc, htsbuf_queue_t *spill) * */ static void -http_serve(int fd, void *opaque, struct sockaddr_in *source) +http_serve(int fd, void *opaque, struct sockaddr_in *peer, + struct sockaddr_in *self) { htsbuf_queue_t spill; http_connection_t hc; @@ -759,7 +760,8 @@ http_serve(int fd, void *opaque, struct sockaddr_in *source) TAILQ_INIT(&hc.hc_req_args); hc.hc_fd = fd; - hc.hc_peer = source; + hc.hc_peer = peer; + hc.hc_self = self; htsbuf_queue_init(&spill, 0); diff --git a/src/http.h b/src/http.h index bb8122da..d91be394 100644 --- a/src/http.h +++ b/src/http.h @@ -39,6 +39,7 @@ typedef struct http_arg { typedef struct http_connection { int hc_fd; struct sockaddr_in *hc_peer; + struct sockaddr_in *hc_self; char *hc_representative; char *hc_url; diff --git a/src/tcp.c b/src/tcp.c index 13401565..8c28fe97 100644 --- a/src/tcp.c +++ b/src/tcp.c @@ -357,7 +357,8 @@ typedef struct tcp_server_launch_t { tcp_server_callback_t *start; void *opaque; int fd; - struct sockaddr_in source; + struct sockaddr_in peer; + struct sockaddr_in self; } tcp_server_launch_t; @@ -386,7 +387,7 @@ tcp_server_start(void *aux) setsockopt(tsl->fd, SOL_TCP, TCP_NODELAY, &val, sizeof(val)); - tsl->start(tsl->fd, tsl->opaque, &tsl->source); + tsl->start(tsl->fd, tsl->opaque, &tsl->peer, &tsl->self); free(tsl); return NULL; @@ -433,7 +434,7 @@ tcp_server_loop(void *aux) slen = sizeof(struct sockaddr_in); tsl->fd = accept(ts->serverfd, - (struct sockaddr *)&tsl->source, &slen); + (struct sockaddr *)&tsl->peer, &slen); if(tsl->fd == -1) { perror("accept"); free(tsl); @@ -441,6 +442,14 @@ tcp_server_loop(void *aux) continue; } + + slen = sizeof(struct sockaddr_in); + if(getsockname(tsl->fd, (struct sockaddr *)&tsl->self, &slen)) { + close(tsl->fd); + free(tsl); + continue; + } + pthread_create(&tid, &attr, tcp_server_start, tsl); } } diff --git a/src/tcp.h b/src/tcp.h index 35e379be..e9d9534c 100644 --- a/src/tcp.h +++ b/src/tcp.h @@ -21,95 +21,14 @@ #include "htsbuf.h" -#if 0 - - -typedef enum { - TCP_CONNECT, - TCP_DISCONNECT, - TCP_INPUT, -} tcpevent_t; - -typedef void (tcp_callback_t)(tcpevent_t event, void *tcpsession); -typedef int (tcp_line_input_t)(void *tcpsession, char *line); - -typedef struct tcpserver { - tcp_callback_t *tcp_callback; - int tcp_fd; - size_t tcp_session_size; - const char *tcp_server_name; -} tcp_server_t; - -#define TCP_MAX_LINE_LEN 4000 - -typedef struct tcp_session { - void *tcp_dispatch_handle; - int tcp_fd; - struct sockaddr_storage tcp_peer_addr; - struct sockaddr_storage tcp_self_addr; - char tcp_peer_txt[100]; - tcp_callback_t *tcp_callback; - tcp_server_t *tcp_server; /* if this is NULL, then we are spawned - as a client */ - - /* These are only used when we spawn as a client */ - - int tcp_enabled; - // dtimer_t tcp_timer; - char *tcp_name; - int tcp_port; - char *tcp_hostname; - void *tcp_resolver; - - /* Output queueing */ - - int tcp_blocked; - htsbuf_queue_t tcp_q[2]; - - htsbuf_queue_t *tcp_q_current; - - /* Input line parser */ - - int tcp_input_buf_ptr; - char tcp_input_buf[TCP_MAX_LINE_LEN]; - -} tcp_session_t; - -void tcp_disconnect(tcp_session_t *ses, int err); - -void tcp_create_server(int port, size_t session_size, const char *name, - tcp_callback_t *cb); - -void tcp_line_read(tcp_session_t *ses, tcp_line_input_t *callback); - -int tcp_line_drain(tcp_session_t *ses, void *buf, int n); - -#define tcp_logname(ses) ((ses)->tcp_peer_txt) - -void tcp_printf(tcp_session_t *ses, const char *fmt, ...); - -void tcp_output_queue(tcp_session_t *ses, int hiprio, htsbuf_queue_t *src); - -void *tcp_create_client(const char *hostname, int port, size_t session_size, - const char *name, tcp_callback_t *cb, int enabled); - -void tcp_destroy_client(tcp_session_t *ses); - -void tcp_enable_disable(tcp_session_t *ses, int enabled); - -void tcp_set_hostname(tcp_session_t *ses, const char *hostname); - -int tcp_send_msg(tcp_session_t *ses, int hiprio, void *data, size_t len); - -#endif - void tcp_server_init(void); int tcp_connect(const char *hostname, int port, char *errbuf, size_t errbufsize, int timeout); typedef void (tcp_server_callback_t)(int fd, void *opaque, - struct sockaddr_in *source); + struct sockaddr_in *peer, + struct sockaddr_in *self); void *tcp_server_create(int port, tcp_server_callback_t *start, void *opaque);