summaryrefslogtreecommitdiff
path: root/validate.c
blob: 49005f007a2d2a59f77d3ac9cff062f7ea7f40da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//
// Created by Keuin on 2021/12/30.
//

#include "validate.h"
#include <stdio.h>

/**
 * Check if a given string is a valid dot-decimal representation of an IPv4 address.
 * @param s the string.
 * @return Non-zero if true, zero if false.
 */
int is_valid_ipv4(const char *s) {
    // TODO buggy
//    unsigned int addr[4];
//    if (sscanf(s, "%ud.%ud.%ud.%ud", &addr[0], &addr[1], &addr[2], &addr[3]) != 4)
//        return 0;
//    for (int i = 0; i < 4; ++i) {
//        if (addr[i] > 255) return 0;
//        if (addr[i] == 0 && (i == 0 || i == 3)) return 0;
//    }
    return 1;
}