make my life easier with a smart_ntop_port() function

git.mgm/mediaproxy-ng/2.2
Richard Fuchs 12 years ago
parent 5ea5dcddfa
commit be4a6c7d58

@ -14,6 +14,7 @@
#include <arpa/inet.h>
#include <pthread.h>
#include <sys/resource.h>
#include <stdio.h>
@ -112,25 +113,39 @@ static inline void smart_ntop(char *o, struct in6_addr *a, size_t len) {
*o = '\0';
}
static inline void smart_ntop_p(char *o, struct in6_addr *a, size_t len) {
static inline char *smart_ntop_p(char *o, struct in6_addr *a, size_t len) {
int l;
if (IN6_IS_ADDR_V4MAPPED(a)) {
if (!inet_ntop(AF_INET, &(a->s6_addr32[3]), o, len))
*o = '\0';
if (inet_ntop(AF_INET, &(a->s6_addr32[3]), o, len))
return o + strlen(o);
*o = '\0';
return NULL;
}
else {
*o = '[';
if (!inet_ntop(AF_INET6, a, o+1, len-2)) {
*o = '\0';
return;
return NULL;
}
l = strlen(o);
o[l] = ']';
o[l+1] = '\0';
return o + (l + 1);
}
}
static inline void smart_ntop_port(char *o, struct sockaddr_in6 *a, size_t len) {
char *e;
e = smart_ntop_p(o, &a->sin6_addr, len);
if (!e)
return;
if (len - (e - o) < 7)
return;
sprintf(e, ":%hu", ntohs(a->sin6_port));
}
static inline int smart_pton(int af, char *src, void *dst) {
char *p;
int ret;

@ -222,11 +222,11 @@ static int stream_packet(struct streamrelay *r, char *b, int l, struct sockaddr_
p = &pe2->rtps[r->idx];
c = cs->call;
m = c->callmaster;
smart_ntop_p(addr, &fsin->sin6_addr, sizeof(addr));
smart_ntop_port(addr, fsin, sizeof(addr));
if (p->fd == -1) {
mylog(LOG_WARNING, LOG_PREFIX_C "RTP packet to port %u discarded from %s:%u",
LOG_PARAMS_C(c), r->localport, addr, ntohs(fsin->sin6_port));
mylog(LOG_WARNING, LOG_PREFIX_C "RTP packet to port %u discarded from %s",
LOG_PARAMS_C(c), r->localport, addr);
r->stats.errors++;
mutex_lock(&m->statspslock);
m->statsps.errors++;
@ -240,8 +240,8 @@ static int stream_packet(struct streamrelay *r, char *b, int l, struct sockaddr_
if (!c->lookup_done || poller_now <= c->lookup_done + 3)
goto peerinfo;
mylog(LOG_DEBUG, LOG_PREFIX_C "Confirmed peer information for port %u - %s:%u",
LOG_PARAMS_C(c), r->localport, addr, ntohs(fsin->sin6_port));
mylog(LOG_DEBUG, LOG_PREFIX_C "Confirmed peer information for port %u - %s",
LOG_PARAMS_C(c), r->localport, addr);
pe->confirmed = 1;

@ -20,7 +20,7 @@ static void control_ng_incoming(struct obj *obj, char *buf, int buf_len, struct
data = memchr(buf, ' ', buf_len);
if (!data || data == buf) {
mylog(LOG_WARNING, "Received invalid data on NG port (no cookie) from %s:%u: %.*s", addr, ntohs(sin->sin6_port), buf_len, buf);
mylog(LOG_WARNING, "Received invalid data on NG port (no cookie) from %s: %.*s", addr, buf_len, buf);
return;
}
@ -38,7 +38,7 @@ static void control_ng_incoming(struct obj *obj, char *buf, int buf_len, struct
reply = cookie_cache_lookup(&c->cookie_cache, cookie);
if (reply) {
mylog(LOG_INFO, "Detected command from %s:%u as a duplicate", addr, ntohs(sin->sin6_port));
mylog(LOG_INFO, "Detected command from %s as a duplicate", addr);
reply_len = strlen(reply); /* XXX fails for embedded nulls */
resp = NULL;
goto send_only;
@ -54,7 +54,7 @@ static void control_ng_incoming(struct obj *obj, char *buf, int buf_len, struct
if (!cmd)
goto err_send;
mylog(LOG_INFO, "Got valid command from %s:%u: %.*s [%.*s]", addr, ntohs(sin->sin6_port), cmd_len, cmd, data_len, data);
mylog(LOG_INFO, "Got valid command from %s: %.*s [%.*s]", addr, cmd_len, cmd, data_len, data);
errstr = NULL;
if (!strmemcmp(cmd, cmd_len, "ping"))
@ -74,7 +74,7 @@ static void control_ng_incoming(struct obj *obj, char *buf, int buf_len, struct
goto send_out;
err_send:
mylog(LOG_WARNING, "Protocol error in packet from %s:%u: %s [%.*s]", addr, ntohs(sin->sin6_port), errstr, data_len, data);
mylog(LOG_WARNING, "Protocol error in packet from %s: %s [%.*s]", addr, errstr, data_len, data);
bencode_dictionary_add_string(resp, "result", "error");
bencode_dictionary_add_string(resp, "error-reason", errstr);
goto send_out;

@ -30,11 +30,11 @@ static void control_udp_incoming(struct obj *obj, char *buf, int len, struct soc
if (ret <= 0) {
ret = pcre_exec(u->fallback_re, NULL, buf, len, 0, 0, ovec, G_N_ELEMENTS(ovec));
if (ret <= 0) {
mylog(LOG_WARNING, "Unable to parse command line from udp:%s:%u: %s", addr, ntohs(sin->sin6_port), buf);
mylog(LOG_WARNING, "Unable to parse command line from udp:%s: %s", addr, buf);
return;
}
mylog(LOG_WARNING, "Failed to properly parse UDP command line '%s' from %s:%u, using fallback RE", buf, addr, ntohs(sin->sin6_port));
mylog(LOG_WARNING, "Failed to properly parse UDP command line '%s' from %s, using fallback RE", buf, addr);
pcre_get_substring_list(buf, ovec, ret, &out);
@ -67,13 +67,13 @@ static void control_udp_incoming(struct obj *obj, char *buf, int len, struct soc
return;
}
mylog(LOG_INFO, "Got valid command from udp:%s:%u: %s", addr, ntohs(sin->sin6_port), buf);
mylog(LOG_INFO, "Got valid command from udp:%s: %s", addr, buf);
pcre_get_substring_list(buf, ovec, ret, &out);
reply = cookie_cache_lookup(&u->cookie_cache, out[RE_UDP_COOKIE]);
if (reply) {
mylog(LOG_INFO, "Detected command from udp:%s:%u as a duplicate", addr, ntohs(sin->sin6_port));
mylog(LOG_INFO, "Detected command from udp:%s as a duplicate", addr);
sendto(u->udp_listener.fd, reply, strlen(reply), 0, (struct sockaddr *) sin, sizeof(*sin));
goto out;
}

@ -39,7 +39,7 @@ static void udp_listener_incoming(int fd, void *p, uintptr_t x) {
}
buf[len] = '\0';
smart_ntop_p(addr, &sin.sin6_addr, sizeof(addr));
smart_ntop_port(addr, &sin, sizeof(addr));
cb->func(cb->p, buf, len, &sin, addr);
}

Loading…
Cancel
Save