]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
tcp server: keep track of the local (ie. our) IP + port
authorAndreas Öman <andreas@lonelycoder.com>
Wed, 18 Nov 2009 22:24:06 +0000 (22:24 +0000)
committerAndreas Öman <andreas@lonelycoder.com>
Wed, 18 Nov 2009 22:24:06 +0000 (22:24 +0000)
src/htsp.c
src/http.c
src/http.h
src/tcp.c
src/tcp.h

index 134cea91480bc3a3e94a138f54b0a90a7a4c3aba..0d05776ab876fb5c2093a56adb6c431ab72c62a5 100644 (file)
@@ -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];
index 7ba118a52651be859f73696307808539663aa1a4..9de8ab79851b28899e4c721c90d92b3d1ab1b8a3 100644 (file)
@@ -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);
 
index bb8122dae44177de8960f406b0311cd47ba6841a..d91be394f4e7b47355b961ad14edb24c23ae290c 100644 (file)
@@ -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;
index 134015659e11389056cfcbc9e29645af5cdf186f..8c28fe97efb4626c8a77456c279c8e01ba82b789 100644 (file)
--- 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);
       }
     }
index 35e379bed6e29a9a3ec267e56a2cfaa4c3879a91..e9d9534c1480fddf334eca6b3feed6e525d08ecb 100644 (file)
--- a/src/tcp.h
+++ b/src/tcp.h
 
 #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);