Merge lp:~hipl-core/hipl/libhip into lp:hipl

Proposed by Xin
Status: Merged
Merged at revision: 6385
Proposed branch: lp:~hipl-core/hipl/libhip
Merge into: lp:hipl
Diff against target: 3681 lines (+2547/-203)
55 files modified
Makefile.am (+56/-46)
doc/HOWTO.xml.in (+106/-0)
hipd/main.c (+3/-3)
hipfw/hipfw.c (+1/-1)
libcore/hostid.c (+12/-4)
libhipl/accessor.h (+3/-3)
libhipl/cert.h (+3/-3)
libhipl/close.h (+3/-3)
libhipl/configfilereader.h (+3/-3)
libhipl/cookie.h (+3/-3)
libhipl/dh.h (+3/-3)
libhipl/esp_prot_anchordb.h (+3/-3)
libhipl/esp_prot_hipd_msg.h (+3/-3)
libhipl/esp_prot_light_update.h (+3/-3)
libhipl/hadb.c (+16/-1)
libhipl/hadb.h (+3/-3)
libhipl/hidb.c (+9/-7)
libhipl/hidb.h (+3/-3)
libhipl/hip_socket.h (+3/-3)
libhipl/hipd.h (+8/-3)
libhipl/hiprelay.h (+3/-3)
libhipl/hit_to_ip.h (+3/-3)
libhipl/init.c (+295/-0)
libhipl/init.h (+8/-4)
libhipl/input.c (+5/-0)
libhipl/input.h (+3/-3)
libhipl/keymat.h (+3/-3)
libhipl/lhipl.c (+536/-0)
libhipl/lhipl.h (+76/-0)
libhipl/lhipl_operations.c (+774/-0)
libhipl/lhipl_operations.h (+55/-0)
libhipl/lhipl_sock.c (+150/-0)
libhipl/lhipl_sock.h (+67/-0)
libhipl/lsidb.h (+3/-3)
libhipl/maintenance.h (+3/-3)
libhipl/nat.h (+3/-3)
libhipl/netdev.h (+3/-3)
libhipl/nsupdate.h (+3/-3)
libhipl/opp_mode.h (+3/-3)
libhipl/output.c (+33/-18)
libhipl/output.h (+3/-3)
libhipl/pkt_handling.h (+3/-3)
libhipl/registration.h (+3/-3)
libhipl/user.h (+3/-3)
libhipl/user_ipsec_hipd_msg.h (+3/-3)
libhipl/user_ipsec_sadb_api.h (+3/-3)
modules/cert/hipd/cert.c (+2/-2)
modules/heartbeat/hipd/heartbeat.c (+9/-9)
modules/heartbeat_update/hipd/hb_update.c (+3/-3)
modules/midauth/hipd/midauth.c (+2/-2)
modules/update/hipd/update.c (+11/-11)
modules/update/hipd/update_builder.c (+2/-2)
modules/update/hipd/update_locator.c (+2/-2)
test/check_libhipl.c (+221/-0)
test/hipd/lsidb.c (+1/-1)
To merge this branch: bzr merge lp:~hipl-core/hipl/libhip
Reviewer Review Type Date Requested Status
Diego Biurrun Approve
Miika Komu Approve
Review via email: mp+105783@code.launchpad.net

Description of the change

May 15, 2012

This version of libhipl fixes issues in Diego's review. Since we agreed to
change the directory layout, the "lib/hipl/" folder has been also changed to
"libhipl/". Therefore I propose a new merge request to make it clear.

=========================================================================

Apr 17, 2012

The merge proposal this time has several big changes, in order to facilitate
further development & merge and reflects suggestions from previous reviews.
The changes are:

1. Divide the 'socket_wrapper.c/h' to 3 parts: 'lhipl.c/h', 'lhipl_sock.c/h'
and 'lhipl_operations.c/h'.
   * 'lhipl.c/h' contains public APIs of libhipl, and it validates the
     parameters input by user. 'lhipl.c/h' relies on the support of the other
     two parts. For libhipl users, lhipl.h is the only header file they need.
   * 'lhipl_sock.c/h' contains functions to maintain libhipl sockets.
   * 'lhipl_operations.c/h' contains functions to operate libhipl sockets.

2. Add single-process version test cases, NO multi-process and NO time waiting
   anymore.

3. The merge size is reduced. Since we get the single-process test cases, we
   decide to postpone the hipnetcat and its test cases to another merge proposal.
   I also improve the code to reduce the code size.

4. The libhipl now can work with 'select()'.

PS: There is one review from Diego for last proposal I didn't address, I have
    fixed them in this proposal.

=============================================

Mar 15, 2012 Fixed issues based on Christof and Stefan's review.

Feb 20, 2012 Code revisd based on Diego's second review and Christof's review
New APIs also have been changed to start with 'hipl_' prefix

Feb 10, 2012 code revised based on Diego's review

=============================================

Libhip merge proposal:

The libhip branch mainly aims to provide a convenient way for system test of HIPL without full installation and configuration. In addition to this purpose, it also provides a socket API alike library approach for application to use HIP. Last by not least, since vanilla Linux TCP does not yet support long periods of disconnectivity [1] , libhip can be a solution in this condition.

In the libhip branch, we build a library version of HIP for upper applications, which only exposes traditional socket like API. when using the libhip, hip control messages are transmitted over TCP or UDP, which is similar to TLS/DTLS but we have a unified protocol to handle both datagram and streaming traffic[2]. Compared to TLS/DTLS, this is a big advantage and it may be better use case for HIP[3].

In the libhip, most of code of hipd has been moved to lib/hipdaemon which then becomes a library for both hipd and libhip. By this way, the libhip can reuse the code of hipd to the max extend. This is also the reason why libhip can be a system test approach for hipd, especially in the process of base exchange, the libhip and hipd share the same code base. Meanwhile, the hip daemon, and other functionalities previous exist in the trunk, are kept unchanged and functioning after this merge.

We also implement a sample application called hipnetcat, which is a client-server style application on top of libhip and these 2 sides establish HIP association via BEX before actual data communication. The hipnetcat is integrated into our automatically test framework (check_hipnetcat). In check_hipnetcat, 2 hipnetcat processes try to establish BEX on loopback address on top of TCP/UDP, therefore, the execution of this test suite will check the functionality of base exchange in a system level.

Currently the libhip is still at an eary stage, there are several features waiting for implementation. First there is no support in the firewall; Second, although the control plane base exchange functions well, the data plane security is not implemented yet. We plan to introduce libsrtp for this purpose; Third, the libhip is not thread save yet, because the socket descriptors we use now are global variables; Last, the support of long period disconnectivity we mention in the first paragraph is not included in this merge, there will be another merge request for this feature.

[1] Schutz et al,. Protocol enhancements for intermittently connected hosts, 2005
[2] Komu et al, Technical report: Sockets and Beyond Assessing the Source Code of Network Applications, 2012
[3] Levä et al, Adoption of General-purpose Communication Protocols: the Case of Host Identity Protocol, unpublished manuscript, 2012

To post a comment you must log in.
Revision history for this message
Miika Komu (miika-iki) wrote :

Please merge on Friday 18th unless somebody disagrees or requests more time for review. Thanks!

review: Approve
Revision history for this message
Diego Biurrun (diego-biurrun) wrote :

 review approve

On Tue, May 15, 2012 at 09:26:17AM +0000, Xin wrote:
> Xin has proposed merging lp:~hipl-core/hipl/libhip into lp:hipl.
>
> --- Makefile.am 2012-05-12 06:54:33 +0000
> +++ Makefile.am 2012-05-15 09:25:24 +0000
> @@ -66,10 +67,14 @@
> TESTS = test/check_hipd \
> test/check_hipfw \
> test/check_libcore \
> + test/check_libhipl \
> + test/check_libcore \
> $(tools_hipdnsproxy_PYTHON)
> +

order

Please fix this before merging.

I don't have time to go through this again in the short-term future,
so let's get this merged and work on the details later.

