forked from intel/net-test-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libslipcat.c
72 lines (56 loc) · 1.44 KB
/
libslipcat.c
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
/*
* Copyright © 2019, Intel Corporation.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU Lesser General Public License,
* version 2.1, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
*/
#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "slipcat.h"
static int opt_debug = 1;
/*
Input:
- ipv4:port
- ipv4
- :port
*/
struct sockaddr_in *s_in_new(const char *s)
{
struct sockaddr_in *s_in = calloc(1, sizeof(*s_in));
int r, addr_len = strlen(s);
const char *addr, *sp = strchr(s, ':');
if (sp) {
int port = atoi(sp + 1);
if (port > 0 && port <= 65535) {
s_in->sin_port = htons(port);
}
addr_len = sp - s;
}
if (addr_len) {
addr = (const char *) strndupa(s, addr_len);
r = inet_pton(AF_INET, addr, &s_in->sin_addr);
if (r <= 0) {
if (r == 0)
fprintf(stderr, "Not in presentation format");
else
perror("inet_pton");
exit(EXIT_FAILURE);
}
}
D("in: '%s', out: %s:%hu", s,
inet_ntoa(s_in->sin_addr), ntohs(s_in->sin_port));
s_in->sin_family = AF_INET;
return s_in;
}