File: strtoip.c

package info (click to toggle)
tlswrapper 0~20251001-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,804 kB
  • sloc: ansic: 7,191; sh: 2,367; makefile: 246
file content (26 lines) | stat: -rw-r--r-- 560 bytes parent folder | download | duplicates (3)
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
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include "strtoip.h"

int strtoip4(unsigned char *ip, const char *x) {

    if (!x) return 0;
    if (inet_pton(AF_INET, x, ip + 12) != 1) return 0;
    memcpy(ip, "\0\0\0\0\0\0\0\0\0\0\377\377", 12);
    return 1;
}

int strtoip6(unsigned char *ip, const char *x) {

    if (!x) return 0;
    if (inet_pton(AF_INET6, x, ip) != 1) return 0;
    return 1;
}

int strtoip(unsigned char *ip, const char *x) {

    if (strtoip4(ip, x)) return 1;
    if (strtoip6(ip, x)) return 1;
    return 0;
}