aboutsummaryrefslogtreecommitdiff
path: root/ezlive_config.c
blob: 08347d8574d6cdad27dd76ddf99c9eae8e79f9e2 (plain)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
#include "ezlive_config.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

EZLiveConfig *ezlive_config;

static char *trim(char *s) {
    char *end;
    while (isspace((unsigned char)*s)) s++;
    if (*s == 0) return s;
    end = s + strlen(s) - 1;
    while (end > s && isspace((unsigned char)*end)) end--;
    end[1] = '\0';
    return s;
}

void EZLiveConfig_init(EZLiveConfig *self) {
    if (!self) return;
    self->listening_addr = strdup("127.0.0.1");
    self->listening_port = 1935;
    self->bucket = NULL;
    self->endpoint = NULL;
    self->s3_path = NULL;
    self->access_key = NULL;
    self->secret_key = NULL;
    self->web_endpoint = NULL;
    self->region = strdup("auto");
}

static void set_field(const char **field, const char *value) {
    if (*field) {
        free((void *)*field);
    }
    *field = strdup(value);
}

void EZLiveConfig_load(EZLiveConfig *self, const char *filename) {
    if (!self || !filename) return;

    FILE *fp = fopen(filename, "r");
    if (!fp) {
        perror("fopen");
        return;
    }

    char line[1024];
    while (fgets(line, sizeof(line), fp)) {
        line[strcspn(line, "\n")] = 0;
        char *hash = strchr(line, '#');
        if (hash) *hash = 0;

        char *trimmed = trim(line);
        if (*trimmed == 0) continue;

        char *eq = strchr(trimmed, '=');
        if (!eq) continue;

        *eq = 0;
        char *key = trim(trimmed);
        char *val = trim(eq + 1);

        if (strcmp(key, "listening_addr") == 0) {
            set_field(&self->listening_addr, val);
        } else if (strcmp(key, "listening_port") == 0) {
            self->listening_port = atoi(val);
        } else if (strcmp(key, "bucket") == 0) {
            set_field(&self->bucket, val);
        } else if (strcmp(key, "endpoint") == 0) {
            set_field(&self->endpoint, val);
        } else if (strcmp(key, "s3_path") == 0) {
            set_field(&self->s3_path, val);
        } else if (strcmp(key, "access_key") == 0) {
            set_field(&self->access_key, val);
        } else if (strcmp(key, "secret_key") == 0) {
            set_field(&self->secret_key, val);
        } else if (strcmp(key, "web_endpoint") == 0) {
            set_field(&self->web_endpoint, val);
        } else if (strcmp(key, "region") == 0) {
            set_field(&self->region, val);
        }
    }

    fclose(fp);
}

int EZLiveConfig_validate(EZLiveConfig *self) {
    if (!self) return -1;
    if (!self->listening_addr || strlen(self->listening_addr) == 0) return -2;
    if (!self->bucket || strlen(self->bucket) == 0) return -3;
    if (!self->endpoint || strlen(self->endpoint) == 0) return -4;
    if (!self->s3_path || strlen(self->s3_path) == 0) return -5;
    if (self->s3_path[strlen(self->s3_path) - 1] != '/') {
        fprintf(stderr, "invalid s3 path. path should end with '\'.\n");
        return -10;
    }
    if (!self->access_key || strlen(self->access_key) == 0) return -6;
    if (!self->secret_key || strlen(self->secret_key) == 0) return -7;
    if (self->listening_port <= 0 || self->listening_port > 65535) return -8;
    if (!self->region || strlen(self->region) == 0) return -9;
    return 0;
}