Skip to content

Commit

Permalink
Getting rid of the incomplete ONVIF implementation, it now lies in it…
Browse files Browse the repository at this point in the history
…s specific branch (onvif)
  • Loading branch information
wberube committed Dec 24, 2024
1 parent c82728c commit 84c58da
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 112 deletions.
14 changes: 0 additions & 14 deletions src/hal/tools.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,18 +203,4 @@ void unescape_uri(char *uri) {
src++;
}
*dst = '\0';
}

void uuid_generate(char *uuid) {
const char *chars = "0123456789abcdef";

int i, j = 0;
for (i = 0; i < 36; i++) {
if (i == 8 || i == 13 || i == 18 || i == 23) {
uuid[i] = '-';
} else {
uuid[i] = chars[rand() % 16];
}
}
uuid[36] = '\0';
}
4 changes: 1 addition & 3 deletions src/hal/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,4 @@ void reverse(void *arr, size_t width);

char *split(char **input, char *sep);

void unescape_uri(char *uri);

void uuid_generate(char *uuid);
void unescape_uri(char *uri);
86 changes: 0 additions & 86 deletions src/network.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "network.h"

pthread_t onvifPid = 0;

struct mdnsd *mdns = {0};

int start_mdns() {
Expand Down Expand Up @@ -48,88 +46,4 @@ void stop_mdns() {
if (mdns)
mdnsd_stop(mdns);
mdns = NULL;
}

void *onvif_thread(void) {
struct ifaddrs *ifaddr, *ifa;
char ipaddr[INET_ADDRSTRLEN];
char msgbuf[4096];
int servfd, msglen;
struct sockaddr_in servaddr, clntaddr;
socklen_t clntsz;

if (getifaddrs(&ifaddr) == -1) {
HAL_DANGER("onvif", "Failed to get network interfaces!\n");
return (void*)EXIT_FAILURE;
}

for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
if (!ifa->ifa_addr || ifa->ifa_addr->sa_family != AF_INET) continue;
if (ifa->ifa_flags & IFF_LOOPBACK) continue;
struct sockaddr_in *addr = (struct sockaddr_in *)ifa->ifa_addr;
inet_ntop(AF_INET, &addr->sin_addr.s_addr, ipaddr, sizeof(ipaddr));
break;
}

if ((servfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
HAL_DANGER("onvif", "Failed to create socket!\n");
return (void*)EXIT_FAILURE;
}

memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(3702);

if (bind(servfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) {
HAL_DANGER("onvif", "Failed to bind socket!\n");
return (void*)EXIT_FAILURE;
}

struct ip_mreq group;
group.imr_multiaddr.s_addr = inet_addr("239.255.255.250");
group.imr_interface.s_addr = inet_addr(ipaddr);
if (setsockopt(servfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group, sizeof(group)) < 0){
close(servfd);
return (void*)EXIT_FAILURE;
}

while (keepRunning) {
clntsz = sizeof(clntaddr);
if ((msglen = recvfrom(servfd, msgbuf, sizeof(msgbuf), 0, (struct sockaddr *)&clntaddr, &clntsz)) < 0)
continue;

msgbuf[msglen] = '\0';
#ifdef DEBUG_ONVIF
HAL_INFO("onvif", "Received message: %s\n", msgbuf);
#endif

if (!strstr(msgbuf, "http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe"))
continue;

char uuid[37], response[2048] = {0};
uuid_generate(uuid);
sendto(servfd, response, strlen(response), 0, (struct sockaddr *)&clntaddr, clntsz);
}

close(servfd);
return (void*)EXIT_SUCCESS;
}

int start_onvif_server() {
pthread_attr_t thread_attr;
pthread_attr_init(&thread_attr);
size_t stacksize;
pthread_attr_getstacksize(&thread_attr, &stacksize);
size_t new_stacksize = 16 * 1024;
if (pthread_attr_setstacksize(&thread_attr, new_stacksize))
HAL_DANGER("onvif", "Can't set stack size %zu\n", new_stacksize);
pthread_create(&onvifPid, &thread_attr, (void *(*)(void *))onvif_thread, NULL);
if (pthread_attr_setstacksize(&thread_attr, stacksize))
HAL_DANGER("onvif", "Can't set stack size %zu\n", stacksize);
pthread_attr_destroy(&thread_attr);
}

void stop_onvif_server() {
pthread_join(onvifPid, NULL);
}
10 changes: 1 addition & 9 deletions src/network.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#pragma once

#include <ifaddrs.h>
#include <netdb.h>
#include <net/if.h>
#include <netinet/in.h>
#include <pthread.h>
#include <stdio.h>
#include <sys/utsname.h>

Expand All @@ -16,9 +13,4 @@ extern int asprintf(char **restrict strp, const char *restrict fmt, ...);
#include "lib/tinysvcmdns.h"

int start_mdns();
void stop_mdns();

void *onvif_thread();

int start_onvif_server();
void stop_onvif_server();
void stop_mdns();

0 comments on commit 84c58da

Please sign in to comment.