Skip to content

Commit

Permalink
Remove BSD dependencies (replace fgetln with getline).
Browse files Browse the repository at this point in the history
Remove use of deprecated declarations and functions (_BSD_SOURCE, mbedtls_sha1)
Fix shoutcast buffer free error
  • Loading branch information
thess committed Aug 27, 2018
1 parent 59d3471 commit 852b3ef
Show file tree
Hide file tree
Showing 23 changed files with 52 additions and 194 deletions.
11 changes: 1 addition & 10 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

AC_PREREQ([2.69])
AC_INIT([pianod],
174.08-sc,
174.09-sc,
[[email protected]],
,[http://deviousfish.com/pianod])
AM_INIT_AUTOMAKE([foreign -Wall -Werror])
Expand Down Expand Up @@ -108,15 +108,6 @@ AM_CONDITIONAL([ENABLE_SHOUT],[test "x$HAS_SHOUT" = "x1"])
AC_SEARCH_LIBS([crypt],[crypt], [],
[AC_MSG_ERROR([Cannot find required library: crypt (DES password encryption).])])

# Fgetln for Football:
# May be in standard libraries, in -lbsd on Linux, or not exist at all.
AC_SEARCH_LIBS([fgetln],[bsd],
[AC_DEFINE([HAVE_FGETLN],[1],[Set to 1 if fgetln is available])])
AC_SEARCH_LIBS([getprogname],[bsd],
[AC_DEFINE([HAVE_GETPROGNAME],[1],[Set to 1 if getprogname/setprogname is available])])

AM_CONDITIONAL([HAVE_FGETLN],[test "$ac_cv_search_fgetln" = 'none required'])

# IPv6 support
AC_MSG_CHECKING([PF_INET6/IPv6 support])
AC_TRY_COMPILE([#include <sys/socket.h>],
Expand Down
4 changes: 0 additions & 4 deletions pianod-sc.cbp
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,6 @@
<Unit filename="src/libfootball/fb_utility.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="src/libfootball/fgetln.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="src/libfootball/fgetln.h" />
<Unit filename="src/libfootball/sha1.c">
<Option compilerVar="CC" />
</Unit>
Expand Down
2 changes: 1 addition & 1 deletion src/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#ifndef __FreeBSD__
#define _POSIX_C_SOURCE 1 /* fileno() */
#define _BSD_SOURCE /* strdup() */
#define _DEFAULT_SOURCE /* strdup() */
#define _DARWIN_C_SOURCE /* strdup() on OS X */
#endif

Expand Down
9 changes: 1 addition & 8 deletions src/libfootball/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
## Process this file with automake to produce Makefile.in

if HAVE_FGETLN
fgetln_files =
else
fgetln_files = fgetln.c fgetln.h
endif

if ENABLE_DEBUG
libfootball_a_CFLAGS = -g
endif
Expand All @@ -15,6 +9,5 @@ noinst_LIBRARIES = libfootball.a
libfootball_a_CPPFLAGS = -Iinclude -I../.. -D_GNU_SOURCE
libfootball_a_SOURCES = fb_event.c fb_parser.c fb_service.c fb_socketmgr.c \
fb_http.c fb_message.c fb_utility.c sha1.c \
fb_public.h fb_service.h sha1.h \
$(fgetln_files)
fb_public.h fb_service.h sha1.h

46 changes: 26 additions & 20 deletions src/libfootball/fb_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <config.h>

#ifndef __FreeBSD__
#define _BSD_SOURCE /* snprintf() */
#define _DEFAULT_SOURCE /* snprintf() */
#endif
#if !defined(__FreeBSD__) && !defined(__APPLE__)
#define _POSIX_C_SOURCE 1 /* required by getaddrinfo() */
Expand All @@ -31,10 +31,6 @@

#include "fb_service.h"

#ifndef HAVE_FGETLN
#include "fgetln.h"
#endif

#ifndef HAVE_MSG_NOSIGNAL
#define MSG_NOSIGNAL (0)
#ifndef HAVE_SO_NOSIGPIPE
Expand Down Expand Up @@ -369,7 +365,7 @@ static char *fb_get_line_bytes (FB_CONNECTION *connection, size_t *length) {
assert (connection->in.size <= connection->in.capacity);
if (connection->in.size == connection->in.capacity) {
if (!fb_set_input_buffer_size (connection, connection->in.capacity * 2 + 64)) {
return false;
return NULL;
}
}
ok = fb_recv_input (connection, 1);
Expand All @@ -390,45 +386,55 @@ static char *fb_get_line_bytes (FB_CONNECTION *connection, size_t *length) {
@param connection the connection from which input is read
@return An FB_EVENT_INPUT event, or input is incomplete.
*/
/* Global buffer for getline() */
static char *buffer = NULL;
static size_t bufsize = 0;

FB_EVENT *fb_read_line_input (FB_EVENT *event, FB_CONNECTION *connection) {
assert (connection);
assert (event);
char *line;
char *buffer = NULL;
size_t buffilled = 0;
size_t lbufsize;
char *evbuffer;
/* If we got here, socket_data will be for a connection */
event->type = FB_EVENT_INPUT;
if (connection->file) {
/* Reading from file connection */
line = fgetln (connection->file, &buffilled);
if (!line || buffilled == 0) {
lbufsize = getline(&buffer, &bufsize, connection->file);
if ((lbufsize == -1) || (buffer == NULL)) {
/* EOF/Connection closed from other end. Initiate closure. */
if (buffer) {
free(buffer);
buffer = NULL;
}
fb_close_connection (connection);
return NULL;
}
line = buffer;
} else {
line = fb_get_line_bytes (connection, &buffilled);
line = fb_get_line_bytes (connection, &lbufsize);
if (!line) {
/* Haven't got a full line yet, or error. */
return NULL;
}
}

/* Make a copy of the line */
buffer = malloc (buffilled + 1);
if (!buffer) {
fb_perror ("malloc");
/* Make a copy of the line for event queue */
evbuffer = malloc (lbufsize + 1);
if (!evbuffer) {
fb_perror ("malloc");
return NULL;
}
memcpy (buffer, line, buffilled);
memcpy (evbuffer, line, lbufsize);
/* strip returns off the end of the unparsed line */
while (buffilled > 0 && (buffer [buffilled - 1] == '\r' || buffer [buffilled - 1] == '\n')) {
buffilled--;
while (lbufsize > 0 && (evbuffer [lbufsize - 1] == '\r' || evbuffer [lbufsize - 1] == '\n')) {
lbufsize--;
}
buffer [buffilled] = '\0';
/* NULL termination always */
evbuffer [lbufsize] = '\0';

/* Store the command and parse it into an argv array */
event->command = buffer;
event->command = evbuffer;
event->argc = fb_create_argv(event->command, &event->argv, &event->argr);
if (event->argc < 0) {
event->argc = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/libfootball/fb_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Sec-WebSocket-Version: 13

#ifndef __FreeBSD__
#define _POSIX_C_SOURCE 1 /* fileno() */
#define _BSD_SOURCE /* strdup() */
#define _DEFAULT_SOURCE /* strdup() */
#define _DARWIN_C_SOURCE /* strdup() on OS X */
#endif
#ifdef __linux__
Expand Down
2 changes: 1 addition & 1 deletion src/libfootball/fb_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
#include <config.h>

#ifndef __FreeBSD__
#define _BSD_SOURCE /* snprintf() */
#define _DEFAULT_SOURCE /* snprintf() */
#endif

#include <stdio.h>
Expand Down
2 changes: 1 addition & 1 deletion src/libfootball/fb_service.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#define _POSIX_C_SOURCE 1 /* fileno,fdopen() */
#endif
#ifndef __FreeBSD__
#define _BSD_SOURCE /* strdup */
#define _DEFAULT_SOURCE /* strdup */
#endif

#include <stdio.h>
Expand Down
2 changes: 1 addition & 1 deletion src/libfootball/fb_socketmgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#ifndef __FreeBSD__
#define _POSIX_C_SOURCE 1 /* fileno() */
#define _BSD_SOURCE /* strdup() */
#define _DEFAULT_SOURCE /* strdup() */
#define _DARWIN_C_SOURCE /* strdup() on OS X */
#endif
#ifdef __linux__
Expand Down
82 changes: 0 additions & 82 deletions src/libfootball/fgetln.c

This file was deleted.

37 changes: 0 additions & 37 deletions src/libfootball/fgetln.h

This file was deleted.

2 changes: 1 addition & 1 deletion src/libpiano/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ if HAVE_JSON_H
have_json_h = -DHAVE_JSON_H=1
endif

libpiano_a_CPPFLAGS = $(json_CFLAGS) -I../include -DGCRYPT_NO_DEPRECATED $(request_assert) $(have_json_json_h) $(have_json_c_json_h) $(have_json_h)
libpiano_a_CPPFLAGS = $(json_CFLAGS) -D_GNU_SOURCE -I../include -DGCRYPT_NO_DEPRECATED $(request_assert) $(have_json_json_h) $(have_json_c_json_h) $(have_json_h)
libpiano_a_SOURCES = crypt.c piano.c request.c response.c list.c \
config.h piano.h crypt.h piano_private.h

Expand Down
2 changes: 1 addition & 1 deletion src/libpiano/piano.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ THE SOFTWARE.
*/

#ifndef __FreeBSD__
#define _BSD_SOURCE /* required by strdup() */
#define _DEFAULT_SOURCE /* required by strdup() */
#define _DARWIN_C_SOURCE /* strdup() on OS X */
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/libpiano/request.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ THE SOFTWARE.
*/

#ifndef __FreeBSD__
#define _BSD_SOURCE /* required by strdup() */
#define _DEFAULT_SOURCE /* required by strdup() */
#define _DARWIN_C_SOURCE /* strdup() on OS X */
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/libpiano/response.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ THE SOFTWARE.
*/

#ifndef __FreeBSD__
#define _BSD_SOURCE /* required by strdup() */
#define _DEFAULT_SOURCE /* required by strdup() */
#define _DARWIN_C_SOURCE /* strdup() on OS X */
#endif

Expand Down
7 changes: 6 additions & 1 deletion src/libwaitress/waitress.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ THE SOFTWARE.

#ifndef __FreeBSD__
#define _POSIX_C_SOURCE 1 /* required by getaddrinfo() */
#define _BSD_SOURCE /* snprintf() */
#define _DEFAULT_SOURCE /* snprintf() */
#define _DARWIN_C_SOURCE /* snprintf() on OS X */
#endif

Expand All @@ -50,6 +50,7 @@ THE SOFTWARE.
#include <mbedtls/entropy.h>
#include <mbedtls/ctr_drbg.h>
#include <mbedtls/x509.h>
#include <mbedtls/version.h>
#include <mbedtls/sha1.h>

struct _mbedtls_ctx
Expand Down Expand Up @@ -795,7 +796,11 @@ static WaitressReturn_t WaitressTlsVerify (const WaitressHandle_t *waith) {
return WAITRESS_RET_TLS_HANDSHAKE_ERR;
}

#if MBEDTLS_VERSION_NUMBER >= 0x02070000
mbedtls_sha1_ret (cert->raw.p, cert->raw.len, fingerprint);
#else
mbedtls_sha1 (cert->raw.p, cert->raw.len, fingerprint);
#endif

if (memcmp (fingerprint, waith->tlsFingerprint, sizeof (fingerprint)) != 0) {
return WAITRESS_RET_TLS_FINGERPRINT_MISMATCH;
Expand Down
Loading

0 comments on commit 852b3ef

Please sign in to comment.