mptsd reads mpegts streams from udp/multicast or http and combines them into one multiple program stream that is suitable for outputting to DVB-C modulator. Tested with Dektec DTE-3114 Quad QAM Modulator and used in production in small DVB-C networks. https://georgi.unixsol.org/programs/mptsd/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

web_server.c 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <stdlib.h>
  2. #include <regex.h>
  3. #include <errno.h>
  4. #include <string.h>
  5. #include <signal.h>
  6. #include <arpa/inet.h>
  7. #include <netinet/in.h>
  8. #include "libfuncs/libfuncs.h"
  9. #include "web_pages.h"
  10. #include "web_server.h"
  11. typedef struct req_info {
  12. int clientsock;
  13. struct sockaddr_in client;
  14. } request_info;
  15. extern int keep_going;
  16. #define NEXT_CLIENT { FREE(path); FREE(buf); pthread_exit(0); }
  17. #define SHUTDOWN_CLIENT { FREE(path); FREE(buf); shutdown_fd(&clientsock); pthread_exit(0); }
  18. #define BUF_SIZE 1024
  19. void *process_web_request(void *);
  20. void *web_server_thread(void *data) {
  21. CONFIG *conf = data;
  22. while (keep_going) {
  23. struct sockaddr_in client;
  24. unsigned int clientlen = sizeof(client);
  25. int clientsock;
  26. clientsock = accept(conf->server_socket, (struct sockaddr *) &client, &clientlen);
  27. if (clientsock < 0) {
  28. if (conf->server_socket > -1) // The server_socket is closed on exit, so do not report errors
  29. LOGf("ERROR : Failed to accept client fd: %i err: %s\n", clientsock, strerror(errno));
  30. if (errno==EMFILE || errno==ENFILE) /* No more FDs */
  31. break;
  32. } else {
  33. request_info *req;
  34. pthread_t req_thread;
  35. req = malloc(sizeof(request_info));
  36. if (!req) {
  37. log_perror("Can't allocate request_info", errno);
  38. continue;
  39. }
  40. req->clientsock = clientsock;
  41. req->client = client;
  42. if (pthread_create(&req_thread, NULL, (void *)&process_web_request, (void *)req)) {
  43. log_perror("Error creating request processing thread.", errno);
  44. exit(1);
  45. }
  46. pthread_detach(req_thread);
  47. }
  48. }
  49. pthread_exit(0);
  50. }
  51. void web_server_start(CONFIG *conf) {
  52. if (conf->server_socket > -1)
  53. pthread_create(&conf->server_thread, NULL, &web_server_thread, conf);
  54. }
  55. void web_server_stop(CONFIG *conf) {
  56. if (conf->server_socket > -1) {
  57. shutdown_fd(&conf->server_socket);
  58. pthread_join(conf->server_thread, NULL);
  59. }
  60. }
  61. void *process_web_request(void *in_req) {
  62. request_info *req = (request_info *)in_req;
  63. int clientsock = req->clientsock;
  64. regmatch_t res[3];
  65. char *path=NULL, *buf=NULL;
  66. FREE(req);
  67. signal(SIGPIPE, SIG_IGN);
  68. if (!keep_going)
  69. pthread_exit(0);
  70. buf = malloc(BUF_SIZE);
  71. if (!buf) {
  72. log_perror("Can't allocate buffer", errno);
  73. SHUTDOWN_CLIENT;
  74. }
  75. if (fdgetline(clientsock,buf,BUF_SIZE)<=0) {
  76. SHUTDOWN_CLIENT;
  77. }
  78. regex_t request_get;
  79. regcomp(&request_get, "^GET /([^ ]*) HTTP/1.*$", REG_EXTENDED);
  80. if (regexec(&request_get,buf,2,res,0)==REG_NOMATCH) {
  81. send_501_not_implemented(clientsock);
  82. SHUTDOWN_CLIENT;
  83. }
  84. buf[res[1].rm_eo]=0;
  85. chomp(buf+res[1].rm_so);
  86. if (buf[res[1].rm_eo-1]=='/') buf[res[1].rm_eo-1]=0;
  87. path = strdup(buf+res[1].rm_so);
  88. regfree(&request_get);
  89. while (fdgetline(clientsock,buf,BUF_SIZE) > 0) {
  90. if (buf[0] == '\n' || buf[0] == '\r') // End of headers
  91. break;
  92. }
  93. if (strlen(path) == 0) {
  94. cmd_index(clientsock);
  95. } else if (strstr(path,"reconnect")==path) {
  96. cmd_reconnect(clientsock);
  97. } else {
  98. send_404_not_found(clientsock);
  99. }
  100. SHUTDOWN_CLIENT;
  101. }