File: ip_port.c

package info (click to toggle)
libreswan 5.2-2.4
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 81,656 kB
  • sloc: ansic: 129,988; sh: 32,018; xml: 20,646; python: 10,303; makefile: 3,022; javascript: 1,506; sed: 574; yacc: 511; perl: 264; awk: 52
file content (76 lines) | stat: -rw-r--r-- 1,774 bytes parent folder | download | duplicates (2)
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
/* ip port (port), for libreswan
 *
 * Copyright (C) 2020 Andrew Cagney
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Library General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <https://www.gnu.org/licenses/lgpl-2.1.txt>.
 *
 * 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 Library General Public
 * License for more details.
 */


#include <netdb.h>		/* for getservbyname() */
#include <arpa/inet.h>		/* for ntohs() */
#include <stdlib.h>		/* for strtol() */

#include "jambuf.h"
#include "constants.h"		/* for thingeq() */
#include "ip_port.h"

const ip_port unset_port; /* aka all ports? */

ip_port ip_hport(unsigned hport)
{
	ip_port port = { .hport = hport, };
	return port;
}

ip_port ip_nport(unsigned nport)
{
	return ip_hport(ntohs(nport));
}

unsigned hport(const ip_port port)
{
	return port.hport;
}

unsigned nport(const ip_port port)
{
	return htons(port.hport);
}

bool port_is_unset(ip_port port)
{
	return thingeq(port, unset_port);
}

size_t jam_hport(struct jambuf *buf, ip_port port)
{
	return jam(buf, PRI_HPORT, hport(port));

}

size_t jam_nport(struct jambuf *buf, ip_port port)
{
	return jam(buf, PRI_NPORT, pri_nport(port));
}

const char *str_hport(ip_port port, port_buf *buf)
{
	struct jambuf jambuf = ARRAY_AS_JAMBUF(buf->buf);
	jam_hport(&jambuf, port);
	return buf->buf;
}

const char *str_nport(ip_port port, port_buf *buf)
{
	struct jambuf jambuf = ARRAY_AS_JAMBUF(buf->buf);
	jam_nport(&jambuf, port);
	return buf->buf;
}