Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/c.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ jobs:
steps:
- uses: actions/checkout@v1
- name: install libcurl
run: sudo apt-get install libcurl4-openssl-dev
run: sudo apt-get install libcurl4-openssl-dev libsdl2-dev
- name: make
run: make
- name: make with sound support
run: make with-sound

9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ addons:
packages: &common_packages
- gcc-8
- valgrind
- libsdl2-dev

matrix:
include:
Expand All @@ -32,13 +33,21 @@ matrix:
env: T=normal

before_install:
- |
travis_retry curl -L https://www.libsdl.org/release/SDL2-2.0.10.tar.gz | tar xz
cd SDL2-2.0.10
./configure
make
sudo make install
cd ../

install:

before_script:

script:
- make
- make with-sound

# whitelist branches to avoid testing feature branches twice (as branch and as pull request)
branches:
Expand Down
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
TARGET = doh
OBJS = doh.o
TARGET = doh
LDLIBS = `curl-config --libs`
CFLAGS := $(CFLAGS) -W -Wall -pedantic -g `curl-config --cflags`
MANUAL = doh.1
Expand All @@ -14,5 +14,13 @@ install:
install -m 0755 $(TARGET) $(DESTDIR)$(BINDIR)
install -m 0744 $(MANUAL) $(MANDIR)/man1/

with-sound: OBJS = doh.o sound/doh-sound.o
with-sound: LDLIBS = -lSDL2 -lpthread `curl-config --libs`
with-sound: CFLAGS := $(CFLAGS) -DHAS_SOUND -W -Wall -pedantic -g `curl-config --cflags`
with-sound:
if [ "$$(./sdl2-util.sh --check)" = "no" ]; then \
./sdl2-util.sh --install; \
fi && $(MAKE) clean && $(MAKE) LDLIBS="$(LDLIBS)" CFLAGS="$(CFLAGS)" OBJS="$(OBJS)"

clean:
rm -f $(OBJS) $(TARGET)
2 changes: 2 additions & 0 deletions doh.1
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Show the help output.
.IP \-k
Run in insecure mode. It completely switches off certificate checking of the
DoH server!
.IP \-s
Play a doh sound in a background thread.
.IP \-t
Test mode. Returns an exit code if there isn't a proper DoH response.
.IP \-v
Expand Down
28 changes: 28 additions & 0 deletions doh.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ enum iptrans { v4, v6, v46 };

#include "version.h"

/* doh-sound related stuff */
#ifdef HAS_SOUND
#include "sound/doh-sound.h"
#include <pthread.h>
#include <errno.h>
pthread_t sound_thread;
static int playing = false;
#endif

#ifdef _WIN32
#define FMT_SIZE_T "Iu"
#else
Expand Down Expand Up @@ -716,6 +725,9 @@ static void help(const char *msg)
fprintf(stderr, "Usage: doh [options] <host> [URL]\n"
" -h this help\n"
" -k insecure mode - don't validate TLS certificate\n"
#ifdef HAS_SOUND
" -s play doh sound\n"
#endif
" -t test mode\n"
" -v verbose mode\n"
" -4 use only IPv4 transport\n"
Expand Down Expand Up @@ -781,6 +793,16 @@ int main(int argc, char **argv)
case 'h': /* help */
help(NULL);
break;
#ifdef HAS_SOUND
case 's':
if (pthread_create(&sound_thread, NULL, play_sound, NULL) != 0) {
fprintf(stderr, "pthread_create failed: %s\n", strerror(errno));
playing = false;
} else {
playing = true;
}
break;
#endif
default:
help("unrecognized option");
break;
Expand Down Expand Up @@ -936,6 +958,12 @@ int main(int argc, char **argv)
printf("CNAME: %s\n", d.cname[i].alloc);
}

#ifdef HAS_SOUND
if (playing) {
pthread_join(sound_thread, NULL);
}
#endif

doh_cleanup(&d);
if (headers != NULL)
curl_slist_free_all(headers);
Expand Down
25 changes: 25 additions & 0 deletions sdl2-util.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash
#set -x
install_SDL2() {
curl --silent -L https://www.libsdl.org/release/SDL2-2.0.10.tar.gz | tar xz -k
cd SDL2-2.0.10
./configure
make
sudo make install
cd ../
}
check_SDL2_installed() {
echo -e "#include<SDL2/SDL.h>\nint main(){}" | gcc -lSDL2 -xc - && echo "yes" || echo "no"
}
if [ "$1" = "--check" ]; then
check_SDL2_installed
elif [ "$1" = "--install" ]; then
if [ "$(check_SDL2_installed)" = "yes" ]; then
echo "SDL2 already installed" >&2
# cleanup
rm a.out
else
echo "SDL2 is not installed, installing..." >&2
install_SDL2
fi
fi
Binary file added sound/doh-sound
Binary file not shown.
77 changes: 77 additions & 0 deletions sound/doh-sound.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* https://gist.github.com/armornick/3447121
*/

#include "doh-sound.h"

static Uint8 *audio_pos; // global pointer to the audio buffer to be played
static Uint32 audio_len; // remaining length of the sample we have to play

// audio callback function
// here you have to copy the data of your audio buffer into the
// requesting audio buffer (stream)
// you should only copy as much as the requested length (len)
void my_audio_callback(void *userdata, Uint8 *stream, int len)
{
(void)userdata;
if (audio_len == 0) {
return;
}

len = ((unsigned)len > audio_len ? (int)audio_len : len);
SDL_memcpy(stream, audio_pos, len);

audio_pos += len;
audio_len -= len;
}

/*
** PLAYING A SOUND IS MUCH MORE COMPLICATED THAN IT SHOULD BE
*/
void *play_sound(void *args)
{
(void)args;
// Initialize SDL.
if (SDL_Init(SDL_INIT_AUDIO) < 0) {
return NULL;
}

// local variables
static Uint32 wav_length; // length of our sample
static Uint8 *wav_buffer; // buffer containing our audio file
static SDL_AudioSpec wav_spec; // the specs of our piece of music

/* Load the WAV */
// the specs, length and buffer of our wav are filled
if (SDL_LoadWAV(DOH_SOUND_FILE, &wav_spec, &wav_buffer, &wav_length) == NULL) {
fprintf(stderr, "Could not load sound file %s\n", DOH_SOUND_FILE);
return NULL;
}

// set the callback function
wav_spec.callback = my_audio_callback;
wav_spec.userdata = NULL;
// set our global static variables
audio_pos = wav_buffer; // copy sound buffer
audio_len = wav_length; // copy file length

/* Open the audio device */
if (SDL_OpenAudio(&wav_spec, NULL) < 0) {
fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
return NULL;
}

/* Start playing */
SDL_PauseAudio(0);

// wait until we're done playing
while (audio_len > 0) {
SDL_Delay(10);
}

// shut everything down
SDL_CloseAudio();
SDL_FreeWAV(wav_buffer);

return NULL;
}
11 changes: 11 additions & 0 deletions sound/doh-sound.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef DOH_SOUND
#define DOH_SOUND

#include <SDL2/SDL.h>

#define DOH_SOUND_FILE "sound/doh-sound.wav"

void my_audio_callback(void *userdata, Uint8 *stream, int len);
void *play_sound(void *args);

#endif /* DOH_SOUND */
Binary file added sound/doh-sound.wav
Binary file not shown.