Diego

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'Makefile.am'
2--- Makefile.am 2012-05-12 06:54:33 +0000
3+++ Makefile.am 2012-05-15 09:25:24 +0000
4@@ -23,7 +23,7 @@
5
6 ACLOCAL_AMFLAGS = -I m4
7
8-HIPL_HEADER_LOCATIONS = hipd/*.h hipfw/*.h libcore/*.h libcore/*/*.h modules/*/*/*.h test/*.h test/*/*.h test/*/*/*.h
9+HIPL_HEADER_LOCATIONS = hipd/*.h hipfw/*.h libcore/*.h libcore/*/*.h libhipl/*.h modules/*/*/*.h test/*.h test/*/*.h test/*/*/*.h
10 HIPL_HEADER_LIST = $(wildcard $(addprefix $(srcdir)/,$(HIPL_HEADER_LOCATIONS)))
11
12 # For "make dist"
13@@ -58,7 +58,8 @@
14
15
16 ### libraries ###
17-lib_LTLIBRARIES = libcore/libcore.la
18+lib_LTLIBRARIES = libcore/libcore.la \
19+ libhipl/libhipl.la
20
21
22 ### tests ###
23@@ -66,10 +67,14 @@
24 TESTS = test/check_hipd \
25 test/check_hipfw \
26 test/check_libcore \
27+ test/check_libhipl \
28+ test/check_libcore \
29 $(tools_hipdnsproxy_PYTHON)
30+
31 check_PROGRAMS = test/check_hipd \
32 test/check_hipfw \
33- test/check_libcore
34+ test/check_libcore \
35+ test/check_libhipl
36 endif
37
38
39@@ -86,47 +91,7 @@
40
41 tools_hipconf_SOURCES = tools/hipconf.c
42
43-hipd_hipd_sources = hipd/accessor.c \
44- hipd/cert.c \
45- hipd/close.c \
46- hipd/configfilereader.c \
47- hipd/cookie.c \
48- hipd/dh.c \
49- hipd/esp_prot_anchordb.c \
50- hipd/esp_prot_hipd_msg.c \
51- hipd/esp_prot_light_update.c \
52- hipd/hadb.c \
53- hipd/hidb.c \
54- hipd/hip_socket.c \
55- hipd/hipd.c \
56- hipd/hiprelay.c \
57- hipd/hit_to_ip.c \
58- hipd/init.c \
59- hipd/input.c \
60- hipd/keymat.c \
61- hipd/lsidb.c \
62- hipd/maintenance.c \
63- hipd/nat.c \
64- hipd/netdev.c \
65- hipd/nsupdate.c \
66- hipd/opp_mode.c \
67- hipd/output.c \
68- hipd/pkt_handling.c \
69- hipd/registration.c \
70- hipd/user.c \
71- hipd/user_ipsec_hipd_msg.c \
72- hipd/user_ipsec_sadb_api.c \
73- modules/heartbeat/hipd/heartbeat.c \
74- modules/heartbeat_update/hipd/hb_update.c \
75- modules/cert/hipd/cert.c \
76- modules/update/hipd/update.c \
77- modules/update/hipd/update_builder.c \
78- modules/update/hipd/update_locator.c \
79- modules/update/hipd/update_param_handling.c
80-
81-hipd_hipd_SOURCES = $(hipd_hipd_sources) \
82- modules/midauth/hipd/midauth.c \
83- hipd/main.c
84+hipd_hipd_SOURCES = hipd/main.c
85
86 dist_sysconf_DATA = hipd/hipd.conf \
87 hipd/hosts \
88@@ -203,6 +168,47 @@
89 libcore_libcore_la_SOURCES += libcore/performance.c
90 endif
91
92+libhipl_libhipl_la_SOURCES = libhipl/accessor.c \
93+ libhipl/cert.c \
94+ libhipl/close.c \
95+ libhipl/configfilereader.c \
96+ libhipl/cookie.c \
97+ libhipl/dh.c \
98+ libhipl/esp_prot_anchordb.c \
99+ libhipl/esp_prot_hipd_msg.c \
100+ libhipl/esp_prot_light_update.c \
101+ libhipl/hadb.c \
102+ libhipl/hidb.c \
103+ libhipl/hip_socket.c \
104+ libhipl/hipd.c \
105+ libhipl/hiprelay.c \
106+ libhipl/hit_to_ip.c \
107+ libhipl/init.c \
108+ libhipl/input.c \
109+ libhipl/keymat.c \
110+ libhipl/lhipl.c \
111+ libhipl/lhipl_sock.c \
112+ libhipl/lhipl_operations.c \
113+ libhipl/lsidb.c \
114+ libhipl/maintenance.c \
115+ libhipl/nat.c \
116+ libhipl/netdev.c \
117+ libhipl/nsupdate.c \
118+ libhipl/opp_mode.c \
119+ libhipl/output.c \
120+ libhipl/pkt_handling.c \
121+ libhipl/registration.c \
122+ libhipl/user.c \
123+ libhipl/user_ipsec_hipd_msg.c \
124+ libhipl/user_ipsec_sadb_api.c \
125+ modules/cert/hipd/cert.c \
126+ modules/heartbeat/hipd/heartbeat.c \
127+ modules/heartbeat_update/hipd/hb_update.c \
128+ modules/midauth/hipd/midauth.c \
129+ modules/update/hipd/update.c \
130+ modules/update/hipd/update_builder.c \
131+ modules/update/hipd/update_locator.c \
132+ modules/update/hipd/update_param_handling.c
133
134 test_check_hipd_SOURCES = test/check_hipd.c \
135 test/hipd/lsidb.c \
136@@ -231,14 +237,18 @@
137 test/libcore/gpl/pk.c \
138 test/libcore/modules/midauth_builder.c
139
140+test_check_libhipl_SOURCES = test/check_libhipl.c
141+
142
143 ### static library dependencies ###
144
145-hipd_hipd_LDADD = libcore/libcore.la
146+hipd_hipd_LDADD = libhipl/libhipl.la
147 hipfw_hipfw_LDADD = libcore/libcore.la
148-test_check_hipd_LDADD = libcore/libcore.la
149+libhipl_libhipl_la_LIBADD = libcore/libcore.la
150+test_check_hipd_LDADD = libhipl/libhipl.la
151 test_check_hipfw_LDADD = libcore/libcore.la
152 test_check_libcore_LDADD = libcore/libcore.la
153+test_check_libhipl_LDADD = libhipl/libhipl.la
154 test_certteststub_LDADD = libcore/libcore.la
155 test_performance_auth_performance_LDADD = libcore/libcore.la
156 test_performance_dh_performance_LDADD = libcore/libcore.la
157
158=== modified file 'doc/HOWTO.xml.in'
159--- doc/HOWTO.xml.in 2012-02-28 18:38:18 +0000
160+++ doc/HOWTO.xml.in 2012-05-15 09:25:24 +0000
161@@ -2659,6 +2659,112 @@
162
163 </section>
164
165+ <section id="ch_libhipl_usage">
166+ <title>The Libhipl Extension</title>
167+
168+ <para>
169+ Libhipl provides HIP functionality as a library for upper layer
170+ applications without requiring the HIPL daemon.
171+ Instead, HIP control messages are transmitted on top of TCP/UDP.
172+ From the applications' point of view, there is an API similar to the
173+ normal socket API. This section describes the libhipl API.
174+ </para>
175+
176+ <para>
177+ The libhipl API is described in <emphasis>"lhipl.h"</emphasis>. Detailed
178+ information about each function is available there.
179+ </para>
180+
181+ <formalpara>
182+ <title>Libhipl initialization</title>
183+ <para>
184+ Libhipl requires initialization before calling any other related
185+ functions. The function: <emphasis>hipl_lib_init_all()</emphasis>
186+ serves this purpose and different debug levels can be set as its
187+ parameters to track problems inside the library.
188+ </para>
189+ </formalpara>
190+
191+ <formalpara>
192+ <title>Name-based API</title>
193+ <para>
194+ Compared to standard socket API, one apparent difference in Libhipl is
195+ that a peer is specified by giving a peer name and a port number instead
196+ of a sockaddr data structure. The peer name is a string representation of
197+ the peer. Currently, libhipl only supports a HIT string as peer name.
198+ </para>
199+ </formalpara>
200+
201+ <formalpara>
202+ <title>Libhipl socket creation</title>
203+ <para>
204+ The function hipl_socket() creates a new libhipl socket. An ID is
205+ assigned to each created libhipl socket and returned by this function,
206+ then the caller can pass this ID to other libhipl functions to operate on
207+ this libhipl socket. NOTE: there is NO relation between libhipl socket
208+ IDs and normal socket file descriptors, and the result of using
209+ this ID as file descriptor is undefined.
210+ </para>
211+ </formalpara>
212+
213+ <formalpara><title>Setup TCP connection</title><para>
214+ When using libhipl in TCP mode, TCP connection setup is the first task.
215+ Similar to the normal socket syntax, libhipl provides:
216+ <emphasis>hipl_connect()</emphasis>, <emphasis>hipl_listen()</emphasis>
217+ and <emphasis>hipl_accept()</emphasis> for this goal. Those functions
218+ process the 3-way handshake and establish a TCP connection.
219+ </para></formalpara>
220+
221+ <formalpara>
222+ <title>libhipl Base Exchange (BEX) and user data transmission</title>
223+ <para>
224+ Data transmission is handled by <emphasis>hipl_sendto()</emphasis> and
225+ <emphasis>hipl_recvfrom()</emphasis>.
226+ In libhipl, The first sending data call triggers BEX and the first
227+ receiving call waits for an incoming BEX message. Once BEX succeeds,
228+ the pending user data is transmitted to the peer side. This BEX process
229+ is handled by libhipl automatically by default. Meanwhile, there is also
230+ an advanced way to handle BEX step by step, which will be explained
231+ bellow.
232+ </para>
233+ </formalpara>
234+
235+ <formalpara>
236+ <title>libhipl assistant API</title>
237+ <para>
238+ <itemizedlist>
239+ <listitem>
240+ <para>
241+ <emphasis>hipl_add_peer_info()</emphasis> adds the peer's HIT and
242+ IP address mappings to the database of libhipl. The library can
243+ use this information to look up a peer's locator when a peer name
244+ is given.
245+ </para>
246+ </listitem>
247+
248+ <listitem>
249+ <para>
250+ <emphasis>hipl_lib_set_bex_feedback()</emphasis>: This function
251+ turns the step-by-step process of BEX on/off. By default, BEX
252+ feedback is off. When it is on, sending/receiving functions only
253+ process one BEX messages at a time, and give different return
254+ values to notify the progress of BEX.
255+ </para>
256+ </listitem>
257+ </itemizedlist>
258+ </para>
259+ </formalpara>
260+
261+ <formalpara>
262+ <title>Libhipl sample program</title>
263+ <para>
264+ For a libhipl usage example, refer to the
265+ <emphasis>check_libhipl</emphasis> program in the
266+ <emphasis>test</emphasis> directory.
267+ </para>
268+ </formalpara>
269+
270+ </section> <!-- ch_libhipl_usage -->
271 </chapter> <!-- ch_exp_extensions -->
272
273 </book>
274
275=== modified file 'hipd/main.c'
276--- hipd/main.c 2012-05-12 06:54:33 +0000
277+++ hipd/main.c 2012-05-15 09:25:24 +0000
278@@ -1,5 +1,5 @@
279 /*
280- * Copyright (c) 2010-2011 Aalto University and RWTH Aachen University.
281+ * Copyright (c) 2010-2012 Aalto University and RWTH Aachen University.
282 *
283 * Permission is hereby granted, free of charge, to any person
284 * obtaining a copy of this software and associated documentation
285@@ -33,9 +33,9 @@
286 #include <sys/types.h>
287
288
289-#include "hipd/hipd.h"
290-#include "init.h"
291 #include "libcore/debug.h"
292+#include "libhipl/hipd.h"
293+#include "libhipl/init.h"
294
295
296 /**
297
298=== modified file 'hipfw/hipfw.c'
299--- hipfw/hipfw.c 2012-05-12 06:54:33 +0000
300+++ hipfw/hipfw.c 2012-05-15 09:25:24 +0000
301@@ -75,7 +75,7 @@
302 #include "libcore/performance.h"
303 #include "libcore/prefix.h"
304 #include "libcore/util.h"
305-#include "hipd/hipd.h"
306+#include "libhipl/hipd.h"
307 #include "config.h"
308 #include "cache.h"
309 #include "cert.h"
310
311=== modified file 'libcore/hostid.c'
312--- libcore/hostid.c 2012-05-12 06:54:33 +0000
313+++ libcore/hostid.c 2012-05-15 09:25:24 +0000
314@@ -1,5 +1,5 @@
315 /*
316- * Copyright (c) 2010-2011 Aalto University and RWTH Aachen University.
317+ * Copyright (c) 2010-2012 Aalto University and RWTH Aachen University.
318 *
319 * Permission is hereby granted, free of charge, to any person
320 * obtaining a copy of this software and associated documentation
321@@ -29,6 +29,7 @@
322 */
323
324 #include <errno.h>
325+#include <libgen.h>
326 #include <stdint.h>
327 #include <stdlib.h>
328 #include <string.h>
329@@ -678,6 +679,7 @@
330 int err = 0, dsa_key_rr_len = 0, rsa_key_rr_len = 0;
331 int dsa_pub_key_rr_len = 0, rsa_pub_key_rr_len = 0;
332 hip_hdr numeric_action = 0;
333+ char *hi_file_dup = NULL;
334 char hostname[HIP_HOST_ID_HOSTNAME_LEN_MAX];
335 const char *rsa_filenamebase = DEFAULT_HOST_RSA_KEY_FILE_BASE DEFAULT_ANON_HI_FILE_NAME_SUFFIX;
336 const char *dsa_filenamebase = DEFAULT_HOST_DSA_KEY_FILE_BASE DEFAULT_ANON_HI_FILE_NAME_SUFFIX;
337@@ -743,11 +745,16 @@
338 goto out_err;
339 }
340 } else if (!use_default) {
341+ hi_file_dup = strdup(hi_file);
342+ if ((err = check_and_create_dir(dirname(hi_file_dup), HIP_DIR_MODE))) {
343+ HIP_ERROR("Could not create directory for path: %s\n", hi_file);
344+ goto out_err;
345+ }
346 if (!strcmp(hi_fmt, "dsa")) {
347 dsa_key = create_dsa_key(dsa_key_bits);
348 HIP_IFEL(!dsa_key, -EINVAL,
349 "Creation of DSA key failed.\n");
350- if ((err = save_dsa_private_key(dsa_filenamebase, dsa_key))) {
351+ if ((err = save_dsa_private_key(hi_file, dsa_key))) {
352 HIP_ERROR("Saving of DSA key failed.\n");
353 goto out_err;
354 }
355@@ -756,7 +763,7 @@
356 ecdsa_key = create_ecdsa_key(ecdsa_nid);
357 HIP_IFEL(!ecdsa_key, -EINVAL,
358 "Creation of ECDSA key failed.\n");
359- if ((err = save_ecdsa_private_key(ecdsa_filenamebase, ecdsa_key))) {
360+ if ((err = save_ecdsa_private_key(hi_file, ecdsa_key))) {
361 HIP_ERROR("Saving of ECDSA key failed.\n");
362 goto out_err;
363 }
364@@ -765,7 +772,7 @@
365 rsa_key = create_rsa_key(rsa_key_bits);
366 HIP_IFEL(!rsa_key, -EINVAL,
367 "Creation of RSA key failed.\n");
368- if ((err = save_rsa_private_key(rsa_filenamebase, rsa_key))) {
369+ if ((err = save_rsa_private_key(hi_file, rsa_key))) {
370 HIP_ERROR("Saving of RSA key failed.\n");
371 goto out_err;
372 }
373@@ -1093,6 +1100,7 @@
374 change_key_file_perms(rsa_filenamebase_pub);
375 }
376
377+ free(hi_file_dup);
378 free(dsa_host_id);
379 free(dsa_pub_host_id);
380 free(rsa_host_id);
381
382=== added directory 'libhipl'
383=== renamed file 'hipd/accessor.c' => 'libhipl/accessor.c'
384=== renamed file 'hipd/accessor.h' => 'libhipl/accessor.h'
385--- hipd/accessor.h 2012-05-12 06:54:33 +0000
386+++ libhipl/accessor.h 2012-05-15 09:25:24 +0000
387@@ -23,8 +23,8 @@
388 * OTHER DEALINGS IN THE SOFTWARE.
389 */
390
391-#ifndef HIPL_HIPD_ACCESSOR_H
392-#define HIPL_HIPD_ACCESSOR_H
393+#ifndef HIPL_LIBHIPL_ACCESSOR_H
394+#define HIPL_LIBHIPL_ACCESSOR_H
395
396 #include "libcore/protodefs.h"
397
398@@ -47,4 +47,4 @@
399 int hipd_get_flag(unsigned int);
400 void hipd_set_flag(unsigned int);
401
402-#endif /* HIPL_HIPD_ACCESSOR_H */
403+#endif /* HIPL_LIBHIPL_ACCESSOR_H */
404
405=== renamed file 'hipd/cert.c' => 'libhipl/cert.c'
406=== renamed file 'hipd/cert.h' => 'libhipl/cert.h'
407--- hipd/cert.h 2012-05-12 06:54:33 +0000
408+++ libhipl/cert.h 2012-05-15 09:25:24 +0000
409@@ -29,8 +29,8 @@
410 * Certificate signing and verification functions.
411 */
412
413-#ifndef HIPL_HIPD_CERT_H
414-#define HIPL_HIPD_CERT_H
415+#ifndef HIPL_LIBHIPL_CERT_H
416+#define HIPL_LIBHIPL_CERT_H
417
418 #include <openssl/rsa.h>
419
420@@ -45,4 +45,4 @@
421 int hip_cert_x509v3_handle_request_to_sign(struct hip_common *);
422 int hip_cert_x509v3_handle_request_to_verify(struct hip_common *);
423
424-#endif /* HIPL_HIPD_CERT_H */
425+#endif /* HIPL_LIBHIPL_CERT_H */
426
427=== renamed file 'hipd/close.c' => 'libhipl/close.c'
428=== renamed file 'hipd/close.h' => 'libhipl/close.h'
429--- hipd/close.h 2012-05-12 06:54:33 +0000
430+++ libhipl/close.h 2012-05-15 09:25:24 +0000
431@@ -23,8 +23,8 @@
432 * OTHER DEALINGS IN THE SOFTWARE.
433 */
434
435-#ifndef HIPL_HIPD_CLOSE_H
436-#define HIPL_HIPD_CLOSE_H
437+#ifndef HIPL_LIBHIPL_CLOSE_H
438+#define HIPL_LIBHIPL_CLOSE_H
439
440 #include "libcore/protodefs.h"
441 #include "libcore/state.h"
442@@ -48,4 +48,4 @@
443 struct hip_packet_context *ctx);
444 int hip_purge_closing_ha(struct hip_hadb_state *ha, void *opaque);
445
446-#endif /* HIPL_HIPD_CLOSE_H */
447+#endif /* HIPL_LIBHIPL_CLOSE_H */
448
449=== renamed file 'hipd/configfilereader.c' => 'libhipl/configfilereader.c'
450=== renamed file 'hipd/configfilereader.h' => 'libhipl/configfilereader.h'
451--- hipd/configfilereader.h 2012-01-06 14:38:54 +0000
452+++ libhipl/configfilereader.h 2012-05-15 09:25:24 +0000
453@@ -22,8 +22,8 @@
454 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE O
455 */
456
457-#ifndef HIPL_HIPD_CONFIGFILEREADER_H
458-#define HIPL_HIPD_CONFIGFILEREADER_H
459+#ifndef HIPL_LIBHIPL_CONFIGFILEREADER_H
460+#define HIPL_LIBHIPL_CONFIGFILEREADER_H
461
462 #include <stdio.h>
463
464@@ -52,4 +52,4 @@
465 struct hip_configfile_value *current);
466 void print_node(struct hip_configfile_value *node);
467
468-#endif /* HIPL_HIPD_CONFIGFILEREADER_H */
469+#endif /* HIPL_LIBHIPL_CONFIGFILEREADER_H */
470
471=== renamed file 'hipd/cookie.c' => 'libhipl/cookie.c'
472=== renamed file 'hipd/cookie.h' => 'libhipl/cookie.h'
473--- hipd/cookie.h 2012-05-12 06:54:33 +0000
474+++ libhipl/cookie.h 2012-05-15 09:25:24 +0000
475@@ -23,8 +23,8 @@
476 * OTHER DEALINGS IN THE SOFTWARE.
477 */
478
479-#ifndef HIPL_HIPD_COOKIE_H
480-#define HIPL_HIPD_COOKIE_H
481+#ifndef HIPL_LIBHIPL_COOKIE_H
482+#define HIPL_LIBHIPL_COOKIE_H
483
484 #include <stdint.h>
485 #include <netinet/in.h>
486@@ -58,4 +58,4 @@
487 int hip_get_puzzle_difficulty_msg(struct hip_common *msg);
488 int hip_set_puzzle_difficulty_msg(struct hip_common *msg);
489
490-#endif /* HIPL_HIPD_COOKIE_H */
491+#endif /* HIPL_LIBHIPL_COOKIE_H */
492
493=== renamed file 'hipd/dh.c' => 'libhipl/dh.c'
494=== renamed file 'hipd/dh.h' => 'libhipl/dh.h'
495--- hipd/dh.h 2011-11-25 17:56:24 +0000
496+++ libhipl/dh.h 2012-05-15 09:25:24 +0000
497@@ -23,8 +23,8 @@
498 * OTHER DEALINGS IN THE SOFTWARE.
499 */
500
501-#ifndef HIPL_HIPD_DH_H
502-#define HIPL_HIPD_DH_H
503+#ifndef HIPL_LIBHIPL_DH_H
504+#define HIPL_LIBHIPL_DH_H
505
506 #include <stdint.h>
507
508@@ -37,4 +37,4 @@
509 int bufsize);
510 int hip_init_cipher(void);
511
512-#endif /* HIPL_HIPD_DH_H */
513+#endif /* HIPL_LIBHIPL_DH_H */
514
515=== renamed file 'hipd/esp_prot_anchordb.c' => 'libhipl/esp_prot_anchordb.c'
516=== renamed file 'hipd/esp_prot_anchordb.h' => 'libhipl/esp_prot_anchordb.h'
517--- hipd/esp_prot_anchordb.h 2012-05-12 06:54:33 +0000
518+++ libhipl/esp_prot_anchordb.h 2012-05-15 09:25:24 +0000
519@@ -33,8 +33,8 @@
520 * @brief Stores anchor elements to be used for the esp protection
521 */
522
523-#ifndef HIPL_HIPD_ESP_PROT_ANCHORDB_H
524-#define HIPL_HIPD_ESP_PROT_ANCHORDB_H
525+#ifndef HIPL_LIBHIPL_ESP_PROT_ANCHORDB_H
526+#define HIPL_LIBHIPL_ESP_PROT_ANCHORDB_H
527
528 #include <stdint.h>
529
530@@ -47,4 +47,4 @@
531 int anchor_db_get_anchor_length(const uint8_t transform);
532 int anchor_db_get_hash_item_length(const uint8_t transform);
533
534-#endif /* HIPL_HIPD_ESP_PROT_ANCHORDB_H */
535+#endif /* HIPL_LIBHIPL_ESP_PROT_ANCHORDB_H */
536
537=== renamed file 'hipd/esp_prot_hipd_msg.c' => 'libhipl/esp_prot_hipd_msg.c'
538=== renamed file 'hipd/esp_prot_hipd_msg.h' => 'libhipl/esp_prot_hipd_msg.h'
539--- hipd/esp_prot_hipd_msg.h 2012-05-12 06:54:33 +0000
540+++ libhipl/esp_prot_hipd_msg.h 2012-05-15 09:25:24 +0000
541@@ -31,8 +31,8 @@
542 * @brief Messaging with hipfw and other HIP instances
543 */
544
545-#ifndef HIPL_HIPD_ESP_PROT_HIPD_MSG_H
546-#define HIPL_HIPD_ESP_PROT_HIPD_MSG_H
547+#ifndef HIPL_LIBHIPL_ESP_PROT_HIPD_MSG_H
548+#define HIPL_LIBHIPL_ESP_PROT_HIPD_MSG_H
549
550 #include <stdint.h>
551
552@@ -72,4 +72,4 @@
553 struct hip_hadb_state *entry,
554 uint32_t *spi);
555
556-#endif /* HIPL_HIPD_ESP_PROT_HIPD_MSG_H */
557+#endif /* HIPL_LIBHIPL_ESP_PROT_HIPD_MSG_H */
558
559=== renamed file 'hipd/esp_prot_light_update.c' => 'libhipl/esp_prot_light_update.c'
560=== renamed file 'hipd/esp_prot_light_update.h' => 'libhipl/esp_prot_light_update.h'
561--- hipd/esp_prot_light_update.h 2012-05-12 06:54:33 +0000
562+++ libhipl/esp_prot_light_update.h 2012-05-15 09:25:24 +0000
563@@ -31,8 +31,8 @@
564 * @brief Messaging required for HHL-based anchor element updates
565 */
566
567-#ifndef HIPL_HIPD_ESP_PROT_LIGHT_UPDATE_H
568-#define HIPL_HIPD_ESP_PROT_LIGHT_UPDATE_H
569+#ifndef HIPL_LIBHIPL_ESP_PROT_LIGHT_UPDATE_H
570+#define HIPL_LIBHIPL_ESP_PROT_LIGHT_UPDATE_H
571
572 #include "libcore/modularization.h"
573 #include "libcore/protodefs.h"
574@@ -48,4 +48,4 @@
575 const enum hip_state ha_state,
576 struct hip_packet_context *ctx);
577
578-#endif /* HIPL_HIPD_ESP_PROT_LIGHT_UPDATE_H */
579+#endif /* HIPL_LIBHIPL_ESP_PROT_LIGHT_UPDATE_H */
580
581=== renamed file 'hipd/hadb.c' => 'libhipl/hadb.c'
582--- hipd/hadb.c 2012-05-12 06:54:33 +0000
583+++ libhipl/hadb.c 2012-05-15 09:25:24 +0000
584@@ -610,9 +610,15 @@
585 HIP_HOST_ID_HOSTNAME_LEN_MAX - 1);
586 }
587
588- if (hip_select_source_address(&peer_map.our_addr, &peer_map.peer_addr)) {
589+ /* hip_select_source_address() is incompatible with libhipl, therefore we skip
590+ * this function and set the source address in peer_map to the ANY source
591+ * address.*/
592+ if (!hipl_is_libhip_mode()
593+ && hip_select_source_address(&peer_map.our_addr, &peer_map.peer_addr)) {
594 HIP_ERROR("Cannot find source address\n");
595 return -1;
596+ } else if (hipl_is_libhip_mode()) {
597+ memset(&peer_map.our_addr, 0, sizeof(peer_map.our_addr));
598 }
599
600 if (hip_for_each_hi(hadb_add_peer_info_wrapper, &peer_map)) {
601@@ -1453,6 +1459,10 @@
602 */
603 void hip_delete_security_associations_and_sp(struct hip_hadb_state *const ha)
604 {
605+ if (hipl_is_libhip_mode()) {
606+ return;
607+ }
608+
609 // Delete previous security policies
610 hip_delete_hit_sp_pair(&ha->hit_our, &ha->hit_peer, 1);
611 hip_delete_hit_sp_pair(&ha->hit_peer, &ha->hit_our, 1);
612@@ -1522,6 +1532,11 @@
613 ha),
614 -1, "Error while changing outbound security association\n");
615
616+ if (hipl_is_libhip_mode()) {
617+ HIP_DEBUG("No SP set up in library mode\n");
618+ goto out_err;
619+ }
620+
621 // Create a new security policy pointing to SAs after SA setup
622 HIP_IFEL(hip_setup_hit_sp_pair(&ha->hit_peer,
623 &ha->hit_our,
624
625=== renamed file 'hipd/hadb.h' => 'libhipl/hadb.h'
626--- hipd/hadb.h 2012-05-12 06:54:33 +0000
627+++ libhipl/hadb.h 2012-05-15 09:25:24 +0000
628@@ -23,8 +23,8 @@
629 * OTHER DEALINGS IN THE SOFTWARE.
630 */
631
632-#ifndef HIPL_HIPD_HADB_H
633-#define HIPL_HIPD_HADB_H
634+#ifndef HIPL_LIBHIPL_HADB_H
635+#define HIPL_LIBHIPL_HADB_H
636
637 #include <stdint.h>
638
639@@ -125,4 +125,4 @@
640 const struct in6_addr *const src_addr,
641 const struct in6_addr *const dst_addr);
642
643-#endif /* HIPL_HIPD_HADB_H */
644+#endif /* HIPL_LIBHIPL_HADB_H */
645
646=== renamed file 'hipd/hidb.c' => 'libhipl/hidb.c'
647--- hipd/hidb.c 2012-05-12 06:54:33 +0000
648+++ libhipl/hidb.c 2012-05-15 09:25:24 +0000
649@@ -596,13 +596,15 @@
650 hip_add_iface_local_route(&in6_lsi);
651
652 /* Adding HITs and LSIs to the interface */
653- if (hip_add_iface_local_hit(&hit)) {
654- HIP_ERROR("Failed to add HIT to the device\n");
655- return -1;
656- }
657- if (hip_add_iface_local_hit(&in6_lsi)) {
658- HIP_ERROR("Failed to add LSI to the device\n");
659- return -1;
660+ if (!hipl_is_libhip_mode()) {
661+ if (hip_add_iface_local_hit(&hit)) {
662+ HIP_ERROR("Failed to add HIT to the device\n");
663+ return -1;
664+ }
665+ if (hip_add_iface_local_hit(&in6_lsi)) {
666+ HIP_ERROR("Failed to add LSI to the device\n");
667+ return -1;
668+ }
669 }
670 }
671
672
673=== renamed file 'hipd/hidb.h' => 'libhipl/hidb.h'
674--- hipd/hidb.h 2012-05-12 06:54:33 +0000
675+++ libhipl/hidb.h 2012-05-15 09:25:24 +0000
676@@ -23,8 +23,8 @@
677 * OTHER DEALINGS IN THE SOFTWARE.
678 */
679
680-#ifndef HIPL_HIPD_HIDB_H
681-#define HIPL_HIPD_HIDB_H
682+#ifndef HIPL_LIBHIPL_HIDB_H
683+#define HIPL_LIBHIPL_HIDB_H
684
685 /**
686 * @file
687@@ -75,4 +75,4 @@
688 int hip_get_default_hit_msg(struct hip_common *msg);
689 int hip_get_default_lsi(struct in_addr *lsi);
690
691-#endif /* HIPL_HIPD_HIDB_H */
692+#endif /* HIPL_LIBHIPL_HIDB_H */
693
694=== renamed file 'hipd/hip_socket.c' => 'libhipl/hip_socket.c'
695=== renamed file 'hipd/hip_socket.h' => 'libhipl/hip_socket.h'
696--- hipd/hip_socket.h 2012-05-12 06:54:33 +0000
697+++ libhipl/hip_socket.h 2012-05-15 09:25:24 +0000
698@@ -23,8 +23,8 @@
699 * OTHER DEALINGS IN THE SOFTWARE.
700 */
701
702-#ifndef HIPL_HIPD_HIP_SOCKET_H
703-#define HIPL_HIPD_HIP_SOCKET_H
704+#ifndef HIPL_LIBHIPL_HIP_SOCKET_H
705+#define HIPL_LIBHIPL_HIP_SOCKET_H
706
707 #include <stdint.h>
708 #include <sys/select.h>
709@@ -50,4 +50,4 @@
710
711 void hip_run_socket_handles(fd_set *read_fdset, struct hip_packet_context *ctx);
712
713-#endif /* HIPL_HIPD_HIP_SOCKET_H */
714+#endif /* HIPL_LIBHIPL_HIP_SOCKET_H */
715
716=== renamed file 'hipd/hipd.c' => 'libhipl/hipd.c'
717=== renamed file 'hipd/hipd.h' => 'libhipl/hipd.h'
718--- hipd/hipd.h 2012-05-12 06:54:33 +0000
719+++ libhipl/hipd.h 2012-05-15 09:25:24 +0000
720@@ -23,10 +23,11 @@
721 * OTHER DEALINGS IN THE SOFTWARE.
722 */
723
724-#ifndef HIPL_HIPD_HIPD_H
725-#define HIPL_HIPD_HIPD_H
726+#ifndef HIPL_LIBHIPL_HIPD_H
727+#define HIPL_LIBHIPL_HIPD_H
728
729 #include <netdb.h>
730+#include <stdbool.h>
731 #include <stdint.h>
732 #include <netinet/in.h>
733 #include <sys/types.h>
734@@ -80,4 +81,8 @@
735 int hipd_parse_cmdline_opts(int argc, char *argv[], uint64_t * flags);
736 int hipd_main(uint64_t flags);
737
738-#endif /* HIPL_HIPD_HIPD_H */
739+/* libhip_mode accessor */
740+bool hipl_is_libhip_mode(void);
741+void hipl_set_libhip_mode(void);
742+
743+#endif /* HIPL_LIBHIPL_HIPD_H */
744
745=== renamed file 'hipd/hiprelay.c' => 'libhipl/hiprelay.c'
746=== renamed file 'hipd/hiprelay.h' => 'libhipl/hiprelay.h'
747--- hipd/hiprelay.h 2012-05-12 06:54:33 +0000
748+++ libhipl/hiprelay.h 2012-05-15 09:25:24 +0000
749@@ -30,8 +30,8 @@
750 * draft-ietf-hip-nat-traversal-03</a>
751 */
752
753-#ifndef HIPL_HIPD_HIPRELAY_H
754-#define HIPL_HIPD_HIPRELAY_H
755+#ifndef HIPL_LIBHIPL_HIPRELAY_H
756+#define HIPL_LIBHIPL_HIPRELAY_H
757
758 #include <stdint.h>
759 #include <netinet/in.h>
760@@ -133,4 +133,4 @@
761 struct hip_relrec *rec,
762 const uint8_t type_hdr);
763
764-#endif /* HIPL_HIPD_HIPRELAY_H */
765+#endif /* HIPL_LIBHIPL_HIPRELAY_H */
766
767=== renamed file 'hipd/hit_to_ip.c' => 'libhipl/hit_to_ip.c'
768=== renamed file 'hipd/hit_to_ip.h' => 'libhipl/hit_to_ip.h'
769--- hipd/hit_to_ip.h 2012-05-12 06:54:33 +0000
770+++ libhipl/hit_to_ip.h 2012-05-15 09:25:24 +0000
771@@ -29,8 +29,8 @@
772 * @brief usually invoked by hip_map_id_to_addr
773 */
774
775-#ifndef HIPL_HIPD_HIT_TO_IP_H
776-#define HIPL_HIPD_HIT_TO_IP_H
777+#ifndef HIPL_LIBHIPL_HIT_TO_IP_H
778+#define HIPL_LIBHIPL_HIT_TO_IP_H
779
780 #include <netinet/in.h>
781
782@@ -42,4 +42,4 @@
783 int hip_get_hit_to_ip_status(void);
784 void hip_hit_to_ip_set(const char *zone);
785
786-#endif /* HIPL_HIPD_HIT_TO_IP_H */
787+#endif /* HIPL_LIBHIPL_HIT_TO_IP_H */
788
789=== renamed file 'hipd/init.c' => 'libhipl/init.c'
790--- hipd/init.c 2012-05-12 06:54:33 +0000
791+++ libhipl/init.c 2012-05-15 09:25:24 +0000
792@@ -34,7 +34,9 @@
793
794 #include <errno.h>
795 #include <limits.h>
796+#include <pwd.h>
797 #include <signal.h>
798+#include <stdbool.h>
799 #include <stdint.h>
800 #include <stdlib.h>
801 #include <string.h>
802@@ -118,6 +120,34 @@
803 #endif
804 /** end ICMPV6_FILTER related stuff */
805
806+#define HIPL_USER_DIR ".hipl"
807+#define HIPL_USER_RSA_KEY_NAME "libhipl_rsa_key"
808+
809+/* Flag to show if hipl is running in libhip mode (=1) or normal mode (=0).
810+ *
811+ * This variable should NOT be accessed directly. Always use the accessor
812+ * functions instead.
813+ */
814+static bool hipd_library_mode = false;
815+
816+/**
817+ * Test if the library mode is on.
818+ *
819+ * @return true if the library mode is on, false otherwise.
820+ */
821+bool hipl_is_libhip_mode(void)
822+{
823+ return hipd_library_mode;
824+}
825+
826+/**
827+ * Turn on library mode.
828+ */
829+void hipl_set_libhip_mode(void)
830+{
831+ hipd_library_mode = true;
832+}
833+
834 /* Startup flags of the HIPD. Keep the around, for they will be used at exit */
835 static uint64_t sflags;
836
837@@ -523,6 +553,199 @@
838 lmod_register_packet_type(HIP_LUPDATE, "HIP_LUPDATE");
839 }
840
841+static int libhip_init_handle_functions(void)
842+{
843+ HIP_DEBUG("Initialize handle functions for libhip.\n");
844+
845+ hip_register_handle_function(HIP_I1, HIP_STATE_UNASSOCIATED, &hip_check_i1, 20000);
846+ hip_register_handle_function(HIP_I1, HIP_STATE_UNASSOCIATED, &hip_handle_i1, 30000);
847+ hip_register_handle_function(HIP_I1, HIP_STATE_UNASSOCIATED, &hip_update_retransmissions, 35000);
848+ hip_register_handle_function(HIP_I1, HIP_STATE_UNASSOCIATED, &hip_send_r1, 40000);
849+ hip_register_handle_function(HIP_I1, HIP_STATE_I1_SENT, &hip_check_i1, 20000);
850+ hip_register_handle_function(HIP_I1, HIP_STATE_I1_SENT, &hip_handle_i1, 30000);
851+ hip_register_handle_function(HIP_I1, HIP_STATE_I1_SENT, &hip_update_retransmissions, 35000);
852+ hip_register_handle_function(HIP_I1, HIP_STATE_I1_SENT, &hip_send_r1, 40000);
853+ hip_register_handle_function(HIP_I1, HIP_STATE_I2_SENT, &hip_check_i1, 20000);
854+ hip_register_handle_function(HIP_I1, HIP_STATE_I2_SENT, &hip_handle_i1, 30000);
855+ hip_register_handle_function(HIP_I1, HIP_STATE_I2_SENT, &hip_update_retransmissions, 35000);
856+ hip_register_handle_function(HIP_I1, HIP_STATE_I2_SENT, &hip_send_r1, 40000);
857+ hip_register_handle_function(HIP_I1, HIP_STATE_R2_SENT, &hip_check_i1, 20000);
858+ hip_register_handle_function(HIP_I1, HIP_STATE_R2_SENT, &hip_handle_i1, 30000);
859+ hip_register_handle_function(HIP_I1, HIP_STATE_R2_SENT, &hip_update_retransmissions, 35000);
860+ hip_register_handle_function(HIP_I1, HIP_STATE_R2_SENT, &hip_send_r1, 40000);
861+ hip_register_handle_function(HIP_I1, HIP_STATE_ESTABLISHED, &hip_check_i1, 20000);
862+ hip_register_handle_function(HIP_I1, HIP_STATE_ESTABLISHED, &hip_handle_i1, 30000);
863+ hip_register_handle_function(HIP_I1, HIP_STATE_ESTABLISHED, &hip_update_retransmissions, 35000);
864+ hip_register_handle_function(HIP_I1, HIP_STATE_ESTABLISHED, &hip_send_r1, 40000);
865+ hip_register_handle_function(HIP_I1, HIP_STATE_CLOSING, &hip_check_i1, 20000);
866+ hip_register_handle_function(HIP_I1, HIP_STATE_CLOSING, &hip_handle_i1, 30000);
867+ hip_register_handle_function(HIP_I1, HIP_STATE_CLOSING, &hip_update_retransmissions, 35000);
868+ hip_register_handle_function(HIP_I1, HIP_STATE_CLOSING, &hip_send_r1, 40000);
869+ hip_register_handle_function(HIP_I1, HIP_STATE_CLOSED, &hip_check_i1, 20000);
870+ hip_register_handle_function(HIP_I1, HIP_STATE_CLOSED, &hip_handle_i1, 30000);
871+ hip_register_handle_function(HIP_I1, HIP_STATE_CLOSED, &hip_update_retransmissions, 35000);
872+ hip_register_handle_function(HIP_I1, HIP_STATE_CLOSED, &hip_send_r1, 40000);
873+ hip_register_handle_function(HIP_I1, HIP_STATE_NONE, &hip_check_i1, 20000);
874+ hip_register_handle_function(HIP_I1, HIP_STATE_NONE, &hip_handle_i1, 30000);
875+ hip_register_handle_function(HIP_I1, HIP_STATE_NONE, &hip_update_retransmissions, 35000);
876+ hip_register_handle_function(HIP_I1, HIP_STATE_NONE, &hip_send_r1, 40000);
877+
878+ hip_register_handle_function(HIP_I2, HIP_STATE_UNASSOCIATED, &hip_check_i2, 20000);
879+ hip_register_handle_function(HIP_I2, HIP_STATE_UNASSOCIATED, &hip_handle_i2, 30000);
880+ hip_register_handle_function(HIP_I2, HIP_STATE_UNASSOCIATED, &hip_update_retransmissions, 30250);
881+ hip_register_handle_function(HIP_I2, HIP_STATE_UNASSOCIATED, &hip_create_r2, 40000);
882+ hip_register_handle_function(HIP_I2, HIP_STATE_UNASSOCIATED, &hip_add_rvs_reg_from, 41000);
883+ hip_register_handle_function(HIP_I2, HIP_STATE_UNASSOCIATED, &hip_hmac2_and_sign, 42000);
884+ hip_register_handle_function(HIP_I2, HIP_STATE_UNASSOCIATED, &hip_add_rvs_relay_to, 43000);
885+ hip_register_handle_function(HIP_I2, HIP_STATE_UNASSOCIATED, &hip_send_r2, 50000);
886+ hip_register_handle_function(HIP_I2, HIP_STATE_I1_SENT, &hip_check_i2, 20000);
887+ hip_register_handle_function(HIP_I2, HIP_STATE_I1_SENT, &hip_handle_i2, 30000);
888+ hip_register_handle_function(HIP_I2, HIP_STATE_I1_SENT, &hip_update_retransmissions, 30250);
889+ hip_register_handle_function(HIP_I2, HIP_STATE_I1_SENT, &hip_create_r2, 40000);
890+ hip_register_handle_function(HIP_I2, HIP_STATE_I1_SENT, &hip_add_rvs_reg_from, 41000);
891+ hip_register_handle_function(HIP_I2, HIP_STATE_I1_SENT, &hip_hmac2_and_sign, 42000);
892+ hip_register_handle_function(HIP_I2, HIP_STATE_I1_SENT, &hip_add_rvs_relay_to, 43000);
893+ hip_register_handle_function(HIP_I2, HIP_STATE_I1_SENT, &hip_send_r2, 50000);
894+ hip_register_handle_function(HIP_I2, HIP_STATE_I2_SENT, &hip_check_i2, 20000);
895+ hip_register_handle_function(HIP_I2, HIP_STATE_I2_SENT, &hip_handle_i2_in_i2_sent, 21000);
896+ hip_register_handle_function(HIP_I2, HIP_STATE_I2_SENT, &hip_handle_i2, 30000);
897+ hip_register_handle_function(HIP_I2, HIP_STATE_I2_SENT, &hip_update_retransmissions, 30250);
898+ hip_register_handle_function(HIP_I2, HIP_STATE_I2_SENT, &hip_create_r2, 40000);
899+ hip_register_handle_function(HIP_I2, HIP_STATE_I2_SENT, &hip_add_rvs_reg_from, 41000);
900+ hip_register_handle_function(HIP_I2, HIP_STATE_I2_SENT, &hip_hmac2_and_sign, 42000);
901+ hip_register_handle_function(HIP_I2, HIP_STATE_I2_SENT, &hip_add_rvs_relay_to, 43000);
902+ hip_register_handle_function(HIP_I2, HIP_STATE_I2_SENT, &hip_send_r2, 50000);
903+ hip_register_handle_function(HIP_I2, HIP_STATE_R2_SENT, &hip_check_i2, 20000);
904+ hip_register_handle_function(HIP_I2, HIP_STATE_R2_SENT, &hip_handle_i2, 30000);
905+ hip_register_handle_function(HIP_I2, HIP_STATE_R2_SENT, &hip_update_retransmissions, 30250);
906+ hip_register_handle_function(HIP_I2, HIP_STATE_R2_SENT, &hip_create_r2, 40000);
907+ hip_register_handle_function(HIP_I2, HIP_STATE_R2_SENT, &hip_add_rvs_reg_from, 41000);
908+ hip_register_handle_function(HIP_I2, HIP_STATE_R2_SENT, &hip_hmac2_and_sign, 42000);
909+ hip_register_handle_function(HIP_I2, HIP_STATE_R2_SENT, &hip_add_rvs_relay_to, 43000);
910+ hip_register_handle_function(HIP_I2, HIP_STATE_R2_SENT, &hip_send_r2, 50000);
911+ hip_register_handle_function(HIP_I2, HIP_STATE_ESTABLISHED, &hip_check_i2, 20000);
912+ hip_register_handle_function(HIP_I2, HIP_STATE_ESTABLISHED, &hip_handle_i2, 30000);
913+ hip_register_handle_function(HIP_I2, HIP_STATE_ESTABLISHED, &hip_update_retransmissions, 30250);
914+ hip_register_handle_function(HIP_I2, HIP_STATE_ESTABLISHED, &hip_create_r2, 40000);
915+ hip_register_handle_function(HIP_I2, HIP_STATE_ESTABLISHED, &hip_add_rvs_reg_from, 41000);
916+ hip_register_handle_function(HIP_I2, HIP_STATE_ESTABLISHED, &hip_hmac2_and_sign, 42000);
917+ hip_register_handle_function(HIP_I2, HIP_STATE_ESTABLISHED, &hip_add_rvs_relay_to, 43000);
918+ hip_register_handle_function(HIP_I2, HIP_STATE_ESTABLISHED, &hip_send_r2, 50000);
919+ hip_register_handle_function(HIP_I2, HIP_STATE_CLOSING, &hip_check_i2, 20000);
920+ hip_register_handle_function(HIP_I2, HIP_STATE_CLOSING, &hip_handle_i2, 30000);
921+ hip_register_handle_function(HIP_I2, HIP_STATE_CLOSING, &hip_update_retransmissions, 30250);
922+ hip_register_handle_function(HIP_I2, HIP_STATE_CLOSING, &hip_create_r2, 40000);
923+ hip_register_handle_function(HIP_I2, HIP_STATE_CLOSING, &hip_add_rvs_reg_from, 41000);
924+ hip_register_handle_function(HIP_I2, HIP_STATE_CLOSING, &hip_hmac2_and_sign, 42000);
925+ hip_register_handle_function(HIP_I2, HIP_STATE_CLOSING, &hip_add_rvs_relay_to, 43000);
926+ hip_register_handle_function(HIP_I2, HIP_STATE_CLOSING, &hip_send_r2, 50000);
927+ hip_register_handle_function(HIP_I2, HIP_STATE_CLOSED, &hip_check_i2, 20000);
928+ hip_register_handle_function(HIP_I2, HIP_STATE_CLOSED, &hip_handle_i2, 30000);
929+ hip_register_handle_function(HIP_I2, HIP_STATE_CLOSED, &hip_update_retransmissions, 30250);
930+ hip_register_handle_function(HIP_I2, HIP_STATE_CLOSED, &hip_create_r2, 40000);
931+ hip_register_handle_function(HIP_I2, HIP_STATE_CLOSED, &hip_add_rvs_reg_from, 41000);
932+ hip_register_handle_function(HIP_I2, HIP_STATE_CLOSED, &hip_hmac2_and_sign, 42000);
933+ hip_register_handle_function(HIP_I2, HIP_STATE_CLOSED, &hip_add_rvs_relay_to, 43000);
934+ hip_register_handle_function(HIP_I2, HIP_STATE_CLOSED, &hip_send_r2, 50000);
935+ hip_register_handle_function(HIP_I2, HIP_STATE_NONE, &hip_check_i2, 20000);
936+ hip_register_handle_function(HIP_I2, HIP_STATE_NONE, &hip_handle_i2, 30000);
937+ hip_register_handle_function(HIP_I2, HIP_STATE_NONE, &hip_update_retransmissions, 30250);
938+ hip_register_handle_function(HIP_I2, HIP_STATE_NONE, &hip_create_r2, 40000);
939+ hip_register_handle_function(HIP_I2, HIP_STATE_NONE, &hip_add_rvs_reg_from, 41000);
940+ hip_register_handle_function(HIP_I2, HIP_STATE_NONE, &hip_hmac2_and_sign, 42000);
941+ hip_register_handle_function(HIP_I2, HIP_STATE_NONE, &hip_add_rvs_relay_to, 43000);
942+ hip_register_handle_function(HIP_I2, HIP_STATE_NONE, &hip_send_r2, 50000);
943+
944+ hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &hip_check_r1, 20000);
945+ hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &hip_handle_r1, 30000);
946+ hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &hip_update_retransmissions, 30500);
947+ hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &hip_build_esp_info, 31000);
948+ hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &hip_build_solution, 32000);
949+ hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &hip_handle_diffie_hellman, 33000);
950+ hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &esp_prot_r1_handle_transforms, 34000);
951+ hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &hip_create_i2, 40000);
952+ hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &hip_add_signed_echo_response, 41000);
953+ hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &hip_mac_and_sign_handler, 42000);
954+ hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &hip_add_unsigned_echo_response, 43000);
955+ hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &hip_send_i2, 50000);
956+ hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &hip_check_r1, 20000);
957+ hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &hip_handle_r1, 30000);
958+ hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &hip_update_retransmissions, 30500);
959+ hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &hip_build_esp_info, 31000);
960+ hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &hip_build_solution, 32000);
961+ hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &hip_handle_diffie_hellman, 33000);
962+ hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &esp_prot_r1_handle_transforms, 34000);
963+ hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &hip_create_i2, 40000);
964+ hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &hip_add_signed_echo_response, 41000);
965+ hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &hip_mac_and_sign_handler, 42000);
966+ hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &hip_add_unsigned_echo_response, 43000);
967+ hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &hip_send_i2, 50000);
968+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &hip_check_r1, 20000);
969+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &hip_handle_r1, 30000);
970+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &hip_update_retransmissions, 30500);
971+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &hip_build_esp_info, 31000);
972+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &hip_build_solution, 32000);
973+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &hip_handle_diffie_hellman, 33000);
974+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &esp_prot_r1_handle_transforms, 34000);
975+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &hip_create_i2, 40000);
976+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &hip_add_signed_echo_response, 41000);
977+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &hip_mac_and_sign_handler, 42000);
978+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &hip_add_unsigned_echo_response, 43000);
979+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &hip_send_i2, 50000);
980+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &hip_check_r1, 20000);
981+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &hip_handle_r1, 30000);
982+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &hip_update_retransmissions, 30500);
983+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &hip_build_esp_info, 31000);
984+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &hip_build_solution, 32000);
985+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &hip_handle_diffie_hellman, 33000);
986+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &esp_prot_r1_handle_transforms, 34000);
987+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &hip_create_i2, 40000);
988+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &hip_add_signed_echo_response, 41000);
989+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &hip_mac_and_sign_handler, 42000);
990+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &hip_add_unsigned_echo_response, 43000);
991+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &hip_send_i2, 50000);
992+
993+ hip_register_handle_function(HIP_R2, HIP_STATE_I2_SENT, &hip_check_r2, 20000);
994+ hip_register_handle_function(HIP_R2, HIP_STATE_I2_SENT, &hip_handle_r2, 30000);
995+ hip_register_handle_function(HIP_R2, HIP_STATE_I2_SENT, &hip_update_retransmissions, 30250);
996+
997+ hip_register_handle_function(HIP_NOTIFY, HIP_STATE_I1_SENT, &hip_check_notify, 20000);
998+ hip_register_handle_function(HIP_NOTIFY, HIP_STATE_I1_SENT, &hip_handle_notify, 30000);
999+ hip_register_handle_function(HIP_NOTIFY, HIP_STATE_I2_SENT, &hip_check_notify, 20000);
1000+ hip_register_handle_function(HIP_NOTIFY, HIP_STATE_I2_SENT, &hip_handle_notify, 30000);
1001+ hip_register_handle_function(HIP_NOTIFY, HIP_STATE_R2_SENT, &hip_check_notify, 20000);
1002+ hip_register_handle_function(HIP_NOTIFY, HIP_STATE_R2_SENT, &hip_handle_notify, 30000);
1003+ hip_register_handle_function(HIP_NOTIFY, HIP_STATE_ESTABLISHED, &hip_check_notify, 20000);
1004+ hip_register_handle_function(HIP_NOTIFY, HIP_STATE_ESTABLISHED, &hip_handle_notify, 30000);
1005+ hip_register_handle_function(HIP_NOTIFY, HIP_STATE_CLOSING, &hip_check_notify, 20000);
1006+ hip_register_handle_function(HIP_NOTIFY, HIP_STATE_CLOSING, &hip_handle_notify, 30000);
1007+ hip_register_handle_function(HIP_NOTIFY, HIP_STATE_CLOSED, &hip_check_notify, 20000);
1008+ hip_register_handle_function(HIP_NOTIFY, HIP_STATE_CLOSED, &hip_handle_notify, 30000);
1009+
1010+ hip_register_handle_function(HIP_CLOSE, HIP_STATE_ESTABLISHED, &hip_close_check_packet, 20000);
1011+ hip_register_handle_function(HIP_CLOSE, HIP_STATE_ESTABLISHED, &hip_update_retransmissions, 25000);
1012+ hip_register_handle_function(HIP_CLOSE, HIP_STATE_ESTABLISHED, &hip_close_create_response, 30000);
1013+ hip_register_handle_function(HIP_CLOSE, HIP_STATE_ESTABLISHED, &hip_close_send_response, 40000);
1014+
1015+ hip_register_handle_function(HIP_CLOSE, HIP_STATE_CLOSING, &hip_close_check_packet, 20000);
1016+ hip_register_handle_function(HIP_CLOSE, HIP_STATE_CLOSING, &hip_update_retransmissions, 25000);
1017+ hip_register_handle_function(HIP_CLOSE, HIP_STATE_CLOSING, &hip_close_create_response, 30000);
1018+ hip_register_handle_function(HIP_CLOSE, HIP_STATE_CLOSING, &hip_close_send_response, 40000);
1019+
1020+ hip_register_handle_function(HIP_CLOSE_ACK, HIP_STATE_CLOSING, &hip_close_ack_check_packet, 20000);
1021+ hip_register_handle_function(HIP_CLOSE_ACK, HIP_STATE_CLOSING, &hip_update_retransmissions, 25000);
1022+ hip_register_handle_function(HIP_CLOSE_ACK, HIP_STATE_CLOSING, &hip_close_ack_handle_packet, 30000);
1023+
1024+ hip_register_handle_function(HIP_CLOSE_ACK, HIP_STATE_CLOSED, &hip_close_ack_check_packet, 20000);
1025+ hip_register_handle_function(HIP_CLOSE_ACK, HIP_STATE_CLOSED, &hip_update_retransmissions, 25000);
1026+ hip_register_handle_function(HIP_CLOSE_ACK, HIP_STATE_CLOSED, &hip_close_ack_handle_packet, 30000);
1027+
1028+ hip_register_handle_function(HIP_LUPDATE, HIP_STATE_ESTABLISHED, &esp_prot_handle_light_update, 20000);
1029+ hip_register_handle_function(HIP_LUPDATE, HIP_STATE_R2_SENT, &esp_prot_handle_light_update, 20000);
1030+
1031+ return 0;
1032+}
1033+
1034 static int init_handle_functions(void)
1035 {
1036 int err = 0;
1037@@ -1090,6 +1313,78 @@
1038 }
1039
1040 /**
1041+ * Initialization function for libhipl.
1042+ *
1043+ * @param debug_level debug level for log output.
1044+ * @return 0 on success, negative number on error.
1045+ */
1046+int hipl_lib_init(enum logdebug debug_level)
1047+{
1048+ int err = 0;
1049+ int keypath_len = 0;
1050+ char *key_path = NULL;
1051+ struct hip_common *msg = NULL;
1052+ struct passwd *pwd;
1053+
1054+ hipl_set_libhip_mode();
1055+ hip_nat_status = 1;
1056+
1057+ /* disable hip_firewall */
1058+ lsi_status = HIP_MSG_LSI_OFF;
1059+
1060+ hip_set_logdebug(debug_level);
1061+
1062+ hip_init_hadb();
1063+ hip_init_hostid_db();
1064+ hip_netdev_init_addresses();
1065+ libhip_init_handle_functions();
1066+
1067+ /* Load default key from ~/.hipl/ */
1068+ if ((pwd = getpwuid(getuid())) == NULL) {
1069+ return -1;
1070+ }
1071+
1072+ /* +3 because we need 2 slashes and a NULL for termination */
1073+ keypath_len = strlen(pwd->pw_dir) +
1074+ strlen(HIPL_USER_DIR) +
1075+ strlen(HIPL_USER_RSA_KEY_NAME) +
1076+ strlen(DEFAULT_PUB_HI_FILE_NAME_SUFFIX) + 3;
1077+ if ((key_path = malloc(keypath_len)) == NULL) {
1078+ HIP_ERROR("malloc() failed\n");
1079+ return -ENOMEM;
1080+ }
1081+
1082+ HIP_IFEL(snprintf(key_path, keypath_len, "%s/%s/%s%s", pwd->pw_dir,
1083+ HIPL_USER_DIR,
1084+ HIPL_USER_RSA_KEY_NAME,
1085+ DEFAULT_PUB_HI_FILE_NAME_SUFFIX) < 0,
1086+ -1, "snprintf() failed");
1087+
1088+ HIP_DEBUG("Using key: %s\n", key_path);
1089+ HIP_IFEL(!(msg = hip_msg_alloc()), -ENOMEM, "hip_msg_alloc()");
1090+ if (hip_serialize_host_id_action(msg, ACTION_ADD, 0, 0, "rsa",
1091+ key_path, 0, 0, 0)) {
1092+ hip_msg_init(msg);
1093+ HIP_IFEL(hip_serialize_host_id_action(msg, ACTION_NEW, 0, 0, "rsa",
1094+ key_path, RSA_KEY_DEFAULT_BITS,
1095+ DSA_KEY_DEFAULT_BITS,
1096+ ECDSA_DEFAULT_CURVE), -1,
1097+ "Fail to create local key at %s.", key_path);
1098+
1099+ hip_msg_init(msg);
1100+ HIP_IFEL(hip_serialize_host_id_action(msg, ACTION_ADD, 0, 0, "rsa",
1101+ key_path, 0, 0, 0), -1,
1102+ "Fail to load local key at %s.", key_path);
1103+ }
1104+ HIP_IFE(hip_handle_add_local_hi(msg), -1);
1105+
1106+out_err:
1107+ free(msg);
1108+ free(key_path);
1109+ return err;
1110+}
1111+
1112+/**
1113 * create a socket to handle UDP encapsulation of HIP control
1114 * packets
1115 *
1116
1117=== renamed file 'hipd/init.h' => 'libhipl/init.h'
1118--- hipd/init.h 2011-11-25 17:56:24 +0000
1119+++ libhipl/init.h 2012-05-15 09:25:24 +0000
1120@@ -1,5 +1,5 @@
1121 /*
1122- * Copyright (c) 2010 Aalto University and RWTH Aachen University.
1123+ * Copyright (c) 2010-2012 Aalto University and RWTH Aachen University.
1124 *
1125 * Permission is hereby granted, free of charge, to any person
1126 * obtaining a copy of this software and associated documentation
1127@@ -23,12 +23,14 @@
1128 * OTHER DEALINGS IN THE SOFTWARE.
1129 */
1130
1131-#ifndef HIPL_HIPD_INIT_H
1132-#define HIPL_HIPD_INIT_H
1133+#ifndef HIPL_LIBHIPL_INIT_H
1134+#define HIPL_LIBHIPL_INIT_H
1135
1136 #include <stdint.h>
1137 #include <netinet/in.h>
1138
1139+#include "libcore/debug.h"
1140+
1141
1142 /* startup flags options to be configured via the command line */
1143 #define HIPD_START_FOREGROUND (1 << 0)
1144@@ -60,4 +62,6 @@
1145 int is_output);
1146 void hip_exit(void);
1147
1148-#endif /* HIPL_HIPD_INIT_H */
1149+int hipl_lib_init(enum logdebug);
1150+
1151+#endif /* HIPL_LIBHIPL_INIT_H */
1152
1153=== renamed file 'hipd/input.c' => 'libhipl/input.c'
1154--- hipd/input.c 2012-05-12 06:54:33 +0000
1155+++ libhipl/input.c 2012-05-15 09:25:24 +0000
1156@@ -707,6 +707,11 @@
1157 &ctx->src_addr);
1158 }
1159
1160+ if (hipl_is_libhip_mode()) {
1161+ ctx->msg_ports.src_port = ctx->hadb_entry->peer_udp_port;
1162+ ctx->msg_ports.dst_port = ctx->hadb_entry->local_udp_port;
1163+ }
1164+
1165 hip_relay_add_rvs_to_ha(ctx->input_msg, ctx->hadb_entry);
1166
1167 #ifdef CONFIG_HIP_RVS
1168
1169=== renamed file 'hipd/input.h' => 'libhipl/input.h'
1170--- hipd/input.h 2012-05-12 06:54:33 +0000
1171+++ libhipl/input.h 2012-05-15 09:25:24 +0000
1172@@ -23,8 +23,8 @@
1173 * OTHER DEALINGS IN THE SOFTWARE.
1174 */
1175
1176-#ifndef HIPL_HIPD_INPUT_H
1177-#define HIPL_HIPD_INPUT_H
1178+#ifndef HIPL_LIBHIPL_INPUT_H
1179+#define HIPL_LIBHIPL_INPUT_H
1180
1181 #include <stdint.h>
1182 #include <netinet/in.h>
1183@@ -133,4 +133,4 @@
1184 UNUSED const enum hip_state ha_state,
1185 struct hip_packet_context *const ctx);
1186
1187-#endif /* HIPL_HIPD_INPUT_H */
1188+#endif /* HIPL_LIBHIPL_INPUT_H */
1189
1190=== renamed file 'hipd/keymat.c' => 'libhipl/keymat.c'
1191=== renamed file 'hipd/keymat.h' => 'libhipl/keymat.h'
1192--- hipd/keymat.h 2012-05-12 06:54:33 +0000
1193+++ libhipl/keymat.h 2012-05-15 09:25:24 +0000
1194@@ -23,8 +23,8 @@
1195 * OTHER DEALINGS IN THE SOFTWARE.
1196 */
1197
1198-#ifndef HIPL_HIPD_KEYMAT_H
1199-#define HIPL_HIPD_KEYMAT_H
1200+#ifndef HIPL_LIBHIPL_KEYMAT_H
1201+#define HIPL_LIBHIPL_KEYMAT_H
1202
1203 #include <stdint.h>
1204 #include <netinet/in.h>
1205@@ -44,4 +44,4 @@
1206 struct hip_keymat_keymat *keymat,
1207 int len);
1208
1209-#endif /* HIPL_HIPD_KEYMAT_H */
1210+#endif /* HIPL_LIBHIPL_KEYMAT_H */
1211
1212=== added file 'libhipl/lhipl.c'
1213--- libhipl/lhipl.c 1970-01-01 00:00:00 +0000
1214+++ libhipl/lhipl.c 2012-05-15 09:25:24 +0000
1215@@ -0,0 +1,536 @@
1216+/*
1217+ * Copyright (c) 2010-2012 Aalto University and RWTH Aachen University.
1218+ *
1219+ * Permission is hereby granted, free of charge, to any person
1220+ * obtaining a copy of this software and associated documentation
1221+ * files (the "Software"), to deal in the Software without
1222+ * restriction, including without limitation the rights to use,
1223+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
1224+ * copies of the Software, and to permit persons to whom the
1225+ * Software is furnished to do so, subject to the following
1226+ * conditions:
1227+ *
1228+ * The above copyright notice and this permission notice shall be
1229+ * included in all copies or substantial portions of the Software.
1230+ *
1231+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1232+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
1233+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
1234+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
1235+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
1236+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
1237+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
1238+ * OTHER DEALINGS IN THE SOFTWARE.
1239+ */
1240+
1241+/**
1242+ * @file
1243+ * Provides the implementation of the libhipl public APIs.
1244+ */
1245+
1246+#define _BSD_SOURCE
1247+
1248+#include <errno.h>
1249+#include <fcntl.h>
1250+#include <string.h>
1251+#include <unistd.h>
1252+
1253+#include "libcore/builder.h"
1254+#include "libcore/ife.h"
1255+#include "libcore/prefix.h"
1256+#include "close.h"
1257+#include "hadb.h"
1258+#include "init.h"
1259+#include "lhipl_sock.h"
1260+#include "lhipl_operations.h"
1261+#include "netdev.h"
1262+#include "output.h"
1263+#include "lhipl.h"
1264+
1265+
1266+/* A switch to turn BEX feedback off/on.
1267+ *
1268+ * If it is on, when the BEX process is conducted during "recvmsg" and "sendmsg",
1269+ * the intermediate result is returned to the caller.
1270+ * If it is off, "recvmsg" and "sendmsg" will only return after the message gets
1271+ * sent or received.
1272+ */
1273+static bool libhipl_bex_feedback;
1274+
1275+/**
1276+ * build a @c sockaddr_in6 address to store a peer's HIT and port number from a
1277+ * string-based peer name and a port number.
1278+ *
1279+ * @param peername the string representation of a peer, only support HIT string
1280+ * currently.
1281+ * @param port the port of the peer.
1282+ * @param peer the @c sockaddr_in6 to hold the peer's HIT and port number.
1283+ * @return 0 on success, -1 on error.
1284+ */
1285+static int build_peer_hit(const char *peername, const uint16_t port,
1286+ struct sockaddr_in6 *peer)
1287+{
1288+ if (!peername || !peer) {
1289+ HIP_ERROR("Invalid input\n");
1290+ return -1;
1291+ }
1292+
1293+ if (inet_pton(AF_INET6, peername, &peer->sin6_addr) != 1) {
1294+ HIP_ERROR("Failed to translate %s into HIT\n", peername);
1295+ }
1296+ peer->sin6_port = htons(port);
1297+ peer->sin6_family = AF_INET6;
1298+ return 0;
1299+}
1300+
1301+/**
1302+ * Initialize libhipl library.
1303+ *
1304+ * This function should be called before using libhipl.
1305+ *
1306+ * @param debug_level the debug level of the libhipl library.
1307+ * @return 0 on success, negative number on error.
1308+ */
1309+int hipl_lib_init_all(enum hipl_lib_loglv debug_level)
1310+{
1311+ hipl_hsock_init();
1312+ switch (debug_level) {
1313+ case HIPL_LIB_LOG_DEBUG:
1314+ return hipl_lib_init(LOGDEBUG_ALL);
1315+ case HIPL_LIB_LOG_INFO:
1316+ return hipl_lib_init(LOGDEBUG_MEDIUM);
1317+ case HIPL_LIB_LOG_ERROR:
1318+ return hipl_lib_init(LOGDEBUG_LOW);
1319+ case HIPL_LIB_LOG_NONE:
1320+ return hipl_lib_init(LOGDEBUG_NONE);
1321+ }
1322+
1323+ return -1;
1324+}
1325+
1326+/**
1327+ * Check the current state of the BEX feedback switch.
1328+ *
1329+ * @return true if the switch is on, false if it is off.
1330+ */
1331+bool hipl_lib_bex_feedback(void)
1332+{
1333+ return libhipl_bex_feedback;
1334+}
1335+
1336+/**
1337+ * Turn the BEX feedback switch off/on.
1338+ *
1339+ * @param val true to turn the switch on, false to turn it off.
1340+ */
1341+void hipl_lib_set_bex_feedback(bool val)
1342+{
1343+ libhipl_bex_feedback = val;
1344+}
1345+
1346+/**
1347+ * Turn the non-blocking feature on/off for a libhipl socket.
1348+ *
1349+ * @param hsock_id ID of the libhipl socket.
1350+ * @param on true to turn non-blocking on, false otherwise.
1351+ * @return 0 on success, negative number on error.
1352+ */
1353+int hipl_lib_set_nonblock(const hipl_sock_id hsock_id, bool on)
1354+{
1355+ int flags;
1356+ struct hipl_sock *hsock = NULL;
1357+
1358+ if ((hsock = hipl_hsock_find(hsock_id)) == NULL) {
1359+ HIP_ERROR("ID %d is not a libhipl socket\n", hsock_id);
1360+ return -EBADF;
1361+ }
1362+
1363+ flags = fcntl(hsock->sock_fd, F_GETFL, 0);
1364+ if (on && !(flags & O_NONBLOCK)) {
1365+ fcntl(hsock->sock_fd, F_SETFL, flags | O_NONBLOCK);
1366+ } else if (!on && (flags & O_NONBLOCK)) {
1367+ fcntl(hsock->sock_fd, F_SETFL, flags & ~O_NONBLOCK);
1368+ }
1369+ return 0;
1370+}
1371+
1372+/**
1373+ * Return the corresponding socket file descriptor of a libhipl socket.
1374+ *
1375+ * @param hsock_id the ID of the libhipl socket
1376+ * @return socket file descriptor on success, -1 on error.
1377+ */
1378+int hipl_lib_get_sockfd(const hipl_sock_id hsock_id)
1379+{
1380+ struct hipl_sock *hsock = NULL;
1381+
1382+ if ((hsock = hipl_hsock_find(hsock_id)) == NULL) {
1383+ HIP_ERROR("ID %d is not a libhipl socket\n", hsock_id);
1384+ return -1;
1385+ }
1386+
1387+ return hsock->sock_fd;
1388+}
1389+
1390+/**
1391+ * Save peer's HIT-to-IP mapping.
1392+ *
1393+ * @param hit peer's hit.
1394+ * @param addr peer's IP address.
1395+ * @return 0 on success, -1 otherwise.
1396+ */
1397+int hipl_add_peer_info(const char *const hit, const char *const addr)
1398+{
1399+ struct in6_addr peer_hit, peer_addr6;
1400+ struct in_addr peer_addr4;
1401+
1402+ if (!hit || !addr) {
1403+ HIP_ERROR("Invalid argument\n");
1404+ return -1;
1405+ }
1406+ if (inet_pton(AF_INET6, hit, &peer_hit) != 1) {
1407+ HIP_ERROR("Invalid hit: %s\n", hit);
1408+ return -1;
1409+ }
1410+ if (!ipv6_addr_is_hit(&peer_hit)) {
1411+ HIP_ERROR("Invalid hit: %s\n", hit);
1412+ return -1;
1413+ }
1414+ if (inet_pton(AF_INET6, addr, &peer_addr6) != 1) {
1415+ if (inet_pton(AF_INET, addr, &peer_addr4) != 1) {
1416+ HIP_ERROR("Invalid address: %s\n", addr);
1417+ return -1;
1418+ }
1419+ IPV4_TO_IPV6_MAP(&peer_addr4, &peer_addr6);
1420+ }
1421+
1422+ return hip_hadb_add_peer_info(&peer_hit, &peer_addr6, NULL, NULL);
1423+}
1424+
1425+/**
1426+ * Create a libhipl socket.
1427+ *
1428+ * @param domain the domain of the libhipl socket (AF_INET / AF_INET6).
1429+ * @param type the type of the libhipl socket (SOCK_DGRAM / SOCK_STREAM).
1430+ * @param protocol the protocol (IPPROTO_UDP / IPPROTO_TCP).
1431+ * @return the ID of the new libhipl socket on success, negative
1432+ * number on error.
1433+ */
1434+int hipl_socket(const int domain, const int type, const int protocol)
1435+{
1436+ if (domain != AF_INET && domain != AF_INET6) {
1437+ HIP_ERROR("Invalid domain: %d\n", domain);
1438+ return -EINVAL;
1439+ }
1440+ if (type != SOCK_DGRAM && type != SOCK_STREAM) {
1441+ HIP_ERROR("Invalid type: %d\n", type);
1442+ return -EINVAL;
1443+ }
1444+ if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
1445+ HIP_ERROR("Invalid protocol: %d\n", protocol);
1446+ return -EINVAL;
1447+ }
1448+
1449+ return hipl_socket_internal(domain, type, protocol);
1450+}
1451+
1452+/**
1453+ * Close a socket.
1454+ *
1455+ * Send HIP CLOSE message to the associated peer and delete the libhipl
1456+ * socket information.
1457+ *
1458+ * @param hsock_id the ID of the libhipl socket to be closed.
1459+ * @return 0 on success, negative number on error.
1460+ */
1461+int hipl_close(const hipl_sock_id hsock_id)
1462+{
1463+ int ret = 0;
1464+ struct hip_common *msg = NULL;
1465+ struct hipl_sock *hsock = NULL;
1466+
1467+ if ((hsock = hipl_hsock_find(hsock_id)) == NULL) {
1468+ HIP_ERROR("ID %d is not a libhipl socket\n", hsock_id);
1469+ return -1;
1470+ }
1471+ if (!hsock->ha || hsock->ha->state == HIP_STATE_CLOSED) {
1472+ HIP_DEBUG("Not sending CLOSE.\n");
1473+ goto skip_close_msg;
1474+ }
1475+
1476+ /* Build HIP_CLOSE message and send it to the peer */
1477+ if ((msg = hip_msg_alloc()) == NULL) {
1478+ HIP_ERROR("hip_msg_alloc() failed");
1479+ ret = -ENOMEM;
1480+ goto skip_close_msg;
1481+ }
1482+ if ((ret = hip_build_param_contents(msg, &hsock->peer_hit,
1483+ HIP_PARAM_HIT,
1484+ sizeof(hsock->peer_hit))) < 0) {
1485+ HIP_ERROR("hip_build_param_contents() failed\n");
1486+ goto skip_close_msg;
1487+ }
1488+ if ((ret = hip_send_close(msg, 1)) < 0) {
1489+ HIP_ERROR("hip_send_close() failed\n");
1490+ }
1491+
1492+skip_close_msg:
1493+ if (hsock) {
1494+ close(hsock->sock_fd);
1495+ hipl_hsock_delete_and_free(hsock);
1496+ }
1497+ free(msg);
1498+ return ret;
1499+}
1500+
1501+/**
1502+ * Bind a libhipl socket to a local IP address.
1503+ *
1504+ * @param hsock_id the ID of the libhipl socket.
1505+ * @param address the IP address to be bound.
1506+ * @param address_len the length of the @c address.
1507+ * @return 0 on success, negative number on error.
1508+ */
1509+int hipl_bind(const hipl_sock_id hsock_id, const struct sockaddr *const address,
1510+ const socklen_t address_len)
1511+{
1512+ struct hipl_sock *hsock;
1513+
1514+ if ((hsock = hipl_hsock_find(hsock_id)) == NULL) {
1515+ HIP_ERROR("ID %d is not a libhipl socket\n", hsock_id);
1516+ return -1;
1517+ }
1518+
1519+ return hipl_bind_internal(hsock, address, address_len);
1520+}
1521+
1522+/**
1523+ * Switch a libhipl socket to listening mode.
1524+ *
1525+ * @param hsock_id the ID of the libhipl socket.
1526+ * @param backlog the max length of the queue for pending connections.
1527+ * @return zero on success, negative number on error.
1528+ */
1529+int hipl_listen(const hipl_sock_id hsock_id, const int backlog)
1530+{
1531+ struct hipl_sock *hsock;
1532+
1533+ if ((hsock = hipl_hsock_find(hsock_id)) == NULL) {
1534+ HIP_ERROR("ID %d is not a libhipl socket.\n", hsock_id);
1535+ return -1;
1536+ }
1537+
1538+ return listen(hsock->sock_fd, backlog);
1539+}
1540+
1541+/**
1542+ * Send data to a peer.
1543+ *
1544+ * Triggers base exchange if no HIP association exists.
1545+ * @note Data is currently sent unencrypted.
1546+ *
1547+ * @param hsock_id the ID of the libhipl socket for sending data.
1548+ * @param msg data to send.
1549+ * @param len size of the data.
1550+ * @param flags the flags of the socket function @c sendto().
1551+ * @param peername the string representation of the peer.
1552+ * @param port peer's port number.
1553+ * @return number of bytes sent on success, negative number on error.
1554+ */
1555+int hipl_sendto(const hipl_sock_id hsock_id, const void *const msg,
1556+ const size_t len, const int flags,
1557+ const char *const peername, uint16_t port)
1558+{
1559+ struct hipl_sock *hsock;
1560+ struct sockaddr_in6 peer;
1561+ struct msghdr params = { 0 };
1562+ struct iovec iov;
1563+ char *buf = NULL;
1564+ int err = 0;
1565+
1566+ if ((hsock = hipl_hsock_find(hsock_id)) == NULL) {
1567+ HIP_ERROR("ID %d is not a libhipl socket.\n", hsock_id);
1568+ return -1;
1569+ }
1570+ if (hsock->sock_proto == IPPROTO_UDP
1571+ && (peername == NULL || build_peer_hit(peername, port, &peer) < 0)) {
1572+ HIP_ERROR("Invalid argument: peername\n");
1573+ return -EINVAL;
1574+ }
1575+ if (hsock->sock_proto == IPPROTO_TCP
1576+ && hsock->peer_locator.ss_family == 0) {
1577+ HIP_ERROR("Not connected!\n");
1578+ return -ENOTCONN;
1579+ }
1580+ if (msg == NULL) {
1581+ HIP_ERROR("Invalid argument: msg\n");
1582+ return -EINVAL;
1583+ }
1584+
1585+ struct sockaddr_storage dst;
1586+ struct sockaddr_in6 *p6;
1587+ if (hsock->sock_proto == IPPROTO_UDP) {
1588+ hipl_build_addrstorage(&peer.sin6_addr, ntohs(peer.sin6_port), &dst);
1589+ } else {
1590+ dst = hsock->peer_locator;
1591+ p6 = (struct sockaddr_in6 *) &dst;
1592+ p6->sin6_addr = hsock->peer_hit;
1593+ p6->sin6_family = AF_INET6;
1594+ }
1595+ if ((buf = malloc(len)) == NULL) {
1596+ return -ENOMEM;
1597+ }
1598+ memcpy(buf, msg, len);
1599+ iov.iov_base = buf;
1600+ iov.iov_len = len;
1601+ params.msg_name = &dst;
1602+ params.msg_namelen = sizeof(dst);
1603+ params.msg_iovlen = 1;
1604+ params.msg_iov = &iov;
1605+
1606+ if (hipl_lib_bex_feedback()) {
1607+ err = hipl_sendmsg_internal(hsock, &params, flags);
1608+ } else {
1609+ fd_set fdset;
1610+ if (hipl_hsock_ha_state(hsock) == HIP_STATE_UNASSOCIATED) {
1611+ HIP_DEBUG("Sending via hsock %d, Triggering BEX.\n", hsock->sid);
1612+ err = hipl_sendmsg_internal(hsock, &params, flags);
1613+ HIP_IFEL(err != -EWAITBEX, -1, "hipl_sendmsg_internal() failed\n");
1614+ }
1615+ if (hipl_hsock_ha_state(hsock) == HIP_STATE_ESTABLISHED) {
1616+ HIP_DEBUG("Sending via hsock %d, HA established.\n", hsock->sid);
1617+ err = hipl_sendmsg_internal(hsock, &params, flags);
1618+ } else {
1619+ while (hipl_hsock_ha_state(hsock) != HIP_STATE_ESTABLISHED) {
1620+ FD_ZERO(&fdset);
1621+ FD_SET(hsock->sock_fd, &fdset);
1622+ HIP_DEBUG("Sending via hsock %d, Waiting BEX.\n", hsock->sid);
1623+ err = select(hsock->sock_fd + 1, &fdset, NULL, NULL, NULL);
1624+ HIP_IFEL(err < 0, -1, "select(): %s\n", strerror(errno));
1625+ err = hipl_sendmsg_internal(hsock, &params, flags);
1626+ HIP_IFEL(err < 0 && err != -EWAITBEX && err != -EBEXESTABLISHED,
1627+ -1, "hipl_sendmsg_internal() failed\n");
1628+ }
1629+ err = hipl_sendmsg_internal(hsock, &params, flags);
1630+ }
1631+ }
1632+
1633+out_err:
1634+ free(buf);
1635+ return err;
1636+}
1637+
1638+/**
1639+ * Receive data from a peer.
1640+ *
1641+ * Wait for base exchange if no host association exists.
1642+ * @note Data is currently sent unencrypted.
1643+ *
1644+ * @param hsock_id the ID of the libhipl socket for receiving data.
1645+ * @param buf buffer for received data.
1646+ * @param len the size of the @c buf.
1647+ * @param flags the flags of the socket function @c recvfrom().
1648+ * @param peername buffer for the HIT of the associated peer, the size of
1649+ * this buffer should be at least @c HIPL_MAX_PEERNAME.
1650+ * @param port buffer for the port of the associated peer.
1651+ * @return number of bytes received on success,
1652+ * negative number on error,
1653+ * 0 on end-of-file.
1654+ */
1655+int hipl_recvfrom(const hipl_sock_id hsock_id, void *const buf,
1656+ const size_t len, const int flags,
1657+ char *const peername, uint16_t *const port)
1658+{
1659+ struct hipl_sock *hsock = NULL;
1660+ struct msghdr params = { 0 };
1661+ struct iovec iov;
1662+ struct sockaddr_in6 hit;
1663+ int ret;
1664+
1665+ if ((hsock = hipl_hsock_find(hsock_id)) == NULL) {
1666+ HIP_ERROR("ID %d is not a libhipl socket.\n", hsock_id);
1667+ return -1;
1668+ }
1669+ if (hsock->sock_proto == IPPROTO_TCP
1670+ && hsock->peer_locator.ss_family == 0) {
1671+ HIP_ERROR("Not connected!\n");
1672+ return -ENOTCONN;
1673+ }
1674+
1675+ iov.iov_base = buf;
1676+ iov.iov_len = len;
1677+ params.msg_name = &hit;
1678+ params.msg_namelen = sizeof(hit);
1679+ params.msg_iovlen = 1;
1680+ params.msg_iov = &iov;
1681+
1682+ if (!hipl_lib_bex_feedback()) {
1683+ fd_set fdset;
1684+ while (hipl_hsock_ha_state(hsock) != HIP_STATE_ESTABLISHED) {
1685+ FD_ZERO(&fdset);
1686+ FD_SET(hsock->sock_fd, &fdset);
1687+ if (select(hsock->sock_fd + 1, &fdset, NULL, NULL, NULL) < 0) {
1688+ HIP_PERROR("select()");
1689+ }
1690+ ret = hipl_recvmsg_internal(hsock, &params, flags);
1691+ if (ret < 0 && ret != -EWAITBEX && ret != -EBEXESTABLISHED) {
1692+ HIP_ERROR("hipl_recvmsg_internal() failed()\n");
1693+ return ret;
1694+ }
1695+ }
1696+ }
1697+ ret = hipl_recvmsg_internal(hsock, &params, flags);
1698+
1699+ if (peername) {
1700+ inet_ntop(AF_INET6, &hit.sin6_addr, peername, HIPL_MAX_PEERNAME);
1701+ }
1702+ if (port) {
1703+ *port = ntohs(hit.sin6_port);
1704+ }
1705+ return ret;
1706+}
1707+
1708+/**
1709+ * Initiate a connection to a peer.
1710+ *
1711+ * @param hsock_id the ID of the libhipl socket to initiate a connection.
1712+ * @param peername the string representation of the peer.
1713+ * @param port the port number of the peer.
1714+ * @return 0 on success, negative number on error.
1715+ */
1716+int hipl_connect(const hipl_sock_id hsock_id, const char *peername,
1717+ const uint16_t port)
1718+{
1719+ struct hipl_sock *hsock = NULL;
1720+ struct sockaddr_in6 peer;
1721+
1722+ if (peername == NULL || build_peer_hit(peername, port, &peer) < 0) {
1723+ HIP_ERROR("Invalid argument: peername\n");
1724+ return -EINVAL;
1725+ }
1726+ if ((hsock = hipl_hsock_find(hsock_id)) == NULL) {
1727+ HIP_ERROR("ID %d is not a libhipl socket.\n", hsock_id);
1728+ return -1;
1729+ }
1730+
1731+ return hipl_connect_internal(hsock, &peer);
1732+}
1733+
1734+/**
1735+ * Wait for an incoming connection.
1736+ *
1737+ * @param hsock_id the ID of the libhipl socket for waiting connections.
1738+ * @return the ID of the accepted libhipl socket, negative number
1739+ * on error.
1740+ */
1741+int hipl_accept(const hipl_sock_id hsock_id)
1742+{
1743+ struct hipl_sock *hsock = NULL;
1744+
1745+ if ((hsock = hipl_hsock_find(hsock_id)) == NULL) {
1746+ HIP_ERROR("ID %d is not a libhipl socket.\n", hsock_id);
1747+ return -1;
1748+ }
1749+
1750+ return hipl_accept_internal(hsock);
1751+}
1752
1753=== added file 'libhipl/lhipl.h'
1754--- libhipl/lhipl.h 1970-01-01 00:00:00 +0000
1755+++ libhipl/lhipl.h 2012-05-15 09:25:24 +0000
1756@@ -0,0 +1,76 @@
1757+/*
1758+ * Copyright (c) 2010-2012 Aalto University and RWTH Aachen University.
1759+ *
1760+ * Permission is hereby granted, free of charge, to any person
1761+ * obtaining a copy of this software and associated documentation
1762+ * files (the "Software"), to deal in the Software without
1763+ * restriction, including without limitation the rights to use,
1764+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
1765+ * copies of the Software, and to permit persons to whom the
1766+ * Software is furnished to do so, subject to the following
1767+ * conditions:
1768+ *
1769+ * The above copyright notice and this permission notice shall be
1770+ * included in all copies or substantial portions of the Software.
1771+ *
1772+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1773+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
1774+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
1775+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
1776+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
1777+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
1778+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
1779+ * OTHER DEALINGS IN THE SOFTWARE.
1780+ */
1781+
1782+#ifndef HIPL_LIB_HIPL_LHIPL_H
1783+#define HIPL_LIB_HIPL_LHIPL_H
1784+
1785+#include <sys/socket.h>
1786+#include <stdbool.h>
1787+#include <stdint.h>
1788+
1789+
1790+#define EWAITBEX 139000
1791+#define EBEXESTABLISHED 139001
1792+
1793+#define HIPL_MAX_PEERNAME 128
1794+
1795+enum hipl_lib_loglv { HIPL_LIB_LOG_DEBUG, HIPL_LIB_LOG_INFO,
1796+ HIPL_LIB_LOG_ERROR, HIPL_LIB_LOG_NONE };
1797+
1798+typedef uint16_t hipl_sock_id;
1799+
1800+int hipl_lib_init_all(enum hipl_lib_loglv);
1801+
1802+void hipl_lib_set_bex_feedback(bool val);
1803+bool hipl_lib_bex_feedback(void);
1804+
1805+int hipl_lib_set_nonblock(const hipl_sock_id hsock_id, bool on);
1806+int hipl_lib_get_sockfd(const hipl_sock_id hsock_id);
1807+
1808+int hipl_add_peer_info(const char *const hit, const char *const addr);
1809+
1810+int hipl_socket(const int domain, const int type, const int protocol);
1811+
1812+int hipl_close(const hipl_sock_id hsock_id);
1813+
1814+int hipl_listen(const hipl_sock_id hsock_id, const int backlog);
1815+
1816+int hipl_bind(const hipl_sock_id hsock_id, const struct sockaddr *const address,
1817+ const socklen_t address_len);
1818+
1819+int hipl_sendto(const hipl_sock_id hsock_id, const void *const msg,
1820+ const size_t len, const int flags,
1821+ const char *const peername, const uint16_t port);
1822+
1823+int hipl_recvfrom(const hipl_sock_id hsock_id, void *const buf,
1824+ const size_t len, const int flags,
1825+ char *const peername, uint16_t *const port);
1826+
1827+int hipl_connect(const hipl_sock_id hsock_id, const char *const peername,
1828+ const uint16_t port);
1829+
1830+int hipl_accept(const hipl_sock_id hsock_id);
1831+
1832+#endif /* HIPL_LIB_HIPL_LHIPL_H */
1833
1834=== added file 'libhipl/lhipl_operations.c'
1835--- libhipl/lhipl_operations.c 1970-01-01 00:00:00 +0000
1836+++ libhipl/lhipl_operations.c 2012-05-15 09:25:24 +0000
1837@@ -0,0 +1,774 @@
1838+/*
1839+ * Copyright (c) 2012 Aalto University and RWTH Aachen University.
1840+ *
1841+ * Permission is hereby granted, free of charge, to any person
1842+ * obtaining a copy of this software and associated documentation
1843+ * files (the "Software"), to deal in the Software without
1844+ * restriction, including without limitation the rights to use,
1845+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
1846+ * copies of the Software, and to permit persons to whom the
1847+ * Software is furnished to do so, subject to the following
1848+ * conditions:
1849+ *
1850+ * The above copyright notice and this permission notice shall be
1851+ * included in all copies or substantial portions of the Software.
1852+ *
1853+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1854+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
1855+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
1856+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
1857+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
1858+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
1859+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
1860+ * OTHER DEALINGS IN THE SOFTWARE.
1861+ */
1862+
1863+/**
1864+ * @file
1865+ * Provides the internal implementation of the libhipl socket related
1866+ * operations.
1867+ */
1868+
1869+#define _BSD_SOURCE
1870+
1871+#include <sys/socket.h>
1872+#include <errno.h>
1873+#include <fcntl.h>
1874+#include <stdbool.h>
1875+#include <string.h>
1876+#include <unistd.h>
1877+
1878+#include "libcore/builder.h"
1879+#include "libcore/hip_udp.h"
1880+#include "libcore/ife.h"
1881+#include "libcore/prefix.h"
1882+#include "lhipl.h"
1883+#include "lhipl_sock.h"
1884+#include "hadb.h"
1885+#include "hidb.h"
1886+#include "input.h"
1887+#include "netdev.h"
1888+#include "output.h"
1889+#include "lhipl_operations.h"
1890+
1891+
1892+/**
1893+ * Automatically bind to a port for a libhipl socket.
1894+ *
1895+ * @param hsock the libhipl socket for port binding.
1896+ * @return 0 on success, -1 on error.
1897+ */
1898+static int auto_bind(struct hipl_sock *const hsock)
1899+{
1900+ struct sockaddr_storage ss = { 0 };
1901+ struct sockaddr_in *addr4;
1902+ struct sockaddr_in6 *addr6;
1903+
1904+ if (hsock->src_port != 0) {
1905+ HIP_DEBUG("A bound port exists, auto_bind stops\n");
1906+ return 0;
1907+ }
1908+
1909+ if (hsock->sock_family == AF_INET) {
1910+ ss.ss_family = AF_INET;
1911+ addr4 = (struct sockaddr_in *) &ss;
1912+ addr4->sin_port = 0;
1913+ return hipl_bind_internal(hsock, (struct sockaddr *) addr4,
1914+ sizeof(ss));
1915+ } else {
1916+ ss.ss_family = AF_INET6;
1917+ addr6 = (struct sockaddr_in6 *) &ss;
1918+ addr6->sin6_port = 0;
1919+ return hipl_bind_internal(hsock, (struct sockaddr *) addr6,
1920+ sizeof(ss));
1921+ }
1922+}
1923+
1924+static uint16_t get_port_from_saddr(const struct sockaddr *const addr)
1925+{
1926+ const struct sockaddr_in *addr4 = (const struct sockaddr_in *) addr;
1927+ const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *) addr;
1928+
1929+ if (addr->sa_family == AF_INET) {
1930+ return addr4->sin_port;
1931+ } else {
1932+ return addr6->sin6_port;
1933+ }
1934+}
1935+
1936+/**
1937+ * Set global variables in order to inter-operate with libhipdeamon.
1938+ *
1939+ * @param sock_fd the socket file descriptor for sending message.
1940+ * @param local_port the local port for sending message.
1941+ * @param remote_port the remote port for receiving message.
1942+ */
1943+static void set_hip_connection_parameters(const int sock_fd,
1944+ const int local_port,
1945+ const int remote_port)
1946+{
1947+ HIP_DEBUG("Set connection params: fd = %d, lport = %d, rport= %d\n",
1948+ sock_fd, local_port, remote_port);
1949+ hip_nat_sock_output_udp = sock_fd;
1950+ hip_raw_sock_output_v4 = sock_fd;
1951+ hip_raw_sock_output_v6 = sock_fd;
1952+ hip_set_local_nat_udp_port(local_port);
1953+ hip_set_peer_nat_udp_port(remote_port);
1954+}
1955+
1956+/**
1957+ * Check whether a received packet is a HIP control packet.
1958+ *
1959+ * @param buf buffer containing the received packet.
1960+ * @param len size of the @c buf.
1961+ * @param hsock the libhipl socket receiving the packet.
1962+ * @return 1 on a control message, 0 on a user message,
1963+ * negative number on error.
1964+ */
1965+static int hipl_is_control_msg(char *const buf, unsigned int len,
1966+ struct hipl_sock *const hsock)
1967+{
1968+ char udp_pad[HIP_UDP_ZERO_BYTES_LEN] = { 0 };
1969+ struct hip_common *msg;
1970+ struct sockaddr_storage src = { 0 };
1971+ socklen_t srclen = sizeof(src);
1972+
1973+ if (len < sizeof(struct hip_common)) {
1974+ return 0;
1975+ }
1976+
1977+ if (!memcmp(udp_pad, buf, HIP_UDP_ZERO_BYTES_LEN)) {
1978+ HIP_DEBUG("Message is padded\n");
1979+ msg = (struct hip_common *) (buf + HIP_UDP_ZERO_BYTES_LEN);
1980+ len -= HIP_UDP_ZERO_BYTES_LEN;
1981+ } else {
1982+ msg = (struct hip_common *) buf;
1983+ }
1984+
1985+ if (getsockname(hsock->sock_fd, (struct sockaddr *) &src, &srclen) < 0) {
1986+ HIP_PERROR("getsockname()");
1987+ return true;
1988+ }
1989+
1990+ return !hip_verify_network_header(msg, (struct sockaddr *) &src,
1991+ (struct sockaddr *) &hsock->peer_locator,
1992+ len);
1993+}
1994+
1995+static void build_packet_context(struct hip_packet_context *const ctx,
1996+ struct sockaddr *ctx_dst, struct sockaddr *ctx_src)
1997+{
1998+ struct sockaddr_in *s4;
1999+ struct sockaddr_in6 *s6;
2000+
2001+ if (ctx_dst->sa_family == AF_INET) {
2002+ s4 = (struct sockaddr_in *) ctx_dst;
2003+ IPV4_TO_IPV6_MAP(&s4->sin_addr, &ctx->dst_addr);
2004+ ctx->msg_ports.dst_port = ntohs(s4->sin_port);
2005+ } else if (ctx_dst->sa_family == AF_INET6) {
2006+ s6 = (struct sockaddr_in6 *) ctx_dst;
2007+ ctx->dst_addr = s6->sin6_addr;
2008+ ctx->msg_ports.dst_port = ntohs(s6->sin6_port);
2009+ }
2010+
2011+ if (ctx_src->sa_family == AF_INET) {
2012+ s4 = (struct sockaddr_in *) ctx_src;
2013+ IPV4_TO_IPV6_MAP(&s4->sin_addr, &ctx->src_addr);
2014+ ctx->msg_ports.src_port = ntohs(s4->sin_port);
2015+ } else if (ctx_src->sa_family == AF_INET6) {
2016+ s6 = (struct sockaddr_in6 *) ctx_src;
2017+ ctx->src_addr = s6->sin6_addr;
2018+ ctx->msg_ports.src_port = ntohs(s6->sin6_port);
2019+ }
2020+}
2021+
2022+/**
2023+ * Receive and pre-process an incoming message.
2024+ *
2025+ * This function discards UDP packet from an unknown peer, identifies
2026+ * user/control packet, eliminates zero padding in control packets and
2027+ * builds the packet context for handling control packets.
2028+ *
2029+ * @param hsock the libhipl socket for receiving message.
2030+ * @param msg buffer to hold the incoming message.
2031+ * @param flags the flags of socket @c recvmsg().
2032+ * @param ctx the HIP packet context to be built.
2033+ * @param is_user_msg true if the message is a user message, false otherwise.
2034+ * @return negative number on errors, 0 on end-of-file, number of
2035+ * bytes received otherwise.
2036+ */
2037+static int recv_msg_wrapper(struct hipl_sock *const hsock,
2038+ struct msghdr *const msg, const int flags,
2039+ struct hip_packet_context *const ctx,
2040+ bool *is_user_msg)
2041+{
2042+ int ret;
2043+ struct sockaddr_storage our_locator = { 0 };
2044+ socklen_t sslen = sizeof(our_locator);
2045+
2046+ *is_user_msg = true;
2047+ if ((ret = recvmsg(hsock->sock_fd, msg, flags)) < 0) {
2048+ HIP_PERROR("recvmsg()");
2049+ return -1;
2050+ }
2051+
2052+ //in UDP mode, we don't know the peer locator until we receive the
2053+ //first message from the peer. Once we get the peer locator, save it
2054+ //to 'hsock'. We should also fill the packet context for UDP.
2055+ if (hsock->sock_proto == IPPROTO_UDP) {
2056+ if (msg->msg_name != NULL && hsock->peer_locator.ss_family == 0) {
2057+ memcpy(&hsock->peer_locator, msg->msg_name,
2058+ sizeof(hsock->peer_locator));
2059+ }
2060+ if (ctx != NULL) {
2061+ if (getsockname(hsock->sock_fd, (struct sockaddr *) &our_locator,
2062+ &sslen) < 0) {
2063+ HIP_PERROR("getsockname()");
2064+ return -1;
2065+ }
2066+ build_packet_context(ctx, (struct sockaddr *) &our_locator,
2067+ (struct sockaddr *) &hsock->peer_locator);
2068+ }
2069+ }
2070+
2071+ char *buf = msg->msg_iov->iov_base;
2072+ if (hipl_is_control_msg(buf, ret, hsock)) {
2073+ memmove(buf, buf + HIP_UDP_ZERO_BYTES_LEN,
2074+ HIP_MAX_PACKET - HIP_UDP_ZERO_BYTES_LEN);
2075+ ret -= HIP_UDP_ZERO_BYTES_LEN;
2076+ *is_user_msg = false;
2077+ }
2078+
2079+ return ret;
2080+}
2081+
2082+static int nonblock_result_check(const int ret, const int err)
2083+{
2084+ if (ret < 0 && err != EWOULDBLOCK && err != EAGAIN) {
2085+ HIP_ERROR("BEX failed, errno = %d, errstr = %s\n", err,
2086+ strerror(err));
2087+ return -1;
2088+ }
2089+ if (ret < 0 && (err == EWOULDBLOCK || err == EAGAIN)) {
2090+ HIP_DEBUG("BEX returns EWOULDBLOCK or EAGAIN\n");
2091+ return -EWAITBEX;
2092+ }
2093+
2094+ return 0;
2095+}
2096+
2097+/**
2098+ * Wait for a HIP I1 packet and continue performing base exchange.
2099+ *
2100+ * @param hsock the libhipl socket waiting for the I1 packet.
2101+ * @param ctx the HIP packet context for HIP packet processing.
2102+ * @return 0 on success, -1 on error, -EWAITBEX when BEX is pending
2103+ */
2104+static int nonblock_await_bex(struct hipl_sock *const hsock,
2105+ struct hip_packet_context *const ctx)
2106+{
2107+ struct msghdr params = { 0 };
2108+ struct iovec iov;
2109+ struct sockaddr_storage ss;
2110+ bool is_user_msg;
2111+ int ret = 0;
2112+ int flag;
2113+
2114+ params.msg_name = &ss;
2115+ params.msg_namelen = sizeof(ss);
2116+ params.msg_iovlen = 1;
2117+ iov.iov_base = ctx->input_msg;
2118+ iov.iov_len = HIP_MAX_PACKET;
2119+ params.msg_iov = &iov;
2120+
2121+ flag = fcntl(hsock->sock_fd, F_GETFL, 0);
2122+ fcntl(hsock->sock_fd, F_SETFL, flag | O_NONBLOCK);
2123+
2124+ set_hip_connection_parameters(hsock->sock_fd, hsock->src_port, 0);
2125+ ret = recv_msg_wrapper(hsock, &params, 0, ctx, &is_user_msg);
2126+ if ((ret = nonblock_result_check(ret, errno)) != 0) {
2127+ HIP_DEBUG("returns %d\n", ret);
2128+ goto out;
2129+ }
2130+
2131+ if (is_user_msg || hip_receive_control_packet(ctx) < 0) {
2132+ HIP_ERROR("hip_receive_control_packet() failed\n");
2133+ ret = -1;
2134+ }
2135+
2136+out:
2137+ fcntl(hsock->sock_fd, F_SETFL, flag);
2138+ return ret;
2139+}
2140+
2141+/**
2142+ * Trigger BEX in a non-blocking way.
2143+ *
2144+ * @param hsock the libhipl socket to trigger BEX.
2145+ * @param src_hit the source HIT for base exchange.
2146+ * @param dst_hit the destination HIT for base exchange.
2147+ * @param dst_port the destination port.
2148+ * @return -1 on error, -EWAITBEX when the BEX is pending, 0 if sending
2149+ * BEX trigger message successfully.
2150+ */
2151+static int nonblock_trigger_bex(struct hipl_sock *hsock,
2152+ const hip_hit_t *src_hit,
2153+ const hip_hit_t *dst_hit,
2154+ const int dst_port)
2155+{
2156+ struct in6_addr dst_addr;
2157+ int err = 0, flag;
2158+
2159+ flag = fcntl(hsock->sock_fd, F_GETFL, 0);
2160+ fcntl(hsock->sock_fd, F_SETFL, flag | O_NONBLOCK);
2161+
2162+ err = hip_map_id_to_addr(dst_hit, NULL, &dst_addr);
2163+ HIP_IFEL(err < 0, -1, "failed to match hit to IP\n");
2164+ HIP_IFEL(ipv6_addr_any(&dst_addr), -1, "Couldn't map HIT to IP\n");
2165+
2166+ set_hip_connection_parameters(hsock->sock_fd, hsock->src_port, dst_port);
2167+ err = netdev_trigger_bex(src_hit, dst_hit, NULL, NULL, NULL, &dst_addr);
2168+ HIP_DEBUG("netdev_trigger_bex returns %d, errno = %d\n", err, errno);
2169+ err = nonblock_result_check(err, errno);
2170+ if (err == 0) {
2171+ hsock->ha = hip_hadb_find_byhits(src_hit, dst_hit);
2172+ }
2173+
2174+out_err:
2175+ fcntl(hsock->sock_fd, F_SETFL, flag);
2176+ return err;
2177+}
2178+
2179+/**
2180+ * Handle BEX for a libhipl socket.
2181+ *
2182+ * If param @c peer_hit is given, current libhipl socket will be the initiator
2183+ * and trigger the BEX. otherwise, it acts as a responder and waits for an I1
2184+ * message.
2185+ *
2186+ * @param hsock the libhipl socket to handle BEX.
2187+ * @param peer_hit the peer's hit and port.
2188+ * @return -1 on error, -EWAITBEX when the BEX is pending, and
2189+ * -EBEXESTABLISHED when BEX finishes.
2190+ */
2191+static int handle_bex(struct hipl_sock *hsock, struct sockaddr_in6 *peer_hit)
2192+{
2193+ int err = 0;
2194+ struct hip_packet_context ctx = { 0 };
2195+
2196+ // We are the initiator, send I1
2197+ if (hipl_hsock_ha_state(hsock) == HIP_STATE_UNASSOCIATED && peer_hit) {
2198+ hsock->peer_hit = peer_hit->sin6_addr;
2199+
2200+ err = nonblock_trigger_bex(hsock, &hsock->src_hit,
2201+ &peer_hit->sin6_addr,
2202+ ntohs(peer_hit->sin6_port));
2203+ // send I1 successfully, return -EWAITBEX.
2204+ if (err == 0) {
2205+ err = -EWAITBEX;
2206+ }
2207+ return err;
2208+ }
2209+
2210+ // waiting for and handle control messages
2211+ hsock->ha = hip_hadb_find_byhits(&hsock->src_hit, &hsock->peer_hit);
2212+ if (hipl_hsock_ha_state(hsock) != HIP_STATE_ESTABLISHED) {
2213+ ctx.input_msg = hip_msg_alloc();
2214+ ctx.output_msg = hip_msg_alloc();
2215+ HIP_IFEL(!ctx.input_msg || !ctx.output_msg, -ENOMEM,
2216+ "hip_msg_alloc() failed\n");
2217+
2218+ err = nonblock_await_bex(hsock, &ctx);
2219+ HIP_DEBUG("nonb_await_bex returns %d\n", err);
2220+ if (err < 0) { /* -1 or -EWAITBEX */
2221+ goto out_err;
2222+ }
2223+ hsock->peer_hit = ctx.input_msg->hit_sender;
2224+ hsock->ha = hip_hadb_find_byhits(&hsock->src_hit, &hsock->peer_hit);
2225+ }
2226+
2227+ if (hipl_hsock_ha_state(hsock) == HIP_STATE_ESTABLISHED) {
2228+ err = -EBEXESTABLISHED;
2229+ } else {
2230+ err = -EWAITBEX;
2231+ }
2232+
2233+out_err:
2234+ free(ctx.input_msg);
2235+ free(ctx.output_msg);
2236+ return err;
2237+}
2238+
2239+static int validate_udp_peer_addr(const struct hipl_sock *const hsock,
2240+ const struct sockaddr_storage *const saddr)
2241+{
2242+ const struct in6_addr *paddr;
2243+ struct in6_addr peer_addr;
2244+
2245+ if (hsock->sock_proto != IPPROTO_UDP) {
2246+ return 0;
2247+ }
2248+
2249+ switch (saddr->ss_family) {
2250+ case AF_INET:
2251+ IPV4_TO_IPV6_MAP(&((const struct sockaddr_in *) saddr)->sin_addr,
2252+ &peer_addr);
2253+ paddr = &peer_addr;
2254+ break;
2255+
2256+ case AF_INET6:
2257+ paddr = &((const struct sockaddr_in6 *) saddr)->sin6_addr;
2258+ break;
2259+
2260+ default:
2261+ HIP_DEBUG("Unsupported family: %d\n", saddr->ss_family);
2262+ return -1;
2263+ }
2264+
2265+ if (ipv6_addr_cmp(&hsock->ha->peer_addr, paddr)) {
2266+ HIP_DEBUG("Packet not from associated address. Dropping.\n");
2267+ HIP_DEBUG_IN6ADDR("expected", &hsock->ha->peer_addr);
2268+ HIP_DEBUG_IN6ADDR("got", paddr);
2269+ return -1;
2270+ }
2271+
2272+ return 0;
2273+}
2274+
2275+/**
2276+ * Build a @c sockaddr_storage from a given IPv6 address and a port number.
2277+ *
2278+ * If the address is V4MAPPED, the storage family will be @c AF_INET,
2279+ * otherwise the storage family will be @c AF_INET6.
2280+ *
2281+ * @param addr a V6 address or a V4MAPPED address.
2282+ * @param port the port number.
2283+ * @param ss the @c sockaddr_storage to be filled.
2284+ */
2285+void hipl_build_addrstorage(const struct in6_addr *const addr,
2286+ const uint16_t port,
2287+ struct sockaddr_storage *const ss)
2288+{
2289+ HIP_ASSERT(addr && ss);
2290+ memset(ss, 0, sizeof(*ss));
2291+
2292+ if (IN6_IS_ADDR_V4MAPPED(addr)) {
2293+ struct sockaddr_in *const in = (struct sockaddr_in *) ss;
2294+ in->sin_family = AF_INET;
2295+ IPV6_TO_IPV4_MAP(addr, &in->sin_addr);
2296+ in->sin_port = htons(port);
2297+ } else {
2298+ struct sockaddr_in6 *const in6 = (struct sockaddr_in6 *) ss;
2299+ in6->sin6_family = AF_INET6;
2300+ ipv6_addr_copy(&in6->sin6_addr, addr);
2301+ in6->sin6_port = htons(port);
2302+ }
2303+}
2304+
2305+/**
2306+ * Create a libhipl socket.
2307+ *
2308+ * @param family the communications domain of the libhipl socket
2309+ * @param type the socket type of the libhipl socket
2310+ * @param protocol the protocol of the libhipl socket
2311+ * @return the ID of the new libhipl socket on success, negative
2312+ * number otherwise
2313+ */
2314+int hipl_socket_internal(const int family, const int type, const int protocol)
2315+{
2316+ int sock;
2317+ int on = 1, off = 0;
2318+ struct hipl_sock *hsock = NULL;
2319+
2320+ //TODO support IPV6
2321+ if (family == AF_INET6) {
2322+ HIP_ERROR("No IPv6 support yet.\n");
2323+ return -ENOTSUP;
2324+ }
2325+
2326+ sock = socket(family, type, protocol);
2327+ if (family == AF_INET) {
2328+ setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
2329+ setsockopt(sock, IPPROTO_IP, IP_PKTINFO, &on, sizeof(on));
2330+ setsockopt(sock, IPPROTO_IP, IP_RECVERR, &off, sizeof(off));
2331+ setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
2332+ } else {
2333+ setsockopt(sock, IPPROTO_IPV6, IPV6_RECVERR, &off, sizeof(off));
2334+ setsockopt(sock, IPPROTO_IPV6, IPV6_2292PKTINFO, &on, sizeof(on));
2335+ setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
2336+ }
2337+ if ((hsock = hipl_hsock_new(family, type, protocol)) == NULL) {
2338+ HIP_ERROR("Failed to create libhipl socket.\n");
2339+ close(sock);
2340+ return -1;
2341+ }
2342+ hsock->sock_fd = sock;
2343+ hip_get_default_hit(&hsock->src_hit);
2344+
2345+ return hsock->sid;
2346+}
2347+
2348+/**
2349+ * Bind a libhipl socket to a local address.
2350+ *
2351+ * @param hsock the libhipl socket for address binding.
2352+ * @param address the IP address to be bound.
2353+ * @param address_len the length of the @c address.
2354+ * @return 0 on success, -1 on error.
2355+ */
2356+int hipl_bind_internal(struct hipl_sock *const hsock,
2357+ const struct sockaddr *const address,
2358+ const socklen_t address_len)
2359+{
2360+ struct sockaddr_storage laddr = { 0 };
2361+ socklen_t laddr_len = sizeof(laddr);
2362+ uint16_t request_port;
2363+
2364+ request_port = get_port_from_saddr(address);
2365+ if (bind(hsock->sock_fd, address, address_len) < 0) {
2366+ HIP_PERROR("bind error:");
2367+ return -1;
2368+ }
2369+
2370+ /* Ask OS for the assigned port number */
2371+ if (request_port == 0) {
2372+ if (0 > getsockname(hsock->sock_fd, (struct sockaddr *) &laddr,
2373+ &laddr_len)) {
2374+ HIP_PERROR("getsockname() error: ");
2375+ return -1;
2376+ }
2377+ request_port = get_port_from_saddr((struct sockaddr *) &laddr);
2378+ }
2379+
2380+ hsock->src_port = ntohs(request_port);
2381+ HIP_DEBUG("bind to port %d\n", hsock->src_port);
2382+
2383+ return 0;
2384+}
2385+
2386+/**
2387+ * Initiate a connection to a peer.
2388+ *
2389+ * @param hsock the libhipl socket to initiate the connection.
2390+ * @param peer the peer's HIT and port number.
2391+ * @return 0 on success, -1 on error.
2392+ */
2393+int hipl_connect_internal(struct hipl_sock *const hsock,
2394+ const struct sockaddr_in6 *const peer)
2395+{
2396+ struct in6_addr dst_addr = { { { 0 } } };
2397+ struct sockaddr_storage ss;
2398+
2399+ if (ipv6_addr_any(&peer->sin6_addr)) {
2400+ HIP_ERROR("Invalid argument: dst_hit.\n");
2401+ return -1;
2402+ }
2403+
2404+ if (hip_map_id_to_addr(&peer->sin6_addr, NULL, &dst_addr) < 0) {
2405+ return -1;
2406+ }
2407+ if (ipv6_addr_any(&dst_addr)) {
2408+ HIP_ERROR("Couldn't map HIT to IP\n");
2409+ return -1;
2410+ }
2411+
2412+ HIP_DEBUG_IN6ADDR("Dest locator is: ", &dst_addr);
2413+ HIP_DEBUG("Dest locator is V4MAPPED: %d\n", IN6_IS_ADDR_V4MAPPED(&dst_addr));
2414+ hipl_build_addrstorage(&dst_addr, ntohs(peer->sin6_port), &ss);
2415+
2416+ if (connect(hsock->sock_fd, (struct sockaddr *) &ss, sizeof(ss)) < 0) {
2417+ HIP_ERROR("connect(): %s\n", strerror(errno));
2418+ return -1;
2419+ }
2420+
2421+ /* Save related information into hsock */
2422+ hsock->peer_hit = peer->sin6_addr;
2423+ hsock->peer_locator = ss;
2424+ if (hsock->src_port == 0) {
2425+ socklen_t taddr_len = sizeof(ss);
2426+
2427+ if (getsockname(hsock->sock_fd, (struct sockaddr *) &ss,
2428+ &taddr_len) < 0) {
2429+ HIP_PERROR("getsockname() error: ");
2430+ return -1;
2431+ }
2432+ hsock->src_port = ntohs(get_port_from_saddr((struct sockaddr *) &ss));
2433+ }
2434+
2435+ return 0;
2436+}
2437+
2438+/**
2439+ * Wait for an incoming connection on a libhipl socket.
2440+ *
2441+ * @param hsock the libhipl socket waiting for the connection.
2442+ * @return the ID of the accepted libhipl socket on success, -1 on error.
2443+ */
2444+int hipl_accept_internal(struct hipl_sock *const hsock)
2445+{
2446+ int new_fd;
2447+ struct hipl_sock *hsock_new = NULL;
2448+ struct sockaddr_storage ss = { 0 };
2449+ socklen_t ss_len = sizeof(ss);
2450+
2451+ new_fd = accept(hsock->sock_fd, (struct sockaddr *) &ss, &ss_len);
2452+ if (new_fd < 0) {
2453+ HIP_PERROR("accept(): ");
2454+ return -1;
2455+ }
2456+
2457+ hsock_new = hipl_hsock_new(hsock->sock_family, hsock->sock_type,
2458+ hsock->sock_proto);
2459+ hsock_new->src_port = hsock->src_port;
2460+ hsock_new->src_hit = hsock->src_hit;
2461+ hsock_new->sock_fd = new_fd;
2462+ hsock_new->peer_locator = ss;
2463+
2464+ return hsock_new->sid;
2465+}
2466+
2467+/**
2468+ * Receive data from a remote peer.
2469+ *
2470+ * Wait for base exchange if no host association exists.
2471+ * @note Data is currently sent unencrypted.
2472+ * @note Scatter read is not supported yet.
2473+ *
2474+ * @param hsock the libhipl socket for receiving data.
2475+ * @param msg the buffer to hold data and peer information.
2476+ * @param flags the flags of the socket function @c recvmsg().
2477+ * @return number of bytes received on success,
2478+ * 0 on end-of-file,
2479+ * -EWAITBEX when the BEX is pending,
2480+ * -EBEXESTABLISHED when BEX finishes,
2481+ * other negative numbers on error.
2482+ */
2483+ssize_t hipl_recvmsg_internal(struct hipl_sock *const hsock,
2484+ struct msghdr *const msg,
2485+ const int flags)
2486+{
2487+ struct hip_packet_context ctx = { 0 };
2488+ struct msghdr params = { 0 };
2489+ struct iovec iov;
2490+ struct sockaddr_storage recv_remote_addr;
2491+ int err = 0;
2492+ bool is_user_msg;
2493+
2494+ if (msg->msg_iovlen != 1) {
2495+ HIP_ERROR("Invalid iovlen: %d, scatter read is not supported yet\n",
2496+ msg->msg_iovlen);
2497+ return -ENOTSUP;
2498+ }
2499+
2500+ /* Bind to an ephemeral port if the src port hasn't been bound yet */
2501+ if (auto_bind(hsock)) {
2502+ HIP_ERROR("Fail to bind the hip socket.\n");
2503+ return -1;
2504+ }
2505+
2506+ /* Handle BEX if HA hasn't established */
2507+ if (hipl_hsock_ha_state(hsock) != HIP_STATE_ESTABLISHED
2508+ && hipl_hsock_ha_state(hsock) != HIP_STATE_CLOSING) {
2509+ return handle_bex(hsock, NULL);
2510+ }
2511+
2512+ ctx.input_msg = hip_msg_alloc();
2513+ ctx.output_msg = hip_msg_alloc();
2514+ HIP_IFEL(!ctx.input_msg || !ctx.output_msg, -ENOMEM,
2515+ "hip_msg_alloc() failed\n");
2516+ params.msg_name = &recv_remote_addr;
2517+ params.msg_namelen = sizeof(recv_remote_addr);
2518+ params.msg_iovlen = 1;
2519+ iov.iov_base = ctx.input_msg;
2520+ iov.iov_len = HIP_MAX_PACKET;
2521+ params.msg_iov = &iov;
2522+
2523+ err = recv_msg_wrapper(hsock, &params, flags, &ctx, &is_user_msg);
2524+ HIP_IFEL(err < 0, -1, "recv_msg_wrapper() failed\n");
2525+
2526+ if (validate_udp_peer_addr(hsock, &recv_remote_addr) < 0) {
2527+ HIP_IFEL(true, -EAGAIN,
2528+ "Received a packet with invalid peer address, dropping.\n");
2529+ }
2530+ if (!is_user_msg) {
2531+ HIP_DEBUG("receive a hip control message.\n");
2532+ hip_receive_control_packet(&ctx);
2533+ if (hipl_hsock_ha_state(hsock) != HIP_STATE_ESTABLISHED) {
2534+ HIP_DEBUG("HA state change to %d, return 0.\n", hsock->ha->state);
2535+ return 0;
2536+ }
2537+ } else {
2538+ HIP_DEBUG("receive a user message.\n");
2539+ // TODO, if buffer size is too small, we should save it to a internal buffer
2540+ // and only return content with length specified by user's buffer (TCP).
2541+ // return error for UDP in this case.
2542+ struct iovec *iovp;
2543+ struct sockaddr_in6 *hitp;
2544+ hitp = msg->msg_name;
2545+ iovp = msg->msg_iov;
2546+ HIP_IFEL(iovp->iov_len < (unsigned int) err, -1,
2547+ "buffer size too small\n");
2548+ HIP_DEBUG_HIT("ha->hit_peer:", &hsock->ha->hit_peer);
2549+ hitp->sin6_family = AF_INET6;
2550+ hitp->sin6_port = get_port_from_saddr((const struct sockaddr *) &hsock->peer_locator);
2551+ HIP_DEBUG("hitp port: %d\n", ntohs(hitp->sin6_port));
2552+ hitp->sin6_addr = hsock->ha->hit_peer;
2553+ memcpy(iovp->iov_base, ctx.input_msg, err);
2554+ }
2555+
2556+out_err:
2557+ free(ctx.input_msg);
2558+ free(ctx.output_msg);
2559+ return err;
2560+}
2561+
2562+/**
2563+ * Send data to a peer.
2564+ *
2565+ * Trigger base exchange if no host association exists.
2566+ * @note Data is currently sent unencrypted.
2567+ * @note Gather write is not supported yet.
2568+ *
2569+ * @param hsock the libhipl socket for sending data.
2570+ * @param msg containing data, and peer information.
2571+ * @param flags the flags of the socket function @c sendmsg().
2572+ * @return number of bytes sent on success,
2573+ * -EWAITBEX if the BEX is pending,
2574+ * -EBEXESTABLISHED if the BEX finishes,
2575+ * other negative number on error.
2576+ */
2577+ssize_t hipl_sendmsg_internal(struct hipl_sock *const hsock,
2578+ struct msghdr *const msg,
2579+ const int flags)
2580+{
2581+ int fd = hsock->sock_fd;
2582+
2583+ /* Gather write is not supported yet */
2584+ if (msg->msg_iovlen > 1) {
2585+ HIP_ERROR("Invalid iovlen: %d, gather write is not supported\n",
2586+ msg->msg_iovlen);
2587+ return -ENOTSUP;
2588+ }
2589+
2590+ /* Bind to an ephemeral port if the src port hasn't been bound yet */
2591+ if (auto_bind(hsock)) {
2592+ HIP_ERROR("Fail to bind the hip socket.\n");
2593+ return -1;
2594+ }
2595+
2596+ /* Start BEX if HA hasn't established */
2597+ if (hipl_hsock_ha_state(hsock) != HIP_STATE_ESTABLISHED) {
2598+ return handle_bex(hsock, msg->msg_name);
2599+ }
2600+
2601+ /* Determine peer's locator and send out the message */
2602+ HIP_DEBUG("BEX ok, start to send user data\n");
2603+ if (hsock->peer_locator.ss_family == 0) {
2604+ hipl_build_addrstorage(&hsock->ha->peer_addr,
2605+ hsock->ha->peer_udp_port,
2606+ &hsock->peer_locator);
2607+ }
2608+ msg->msg_namelen = sizeof(hsock->peer_locator);
2609+ memcpy(msg->msg_name, &hsock->peer_locator, msg->msg_namelen);
2610+ return sendmsg(fd, msg, flags);
2611+}
2612
2613=== added file 'libhipl/lhipl_operations.h'
2614--- libhipl/lhipl_operations.h 1970-01-01 00:00:00 +0000
2615+++ libhipl/lhipl_operations.h 2012-05-15 09:25:24 +0000
2616@@ -0,0 +1,55 @@
2617+/*
2618+ * Copyright (c) 2012 Aalto University and RWTH Aachen University.
2619+ *
2620+ * Permission is hereby granted, free of charge, to any person
2621+ * obtaining a copy of this software and associated documentation
2622+ * files (the "Software"), to deal in the Software without
2623+ * restriction, including without limitation the rights to use,
2624+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
2625+ * copies of the Software, and to permit persons to whom the
2626+ * Software is furnished to do so, subject to the following
2627+ * conditions:
2628+ *
2629+ * The above copyright notice and this permission notice shall be
2630+ * included in all copies or substantial portions of the Software.
2631+ *
2632+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2633+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
2634+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2635+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
2636+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
2637+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2638+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2639+ * OTHER DEALINGS IN THE SOFTWARE.
2640+ */
2641+
2642+#ifndef HIPL_LIB_HIPL_LHIPL_OPERATIONS_H
2643+#define HIPL_LIB_HIPL_LHIPL_OPERATIONS_H
2644+
2645+#include "lhipl_sock.h"
2646+
2647+
2648+int hipl_socket_internal(const int family, const int type, const int protocol);
2649+
2650+int hipl_bind_internal(struct hipl_sock *const hsock,
2651+ const struct sockaddr *const address,
2652+ const socklen_t address_len);
2653+
2654+int hipl_connect_internal(struct hipl_sock *const hsock,
2655+ const struct sockaddr_in6 *const addr);
2656+
2657+int hipl_accept_internal(struct hipl_sock *const hsock);
2658+
2659+ssize_t hipl_recvmsg_internal(struct hipl_sock *const hsock,
2660+ struct msghdr *const msg,
2661+ const int flags);
2662+
2663+ssize_t hipl_sendmsg_internal(struct hipl_sock *const hsock,
2664+ struct msghdr *const msg,
2665+ const int flags);
2666+
2667+void hipl_build_addrstorage(const struct in6_addr *const addr,
2668+ const uint16_t port,
2669+ struct sockaddr_storage *const ss);
2670+
2671+#endif /* HIPL_LIB_HIPL_LHIPL_OPERATIONS_H */
2672
2673=== added file 'libhipl/lhipl_sock.c'
2674--- libhipl/lhipl_sock.c 1970-01-01 00:00:00 +0000
2675+++ libhipl/lhipl_sock.c 2012-05-15 09:25:24 +0000
2676@@ -0,0 +1,150 @@
2677+/*
2678+ * Copyright (c) 2012 Aalto University and RWTH Aachen University.
2679+ *
2680+ * Permission is hereby granted, free of charge, to any person
2681+ * obtaining a copy of this software and associated documentation
2682+ * files (the "Software"), to deal in the Software without
2683+ * restriction, including without limitation the rights to use,
2684+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
2685+ * copies of the Software, and to permit persons to whom the
2686+ * Software is furnished to do so, subject to the following
2687+ * conditions:
2688+ *
2689+ * The above copyright notice and this permission notice shall be
2690+ * included in all copies or substantial portions of the Software.
2691+ *
2692+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2693+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
2694+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2695+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
2696+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
2697+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2698+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2699+ * OTHER DEALINGS IN THE SOFTWARE.
2700+ */
2701+
2702+/**
2703+ * @file
2704+ * Provides functions for maintaining libhipl sockets.
2705+ */
2706+
2707+#define _BSD_SOURCE
2708+
2709+#include <sys/socket.h>
2710+#include <string.h>
2711+#include <unistd.h>
2712+
2713+#include "libcore/debug.h"
2714+#include "libcore/hashtable.h"
2715+#include "lhipl_sock.h"
2716+
2717+
2718+/* A hashtable to record all opening libhipl sockets */
2719+static HIP_HASHTABLE *hsocks = NULL;
2720+
2721+static unsigned long hipl_sk_hash(const struct hipl_sock *hsock)
2722+{
2723+ return hsock->sid;
2724+}
2725+
2726+STATIC_IMPLEMENT_LHASH_HASH_FN(hipl_sk, struct hipl_sock)
2727+
2728+static int hipl_sk_cmp(const struct hipl_sock *hsock1,
2729+ const struct hipl_sock *hsock2)
2730+{
2731+ return memcmp(&hsock1->sid, &hsock2->sid, sizeof(hsock1->sid));
2732+}
2733+
2734+STATIC_IMPLEMENT_LHASH_COMP_FN(hipl_sk, struct hipl_sock)
2735+
2736+static uint32_t hsock_generate_id(void)
2737+{
2738+ static uint32_t id_generator = HIPL_LIB_HSOCK_ID_MIN;
2739+
2740+ if (id_generator == HIPL_LIB_HSOCK_ID_MAX) {
2741+ id_generator = HIPL_LIB_HSOCK_ID_MIN;
2742+ } else {
2743+ id_generator += 1;
2744+ }
2745+
2746+ return id_generator;
2747+}
2748+
2749+/**
2750+ * Initialize the libhipl socket hashtable.
2751+ */
2752+void hipl_hsock_init(void)
2753+{
2754+ hsocks = hip_ht_init(LHASH_HASH_FN(hipl_sk), LHASH_COMP_FN(hipl_sk));
2755+}
2756+
2757+/**
2758+ * Create a new libhipl socket and save it to the libhipl socket hashtable.
2759+ *
2760+ * @param family the address family of the libhipl socket (INET or INET6).
2761+ * @param type the type of the protocol.
2762+ * @param protocol the protocol of the libhipl socket (TCP or UDP).
2763+ * @return pointer to the created libhipl socket on success, NULL on
2764+ * error.
2765+ */
2766+struct hipl_sock *hipl_hsock_new(const int family, const int type,
2767+ const int protocol)
2768+{
2769+ struct hipl_sock *hsock = NULL;
2770+
2771+ hsock = calloc(sizeof(struct hipl_sock), sizeof(uint8_t));
2772+ if (hsock == NULL) {
2773+ HIP_ERROR("calloc() failed.\n");
2774+ return NULL;
2775+ }
2776+
2777+ hsock->sid = hsock_generate_id();
2778+ hsock->sock_family = family;
2779+ hsock->sock_type = type;
2780+ hsock->sock_proto = protocol;
2781+ hip_ht_add(hsocks, hsock);
2782+ return hsock;
2783+}
2784+
2785+/**
2786+ * Get a libhipl socket by its ID.
2787+ *
2788+ * @param hsock_id the ID of the libhipl socket.
2789+ * @return pointer to the libhipl socket on success, or NULL if the
2790+ * given ID doesn't match any record.
2791+ */
2792+struct hipl_sock *hipl_hsock_find(const uint16_t hsock_id)
2793+{
2794+ struct hipl_sock hsock;
2795+
2796+ hsock.sid = hsock_id;
2797+ return hip_ht_find(hsocks, &hsock);
2798+}
2799+
2800+/**
2801+ * Delete a libhipl socket and free the memory it occupies.
2802+ *
2803+ * @param hsock pointer to the libhipl socket to be deleted.
2804+ */
2805+void hipl_hsock_delete_and_free(struct hipl_sock *const hsock)
2806+{
2807+ struct hipl_sock *deleted_item;
2808+
2809+ deleted_item = hip_ht_delete(hsocks, hsock);
2810+ free(deleted_item);
2811+}
2812+
2813+/**
2814+ * Get the HIP association state of a given libhipl socket.
2815+ *
2816+ * @param hsock the libhipl socket.
2817+ * @return the HIP association state of the libhipl socket.
2818+ */
2819+enum hip_state hipl_hsock_ha_state(const struct hipl_sock *const hsock)
2820+{
2821+ if (!hsock->ha) {
2822+ return HIP_STATE_UNASSOCIATED;
2823+ } else {
2824+ return hsock->ha->state;
2825+ }
2826+}
2827
2828=== added file 'libhipl/lhipl_sock.h'
2829--- libhipl/lhipl_sock.h 1970-01-01 00:00:00 +0000
2830+++ libhipl/lhipl_sock.h 2012-05-15 09:25:24 +0000
2831@@ -0,0 +1,67 @@
2832+/*
2833+ * Copyright (c) 2012 Aalto University and RWTH Aachen University.
2834+ *
2835+ * Permission is hereby granted, free of charge, to any person
2836+ * obtaining a copy of this software and associated documentation
2837+ * files (the "Software"), to deal in the Software without
2838+ * restriction, including without limitation the rights to use,
2839+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
2840+ * copies of the Software, and to permit persons to whom the
2841+ * Software is furnished to do so, subject to the following
2842+ * conditions:
2843+ *
2844+ * The above copyright notice and this permission notice shall be
2845+ * included in all copies or substantial portions of the Software.
2846+ *
2847+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2848+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
2849+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2850+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
2851+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
2852+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2853+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2854+ * OTHER DEALINGS IN THE SOFTWARE.
2855+ */
2856+
2857+#ifndef HIPL_LIB_HIPL_LHIPL_SOCK_H
2858+#define HIPL_LIB_HIPL_LHIPL_SOCK_H
2859+
2860+#include <limits.h>
2861+
2862+#include "libcore/protodefs.h"
2863+#include "libcore/state.h"
2864+
2865+
2866+#define HIPL_LIB_HSOCK_ID_MIN 1
2867+#define HIPL_LIB_HSOCK_ID_MAX USHRT_MAX
2868+#define HIPL_LIB_HSOCK_MAX 1024
2869+
2870+/* The internal information about each libhipl socket.
2871+ *
2872+ * It is generated when a new libhipl socket is created.
2873+ */
2874+struct hipl_sock {
2875+ uint16_t sid; /* libhipl socket ID */
2876+ struct hip_hadb_state *ha;
2877+ hip_hit_t peer_hit;
2878+ struct sockaddr_storage peer_locator;
2879+ hip_hit_t src_hit; /* our HIT */
2880+ int src_port; /* our port number */
2881+ int sock_fd; /* underlying socket */
2882+ int sock_family;
2883+ int sock_type;
2884+ int sock_proto;
2885+};
2886+
2887+void hipl_hsock_init(void);
2888+
2889+enum hip_state hipl_hsock_ha_state(const struct hipl_sock *const hsock);
2890+
2891+struct hipl_sock *hipl_hsock_new(const int family, const int type,
2892+ const int protocol);
2893+
2894+struct hipl_sock *hipl_hsock_find(const uint16_t hsock_id);
2895+
2896+void hipl_hsock_delete_and_free(struct hipl_sock *const hsock);
2897+
2898+#endif /* HIPL_LIB_HIPL_LHIPL_SOCK_H */
2899
2900=== renamed file 'hipd/lsidb.c' => 'libhipl/lsidb.c'
2901=== renamed file 'hipd/lsidb.h' => 'libhipl/lsidb.h'
2902--- hipd/lsidb.h 2012-05-12 06:54:33 +0000
2903+++ libhipl/lsidb.h 2012-05-15 09:25:24 +0000
2904@@ -30,8 +30,8 @@
2905 * host and which LSIs are still available.
2906 */
2907
2908-#ifndef HIPL_HIPD_LSIDB_H
2909-#define HIPL_HIPD_LSIDB_H
2910+#ifndef HIPL_LIBHIPL_LSIDB_H
2911+#define HIPL_LIBHIPL_LSIDB_H
2912
2913 #include <stdbool.h>
2914
2915@@ -40,4 +40,4 @@
2916 bool lsidb_allocate_lsi(hip_lsi_t *const lsi);
2917 bool lsidb_free_lsi(const hip_lsi_t const lsi);
2918
2919-#endif /* HIPL_HIPD_LSIDB_H */
2920+#endif /* HIPL_LIBHIPL_LSIDB_H */
2921
2922=== renamed file 'hipd/maintenance.c' => 'libhipl/maintenance.c'
2923=== renamed file 'hipd/maintenance.h' => 'libhipl/maintenance.h'
2924--- hipd/maintenance.h 2012-03-20 21:45:03 +0000
2925+++ libhipl/maintenance.h 2012-05-15 09:25:24 +0000
2926@@ -23,8 +23,8 @@
2927 * OTHER DEALINGS IN THE SOFTWARE.
2928 */
2929
2930-#ifndef HIPL_HIPD_MAINTENANCE_H
2931-#define HIPL_HIPD_MAINTENANCE_H
2932+#ifndef HIPL_LIBHIPL_MAINTENANCE_H
2933+#define HIPL_LIBHIPL_MAINTENANCE_H
2934
2935 #include <stdint.h>
2936 #include <netinet/in.h>
2937@@ -43,4 +43,4 @@
2938 struct in6_addr *hit_r);
2939 int hipfw_set_esp_relay(int action);
2940
2941-#endif /* HIPL_HIPD_MAINTENANCE_H */
2942+#endif /* HIPL_LIBHIPL_MAINTENANCE_H */
2943
2944=== renamed file 'hipd/nat.c' => 'libhipl/nat.c'
2945=== renamed file 'hipd/nat.h' => 'libhipl/nat.h'
2946--- hipd/nat.h 2012-05-12 06:54:33 +0000
2947+++ libhipl/nat.h 2012-05-15 09:25:24 +0000
2948@@ -35,8 +35,8 @@
2949 * @note All Doxygen comments have been added in version 1.1.
2950 */
2951
2952-#ifndef HIPL_HIPD_NAT_H
2953-#define HIPL_HIPD_NAT_H
2954+#ifndef HIPL_LIBHIPL_NAT_H
2955+#define HIPL_LIBHIPL_NAT_H
2956
2957 #include "libcore/protodefs.h"
2958
2959@@ -52,4 +52,4 @@
2960 int hip_nat_refresh_port(void);
2961 int hip_user_nat_mode(int nat_mode);
2962
2963-#endif /* HIPL_HIPD_NAT_H */
2964+#endif /* HIPL_LIBHIPL_NAT_H */
2965
2966=== renamed file 'hipd/netdev.c' => 'libhipl/netdev.c'
2967=== renamed file 'hipd/netdev.h' => 'libhipl/netdev.h'
2968--- hipd/netdev.h 2012-05-12 06:54:33 +0000
2969+++ libhipl/netdev.h 2012-05-15 09:25:24 +0000
2970@@ -29,8 +29,8 @@
2971 * events over netlink from the kernel.
2972 */
2973
2974-#ifndef HIPL_HIPD_NETDEV_H
2975-#define HIPL_HIPD_NETDEV_H
2976+#ifndef HIPL_LIBHIPL_NETDEV_H
2977+#define HIPL_LIBHIPL_NETDEV_H
2978
2979 #include <stdbool.h>
2980 #include <netinet/in.h>
2981@@ -73,4 +73,4 @@
2982 int hip_map_id_to_addr(const hip_hit_t *hit, const hip_lsi_t *lsi,
2983 struct in6_addr *addr);
2984
2985-#endif /* HIPL_HIPD_NETDEV_H */
2986+#endif /* HIPL_LIBHIPL_NETDEV_H */
2987
2988=== renamed file 'hipd/nsupdate.c' => 'libhipl/nsupdate.c'
2989=== renamed file 'hipd/nsupdate.h' => 'libhipl/nsupdate.h'
2990--- hipd/nsupdate.h 2011-11-25 17:56:24 +0000
2991+++ libhipl/nsupdate.h 2012-05-15 09:25:24 +0000
2992@@ -33,12 +33,12 @@
2993 * @brief and nsupdate by hip_send_locators_to_all_peers and hipd_init
2994 */
2995
2996-#ifndef HIPL_HIPD_NSUPDATE_H
2997-#define HIPL_HIPD_NSUPDATE_H
2998+#ifndef HIPL_LIBHIPL_NSUPDATE_H
2999+#define HIPL_LIBHIPL_NSUPDATE_H
3000
3001 void hip_set_nsupdate_status(int status);
3002 int hip_get_nsupdate_status(void);
3003
3004 int nsupdate(int start);
3005
3006-#endif /* HIPL_HIPD_NSUPDATE_H */
3007+#endif /* HIPL_LIBHIPL_NSUPDATE_H */
3008
3009=== renamed file 'hipd/opp_mode.c' => 'libhipl/opp_mode.c'
3010=== renamed file 'hipd/opp_mode.h' => 'libhipl/opp_mode.h'
3011--- hipd/opp_mode.h 2012-05-12 06:54:33 +0000
3012+++ libhipl/opp_mode.h 2012-05-15 09:25:24 +0000
3013@@ -31,8 +31,8 @@
3014 * @brief Interface of the HIP opportunistic mode
3015 */
3016
3017-#ifndef HIPL_HIPD_OPP_MODE_H
3018-#define HIPL_HIPD_OPP_MODE_H
3019+#ifndef HIPL_LIBHIPL_OPP_MODE_H
3020+#define HIPL_LIBHIPL_OPP_MODE_H
3021
3022 #include <netinet/in.h>
3023
3024@@ -43,4 +43,4 @@
3025 const struct in6_addr *const src_addr);
3026 int hip_handle_opp_r1(struct hip_packet_context *ctx);
3027
3028-#endif /* HIPL_HIPD_OPP_MODE_H */
3029+#endif /* HIPL_LIBHIPL_OPP_MODE_H */
3030
3031=== renamed file 'hipd/output.c' => 'libhipl/output.c'
3032--- hipd/output.c 2012-05-12 06:54:33 +0000
3033+++ libhipl/output.c 2012-05-15 09:25:24 +0000
3034@@ -848,7 +848,8 @@
3035 HIP_ASSERT(!hit_is_opportunistic_hit(&ctx->input_msg->hit_receiver));
3036
3037 /* Case: I ----->IPv4---> RVS ---IPv6---> R */
3038- if (IN6_IS_ADDR_V4MAPPED(r1_src_addr) !=
3039+ if (!hipl_is_libhip_mode() &&
3040+ IN6_IS_ADDR_V4MAPPED(r1_src_addr) !=
3041 IN6_IS_ADDR_V4MAPPED(r1_dst_addr)) {
3042 HIP_DEBUG_IN6ADDR("r1_src_addr", r1_src_addr);
3043 HIP_DEBUG_IN6ADDR("r1_dst_addr", r1_dst_addr);
3044@@ -1217,6 +1218,9 @@
3045 goto out_err;
3046 }
3047
3048+ if (hipl_is_libhip_mode()) {
3049+ udp = 1;
3050+ }
3051 dst_is_ipv4 = IN6_IS_ADDR_V4MAPPED(peer_addr);
3052 len = hip_get_msg_total_len(msg);
3053
3054@@ -1245,13 +1249,16 @@
3055 if (local_addr) {
3056 HIP_DEBUG("local address given\n");
3057 memcpy(&my_addr, local_addr, sizeof(struct in6_addr));
3058- } else {
3059+ } else if (!hipl_is_libhip_mode()) {
3060 HIP_DEBUG("no local address, selecting one\n");
3061 HIP_IFEL(hip_select_source_address(&my_addr, peer_addr), -1,
3062 "Cannot find source address\n");
3063+ } else {
3064+ memset(&my_addr, 0, sizeof(my_addr));
3065 }
3066
3067- src_is_ipv4 = IN6_IS_ADDR_V4MAPPED(&my_addr);
3068+ src_is_ipv4 = IN6_IS_ADDR_V4MAPPED(&my_addr) ||
3069+ (dst_is_ipv4 && ipv6_addr_any(&my_addr));
3070
3071 if (src_is_ipv4) {
3072 IPV6_TO_IPV4_MAP(&my_addr, &src4->sin_addr);
3073@@ -1305,9 +1312,10 @@
3074
3075 /* Handover may cause e.g. on-link duplicate address detection
3076 * which may cause bind to fail. */
3077-
3078- HIP_IFEL(bind(hip_raw_sock_output, (struct sockaddr *) &src, sa_size),
3079- -1, "Binding to raw sock failed\n");
3080+ if (!hipl_is_libhip_mode()) {
3081+ HIP_IFEL(bind(hip_raw_sock_output, (struct sockaddr *) &src, sa_size),
3082+ -1, "Binding to raw sock failed\n");
3083+ }
3084
3085 #if (HIP_SIMULATE_PACKET_LOSS_PROBABILITY > 0)
3086 if (HIP_SIMULATE_PACKET_LOSS && HIP_SIMULATE_PACKET_IS_LOST()) {
3087@@ -1324,18 +1332,25 @@
3088 len = hip_get_msg_total_len(msg);
3089
3090 if (udp) {
3091- struct udphdr *uh = (struct udphdr *) msg;
3092-
3093- /* Insert 32 bits of zero bytes between UDP and HIP */
3094- memmove((char *) msg + HIP_UDP_ZERO_BYTES_LEN + sizeof(struct udphdr), msg, len);
3095- memset(msg, 0, HIP_UDP_ZERO_BYTES_LEN + sizeof(struct udphdr));
3096- len += HIP_UDP_ZERO_BYTES_LEN + sizeof(struct udphdr);
3097-
3098- uh->source = htons(src_port);
3099- uh->dest = htons(dst_port);
3100- uh->len = htons(len);
3101- uh->check = 0;
3102- memmoved = 1;
3103+ if (!hipl_is_libhip_mode()) {
3104+ /* Insert 32 bits of zero bytes between UDP and HIP */
3105+ memmove((char *) msg + HIP_UDP_ZERO_BYTES_LEN + sizeof(struct udphdr), msg, len);
3106+ memset(msg, 0, HIP_UDP_ZERO_BYTES_LEN + sizeof(struct udphdr));
3107+ len += HIP_UDP_ZERO_BYTES_LEN + sizeof(struct udphdr);
3108+
3109+ struct udphdr *uh = (struct udphdr *) msg;
3110+ uh->source = htons(src_port);
3111+ uh->dest = htons(dst_port);
3112+ uh->len = htons(len);
3113+ uh->check = 0;
3114+ } else {
3115+ memmove((char *) msg + HIP_UDP_ZERO_BYTES_LEN, msg, len);
3116+ memset(msg, 0, HIP_UDP_ZERO_BYTES_LEN);
3117+ len += HIP_UDP_ZERO_BYTES_LEN;
3118+
3119+ dst4->sin_port = htons(dst_port);
3120+ }
3121+ memmoved = 1;
3122 }
3123
3124 sent = sendto(hip_raw_sock_output, msg, len, 0,
3125
3126=== renamed file 'hipd/output.h' => 'libhipl/output.h'
3127--- hipd/output.h 2012-05-12 06:54:33 +0000
3128+++ libhipl/output.h 2012-05-15 09:25:24 +0000
3129@@ -23,8 +23,8 @@
3130 * OTHER DEALINGS IN THE SOFTWARE.
3131 */
3132
3133-#ifndef HIPL_HIPD_OUTPUT_H
3134-#define HIPL_HIPD_OUTPUT_H
3135+#ifndef HIPL_LIBHIPL_OUTPUT_H
3136+#define HIPL_LIBHIPL_OUTPUT_H
3137
3138 #include <netinet/in.h>
3139
3140@@ -116,4 +116,4 @@
3141 in_port_t src_port, in_port_t dst_port,
3142 const void *msg, int length);
3143
3144-#endif /* HIPL_HIPD_OUTPUT_H */
3145+#endif /* HIPL_LIBHIPL_OUTPUT_H */
3146
3147=== renamed file 'hipd/pkt_handling.c' => 'libhipl/pkt_handling.c'
3148=== renamed file 'hipd/pkt_handling.h' => 'libhipl/pkt_handling.h'
3149--- hipd/pkt_handling.h 2012-05-12 06:54:33 +0000
3150+++ libhipl/pkt_handling.h 2012-05-15 09:25:24 +0000
3151@@ -23,8 +23,8 @@
3152 * OTHER DEALINGS IN THE SOFTWARE.
3153 */
3154
3155-#ifndef HIPL_HIPD_PKT_HANDLING_H
3156-#define HIPL_HIPD_PKT_HANDLING_H
3157+#ifndef HIPL_LIBHIPL_PKT_HANDLING_H
3158+#define HIPL_LIBHIPL_PKT_HANDLING_H
3159
3160 #include <stdint.h>
3161
3162@@ -44,4 +44,4 @@
3163
3164 void hip_uninit_handle_functions(void);
3165
3166-#endif /* HIPL_HIPD_PKT_HANDLING_H */
3167+#endif /* HIPL_LIBHIPL_PKT_HANDLING_H */
3168
3169=== renamed file 'hipd/registration.c' => 'libhipl/registration.c'
3170=== renamed file 'hipd/registration.h' => 'libhipl/registration.h'
3171--- hipd/registration.h 2012-05-12 06:54:33 +0000
3172+++ libhipl/registration.h 2012-05-15 09:25:24 +0000
3173@@ -31,8 +31,8 @@
3174 * @see hiprelay.h
3175 */
3176
3177-#ifndef HIPL_HIPD_REGISTRATION_H
3178-#define HIPL_HIPD_REGISTRATION_H
3179+#ifndef HIPL_LIBHIPL_REGISTRATION_H
3180+#define HIPL_LIBHIPL_REGISTRATION_H
3181
3182 #include <stdint.h>
3183 #include <sys/types.h>
3184@@ -80,4 +80,4 @@
3185 int hip_handle_reg_from(struct hip_hadb_state *entry, struct hip_common *msg);
3186 int hip_handle_req_user_msg(const struct hip_common *const msg);
3187
3188-#endif /* HIPL_HIPD_REGISTRATION_H */
3189+#endif /* HIPL_LIBHIPL_REGISTRATION_H */
3190
3191=== renamed file 'hipd/user.c' => 'libhipl/user.c'
3192=== renamed file 'hipd/user.h' => 'libhipl/user.h'
3193--- hipd/user.h 2012-05-12 06:54:33 +0000
3194+++ libhipl/user.h 2012-05-15 09:25:24 +0000
3195@@ -23,8 +23,8 @@
3196 * OTHER DEALINGS IN THE SOFTWARE.
3197 */
3198
3199-#ifndef HIPL_HIPD_USER_H
3200-#define HIPL_HIPD_USER_H
3201+#ifndef HIPL_LIBHIPL_USER_H
3202+#define HIPL_LIBHIPL_USER_H
3203
3204 #include <netinet/in.h>
3205
3206@@ -43,4 +43,4 @@
3207 int hip_handle_user_msg(struct hip_common *msg,
3208 struct sockaddr_in6 *src);
3209
3210-#endif /* HIPL_HIPD_USER_H */
3211+#endif /* HIPL_LIBHIPL_USER_H */
3212
3213=== renamed file 'hipd/user_ipsec_hipd_msg.c' => 'libhipl/user_ipsec_hipd_msg.c'
3214=== renamed file 'hipd/user_ipsec_hipd_msg.h' => 'libhipl/user_ipsec_hipd_msg.h'
3215--- hipd/user_ipsec_hipd_msg.h 2012-05-12 06:54:33 +0000
3216+++ libhipl/user_ipsec_hipd_msg.h 2012-05-15 09:25:24 +0000
3217@@ -30,8 +30,8 @@
3218 * @brief userspace IPsec hipd <-> hipfw communication
3219 */
3220
3221-#ifndef HIPL_HIPD_USER_IPSEC_HIPD_MSG_H
3222-#define HIPL_HIPD_USER_IPSEC_HIPD_MSG_H
3223+#ifndef HIPL_LIBHIPL_USER_IPSEC_HIPD_MSG_H
3224+#define HIPL_LIBHIPL_USER_IPSEC_HIPD_MSG_H
3225
3226 #include <stdint.h>
3227 #include <netinet/in.h>
3228@@ -57,4 +57,4 @@
3229 const int dst_port);
3230 struct hip_common *create_flush_all_sa_msg(void);
3231
3232-#endif /* HIPL_HIPD_USER_IPSEC_HIPD_MSG_H */
3233+#endif /* HIPL_LIBHIPL_USER_IPSEC_HIPD_MSG_H */
3234
3235=== renamed file 'hipd/user_ipsec_sadb_api.c' => 'libhipl/user_ipsec_sadb_api.c'
3236=== renamed file 'hipd/user_ipsec_sadb_api.h' => 'libhipl/user_ipsec_sadb_api.h'
3237--- hipd/user_ipsec_sadb_api.h 2012-05-12 06:54:33 +0000
3238+++ libhipl/user_ipsec_sadb_api.h 2012-05-15 09:25:24 +0000
3239@@ -31,8 +31,8 @@
3240 * @brief API used by the hipd to set up and maintain userspace IPsec state
3241 */
3242
3243-#ifndef HIPL_HIPD_USER_IPSEC_SADB_API_H
3244-#define HIPL_HIPD_USER_IPSEC_SADB_API_H
3245+#ifndef HIPL_LIBHIPL_USER_IPSEC_SADB_API_H
3246+#define HIPL_LIBHIPL_USER_IPSEC_SADB_API_H
3247
3248 #include <stdint.h>
3249 #include <netinet/in.h>
3250@@ -52,4 +52,4 @@
3251
3252 int hip_userspace_ipsec_setup_default_sp_prefix_pair(void);
3253
3254-#endif /* HIPL_HIPD_USER_IPSEC_SADB_API_H */
3255+#endif /* HIPL_LIBHIPL_USER_IPSEC_SADB_API_H */
3256
3257=== modified file 'modules/cert/hipd/cert.c'
3258--- modules/cert/hipd/cert.c 2012-05-12 06:54:33 +0000
3259+++ modules/cert/hipd/cert.c 2012-05-15 09:25:24 +0000
3260@@ -33,13 +33,13 @@
3261 #include <stdlib.h>
3262 #include <string.h>
3263
3264-#include "hipd/hipd.h"
3265-#include "hipd/pkt_handling.h"
3266 #include "libcore/builder.h"
3267 #include "libcore/cert.h"
3268 #include "libcore/debug.h"
3269 #include "libcore/ife.h"
3270 #include "libcore/protodefs.h"
3271+#include "libhipl/hipd.h"
3272+#include "libhipl/pkt_handling.h"
3273 #include "modules/midauth/hipd/midauth.h"
3274 #include "modules/update/hipd/update.h"
3275 #include "cert.h"
3276
3277=== modified file 'modules/heartbeat/hipd/heartbeat.c'
3278--- modules/heartbeat/hipd/heartbeat.c 2012-05-12 06:54:33 +0000
3279+++ modules/heartbeat/hipd/heartbeat.c 2012-05-15 09:25:24 +0000
3280@@ -65,15 +65,6 @@
3281 #include <sys/types.h>
3282 #include <sys/socket.h>
3283
3284-#include "hipd/close.h"
3285-#include "hipd/hadb.h"
3286-#include "hipd/init.h"
3287-#include "hipd/hip_socket.h"
3288-#include "hipd/maintenance.h"
3289-#include "hipd/nat.h"
3290-#include "hipd/output.h"
3291-#include "hipd/pkt_handling.h"
3292-#include "hipd/user.h"
3293 #include "libcore/common.h"
3294 #include "libcore/debug.h"
3295 #include "libcore/icomm.h"
3296@@ -84,6 +75,15 @@
3297 #include "libcore/straddr.h"
3298 #include "libcore/modularization.h"
3299 #include "libcore/gpl/nlink.h"
3300+#include "libhipl/close.h"
3301+#include "libhipl/hadb.h"
3302+#include "libhipl/hip_socket.h"
3303+#include "libhipl/init.h"
3304+#include "libhipl/maintenance.h"
3305+#include "libhipl/nat.h"
3306+#include "libhipl/output.h"
3307+#include "libhipl/pkt_handling.h"
3308+#include "libhipl/user.h"
3309 #include "heartbeat.h"
3310
3311 #define HIP_MAX_ICMP_PACKET 512
3312
3313=== modified file 'modules/heartbeat_update/hipd/hb_update.c'
3314--- modules/heartbeat_update/hipd/hb_update.c 2012-05-12 06:54:33 +0000
3315+++ modules/heartbeat_update/hipd/hb_update.c 2012-05-15 09:25:24 +0000
3316@@ -1,5 +1,5 @@
3317 /*
3318- * Copyright (c) 2010 Aalto University and RWTH Aachen University.
3319+ * Copyright (c) 2010, 2012 Aalto University and RWTH Aachen University.
3320 *
3321 * Permission is hereby granted, free of charge, to any person
3322 * obtaining a copy of this software and associated documentation
3323@@ -40,12 +40,12 @@
3324 #include <stdint.h>
3325 #include <stdlib.h>
3326
3327-#include "hipd/hadb.h"
3328-#include "hipd/maintenance.h"
3329 #include "libcore/builder.h"
3330 #include "libcore/common.h"
3331 #include "libcore/debug.h"
3332 #include "libcore/protodefs.h"
3333+#include "libhipl/hadb.h"
3334+#include "libhipl/maintenance.h"
3335 #include "modules/update/hipd/update.h"
3336 #include "modules/update/hipd/update_locator.h"
3337 #include "hb_update.h"
3338
3339=== modified file 'modules/midauth/hipd/midauth.c'
3340--- modules/midauth/hipd/midauth.c 2012-05-12 06:54:33 +0000
3341+++ modules/midauth/hipd/midauth.c 2012-05-15 09:25:24 +0000
3342@@ -33,8 +33,6 @@
3343 #include <stdint.h>
3344 #include <string.h>
3345
3346-#include "hipd/hidb.h"
3347-#include "hipd/pkt_handling.h"
3348 #include "libcore/builder.h"
3349 #include "libcore/common.h"
3350 #include "libcore/ife.h"
3351@@ -42,6 +40,8 @@
3352 #include "libcore/protodefs.h"
3353 #include "libcore/solve.h"
3354 #include "libcore/state.h"
3355+#include "libhipl/hidb.h"
3356+#include "libhipl/pkt_handling.h"
3357 #include "modules/midauth/lib/midauth_builder.h"
3358 #include "modules/update/hipd/update.h"
3359 #include "midauth.h"
3360
3361=== modified file 'modules/update/hipd/update.c'
3362--- modules/update/hipd/update.c 2012-05-12 06:54:33 +0000
3363+++ modules/update/hipd/update.c 2012-05-15 09:25:24 +0000
3364@@ -37,17 +37,6 @@
3365 #include <string.h>
3366
3367 #include "config.h"
3368-#include "hipd/cookie.h"
3369-#include "hipd/hadb.h"
3370-#include "hipd/hidb.h"
3371-#include "hipd/hipd.h"
3372-#include "hipd/input.h"
3373-#include "hipd/maintenance.h"
3374-#include "hipd/netdev.h"
3375-#include "hipd/nsupdate.h"
3376-#include "hipd/output.h"
3377-#include "hipd/pkt_handling.h"
3378-#include "hipd/user.h"
3379 #include "libcore/builder.h"
3380 #include "libcore/debug.h"
3381 #include "libcore/hip_udp.h"
3382@@ -57,6 +46,17 @@
3383 #include "libcore/prefix.h"
3384 #include "libcore/state.h"
3385 #include "libcore/performance.h"
3386+#include "libhipl/cookie.h"
3387+#include "libhipl/hadb.h"
3388+#include "libhipl/hidb.h"
3389+#include "libhipl/hipd.h"
3390+#include "libhipl/input.h"
3391+#include "libhipl/maintenance.h"
3392+#include "libhipl/netdev.h"
3393+#include "libhipl/nsupdate.h"
3394+#include "libhipl/output.h"
3395+#include "libhipl/pkt_handling.h"
3396+#include "libhipl/user.h"
3397 #include "update_builder.h"
3398 #include "update_locator.h"
3399 #include "update_param_handling.h"
3400
3401=== modified file 'modules/update/hipd/update_builder.c'
3402--- modules/update/hipd/update_builder.c 2012-05-12 06:54:33 +0000
3403+++ modules/update/hipd/update_builder.c 2012-05-15 09:25:24 +0000
3404@@ -34,12 +34,12 @@
3405 #include <string.h>
3406 #include <errno.h>
3407
3408-#include "hipd/hadb.h"
3409-#include "hipd/netdev.h"
3410 #include "libcore/builder.h"
3411 #include "libcore/ife.h"
3412 #include "libcore/list.h"
3413 #include "libcore/prefix.h"
3414+#include "libhipl/hadb.h"
3415+#include "libhipl/netdev.h"
3416 #include "update_builder.h"
3417
3418 enum hip_locator_traffic_type {
3419
3420=== modified file 'modules/update/hipd/update_locator.c'
3421--- modules/update/hipd/update_locator.c 2012-05-12 06:54:33 +0000
3422+++ modules/update/hipd/update_locator.c 2012-05-15 09:25:24 +0000
3423@@ -1,5 +1,5 @@
3424 /*
3425- * Copyright (c) 2010 Aalto University and RWTH Aachen University.
3426+ * Copyright (c) 2010, 2012 Aalto University and RWTH Aachen University.
3427 *
3428 * Permission is hereby granted, free of charge, to any person
3429 * obtaining a copy of this software and associated documentation
3430@@ -34,11 +34,11 @@
3431 #include <string.h>
3432 #include <openssl/lhash.h>
3433
3434-#include "hipd/maintenance.h"
3435 #include "libcore/builder.h"
3436 #include "libcore/debug.h"
3437 #include "libcore/ife.h"
3438 #include "libcore/protodefs.h"
3439+#include "libhipl/maintenance.h"
3440 #include "update_builder.h"
3441 #include "update.h"
3442 #include "update_locator.h"
3443
3444=== added file 'test/check_libhipl.c'
3445--- test/check_libhipl.c 1970-01-01 00:00:00 +0000
3446+++ test/check_libhipl.c 2012-05-15 09:25:24 +0000
3447@@ -0,0 +1,221 @@
3448+/*
3449+ * Copyright (c) 2012 Aalto University and RWTH Aachen University.
3450+ *
3451+ * Permission is hereby granted, free of charge, to any person
3452+ * obtaining a copy of this software and associated documentation
3453+ * files (the "Software"), to deal in the Software without
3454+ * restriction, including without limitation the rights to use,
3455+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
3456+ * copies of the Software, and to permit persons to whom the
3457+ * Software is furnished to do so, subject to the following
3458+ * conditions:
3459+ *
3460+ * The above copyright notice and this permission notice shall be
3461+ * included in all copies or substantial portions of the Software.
3462+ *
3463+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
3464+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
3465+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
3466+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
3467+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
3468+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
3469+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
3470+ * OTHER DEALINGS IN THE SOFTWARE.
3471+ */
3472+
3473+/**
3474+ * @file
3475+ * @brief Tests of libhipl on localhost (see doc/HACKING on unit tests).
3476+ */
3477+
3478+#include <arpa/inet.h>
3479+#include <check.h>
3480+#include <errno.h>
3481+#include <stdbool.h>
3482+#include <stdio.h>
3483+#include <stdlib.h>
3484+#include <sys/select.h>
3485+#include <unistd.h>
3486+
3487+#include "libhipl/hidb.h"
3488+#include "libhipl/lhipl.h"
3489+
3490+#ifndef max
3491+#define max(a, b) (((a) > (b)) ? (a) : (b))
3492+#endif
3493+
3494+#define LO_IP "127.0.0.1"
3495+#define TEST_MSG "Hello Sailor!"
3496+#define SEND_PORT 22345
3497+#define RECV_PORT 22300
3498+
3499+static struct in6_addr lo_hit;
3500+static char peername[HIPL_MAX_PEERNAME];
3501+static struct sockaddr_in send_addr;
3502+static struct sockaddr_in recv_addr;
3503+
3504+static int sender;
3505+static int receiver;
3506+
3507+static void test_libhipl_lo_init(int proto)
3508+{
3509+ if (hipl_lib_init_all(HIPL_LIB_LOG_NONE) < 0) {
3510+ fail("hipl_lib_init_all");
3511+ }
3512+
3513+ fail_if(hip_get_default_hit(&lo_hit) < 0, "Failed to load local hit");
3514+ inet_ntop(AF_INET6, &lo_hit, peername, HIPL_MAX_PEERNAME);
3515+ fail_if(hipl_add_peer_info(peername, LO_IP) < 0,
3516+ "Failed to insert peer info");
3517+
3518+ send_addr.sin_family = AF_INET;
3519+ inet_pton(AF_INET, LO_IP, &send_addr.sin_addr);
3520+ send_addr.sin_port = htons(SEND_PORT);
3521+ recv_addr.sin_family = AF_INET;
3522+ inet_pton(AF_INET, LO_IP, &recv_addr.sin_addr);
3523+ recv_addr.sin_port = htons(RECV_PORT);
3524+
3525+ if (proto == IPPROTO_TCP) {
3526+ sender = hipl_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
3527+ receiver = hipl_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
3528+ } else if (proto == IPPROTO_UDP) {
3529+ sender = hipl_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
3530+ receiver = hipl_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
3531+ }
3532+ fail_if(sender <= 0 || receiver <= 0);
3533+
3534+ if (hipl_bind(sender, (struct sockaddr *) &send_addr,
3535+ sizeof(send_addr)) < 0) {
3536+ fail("hipl_bind() for sender");
3537+ }
3538+ if (hipl_bind(receiver, (struct sockaddr *) &recv_addr,
3539+ sizeof(recv_addr)) < 0) {
3540+ fail("hipl_bind() for receiver");
3541+ }
3542+
3543+ hipl_lib_set_bex_feedback(true);
3544+}
3545+
3546+static void test_libhipl_lo_main_loop(int send_hid, int recv_hid)
3547+{
3548+ char peer[HIPL_MAX_PEERNAME];
3549+ uint16_t peer_port;
3550+ char buf[1024];
3551+ fd_set rset;
3552+ int ret = 0;
3553+ int maxfd = max(hipl_lib_get_sockfd(send_hid),
3554+ hipl_lib_get_sockfd(recv_hid));
3555+
3556+ //trigger BEX
3557+ ret = hipl_sendto(send_hid, TEST_MSG, strlen(TEST_MSG), 0,
3558+ peername, RECV_PORT);
3559+ fail_if(ret != -EWAITBEX);
3560+
3561+ while (1) {
3562+ FD_ZERO(&rset);
3563+ FD_SET(hipl_lib_get_sockfd(send_hid), &rset);
3564+ FD_SET(hipl_lib_get_sockfd(recv_hid), &rset);
3565+ fail_if(select(maxfd + 1, &rset, NULL, NULL, NULL) < 0);
3566+
3567+ if (FD_ISSET(hipl_lib_get_sockfd(recv_hid), &rset)) {
3568+ ret = hipl_recvfrom(recv_hid, buf, 1024, 0, peer, &peer_port);
3569+ fail_if(ret < 0 && ret != -EWAITBEX && ret != -EBEXESTABLISHED);
3570+ fail_if(ret > 0 && ret != strlen(TEST_MSG));
3571+
3572+ if (ret == strlen(TEST_MSG)) {
3573+ buf[ret] = '\0';
3574+ fail_if(strcmp(buf, TEST_MSG) != 0);
3575+ fail_if(strcmp(peername, peer) != 0);
3576+ fail_if(peer_port != SEND_PORT);
3577+ // Finish test
3578+ break;
3579+ }
3580+ }
3581+ if (FD_ISSET(hipl_lib_get_sockfd(send_hid), &rset)) {
3582+ ret = hipl_sendto(send_hid, TEST_MSG, strlen(TEST_MSG), 0,
3583+ peername, RECV_PORT);
3584+ fail_if(ret < 0 && ret != -EWAITBEX && ret != -EBEXESTABLISHED);
3585+ fail_if(ret > 0 && ret != strlen(TEST_MSG));
3586+ }
3587+ }
3588+
3589+ hipl_close(send_hid);
3590+ hipl_close(recv_hid);
3591+}
3592+
3593+START_TEST(test_libhipl_lo_tcp)
3594+{
3595+ fd_set wset, rset;
3596+ int maxfd, recv_slave = 0;
3597+
3598+ test_libhipl_lo_init(IPPROTO_TCP);
3599+
3600+ // Setup TCP connection
3601+ maxfd = max(hipl_lib_get_sockfd(sender), hipl_lib_get_sockfd(receiver));
3602+ if (hipl_listen(receiver, 5) < 0) {
3603+ fail("hipl_listen()");
3604+ }
3605+ fail_if(hipl_lib_set_nonblock(sender, true) < 0);
3606+ fail_if(hipl_lib_set_nonblock(receiver, true) < 0);
3607+ hipl_accept(receiver);
3608+ hipl_connect(sender, peername, RECV_PORT);
3609+ while (recv_slave <= 0) {
3610+ FD_ZERO(&wset);
3611+ FD_ZERO(&rset);
3612+ FD_SET(hipl_lib_get_sockfd(sender), &wset);
3613+ FD_SET(hipl_lib_get_sockfd(receiver), &rset);
3614+ fail_if(select(maxfd + 1, &rset, &wset, NULL, NULL) < 0);
3615+ if (FD_ISSET(hipl_lib_get_sockfd(receiver), &rset)) {
3616+ if ((recv_slave = hipl_accept(receiver)) < 0) {
3617+ fail("hipl_accept(), %s", strerror(errno));
3618+ }
3619+ }
3620+ if (FD_ISSET(hipl_lib_get_sockfd(sender), &wset)) {
3621+ if (hipl_connect(sender, peername, RECV_PORT) < 0
3622+ && errno != EINPROGRESS && errno != EISCONN) {
3623+ fail("hipl_connect() %s", strerror(errno));
3624+ }
3625+ }
3626+ }
3627+ fail_if(hipl_connect(sender, peername, RECV_PORT) < 0 && errno != EISCONN);
3628+ fail_if(hipl_lib_set_nonblock(sender, false) < 0);
3629+
3630+ // Process base exchange and user data
3631+ test_libhipl_lo_main_loop(sender, recv_slave);
3632+ hipl_close(receiver);
3633+}
3634+END_TEST
3635+
3636+START_TEST(test_libhipl_lo_udp)
3637+{
3638+ test_libhipl_lo_init(IPPROTO_UDP);
3639+ test_libhipl_lo_main_loop(sender, receiver);
3640+}
3641+END_TEST
3642+
3643+static Suite *hipnc_suite(void)
3644+{
3645+ Suite *s = suite_create("libhipl");
3646+
3647+ TCase *tc_libhipl_lo = tcase_create("libhipl_lo");
3648+ tcase_add_test(tc_libhipl_lo, test_libhipl_lo_udp);
3649+ tcase_add_test(tc_libhipl_lo, test_libhipl_lo_tcp);
3650+ suite_add_tcase(s, tc_libhipl_lo);
3651+
3652+ return s;
3653+}
3654+
3655+int main(void)
3656+{
3657+ int number_failed;
3658+ Suite *s = hipnc_suite();
3659+ SRunner *sr = srunner_create(NULL);
3660+
3661+ srunner_add_suite(sr, s);
3662+ srunner_run_all(sr, CK_NORMAL);
3663+
3664+ number_failed = srunner_ntests_failed(sr);
3665+ srunner_free(sr);
3666+
3667+ return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
3668+}
3669
3670=== modified file 'test/hipd/lsidb.c'
3671--- test/hipd/lsidb.c 2012-01-18 21:09:47 +0000
3672+++ test/hipd/lsidb.c 2012-05-15 09:25:24 +0000
3673@@ -26,7 +26,7 @@
3674 #include <check.h>
3675 #include <stdlib.h>
3676
3677-#include "hipd/lsidb.c"
3678+#include "libhipl/lsidb.c"
3679 #include "test_suites.h"
3680
3681 START_TEST(test_lsidb_allocate_lsi_valid)

Subscribers

People subscribed via source and target branches

to all changes: