File: socket.c

package info (click to toggle)
pcaputils 0.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 372 kB
  • sloc: ansic: 2,945; sh: 44; makefile: 38
file content (183 lines) | stat: -rw-r--r-- 4,577 bytes parent folder | download | duplicates (4)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <stdbool.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>

#include "byte.h"
#include "socket.h"
#include "uint.h"

/* from libowfat */

/*
 Copyright (c) 2001 Felix von Leitner.
 All rights reserved.

 This program is free software; you can redistribute it and/or modify it
 under the terms of the GNU General Public License Version 2 as published
 by the Free Software Foundation.
*/

const char V4any[4]={0,0,0,0};
const char V4loopback[4]={127,0,0,1};
const char V4mappedprefix[12]={0,0,0,0,0,0,0,0,0,0,0xff,0xff};
const char V6loopback[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
const char V6any[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

int socket_tcp6(void){
	int s;

	s = socket(PF_INET6, SOCK_STREAM, 0);
	if(s == -1 && errno == EINVAL)
		s = socket(PF_INET, SOCK_STREAM, 0);
	return s;
}

int socket_tcp(void){
	int s;

	s = socket(PF_INET, SOCK_STREAM, 0);
	if (s == -1) return -1;
	return s;
}

int socket_bind6(int s, const char ip[16], u16 port, u32 scope_id){
	struct sockaddr_in6 sa;
	
	byte_zero(&sa, sizeof sa);
	sa.sin6_family = AF_INET6;
	u16_pack_big((char *) &sa.sin6_port, port);
	/* implicit: sa.sin6_flowinfo = 0; */
	byte_copy((char *) &sa.sin6_addr, 16, ip);
	sa.sin6_scope_id = scope_id;

	return bind(s, (struct sockaddr *) &sa, sizeof sa);
}

int socket_bind6_reuse(int s, const char ip[16], u16 port, u32 scope_id){
	int opt = 1;
	setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt);
	return socket_bind6(s, ip, port, scope_id);
}

int socket_listen(int s, unsigned backlog){
	return listen(s, backlog);
}

int socket_accept6(int s, char ip[16], u16 *port, u32 *scope_id){
	struct sockaddr_in6 sa;
	unsigned dummy = sizeof sa;
	int fd;

	fd = accept(s, (struct sockaddr *) &sa, &dummy);
	if(fd == -1) return -1;
	if(sa.sin6_family == AF_INET) {
		struct sockaddr_in *sa4 = (struct sockaddr_in *) &sa;
		byte_copy(ip, 12, V4mappedprefix);
		byte_copy(ip + 12, 4, (char *) &sa4->sin_addr);
		u16_unpack_big((char *) &sa4->sin_port, port);
		return fd;
	}
	byte_copy(ip, 16, (char *) &sa.sin6_addr);
	u16_unpack_big((char *) &sa.sin6_port, port);
	if(scope_id) *scope_id = sa.sin6_scope_id;
	return fd;
}

int socket_recv6(int s, char *buf, unsigned len, char ip[16], u16 *port, u32 *scope_id){
	struct sockaddr_in6 sa;
	unsigned int dummy = sizeof sa;
	int r;

	byte_zero(&sa, dummy);
	r = recvfrom(s, buf, len, 0, (struct sockaddr *) &sa, &dummy);
	if(r == -1) return -1;

	byte_copy(ip, 16, (char *) &sa.sin6_addr);
	u16_unpack_big((char *) &sa.sin6_port, port);
	if(scope_id) *scope_id = sa.sin6_scope_id;

	return r;
}

int socket_bind4(int s, const char ip[4], u16 port){
	struct sockaddr_in sa;

	byte_zero(&sa, sizeof sa);
	sa.sin_family = AF_INET;
	u16_pack_big((char *) &sa.sin_port,port);
	byte_copy((char *) &sa.sin_addr, 4, ip);

	return bind(s, (struct sockaddr *) &sa, sizeof sa);
}

int socket_bind4_reuse(int s, const char ip[4], u16 port){
	int opt = 1;
	setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt);
	return socket_bind4(s, ip, port);
}

int socket_connect4(int s, const char ip[4], u16 port){
	struct sockaddr_in sa;

	byte_zero(&sa, sizeof sa);
	sa.sin_family = AF_INET;
	u16_pack_big((char *) &sa.sin_port, port);
	byte_copy((char *) &sa.sin_addr, 4, ip);

	return connect(s, (struct sockaddr *) &sa, sizeof sa);
}

int socket_connect6(int s, const char ip[16], u16 port, u32 scope_id){
	struct sockaddr_in6 sa;

	byte_zero(&sa, sizeof sa);
	sa.sin6_family = PF_INET6;
	u16_pack_big((char *) &sa.sin6_port, port);
	sa.sin6_flowinfo = 0;
	sa.sin6_scope_id = scope_id;
	byte_copy((char *) &sa.sin6_addr, 16, ip);

	return connect(s, (struct sockaddr *) &sa, sizeof sa);
}

int socket_udp6(void){
	int s;

	s = socket(PF_INET6, SOCK_DGRAM, 0);
	if(s == -1){
		if(errno == EINVAL)
			s = socket(PF_INET, SOCK_DGRAM, 0);
	}
	return s;
}

int socket_udp(void){
	return socket(PF_INET, SOCK_DGRAM, 0);
}

int socket_send4(int s, const char *buf, int len, const char ip[4], u16 port){
	struct sockaddr_in sa;

	byte_zero(&sa, sizeof(sa));
	sa.sin_family = AF_INET;
	u16_pack_big((char *) &sa.sin_port, port);
	byte_copy((char *) &sa.sin_addr, 4, ip);

	return sendto(s, buf, len, 0, (struct sockaddr *) &sa, sizeof(sa));
}

int socket_recv4(int s, char *buf, int len, char ip[4], u16 *port){
	struct sockaddr_in sa;
	socklen_t dummy = sizeof(sa);
	int r;

	r = recvfrom(s, buf, len, 0, (struct sockaddr *) &sa, &dummy);
	if(r == -1) return -1;
	byte_copy(ip, 4, (char *) &sa.sin_addr);
	u16_unpack_big((char *) &sa.sin_port, port);

	return r;
}