http: optimize tcp_get_ip_str() calls for the peer ip addr

This commit is contained in:
Jaroslav Kysela 2015-03-23 15:47:42 +01:00
parent fc68ad0a74
commit 5518397784
6 changed files with 25 additions and 52 deletions

View file

@ -335,15 +335,12 @@ void
http_error(http_connection_t *hc, int error)
{
const char *errtxt = http_rc2str(error);
char addrstr[50];
if (!http_server) return;
tcp_get_ip_str((struct sockaddr*)hc->hc_peer, addrstr, 50);
if (error != HTTP_STATUS_FOUND && error != HTTP_STATUS_MOVED)
tvhlog(error < 400 ? LOG_INFO : LOG_ERR, "http", "%s: %s %s %s -- %d",
addrstr, http_ver2str(hc->hc_version),
hc->hc_peer_ipstr, http_ver2str(hc->hc_version),
http_cmd2str(hc->hc_cmd), hc->hc_url, error);
if (hc->hc_version != RTSP_VERSION_1_0) {
@ -447,10 +444,8 @@ http_access_verify_ticket(http_connection_t *hc)
hc->hc_access = access_ticket_verify2(ticket_id, hc->hc_url);
if (hc->hc_access == NULL)
return;
char addrstr[50];
tcp_get_ip_str((struct sockaddr*)hc->hc_peer, addrstr, 50);
tvhlog(LOG_INFO, "http", "%s: using ticket %s for %s",
addrstr, ticket_id, hc->hc_url);
hc->hc_peer_ipstr, ticket_id, hc->hc_url);
}
/**
@ -675,7 +670,7 @@ process_request(http_connection_t *hc, htsbuf_queue_t *spill)
{
char *v, *argv[2];
int n, rval = -1;
uint8_t authbuf[150];
char authbuf[150];
hc->hc_url_orig = tvh_strdupa(hc->hc_url);
@ -715,11 +710,11 @@ process_request(http_connection_t *hc, htsbuf_queue_t *spill)
/* Extract authorization */
if((v = http_arg_get(&hc->hc_args, "Authorization")) != NULL) {
if((n = http_tokenize(v, argv, 2, -1)) == 2) {
n = base64_decode(authbuf, argv[1], sizeof(authbuf) - 1);
n = base64_decode((uint8_t *)authbuf, argv[1], sizeof(authbuf) - 1);
if (n < 0)
n = 0;
authbuf[n] = 0;
if((n = http_tokenize((char *)authbuf, argv, 2, ':')) == 2) {
if((n = http_tokenize(authbuf, argv, 2, ':')) == 2) {
hc->hc_username = strdup(argv[0]);
hc->hc_password = strdup(argv[1]);
// No way to actually track this
@ -727,13 +722,9 @@ process_request(http_connection_t *hc, htsbuf_queue_t *spill)
}
}
if(hc->hc_username != NULL) {
hc->hc_representative = strdup(hc->hc_username);
} else {
hc->hc_representative = malloc(50);
/* Not threadsafe ? */
tcp_get_ip_str((struct sockaddr*)hc->hc_peer, hc->hc_representative, 50);
}
tcp_get_ip_str((struct sockaddr*)hc->hc_peer, authbuf, sizeof(authbuf));
hc->hc_peer_ipstr = strdup(authbuf);
hc->hc_representative = strdup(hc->hc_username ?: authbuf);
switch(hc->hc_version) {
case RTSP_VERSION_1_0:
@ -752,6 +743,7 @@ process_request(http_connection_t *hc, htsbuf_queue_t *spill)
break;
}
free(hc->hc_representative);
free(hc->hc_peer_ipstr);
free(hc->hc_session);
hc->hc_session = NULL;
return rval;

View file

@ -120,6 +120,7 @@ typedef enum http_ver {
typedef struct http_connection {
int hc_fd;
struct sockaddr_storage *hc_peer;
char *hc_peer_ipstr;
struct sockaddr_storage *hc_self;
char *hc_representative;

View file

@ -787,7 +787,7 @@ rtsp_parse_cmd
const char *caller;
mpegts_apids_t pids, addpids, delpids;
dvb_mux_conf_t *dmc;
char buf[256], addrbuf[50];
char buf[256];
http_arg_t *arg;
switch (cmd) {
@ -803,8 +803,6 @@ rtsp_parse_cmd
mpegts_pid_init(&addpids);
mpegts_pid_init(&delpids);
tcp_get_ip_str((struct sockaddr*)hc->hc_peer, addrbuf, sizeof(addrbuf));
has_args = !TAILQ_EMPTY(&hc->hc_req_args);
fe = atoi(http_arg_get_remove(&hc->hc_req_args, "fe"));
@ -1045,7 +1043,7 @@ play:
tvhdebug("satips", "%i/%s/%d: %s from %s:%d %s",
rs->frontend, rs->session, rs->stream,
caller, addrbuf, IP_PORT(*hc->hc_peer), buf);
caller, hc->hc_peer_ipstr, IP_PORT(*hc->hc_peer), buf);
ok:
errcode = 0;
@ -1257,11 +1255,9 @@ rtsp_process_play(http_connection_t *hc, int setup)
{
session_t *rs;
int errcode = HTTP_STATUS_BAD_REQUEST, valid = 0, oldstate = 0, i, stream;;
char buf[256], addrbuf[50], *u = tvh_strdupa(hc->hc_url);
char buf[256], *u = tvh_strdupa(hc->hc_url);
http_arg_list_t args;
tcp_get_ip_str((struct sockaddr*)hc->hc_peer, addrbuf, sizeof(addrbuf));
http_arg_init(&args);
if ((u = rtsp_check_urlbase(u)) == NULL)
@ -1285,14 +1281,14 @@ rtsp_process_play(http_connection_t *hc, int setup)
errcode = HTTP_STATUS_INTERNAL;
goto error;
}
if (udp_connect(rs->udp_rtp, "RTP", addrbuf, rs->rtp_peer_port) ||
udp_connect(rs->udp_rtcp, "RTCP", addrbuf, rs->rtp_peer_port + 1)) {
if (udp_connect(rs->udp_rtp, "RTP", hc->hc_peer_ipstr, rs->rtp_peer_port) ||
udp_connect(rs->udp_rtcp, "RTCP", hc->hc_peer_ipstr, rs->rtp_peer_port + 1)) {
errcode = HTTP_STATUS_INTERNAL;
goto error;
}
}
if ((errcode = rtsp_start(hc, rs, addrbuf, valid, setup, oldstate)) < 0)
if ((errcode = rtsp_start(hc, rs, hc->hc_peer_ipstr, valid, setup, oldstate)) < 0)
goto error;
if (setup) {
@ -1336,11 +1332,9 @@ rtsp_process_teardown(http_connection_t *hc)
char *u = tvh_strdupa(hc->hc_url);
struct session *rs = NULL;
http_arg_list_t args;
char addrbuf[50], session[16];
char session[16];
int stream;
tcp_get_ip_str((struct sockaddr*)hc->hc_peer, addrbuf, sizeof(addrbuf));
if ((u = rtsp_check_urlbase(u)) == NULL ||
(stream = rtsp_parse_args(hc, u)) < 0) {
http_error(hc, HTTP_STATUS_BAD_REQUEST);
@ -1348,7 +1342,7 @@ rtsp_process_teardown(http_connection_t *hc)
}
tvhdebug("satips", "-/%s/%i: teardown from %s:%d",
hc->hc_session, stream, addrbuf, IP_PORT(*hc->hc_peer));
hc->hc_session, stream, hc->hc_peer_ipstr, IP_PORT(*hc->hc_peer));
pthread_mutex_lock(&rtsp_lock);
rs = rtsp_find_session(hc, stream);

View file

@ -162,9 +162,8 @@ satip_server_http_xml(http_connection_t *hc)
htsbuf_queue_flush(&q);
if (devicelist == NULL || devicelist[0] == '\0') {
tcp_get_ip_str((struct sockaddr*)hc->hc_peer, buf, sizeof(buf));
tvhwarn("satips", "SAT>IP server announces an empty tuner list to a client %s (missing %s)",
buf, !tuners ? "tuner settings - global config" : "network assignment");
hc->hc_peer_ipstr, !tuners ? "tuner settings - global config" : "network assignment");
}
if (satip_server_rtsp_port != 554)

View file

@ -142,16 +142,13 @@ comet_access_update(http_connection_t *hc, comet_mailbox_t *cmb)
htsmsg_t *m = htsmsg_create_map();
const char *username = hc->hc_access ? (hc->hc_access->aa_username ?: "") : "";
char addrstr[50];
htsmsg_add_str(m, "notificationClass", "accessUpdate");
tcp_get_ip_str((struct sockaddr*)hc->hc_peer, addrstr, sizeof(addrstr));
if (!access_noacl)
htsmsg_add_str(m, "username", username);
if (addrstr[0])
htsmsg_add_str(m, "address", addrstr);
if (hc->hc_peer_ipstr)
htsmsg_add_str(m, "address", hc->hc_peer_ipstr);
htsmsg_add_u32(m, "dvr", !http_access_verify(hc, ACCESS_RECORDER));
htsmsg_add_u32(m, "admin", !http_access_verify(hc, ACCESS_ADMIN));

View file

@ -744,7 +744,6 @@ http_stream_service(http_connection_t *hc, service_t *service, int weight)
const char *str;
size_t qsize;
const char *name;
char addrbuf[50];
void *tcp_id;
int res = HTTP_STATUS_SERVICE;
@ -767,11 +766,9 @@ http_stream_service(http_connection_t *hc, service_t *service, int weight)
profile_chain_init(&prch, pro, service);
if (!profile_chain_open(&prch, NULL, 0, qsize)) {
tcp_get_ip_str((struct sockaddr*)hc->hc_peer, addrbuf, 50);
s = subscription_create_from_service(&prch, NULL, weight ?: 100, "HTTP",
prch.prch_flags | SUBSCRIPTION_STREAMING,
addrbuf,
hc->hc_peer_ipstr,
hc->hc_username,
http_arg_get(&hc->hc_args, "User-Agent"),
NULL);
@ -803,7 +800,6 @@ http_stream_mux(http_connection_t *hc, mpegts_mux_t *mm, int weight)
profile_chain_t prch;
size_t qsize;
const char *name, *str;
char addrbuf[50];
void *tcp_id;
char *p, *saveptr = NULL;
mpegts_apids_t pids;
@ -847,12 +843,10 @@ http_stream_mux(http_connection_t *hc, mpegts_mux_t *mm, int weight)
if (!profile_chain_raw_open(&prch, mm, qsize, 1)) {
tcp_get_ip_str((struct sockaddr*)hc->hc_peer, addrbuf, 50);
s = subscription_create_from_mux(&prch, NULL, weight ?: 10, "HTTP",
prch.prch_flags |
SUBSCRIPTION_STREAMING,
addrbuf, hc->hc_username,
hc->hc_peer_ipstr, hc->hc_username,
http_arg_get(&hc->hc_args, "User-Agent"),
NULL);
if (s) {
@ -887,7 +881,6 @@ http_stream_channel(http_connection_t *hc, channel_t *ch, int weight)
char *str;
size_t qsize;
const char *name;
char addrbuf[50];
void *tcp_id;
int res = HTTP_STATUS_SERVICE;
@ -910,12 +903,10 @@ http_stream_channel(http_connection_t *hc, channel_t *ch, int weight)
profile_chain_init(&prch, pro, ch);
if (!profile_chain_open(&prch, NULL, 0, qsize)) {
tcp_get_ip_str((struct sockaddr*)hc->hc_peer, addrbuf, 50);
s = subscription_create_from_channel(&prch,
NULL, weight ?: 100, "HTTP",
prch.prch_flags | SUBSCRIPTION_STREAMING,
addrbuf, hc->hc_username,
hc->hc_peer_ipstr, hc->hc_username,
http_arg_get(&hc->hc_args, "User-Agent"),
NULL);
@ -1240,12 +1231,11 @@ page_dvrfile(http_connection_t *hc, const char *remain, void *opaque)
pthread_mutex_lock(&global_lock);
tcp_id = http_stream_preop(hc);
tcp_get_ip_str((struct sockaddr*)hc->hc_peer, range_buf, 50);
sub = NULL;
if (tcp_id && !hc->hc_no_output && content_len > 64*1024) {
sub = subscription_create(NULL, 1, "HTTP",
SUBSCRIPTION_NONE, NULL,
range_buf, hc->hc_username,
hc->hc_peer_ipstr, hc->hc_username,
http_arg_get(&hc->hc_args, "User-Agent"));
if (sub == NULL) {
http_stream_postop(tcp_id);