libndi_discovery_microdns.c 3.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
libndi
Copyright (C) 2020 VideoLAN

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

/**
 * Service discovery implementation using libmicrodns
 */
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>

#include <microdns/microdns.h>
#include "libndi.h"

struct ndi_discovery_ctx {
33
    ndi_discovery_cb cb;
34
35
36
37
    struct mdns_ctx *mctx;
    pthread_t thread;
    pthread_mutex_t mutex;
    bool stopped;
38
    void *user_data;
39
40
};

41
ndi_discovery_ctx_t *libndi_discovery_init(ndi_discovery_cb callback, void *user_data)
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
{
    ndi_discovery_ctx_t *ctx = calloc(1, sizeof(ndi_discovery_ctx_t));
    if (!ctx)
        return NULL;

    if (pthread_mutex_init(&ctx->mutex, NULL) != 0) {
        free(ctx);
        return NULL;
    }

    int ret = mdns_init(&ctx->mctx, MDNS_ADDR_IPV4, MDNS_PORT);

    if (ret != 0) {
        pthread_mutex_destroy(&ctx->mutex);
        free(ctx);
        return NULL;
    }

    ctx->cb = callback;
61
    ctx->user_data = user_data;
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
    ctx->stopped = true;

    return ctx;
}

static bool cb_mdns_should_stop(void *p_cookie)
{
    ndi_discovery_ctx_t *ctx = p_cookie;
    pthread_mutex_lock(&ctx->mutex);
    bool stop = ctx->stopped;
    pthread_mutex_unlock(&ctx->mutex);
    return stop;
}

static void cb_mdns(void *p_cookie, int status, const struct rr_entry *entries)
{
    ndi_discovery_ctx_t *ctx = p_cookie;
    if (status < 0) {
        return;
    }

    ndi_discovery_item_t *item = calloc(1, sizeof(ndi_discovery_item_t));
    if (item == NULL) {
        return;
    }

    for (const struct rr_entry *e = entries; e != NULL; e = e->next) {
        switch (e->type) {
            case RR_A: {
                item->name = strdup(e->name);
                item->ip = strdup(e->data.A.addr_str);
                break;
            }
            case RR_SRV: {
                char *portstr = malloc(6);
                snprintf(portstr, 6, "%u", e->data.SRV.port);
                item->port = portstr;
                break;
            }
            default:
                // Skip entry types we do not handle
                continue;
        }
    }

107
    ctx->cb(item, ctx->user_data);
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
}

void *discovery_thread_func(void *data) {
    ndi_discovery_ctx_t *ctx = data;

    static const char *names = "_ndi._tcp.local";
    mdns_listen(ctx->mctx, &names, 1, RR_PTR,
                5, cb_mdns_should_stop, cb_mdns, ctx);

    return NULL;
}

int libndi_discovery_start(ndi_discovery_ctx_t *ctx)
{
    assert(ctx != NULL);

    pthread_mutex_lock(&ctx->mutex);
    bool stop = ctx->stopped;
    ctx->stopped = false;
    pthread_mutex_unlock(&ctx->mutex);

    // Check if already running
    if (!stop)
        return -1;

    if (pthread_create(&ctx->thread, NULL, discovery_thread_func, ctx) != 0)
        return -1;
    return 0;
}

int libndi_discovery_stop(ndi_discovery_ctx_t *ctx)
{
    assert(ctx != NULL);

    pthread_mutex_lock(&ctx->mutex);
    bool stop = ctx->stopped;
    ctx->stopped = true;
    pthread_mutex_unlock(&ctx->mutex);

    // Check if we are already stopped
    if (stop)
        return -1;

    pthread_join(ctx->thread, NULL);
    return 0;
}

void libndi_discovery_destroy(ndi_discovery_ctx_t *ctx)
{
    libndi_discovery_stop(ctx);
    mdns_destroy(ctx->mctx);
    pthread_mutex_destroy(&ctx->mutex);
    free(ctx);
}