-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddresses.c
211 lines (192 loc) · 5.13 KB
/
addresses.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
*
* SCTP test tool stt.
*
* Copyright (C) 2002-2008 by Michael Tuexen
*
* Realized in co-operation between Siemens AG and the Muenster University
* of Applied Sciences.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program 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 General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Contact: [email protected]
*
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <libguile.h>
scm_t_bits address_tag;
static SCM
make_ipv4_address(SCM s_address)
{
struct sockaddr_in *addr;
struct in_addr sin_addr;
char *address;
address = scm_to_locale_string(s_address);
if (inet_pton(AF_INET, address, &sin_addr) == 1) {
free(address);
addr = (struct sockaddr_in *)
scm_gc_malloc(sizeof(struct sockaddr_in), "address");
memset((void *) addr, 0, sizeof(struct sockaddr_in));
#if defined (HAVE_SIN_LEN)
addr->sin_len = sizeof(struct sockaddr_in);
#endif
addr->sin_family = AF_INET;
addr->sin_port = 0;
addr->sin_addr = sin_addr;
SCM_RETURN_NEWSMOB(address_tag, addr);
} else {
free(address);
return SCM_BOOL_F;
}
}
static SCM
make_ipv6_address(SCM s_address)
{
struct sockaddr_in6 *addr;
struct in6_addr sin6_addr;
char *address;
address = scm_to_locale_string(s_address);
if (inet_pton(AF_INET6, address, &sin6_addr) == 1) {
free(address);
addr = (struct sockaddr_in6 *)
scm_gc_malloc(sizeof(struct sockaddr_in6), "address");
memset((void *) addr, 0, sizeof(struct sockaddr_in6));
#if defined (HAVE_SIN6_LEN)
addr->sin6_len = sizeof(struct sockaddr_in6);
#endif
addr->sin6_family = AF_INET6;
addr->sin6_port = 0;
addr->sin6_flowinfo = 0;
memcpy((void *) &(addr->sin6_addr),
(const void *) &sin6_addr,
sizeof(struct in6_addr));
SCM_RETURN_NEWSMOB (address_tag, addr);
} else {
free(address);
return SCM_BOOL_F;
}
}
static SCM
mark_address(SCM address_smob)
{
return SCM_BOOL_F;
}
static size_t
free_address (SCM address_smob)
{
struct sockaddr *address;
address = (struct sockaddr *) SCM_SMOB_DATA (address_smob);
switch (address->sa_family) {
case AF_INET:
scm_gc_free(address, sizeof(struct sockaddr_in), "address");
break;
case AF_INET6:
scm_gc_free(address, sizeof(struct sockaddr_in6), "address");
break;
default:
break;
}
return 0;
}
static int
print_address (SCM address_smob, SCM port, scm_print_state *pstate)
{
struct sockaddr *address;
char address_string[INET6_ADDRSTRLEN];
memset((void *)address_string, 0, INET6_ADDRSTRLEN);
scm_puts("#<address: ", port);
address = (struct sockaddr *) SCM_SMOB_DATA (address_smob);
switch (address->sa_family) {
case AF_INET:
inet_ntop(AF_INET,
&(((struct sockaddr_in *)address)->sin_addr),
address_string,
INET_ADDRSTRLEN);
break;
case AF_INET6:
inet_ntop(AF_INET6,
&(((struct sockaddr_in6 *)address)->sin6_addr),
address_string,
INET6_ADDRSTRLEN);
break;
default:
break;
}
scm_puts(address_string, port);
scm_puts (">", port);
return 1;
}
static SCM
equalp_address(SCM address_1_smob, SCM address_2_smob)
{
struct sockaddr *address_1, *address_2;
address_1 = (struct sockaddr *) SCM_SMOB_DATA (address_1_smob);
address_2 = (struct sockaddr *) SCM_SMOB_DATA (address_2_smob);
switch (address_1->sa_family) {
case AF_INET:
switch (address_2->sa_family) {
case AF_INET:
if (memcmp(&((struct sockaddr_in *)address_1)->sin_addr,
&((struct sockaddr_in *)address_2)->sin_addr,
sizeof(struct in_addr)) == 0) {
return SCM_BOOL_T;
} else {
return SCM_BOOL_F;
}
case AF_INET6:
return SCM_BOOL_F;
break;
default:
return SCM_BOOL_F;
break;
}
break;
case AF_INET6:
switch (address_2->sa_family) {
case AF_INET:
return SCM_BOOL_F;
break;
case AF_INET6:
if (memcmp(&((struct sockaddr_in6 *)address_1)->sin6_addr,
&((struct sockaddr_in6 *)address_2)->sin6_addr,
sizeof(struct in6_addr)) == 0) {
return SCM_BOOL_T;
} else {
return SCM_BOOL_F;
}
break;
default:
return SCM_BOOL_F;
break;
}
break;
default:
break;
}
return SCM_BOOL_F;
}
void
init_addresses(void)
{
address_tag = scm_make_smob_type("address", 0);
scm_set_smob_mark(address_tag, mark_address);
scm_set_smob_free(address_tag, free_address);
scm_set_smob_print(address_tag, print_address);
scm_set_smob_equalp(address_tag, equalp_address);
scm_c_define_gsubr("make-ipv4-address", 1, 0, 0, make_ipv4_address);
scm_c_define_gsubr("make-ipv6-address", 1, 0, 0, make_ipv6_address);
}