20 Commits

Author SHA1 Message Date
Stefan Eßer
5df410a53d Do not remove kevent from closed request sockwt.
After a successful transfer pass EV_FLAG_CLOSING to the kevent delete
function since the corresponding socket has been been closed and there
is no kevent request record to delete.

This prevents spurious error messages:

    kqueue.c:210: error: kevent() error 9 on 2391 filter:-1 flags:0x4000
2023-05-31 00:02:12 -07:00
Peter Urbanec
2c66335a9b upnpevents: Fix leaked sockets
Commit f9a78d598e48132a8c6cf9ce31b51163b6bd2f67 refactored the code, but
in the process introduced a socket leak. When connect() is called on a
socket set to non-blocking mode, the returned error code is EINPROGRESS.
In that case, the code never initialises the ev structure and the socket
reference is lost. Given enough time (on my network about a day and a half)
this will eventually lead to a non-responsive server because the process
runs out of fds. Netstat will show an excessive number of sockets stuck in
CLOSE_WAIT state forever and ls /proc/<pid>/fd will confirm a lot of open
sockets.

Initialising the ev struct before a call to connect() ensures that the
socket reference is not lost. Verifying with netstat, one can see the
sockets in TIME_WAIT state for a brief period of time and /proc/<pid>/fd
shows a reasonable number of sockets being open.
2022-02-11 00:30:04 -08:00
Gleb Smirnoff
7ba9e52fc8 Add kqueue event module. Code inspired by nginx. There are a lot of room for optimization here, this is just first working version. 2018-01-16 16:55:23 -08:00
Gleb Smirnoff
f9a78d598e Support for pluggable event modules, stage 1. Take out select() loop and associated code into a separate module select.c. Detailed list of changes down below.
The idea is taken from the nginx web server, but much simplified and
almost no copypaste left. This will allow minidlna to use different
event dispatcher APIs, which would be defined at compile time.

My personal goal is to convert minidlna to kqueue(2) on FreeBSD. This
would later allow for kqueue based directory change notification, which
won't conflict with select(2) like the current patch does.
Other platforms will also benefit from the pluggability of the event
system, Linux can switch to epoll(2) or at least to poll(2).

Detailed list of changes:

* event.h [New]
  Our internal API to unify different event dispatch systems.
* select.c [New]
  Much simplified version of nginx's ngx_select_module.c.
* minidlna.c
  - Split out listen socket event processing into separate function
    ProcessListen(), which matches event_process_t type.
  - Create and initialize struct event for the monitor socket, SSDP
    socket, HTTP socket and beacon socket.
  - Simplify and make more precise timeout calculation using
    helper timeval functions from utils.c. Treat gettimeofday() error
    as a fatal event.
  - Rip out all stuff related to select(2). Just call event_module.process().
* upnpevents.c
  - Embed struct event into upnp_event_notify.
  - Merge upnp_event_create_notify() with upnp_event_notify_connect().
    Start connecting immediately after socket creation. Garbage collect
    now useless ECreated state.
  - Make upnp_event_process_notify() of event_process_t type, and use it
    as process callback for upnp_event_notify event.
  - Looks like we always create upnp_event_notify with existing subscriber,
    and never clear it later. Remove checks for obj->sub and assert that it
    is never NULL. Simplifies things.
  - When switching obj state, add/del it to event dispatcher accrodingly.
  - Garbage collect upnpevents_selectfds().
  - Garbage collect select(2) related stuff from upnpevents_processfds().
    Rename function to upnpevents_gc(), since the remaining functionality
    is garbage collecting, not file descriptor processing.
    Actually, this can be simplified even more. We can safely close sockets
    and free objects immediately, eliminating need for upnpevents_gc(). But
    this change would be beyond scope of this commit.
* upnphttp.c, upnphttp.h
  Embed struct event into struct upnphttp. Adjust Process_upnphttp() to match
  event_process_t type. Add/del to event dispatcher once creating/closing a
  socket.
* minissdp.c, minissdp.h
  Make ProcessSSDPRequest() of event_process_t type.
* getifaddr.c, getifaddr.h
  Make ProcessMonitorEvent() of event_process_t type.
2018-01-16 16:53:08 -08:00
Justin Maggard
37346fb0a0 events: Limit subscriber count
Protect against DoS by limiting the number of subscribers we allow to a
total of 500, and immediately removing subscribers from our list if we
encounter an error communicating with them.
2017-08-22 12:20:38 -07:00
Justin Maggard
d492b43ef8 Fix various potential illegal access and memory leaks in error conditions. 2014-04-07 11:20:19 -07:00
Justin Maggard
61fbce18ba * Replace sqlite_int64 with int64_t in many places, to better allow for the possibility of other databases. (Thanks Douglas Carmichael) 2012-06-29 23:14:27 +00:00
Justin Maggard
6fcd16985e * Miscellaneous minor cleanups for string termination and unfreed memory at exit. 2012-02-13 22:48:48 +00:00
Justin Maggard
08172a4ed2 * Enhance error checking in some additional places as required by the latest UPnP CTT. 2012-01-21 01:00:26 +00:00
Justin Maggard
773e1f6566 * Add support for other operating systems (kFreeBSD, FreeBSD, and OSX for now).
* Switch to autoconf from genconfig.sh.
2011-09-16 23:39:58 +00:00
Justin Maggard
8e0fcd56f9 * Complete/clarify license information. 2010-11-11 23:48:14 +00:00
Justin Maggard
25861ebf92 * Use our own time-based UUID generation functions, to help eliminate potential compiling issues. 2009-09-01 02:54:20 +00:00
Justin Maggard
c10c913b30 * Some fixes for the UPnP Device Validator. 2009-05-19 23:43:51 +00:00
Justin Maggard
bdf2c86075 * Sort out Server description in HTTP headers. This actually makes Windows Media Player recognize a MiniDLNA server also. 2009-05-10 03:24:29 +00:00
Justin Maggard
3ae378cdc9 * Treat an empty Filter argument as "*".
* Make sure we allocate enough memory to hold SourceProtocolInfo response data.
2009-03-09 20:53:21 +00:00
Justin Maggard
2da2f6d4a1 * Add some preliminary code for the eventual possibility of TiVo support. 2009-02-23 23:10:27 +00:00
Justin Maggard
45f294b404 * Use our own logging mechanism instead of syslog/printf. 2009-02-20 10:21:23 +00:00
Justin Maggard
7e30949498 * Code cleanup. 2009-01-31 00:25:03 +00:00
Justin Maggard
74d73037d0 Lots of changes, but notably:
* MiniDLNA can now pass the DLNA Conformance Test!
 * Dependence on libdlna has been removed, and the ffmpeg libs are used directly.
 * Lots of unused code has been cleaned up.
 * File transfers will now be forked off into a new process, so as not to tie up the server when sending data.
2009-01-22 00:25:20 +00:00
Justin Maggard
f557f8ea77 Initial checkin 2008-10-23 17:30:45 +00:00