tcp server: keep track of the local (ie. our) IP + port

This commit is contained in:
Andreas Öman 2009-11-18 22:24:06 +00:00
parent c23296eb1d
commit ab3ce01643
5 changed files with 21 additions and 89 deletions

View 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];

View 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);

View 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;

View file

@ -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);
}
}

View file

@ -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);