Skip to content

Instantly share code, notes, and snippets.

@klaxa

klaxa/ffserver.c Secret

Created August 17, 2015 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klaxa/fa446a0141167924bfee to your computer and use it in GitHub Desktop.
Save klaxa/fa446a0141167924bfee to your computer and use it in GitHub Desktop.
static void new_http_connection(AVIOContext *server)
{
socklen_t len;
struct sockaddr_storage *addr;
int ret;
HTTPContext *c = NULL;
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
char *peeraddr;
/* add a new connection */
c = av_mallocz(sizeof(HTTPContext));
if (!c)
goto fail;
if ((ret = avio_accept(server, &c->proto_ctx)) < 0)
goto fail;
addr = av_mallocz(sizeof(struct sockaddr_storage));
av_log(server, AV_LOG_TRACE, "Accepted client\n");
if (nb_connections >= config.nb_max_connections) {
http_send_too_busy_reply(c);
goto fail;
}
c->fd = avpriv_avio_get_file_handle(c->proto_ctx);
c->poll_entry = NULL;
if (av_opt_get(c->proto_ctx, "sock_addr_str", AV_OPT_SEARCH_CHILDREN, &peeraddr) < 0)
goto fail;
// SEGFAULT in this line ---v
if (av_opt_get(c->proto_ctx, "sock_addr", AV_OPT_SEARCH_CHILDREN, addr) < 0)
goto fail;
c->from_addr = *(struct sockaddr_in*) addr;
getnameinfo((struct sockaddr*) addr, sizeof(struct sockaddr_storage), hbuf, sizeof(hbuf), sbuf,
sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV);
av_log(server, AV_LOG_TRACE, "Accepted peer's address: %s\n", peeraddr);
av_log(server, AV_LOG_TRACE, "host=%s, serv=%s\n", hbuf, sbuf);
av_log(server, AV_LOG_TRACE, "Accepted peer's struct address: %u\n", ((struct in_addr*)&c->from_addr.sin_addr)->s_addr);
c->buffer_size = IOBUFFER_INIT_SIZE;
c->buffer = av_malloc(c->buffer_size);
if (!c->buffer)
goto fail;
c->next = first_http_ctx;
first_http_ctx = c;
c->request_parsed = 0;
nb_connections++;
start_wait_request(c, 0);
return;
fail:
if (c) {
avio_closep(&c->proto_ctx);
av_freep(&c->buffer);
av_free(c);
}
}
int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
{
void *dst, *target_obj;
const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
uint8_t *bin, buf[128];
int len, i, ret;
int64_t i64;
if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST))
return AVERROR_OPTION_NOT_FOUND;
dst = (uint8_t*)target_obj + o->offset;
buf[0] = 0;
switch (o->type) {
case AV_OPT_TYPE_FLAGS: ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);break;
case AV_OPT_TYPE_INT: ret = snprintf(buf, sizeof(buf), "%d" , *(int *)dst);break;
case AV_OPT_TYPE_INT64: ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
case AV_OPT_TYPE_FLOAT: ret = snprintf(buf, sizeof(buf), "%f" , *(float *)dst);break;
case AV_OPT_TYPE_DOUBLE: ret = snprintf(buf, sizeof(buf), "%f" , *(double *)dst);break;
case AV_OPT_TYPE_VIDEO_RATE:
case AV_OPT_TYPE_RATIONAL: ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
case AV_OPT_TYPE_CONST: ret = snprintf(buf, sizeof(buf), "%f" , o->default_val.dbl);break;
case AV_OPT_TYPE_STRING:
if (*(uint8_t**)dst)
*out_val = av_strdup(*(uint8_t**)dst);
else
*out_val = av_strdup("");
return *out_val ? 0 : AVERROR(ENOMEM);
case AV_OPT_TYPE_BINARY:
len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
if ((uint64_t)len*2 + 1 > INT_MAX)
return AVERROR(EINVAL);
if (!(*out_val = av_malloc(len*2 + 1)))
return AVERROR(ENOMEM);
if (!len) {
*out_val[0] = '\0';
return 0;
}
bin = *(uint8_t**)dst;
for (i = 0; i < len; i++)
// SEGFAULT here: ------v
snprintf(*out_val + i*2, 3, "%02X", bin[i]);
return 0;
case AV_OPT_TYPE_IMAGE_SIZE:
ret = snprintf(buf, sizeof(buf), "%dx%d", ((int *)dst)[0], ((int *)dst)[1]);
break;
case AV_OPT_TYPE_PIXEL_FMT:
ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_pix_fmt_name(*(enum AVPixelFormat *)dst), "none"));
break;
case AV_OPT_TYPE_SAMPLE_FMT:
ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_sample_fmt_name(*(enum AVSampleFormat *)dst), "none"));
break;
case AV_OPT_TYPE_DURATION:
i64 = *(int64_t *)dst;
ret = snprintf(buf, sizeof(buf), "%"PRIi64":%02d:%02d.%06d",
i64 / 3600000000, (int)((i64 / 60000000) % 60),
(int)((i64 / 1000000) % 60), (int)(i64 % 1000000));
break;
case AV_OPT_TYPE_COLOR:
ret = snprintf(buf, sizeof(buf), "0x%02x%02x%02x%02x",
(int)((uint8_t *)dst)[0], (int)((uint8_t *)dst)[1],
(int)((uint8_t *)dst)[2], (int)((uint8_t *)dst)[3]);
break;
case AV_OPT_TYPE_CHANNEL_LAYOUT:
i64 = *(int64_t *)dst;
ret = snprintf(buf, sizeof(buf), "0x%"PRIx64, i64);
break;
default:
return AVERROR(EINVAL);
}
if (ret >= sizeof(buf))
return AVERROR(EINVAL);
*out_val = av_strdup(buf);
return *out_val ? 0 : AVERROR(ENOMEM);
}
ffserver version N-74444-g9bc61f6 Copyright (c) 2000-2015 the FFmpeg developers
built with gcc 4.9.2 (GCC) 20150212 (Red Hat 4.9.2-6)
configuration: --disable-stripping --enable-debug --disable-optimizations --enable-libfdk-aac --enable-libmp3lame --enable-libopus --enable-libpulse --enable-libvorbis --enable-libvpx --enable-libx264 --enable-openssl --enable-gpl --enable-nonfree
libavutil 54. 30.100 / 54. 30.100
libavcodec 56. 57.100 / 56. 57.100
libavformat 56. 40.101 / 56. 40.101
libavdevice 56. 4.100 / 56. 4.100
libavfilter 5. 33.100 / 5. 33.100
libswscale 3. 1.101 / 3. 1.101
libswresample 1. 2.101 / 1. 2.101
libpostproc 53. 3.100 / 53. 3.100
doc/ffserver.conf:164: Setting default value for video bit rate tolerance = 16000. Use NoDefaults to disable it.
doc/ffserver.conf:164: Setting default value for video rate control equation = tex^qComp. Use NoDefaults to disable it.
doc/ffserver.conf:164: Setting default value for video max rate = 128000. Use NoDefaults to disable it.
doc/ffserver.conf:219: Setting default value for audio sample rate = 22050. Use NoDefaults to disable it.
doc/ffserver.conf:219: Setting default value for audio channel count = 1. Use NoDefaults to disable it.
doc/ffserver.conf:219: Setting default value for video bit rate tolerance = 64000. Use NoDefaults to disable it.
doc/ffserver.conf:219: Setting default value for video rate control equation = tex^qComp. Use NoDefaults to disable it.
doc/ffserver.conf:219: Setting default value for video max rate = 512000. Use NoDefaults to disable it.
HTTP hostname=0.0.0.0
Mon Aug 17 18:02:05 2015 Probing ffm score:101 size:2048
Mon Aug 17 18:02:05 2015 [ffm @ 0x1e7bc20]Format ffm probed with size=2048 and score=101
Mon Aug 17 18:02:05 2015 [NULL @ 0x1e7d720]Setting entry with key 'ab' to value '32000'
Mon Aug 17 18:02:05 2015 [NULL @ 0x1e7d720]Setting entry with key 'ac' to value '1'
Mon Aug 17 18:02:05 2015 [NULL @ 0x1e7d720]Setting entry with key 'ar' to value '44100'
Mon Aug 17 18:02:05 2015 [NULL @ 0x1e7dbe0]Setting entry with key 'b' to value '64000'
Mon Aug 17 18:02:05 2015 [NULL @ 0x1e7dbe0]Setting entry with key 'bufsize' to value '327680'
Mon Aug 17 18:02:05 2015 [NULL @ 0x1e7dbe0]Setting entry with key 'time_base' to value '1/24'
Mon Aug 17 18:02:05 2015 [NULL @ 0x1e7dbe0]Setting entry with key 'video_size' to value '160x128'
Mon Aug 17 18:02:05 2015 [NULL @ 0x1e7dbe0]Setting entry with key 'g' to value '12'
Mon Aug 17 18:02:05 2015 [NULL @ 0x1e7dbe0]Setting entry with key 'bt' to value '16000'
Mon Aug 17 18:02:05 2015 [NULL @ 0x1e7dbe0]Setting entry with key 'maxrate' to value '128000'
Mon Aug 17 18:02:05 2015 [NULL @ 0x1e7e540]Setting entry with key 'ab' to value '64000'
Mon Aug 17 18:02:05 2015 [NULL @ 0x1e7e540]Setting entry with key 'ar' to value '22050'
Mon Aug 17 18:02:05 2015 [NULL @ 0x1e7e540]Setting entry with key 'ac' to value '1'
Mon Aug 17 18:02:05 2015 [NULL @ 0x1e7ea00]Setting entry with key 'time_base' to value '1/15'
Mon Aug 17 18:02:05 2015 [NULL @ 0x1e7ea00]Setting entry with key 'video_size' to value '352x240'
Mon Aug 17 18:02:05 2015 [NULL @ 0x1e7ea00]Setting entry with key 'b' to value '256000'
Mon Aug 17 18:02:05 2015 [NULL @ 0x1e7ea00]Setting entry with key 'bufsize' to value '327680'
Mon Aug 17 18:02:05 2015 [NULL @ 0x1e7ea00]Setting entry with key 'g' to value '30'
Mon Aug 17 18:02:05 2015 [NULL @ 0x1e7ea00]Setting entry with key 'bt' to value '64000'
Mon Aug 17 18:02:05 2015 [NULL @ 0x1e7ea00]Setting entry with key 'maxrate' to value '512000'
Mon Aug 17 18:02:05 2015 [AVIOContext @ 0x1e671e0]Statistics: 4096 bytes read, 0 seeks
Mon Aug 17 18:02:05 2015 [AVIOContext @ 0x1e744e0]Started server.
Mon Aug 17 18:02:05 2015 FFserver started.
Mon Aug 17 18:02:05 2015 [AVIOContext @ 0x1e744e0]server fd=3
Mon Aug 17 18:02:06 2015 [AVIOContext @ 0x1e744e0]server fd=3
Mon Aug 17 18:02:07 2015 [AVIOContext @ 0x1e744e0]server fd=3
Mon Aug 17 18:02:07 2015 [tcp @ 0x1e672e0]accepted fd=4
Mon Aug 17 18:02:07 2015 [tcp @ 0x1e672e0]got peer name (len:16)
Mon Aug 17 18:02:07 2015 [tcp @ 0x1e672e0]host=127.0.0.1, port=54963
Mon Aug 17 18:02:07 2015 [http @ 0x1e7f600]Peer address: 127.0.0.1:54963
Mon Aug 17 18:02:07 2015 [AVIOContext @ 0x1e744e0]Accepted client
static int tcp_accept(URLContext *s, URLContext **c)
{
TCPContext *sc = s->priv_data;
TCPContext *cc;
int ret;
socklen_t len = sizeof(struct sockaddr_storage);
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
char sock_addr_str[NI_MAXHOST + NI_MAXSERV + 1];
av_assert0(sc->listen);
if ((ret = ffurl_alloc(c, s->filename, s->flags, &s->interrupt_callback)) < 0)
return ret;
cc = (*c)->priv_data;
ret = ff_accept(sc->fd, sc->listen_timeout, s);
if (ret < 0)
return ff_neterrno();
cc->fd = ret;
av_log(cc, AV_LOG_TRACE, "accepted fd=%d\n", cc->fd);
// Allocating memory here ---v
if (!(cc->sock_addr = av_mallocz(len)))
return AVERROR(ENOMEM);
if ((ret = getpeername(cc->fd, (struct sockaddr*) &cc->sock_addr, &len)) < 0) {
av_log(cc, AV_LOG_TRACE, "ret=%d; error: %s\n", ret, strerror(errno));
return ff_neterrno();
}
cc->sock_addr_len = len;
av_log(cc, AV_LOG_TRACE, "got peer name (len:%d)\n", len);
if ((ret = getnameinfo((struct sockaddr*) &cc->sock_addr, len, hbuf, sizeof(hbuf), sbuf,
sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) < 0)
return ff_neterrno();
// Correct output here: ----v
av_log(cc, AV_LOG_TRACE, "host=%s, port=%s\n", hbuf, sbuf);
snprintf(sock_addr_str, sizeof(sock_addr_str), "%s:%s", hbuf, sbuf);
cc->sock_addr_str = av_strdup(sock_addr_str);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment