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

Proposed by Xin
Status: Rejected
Rejected by: Xin
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 Needs Fixing
Miika Komu Approve
Christof Mroz Abstain
Review via email: mp+102342@code.launchpad.net

Description of the change

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
Christof Mroz (christof-mroz) wrote :

I can't possibly find the time for a thorough review in the near future, sorry.

review: Abstain
Revision history for this message
Miika Komu (miika-iki) wrote :

The lhip code base is starting to look good and clean. Also, it's mostly in new files which is nice.

You still clean the branch from warnings:

make clean
make -j 4 2>/tmp/errors
grep lhip /tmp/errors|wc
     36 242 2984

review: Needs Fixing
Revision history for this message
Miika Komu (miika-iki) wrote :

The following tests work all right:

* make alltests: OK
* tools/hipl_autobuild: OK
* normal base exchange (without library): OK

I would suggest that fix the compilation warnings and make a new proposal. I am going to accept it and then we'll wait for comments until next Friday (11.5) for further feedback. If there's none, you can merge the code.

Revision history for this message
Xin (eric-nevup) wrote :

On 04/05/12 20:11, Miika Komu wrote:
>
> You still clean the branch from warnings:
>
> make clean
> make -j 4 2>/tmp/errors
> grep lhip /tmp/errors|wc
> 36 242 2984
>
I don't get any compilation warnings in my machine, do I forget to open
some kind of switch? I think the Werror is on already.

Xin

Revision history for this message
Miika Komu (miika-iki) wrote :
Download full text (3.2 KiB)

Hi,

On 04/05/12 20:36, Xin Gu wrote:
> On 04/05/12 20:11, Miika Komu wrote:
>>
>> You still clean the branch from warnings:
>>
>> make clean
>> make -j 4 2>/tmp/errors
>> grep lhip /tmp/errors|wc
>> 36 242 2984
>>
> I don't get any compilation warnings in my machine, do I forget to open
> some kind of switch? I think the Werror is on already.

no switches, just autoreconf and make:

lib/hipl/lhipl.c: In function ‘hipl_sendto’:
lib/hipl/lhipl.c:378: warning: dereferencing pointer ‘p6’ does break
strict-aliasing rules
lib/hipl/lhipl.c:377: warning: dereferencing pointer ‘p6’ does break
strict-aliasing rules
lib/hipl/lhipl.c:376: note: initialized from here
lib/hipl/lhipl_operations.c: In function ‘recv_msg_wrapper’:
lib/hipl/lhipl_operations.c:168: warning: dereferencing pointer
‘our_locator.26’ does break strict-aliasing rules
lib/hipl/lhipl_operations.c:164: warning: dereferencing pointer
‘our_locator.26’ does break strict-aliasing rules
lib/hipl/lhipl_operations.c:229: note: initialized from here
lib/hipl/lhipl_operations.c:167: warning: dereferencing pointer ‘s4’
does break strict-aliasing rules
lib/hipl/lhipl_operations.c:166: warning: dereferencing pointer ‘s4’
does break strict-aliasing rules
lib/hipl/lhipl_operations.c:165: note: initialized from here
lib/hipl/lhipl_operations.c:171: warning: dereferencing pointer ‘s6’
does break strict-aliasing rules
lib/hipl/lhipl_operations.c:170: warning: dereferencing pointer ‘s6’
does break strict-aliasing rules
lib/hipl/lhipl_operations.c:169: note: initialized from here
lib/hipl/lhipl_operations.c: In function ‘hipl_bind_internal’:
lib/hipl/lhipl_operations.c:92: warning: dereferencing pointer
‘laddr.45’ does break strict-aliasing rules
lib/hipl/lhipl_operations.c:537: note: initialized from here
lib/hipl/lhipl_operations.c:93: warning: dereferencing pointer ‘addr4’
does break strict-aliasing rules
lib/hipl/lhipl_operations.c:89: note: initialized from here
lib/hipl/lhipl_operations.c:95: warning: dereferencing pointer ‘addr6’
does break strict-aliasing rules
lib/hipl/lhipl_operations.c:90: note: initialized from here
lib/hipl/lhipl_operations.c: In function ‘auto_bind’:
lib/hipl/lhipl_operations.c:75: warning: dereferencing pointer ‘addr4’
does break strict-aliasing rules
lib/hipl/lhipl_operations.c:74: note: initialized from here
lib/hipl/lhipl_operations.c:81: warning: dereferencing pointer ‘addr6’
does break strict-aliasing rules
lib/hipl/lhipl_operations.c:80: note: initialized from here
lib/hipl/lhipl_operations.c: In function ‘hipl_recvmsg_internal’:
lib/hipl/lhipl_operations.c:411: warning: dereferencing pointer
‘saddr.38’ does break strict-aliasing rules
lib/hipl/lhipl_operations.c:411: note: initialized from here
lib/hipl/lhipl_operations.c: In function ‘hipl_connect_internal’:
lib/hipl/lhipl_operations.c:92: warning: dereferencing pointer ‘ss.50’
does break strict-aliasing rules
lib/hipl/lhipl_operations.c:592: note: initialized from here
lib/hipl/lhipl_operations.c:93: warning: dereferencing pointer ‘addr4’
does break strict-aliasing rules
lib/hipl/lhipl_operations.c:89: note: initialized from here
lib/hipl/lhipl_operations.c:95: warning: dereferencing pointer ‘addr6’
d...

Read more...

Revision history for this message
Miika Komu (miika-iki) wrote :

The compilation warnings appear in older Ubuntu LTS (Lucid) and not in the new one (Precise), so I withdraw my comment. Must be something with the gcc versions.

Xin, please merge Friday 11.5 unless someone objects or explicitly requests to get more time for the review.

review: Approve
Revision history for this message
Diego Biurrun (diego-biurrun) wrote :
Download full text (15.0 KiB)

 review needs-fixing

On Tue, Apr 17, 2012 at 04:10:25PM +0000, Xin wrote:
> Xin has proposed merging lp:~hipl-core/hipl/libhip into lp:hipl.
>
> 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.

\o/

> --- Makefile.am 2012-03-13 15:27:06 +0000
> +++ Makefile.am 2012-04-17 16:09:27 +0000
> @@ -58,17 +58,21 @@
>
> ### libraries ###
> -lib_LTLIBRARIES = lib/core/libhipcore.la
> +lib_LTLIBRARIES = lib/core/libhipcore.la \
> + lib/hipl/libhipl.la

I still don't like the general directory layout we are designing.

What about just

core/
libhipl/

We do not place hipd and hipfw below a binaries directory, so we might
as well have the libraries at the top level.

> @@ -202,6 +166,47 @@
>
> +lib_hipl_libhipl_la_SOURCES = lib/hipl/accessor.c \
> + lib/hipl/lhipl.c \
> + lib/hipl/lhipl_sock.c \
> + lib/hipl/lhipl_operations.c \

Why this lhipl prefix?

> --- doc/HOWTO.xml.in 2012-02-28 18:38:18 +0000
> +++ doc/HOWTO.xml.in 2012-04-17 16:09:27 +0000
> @@ -2659,6 +2659,112 @@
> +
> + <para>
> + Libhipl provides HIP functionality as a library for upper layer
> + applications and it does not require the presence of the HIPL daemon.

without requiring the HIPL daemon.

> + Instead, the HIP control messages are transmitted on top of TCP/UDP.

s/the//

> + <para>
> + Libhipl API set is located in <emphasis>"lhipl.h"</emphasis>. Refer to
> + its API documentation for detail information of each function.

The libhipl API is described in <emphasis>"lhipl.h"</emphasis>. Detailed
information about each function is available there.

> + <formalpara>
> + <title>Name-based API</title>
> + <para>
> + Compared to standard socket API, one apparent difference in Libhipl is
> + that a peer is specified by given a peer name and a port number instead

s/given/giving/

> + of a sockaddr data structure. The peer name is a string representation of
> + the peer. Currently, Libhipl only support HIT string as peer name.

supports a

In other places you lowercase "libhipl", so be consistent here.

> + The function hipl_socket() creates a new libhipl socket. An ID is
> + assigned to each created libhipl socket and returned by this function,
> + then caller can pass this ID to other libhipl functions to operates on

then the caller, operate

> + this libhipl socket. NOTE: there is NO relation between libhipl socket
> + ID and normal socket file descriptor, and the result of using

IDs, descriptors

> + <formalpara><title>Setup TCP connection</title><para>
> + When using libhipl in TCP mode, TCP connection setup is the first task.
> + similar to the normal socket syntax, libhipl provides:

Similar

> + <formalpara>
> + <title>libhipl Base Exchange (BEX) and user data transmission</title>...

review: Needs Fixing
lp:~hipl-core/hipl/libhip updated
4906. By Xin

Revise code based on review feedback.

Revision history for this message
Xin (eric-nevup) wrote :
Download full text (7.9 KiB)

On 11/05/12 00:55, Diego Biurrun wrote:
> review needs-fixing
>
>
>> @@ -202,6 +166,47 @@
>>
>> +lib_hipl_libhipl_la_SOURCES = lib/hipl/accessor.c \
>> + lib/hipl/lhipl.c \
>> + lib/hipl/lhipl_sock.c \
>> + lib/hipl/lhipl_operations.c \
> Why this lhipl prefix?

It is a short form for libhipl. This prefix can help to identify src
code files related to libhipl.

>> --- lib/core/hostid.c 2012-03-01 14:26:43 +0000
>> +++ lib/core/hostid.c 2012-04-17 16:09:27 +0000
>> @@ -743,11 +745,16 @@
>> goto out_err;
>> }
>> } else if (!use_default) {
>> + hi_file_dup = strdup(hi_file);
>> + if ((err = check_and_create_dir(dirname(hi_file_dup), HIP_DIR_MODE))) {
>> + HIP_ERROR("Could not create directory for path: %s\n", hi_file);
>> + goto out_err;
>> + }
>> @@ -1093,6 +1100,7 @@
>>
>> + free(hi_file_dup);
>> free(dsa_host_id);
>> free(dsa_pub_host_id);
>> free(rsa_host_id);
> This string duplication is not necessary AFAICT.

strdup() is necessary before using dirname(), or you are suggesting
other ways? can you clarify?

>> +int hipl_lib_init(enum logdebug debug_level)
>> +{
>> + int err = 0;
>> + int keypath_len = 0;
>> + char *key_path = NULL;
>> + struct hip_common *msg = NULL;
>> + struct passwd *pwd;
>> +
>> + hipl_set_libhip_mode();
>> + hip_nat_status = 1;
>> +
>> + /* disable hip_firewall */
>> + lsi_status = HIP_MSG_LSI_OFF;
> Why are you disabling the firewall?
>
>

Because we don't use hipfw when using libhipl. libhipl also doesn't use
kernel based ipsec.

>> + HIP_IFEL(snprintf(key_path, keypath_len, "%s/%s/%s%s", pwd->pw_dir,
>> + HIPL_USER_DIR,
>> + HIPL_USER_RSA_KEY_NAME,
>> + DEFAULT_PUB_HI_FILE_NAME_SUFFIX)< 0,
>> + -1, "snprintf() failed");
>> +
>> + HIP_DEBUG("Using key: %s\n", key_path);
>> + HIP_IFEL(!(msg = hip_msg_alloc()), -ENOMEM, "hip_msg_alloc()");
>> + if (hip_serialize_host_id_action(msg, ACTION_ADD, 0, 0, "rsa",
>> + key_path, 0, 0, 0)) {
>> + free(msg);
>> + HIP_IFEL(!(msg = hip_msg_alloc()), -ENOMEM, "hip_msg_alloc()");
>> + HIP_IFEL(hip_serialize_host_id_action(msg, ACTION_NEW, 0, 0, "rsa",
>> + key_path, RSA_KEY_DEFAULT_BITS,
>> + DSA_KEY_DEFAULT_BITS,
>> + ECDSA_DEFAULT_CURVE), -1,
>> + "Fail to create local key at %s.", key_path);
>> + free(msg);
>> + HIP_IFE(!(msg = hip_msg_alloc()), -ENOMEM);
>> + HIP_IFEL(hip_serialize_host_id_action(msg, ACTION_ADD, 0, 0, "rsa",
>> + key_path, 0, 0, 0), -1,
>> + "Fail to load local key at %s.", key_path);
>> + }
>> +...

Read more...

lp:~hipl-core/hipl/libhip updated
4907. By Xin

Merge trunk and change directory lib/hipl => libhipl/

Revision history for this message
Diego Biurrun (diego-biurrun) wrote :
Download full text (3.6 KiB)

On Sat, May 12, 2012 at 10:13:17AM +0300, Xin Gu wrote:
> On 11/05/12 00:55, Diego Biurrun wrote:
> >>@@ -202,6 +166,47 @@
> >>
> >>+lib_hipl_libhipl_la_SOURCES = lib/hipl/accessor.c \
> >>+ lib/hipl/lhipl.c \
> >>+ lib/hipl/lhipl_sock.c \
> >>+ lib/hipl/lhipl_operations.c \
> >Why this lhipl prefix?
>
> It is a short form for libhipl. This prefix can help to identify src
> code files related to libhipl.

There is no need to identify files in the libhipl directory as belonging
to libhipl. What else would they belong to? We don't have hipfw_ and
hipd_ prefixes in the hipd/ and hipfw/ directories.

> >>--- lib/core/hostid.c 2012-03-01 14:26:43 +0000
> >>+++ lib/core/hostid.c 2012-04-17 16:09:27 +0000
> >>@@ -743,11 +745,16 @@
> >> goto out_err;
> >> }
> >> } else if (!use_default) {
> >>+ hi_file_dup = strdup(hi_file);
> >>+ if ((err = check_and_create_dir(dirname(hi_file_dup), HIP_DIR_MODE))) {
> >>+ HIP_ERROR("Could not create directory for path: %s\n", hi_file);
> >>+ goto out_err;
> >>+ }
> >>@@ -1093,6 +1100,7 @@
> >>
> >>+ free(hi_file_dup);
> >> free(dsa_host_id);
> >> free(dsa_pub_host_id);
> >> free(rsa_host_id);
> >This string duplication is not necessary AFAICT.
>
> strdup() is necessary before using dirname(), or you are suggesting
> other ways? can you clarify?

Constructing the path on the fly does not seem more complicated than
strdup() and free(), but just do whatever you want.

> >>+int hipl_sendto(const hipl_sock_id hsock_id, const void *const msg,
> >>+ const size_t len, const int flags,
> >>+ const char *const peername, uint16_t port)
> >>+{
> >>+
> >>+ if (hipl_lib_bex_feedback()) {
> >>+ err = hipl_sendmsg_internal(hsock,&params, flags);
> >>+ } else {
> >>+ fd_set fdset;
> >>+ if (hipl_hsock_ha_state(hsock) == HIP_STATE_UNASSOCIATED) {
> >>+ HIP_DEBUG("Sending via hsock %d, Triggering BEX.\n", hsock->sid);
> >>+ err = hipl_sendmsg_internal(hsock,&params, flags);
> >>+ HIP_IFEL(err != -EWAITBEX, -1, "hipl_sendmsg_internal() failed\n");
> >>+ }
> >>+ if (hipl_hsock_ha_state(hsock) == HIP_STATE_ESTABLISHED) {
> >>+ HIP_DEBUG("Sending via hsock %d, HA established.\n", hsock->sid);
> >>+ err = hipl_sendmsg_internal(hsock,&params, flags);
> >>+ } else {
> >>+ while (hipl_hsock_ha_state(hsock) != HIP_STATE_ESTABLISHED) {
> >>+ FD_ZERO(&fdset);
> >>+ FD_SET(hsock->sock_fd,&fdset);
> >>+ HIP_DEBUG("Sending via hsock %d, Waiting BEX.\n", hsock->sid);
> >>+ err = select(hsock->sock_fd + 1,&fdset, NULL, NULL, NULL);
> >>+ HIP_IFEL(err< 0, -1, "select(): %s\n", strerror(errno));
> >>+ err = hipl_sendmsg_internal(hsock,&params, flags);
> >>+ HIP_IFEL(err< 0&& err != -EWAITBEX&& err != -EBEXEST...

Read more...

Revision history for this message
Miika Komu (miika-iki) wrote :

Hi,

On 12/05/12 14:00, Diego Biurrun wrote:
>>>> +int hipl_sendto(const hipl_sock_id hsock_id, const void
>>>> *const msg,
>>>>>>> + const size_t len, const int flags, +
>>>>>>> const char *const peername, uint16_t port) +{ + + if
>>>>>>> (hipl_lib_bex_feedback()) { + err =
>>>>>>> hipl_sendmsg_internal(hsock,&params, flags); + }
>>>>>>> else { + fd_set fdset; + if
>>>>>>> (hipl_hsock_ha_state(hsock) == HIP_STATE_UNASSOCIATED)
>>>>>>> { + HIP_DEBUG("Sending via hsock %d,
>>>>>>> Triggering BEX.\n", hsock->sid); + err =
>>>>>>> hipl_sendmsg_internal(hsock,&params, flags); +
>>>>>>> HIP_IFEL(err != -EWAITBEX, -1, "hipl_sendmsg_internal()
>>>>>>> failed\n"); + } + if
>>>>>>> (hipl_hsock_ha_state(hsock) == HIP_STATE_ESTABLISHED)
>>>>>>> { + HIP_DEBUG("Sending via hsock %d, HA
>>>>>>> established.\n", hsock->sid); + err =
>>>>>>> hipl_sendmsg_internal(hsock,&params, flags); + }
>>>>>>> else { + while (hipl_hsock_ha_state(hsock)
>>>>>>> != HIP_STATE_ESTABLISHED) { +
>>>>>>> FD_ZERO(&fdset); +
>>>>>>> FD_SET(hsock->sock_fd,&fdset); +
>>>>>>> HIP_DEBUG("Sending via hsock %d, Waiting BEX.\n",
>>>>>>> hsock->sid); + err =
>>>>>>> select(hsock->sock_fd + 1,&fdset, NULL, NULL, NULL); +
>>>>>>> HIP_IFEL(err< 0, -1, "select(): %s\n",
>>>>>>> strerror(errno)); + err =
>>>>>>> hipl_sendmsg_internal(hsock,&params, flags); +
>>>>>>> HIP_IFEL(err< 0&& err != -EWAITBEX&& err !=
>>>>>>> -EBEXESTABLISHED, + -1,
>>>>>>> "hipl_sendmsg_internal() failed\n"); + } +
>>>>>>> err = hipl_sendmsg_internal(hsock,&params, flags); +
>>>>>>> } + } + +out_err: + free(buf); + return err;
>>>>>>> +}
>>>>> HIP_IFEL ugliness
>>>>>
>>>
>>> I think I followed the guideline of HIP_IFEL, I use it here
>>> because I need to free memory at the end.
> I maintain that there is never any good reason to use HIP_IFEL in
> new code.

there is no strong consensus on this, this is a matter of Diego's
personal opinion especially as the matter involves memory dellocation.
So Xin, you can decide on this issue by yourself.

Revision history for this message
Xin (eric-nevup) wrote :

Hi,

On 12/05/12 14:00, Diego Biurrun wrote:
> On Sat, May 12, 2012 at 10:13:17AM +0300, Xin Gu wrote:
>> On 11/05/12 00:55, Diego Biurrun wrote:
>>>> @@ -202,6 +166,47 @@
>>>>
>>>> +lib_hipl_libhipl_la_SOURCES = lib/hipl/accessor.c \
>>>> + lib/hipl/lhipl.c \
>>>> + lib/hipl/lhipl_sock.c \
>>>> + lib/hipl/lhipl_operations.c \
>>> Why this lhipl prefix?
>> It is a short form for libhipl. This prefix can help to identify src
>> code files related to libhipl.
> There is no need to identify files in the libhipl directory as belonging
> to libhipl. What else would they belong to? We don't have hipfw_ and
> hipd_ prefixes in the hipd/ and hipfw/ directories.
>

I was not so clear in previous reply. This prefix aims to identify src
code files related to library-based HIP.

Xin

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

On Sat, May 12, 2012 at 02:53:01PM +0300, Xin Gu wrote:
> On 12/05/12 14:00, Diego Biurrun wrote:
> >On Sat, May 12, 2012 at 10:13:17AM +0300, Xin Gu wrote:
> >>On 11/05/12 00:55, Diego Biurrun wrote:
> >>>>@@ -202,6 +166,47 @@
> >>>>
> >>>>+lib_hipl_libhipl_la_SOURCES = lib/hipl/accessor.c \
> >>>>+ lib/hipl/lhipl.c \
> >>>>+ lib/hipl/lhipl_sock.c \
> >>>>+ lib/hipl/lhipl_operations.c \
> >>>Why this lhipl prefix?
> >>It is a short form for libhipl. This prefix can help to identify src
> >>code files related to libhipl.
> >There is no need to identify files in the libhipl directory as belonging
> >to libhipl. What else would they belong to? We don't have hipfw_ and
> >hipd_ prefixes in the hipd/ and hipfw/ directories.
>
> I was not so clear in previous reply. This prefix aims to identify
> src code files related to library-based HIP.

Maybe it was not clear enough in my previous reply: We have a directory
to identify source code files related to librarized HIPL, there is no
need for a prefix.

Diego

lp:~hipl-core/hipl/libhip updated
4908. By Xin

Closing the libhipl socket after unit tests.

Revision history for this message
Xin (eric-nevup) wrote :

Hi,
On 12/05/12 15:23, Diego Biurrun wrote:
> On Sat, May 12, 2012 at 02:53:01PM +0300, Xin Gu wrote:
>> On 12/05/12 14:00, Diego Biurrun wrote:
>>> On Sat, May 12, 2012 at 10:13:17AM +0300, Xin Gu wrote:
>>>> On 11/05/12 00:55, Diego Biurrun wrote:
>>>>>> @@ -202,6 +166,47 @@
>>>>>>
>>>>>> +lib_hipl_libhipl_la_SOURCES = lib/hipl/accessor.c \
>>>>>> + lib/hipl/lhipl.c \
>>>>>> + lib/hipl/lhipl_sock.c \
>>>>>> + lib/hipl/lhipl_operations.c \
>>>>> Why this lhipl prefix?
>>>> It is a short form for libhipl. This prefix can help to identify src
>>>> code files related to libhipl.
>>> There is no need to identify files in the libhipl directory as belonging
>>> to libhipl. What else would they belong to? We don't have hipfw_ and
>>> hipd_ prefixes in the hipd/ and hipfw/ directories.
>> I was not so clear in previous reply. This prefix aims to identify
>> src code files related to library-based HIP.
> Maybe it was not clear enough in my previous reply: We have a directory
> to identify source code files related to librarized HIPL, there is no
> need for a prefix.
>

Those files with lhipl prefix are the implementation of the
library-based HIP (see my merge proposal). The prefix can group them
together so everyone can tell them easily. If you think current prefix
is not intuitive, I am fine to change it to something you propose.

Regrading to memleak, I didn't find suspicious records from the output
of valgrind related to library-based HIP. One improvement I make is
closing the libhipl socket when the unit tests finish.

Xin

Unmerged revisions

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'Makefile.am'
--- Makefile.am 2012-05-12 06:54:33 +0000
+++ Makefile.am 2012-05-12 14:17:22 +0000
@@ -23,7 +23,7 @@
2323
24ACLOCAL_AMFLAGS = -I m424ACLOCAL_AMFLAGS = -I m4
2525
26HIPL_HEADER_LOCATIONS = hipd/*.h hipfw/*.h libcore/*.h libcore/*/*.h modules/*/*/*.h test/*.h test/*/*.h test/*/*/*.h26HIPL_HEADER_LOCATIONS = hipd/*.h hipfw/*.h libcore/*.h libcore/*/*.h libhipl/*.h modules/*/*/*.h test/*.h test/*/*.h test/*/*/*.h
27HIPL_HEADER_LIST = $(wildcard $(addprefix $(srcdir)/,$(HIPL_HEADER_LOCATIONS)))27HIPL_HEADER_LIST = $(wildcard $(addprefix $(srcdir)/,$(HIPL_HEADER_LOCATIONS)))
2828
29# For "make dist"29# For "make dist"
@@ -58,7 +58,8 @@
5858
5959
60### libraries ###60### libraries ###
61lib_LTLIBRARIES = libcore/libcore.la61lib_LTLIBRARIES = libcore/libcore.la \
62 libhipl/libhipl.la
6263
6364
64### tests ###65### tests ###
@@ -66,10 +67,14 @@
66TESTS = test/check_hipd \67TESTS = test/check_hipd \
67 test/check_hipfw \68 test/check_hipfw \
68 test/check_libcore \69 test/check_libcore \
70 test/check_libhipl \
71 test/check_libcore \
69 $(tools_hipdnsproxy_PYTHON)72 $(tools_hipdnsproxy_PYTHON)
73
70check_PROGRAMS = test/check_hipd \74check_PROGRAMS = test/check_hipd \
71 test/check_hipfw \75 test/check_hipfw \
72 test/check_libcore76 test/check_libcore \
77 test/check_libhipl
73endif78endif
7479
7580
@@ -86,47 +91,7 @@
8691
87tools_hipconf_SOURCES = tools/hipconf.c92tools_hipconf_SOURCES = tools/hipconf.c
8893
89hipd_hipd_sources = hipd/accessor.c \94hipd_hipd_SOURCES = hipd/main.c
90 hipd/cert.c \
91 hipd/close.c \
92 hipd/configfilereader.c \
93 hipd/cookie.c \
94 hipd/dh.c \
95 hipd/esp_prot_anchordb.c \
96 hipd/esp_prot_hipd_msg.c \
97 hipd/esp_prot_light_update.c \
98 hipd/hadb.c \
99 hipd/hidb.c \
100 hipd/hip_socket.c \
101 hipd/hipd.c \
102 hipd/hiprelay.c \
103 hipd/hit_to_ip.c \
104 hipd/init.c \
105 hipd/input.c \
106 hipd/keymat.c \
107 hipd/lsidb.c \
108 hipd/maintenance.c \
109 hipd/nat.c \
110 hipd/netdev.c \
111 hipd/nsupdate.c \
112 hipd/opp_mode.c \
113 hipd/output.c \
114 hipd/pkt_handling.c \
115 hipd/registration.c \
116 hipd/user.c \
117 hipd/user_ipsec_hipd_msg.c \
118 hipd/user_ipsec_sadb_api.c \
119 modules/heartbeat/hipd/heartbeat.c \
120 modules/heartbeat_update/hipd/hb_update.c \
121 modules/cert/hipd/cert.c \
122 modules/update/hipd/update.c \
123 modules/update/hipd/update_builder.c \
124 modules/update/hipd/update_locator.c \
125 modules/update/hipd/update_param_handling.c
126
127hipd_hipd_SOURCES = $(hipd_hipd_sources) \
128 modules/midauth/hipd/midauth.c \
129 hipd/main.c
13095
131dist_sysconf_DATA = hipd/hipd.conf \96dist_sysconf_DATA = hipd/hipd.conf \
132 hipd/hosts \97 hipd/hosts \
@@ -203,6 +168,47 @@
203libcore_libcore_la_SOURCES += libcore/performance.c168libcore_libcore_la_SOURCES += libcore/performance.c
204endif169endif
205170
171libhipl_libhipl_la_SOURCES = libhipl/accessor.c \
172 libhipl/cert.c \
173 libhipl/close.c \
174 libhipl/configfilereader.c \
175 libhipl/cookie.c \
176 libhipl/dh.c \
177 libhipl/esp_prot_anchordb.c \
178 libhipl/esp_prot_hipd_msg.c \
179 libhipl/esp_prot_light_update.c \
180 libhipl/hadb.c \
181 libhipl/hidb.c \
182 libhipl/hip_socket.c \
183 libhipl/hipd.c \
184 libhipl/hiprelay.c \
185 libhipl/hit_to_ip.c \
186 libhipl/init.c \
187 libhipl/input.c \
188 libhipl/keymat.c \
189 libhipl/lhipl.c \
190 libhipl/lhipl_sock.c \
191 libhipl/lhipl_operations.c \
192 libhipl/lsidb.c \
193 libhipl/maintenance.c \
194 libhipl/nat.c \
195 libhipl/netdev.c \
196 libhipl/nsupdate.c \
197 libhipl/opp_mode.c \
198 libhipl/output.c \
199 libhipl/pkt_handling.c \
200 libhipl/registration.c \
201 libhipl/user.c \
202 libhipl/user_ipsec_hipd_msg.c \
203 libhipl/user_ipsec_sadb_api.c \
204 modules/cert/hipd/cert.c \
205 modules/heartbeat/hipd/heartbeat.c \
206 modules/heartbeat_update/hipd/hb_update.c \
207 modules/midauth/hipd/midauth.c \
208 modules/update/hipd/update.c \
209 modules/update/hipd/update_builder.c \
210 modules/update/hipd/update_locator.c \
211 modules/update/hipd/update_param_handling.c
206212
207test_check_hipd_SOURCES = test/check_hipd.c \213test_check_hipd_SOURCES = test/check_hipd.c \
208 test/hipd/lsidb.c \214 test/hipd/lsidb.c \
@@ -231,14 +237,18 @@
231 test/libcore/gpl/pk.c \237 test/libcore/gpl/pk.c \
232 test/libcore/modules/midauth_builder.c238 test/libcore/modules/midauth_builder.c
233239
240test_check_libhipl_SOURCES = test/check_libhipl.c
241
234242
235### static library dependencies ###243### static library dependencies ###
236244
237hipd_hipd_LDADD = libcore/libcore.la245hipd_hipd_LDADD = libhipl/libhipl.la
238hipfw_hipfw_LDADD = libcore/libcore.la246hipfw_hipfw_LDADD = libcore/libcore.la
239test_check_hipd_LDADD = libcore/libcore.la247libhipl_libhipl_la_LIBADD = libcore/libcore.la
248test_check_hipd_LDADD = libhipl/libhipl.la
240test_check_hipfw_LDADD = libcore/libcore.la249test_check_hipfw_LDADD = libcore/libcore.la
241test_check_libcore_LDADD = libcore/libcore.la250test_check_libcore_LDADD = libcore/libcore.la
251test_check_libhipl_LDADD = libhipl/libhipl.la
242test_certteststub_LDADD = libcore/libcore.la252test_certteststub_LDADD = libcore/libcore.la
243test_performance_auth_performance_LDADD = libcore/libcore.la253test_performance_auth_performance_LDADD = libcore/libcore.la
244test_performance_dh_performance_LDADD = libcore/libcore.la254test_performance_dh_performance_LDADD = libcore/libcore.la
245255
=== modified file 'doc/HOWTO.xml.in'
--- doc/HOWTO.xml.in 2012-02-28 18:38:18 +0000
+++ doc/HOWTO.xml.in 2012-05-12 14:17:22 +0000
@@ -2659,6 +2659,112 @@
26592659
2660 </section>2660 </section>
26612661
2662 <section id="ch_libhipl_usage">
2663 <title>The Libhipl Extension</title>
2664
2665 <para>
2666 Libhipl provides HIP functionality as a library for upper layer
2667 applications without requiring the HIPL daemon.
2668 Instead, HIP control messages are transmitted on top of TCP/UDP.
2669 From the applications' point of view, there is an API similar to the
2670 normal socket API. This section describes the libhipl API.
2671 </para>
2672
2673 <para>
2674 The libhipl API is described in <emphasis>"lhipl.h"</emphasis>. Detailed
2675 information about each function is available there.
2676 </para>
2677
2678 <formalpara>
2679 <title>Libhipl initialization</title>
2680 <para>
2681 Libhipl requires initialization before calling any other related
2682 functions. The function: <emphasis>hipl_lib_init_all()</emphasis>
2683 serves this purpose and different debug levels can be set as its
2684 parameters to track problems inside the library.
2685 </para>
2686 </formalpara>
2687
2688 <formalpara>
2689 <title>Name-based API</title>
2690 <para>
2691 Compared to standard socket API, one apparent difference in Libhipl is
2692 that a peer is specified by giving a peer name and a port number instead
2693 of a sockaddr data structure. The peer name is a string representation of
2694 the peer. Currently, libhipl only supports a HIT string as peer name.
2695 </para>
2696 </formalpara>
2697
2698 <formalpara>
2699 <title>Libhipl socket creation</title>
2700 <para>
2701 The function hipl_socket() creates a new libhipl socket. An ID is
2702 assigned to each created libhipl socket and returned by this function,
2703 then the caller can pass this ID to other libhipl functions to operate on
2704 this libhipl socket. NOTE: there is NO relation between libhipl socket
2705 IDs and normal socket file descriptors, and the result of using
2706 this ID as file descriptor is undefined.
2707 </para>
2708 </formalpara>
2709
2710 <formalpara><title>Setup TCP connection</title><para>
2711 When using libhipl in TCP mode, TCP connection setup is the first task.
2712 Similar to the normal socket syntax, libhipl provides:
2713 <emphasis>hipl_connect()</emphasis>, <emphasis>hipl_listen()</emphasis>
2714 and <emphasis>hipl_accept()</emphasis> for this goal. Those functions
2715 process the 3-way handshake and establish a TCP connection.
2716 </para></formalpara>
2717
2718 <formalpara>
2719 <title>libhipl Base Exchange (BEX) and user data transmission</title>
2720 <para>
2721 Data transmission is handled by <emphasis>hipl_sendto()</emphasis> and
2722 <emphasis>hipl_recvfrom()</emphasis>.
2723 In libhipl, The first sending data call triggers BEX and the first
2724 receiving call waits for an incoming BEX message. Once BEX succeeds,
2725 the pending user data is transmitted to the peer side. This BEX process
2726 is handled by libhipl automatically by default. Meanwhile, there is also
2727 an advanced way to handle BEX step by step, which will be explained
2728 bellow.
2729 </para>
2730 </formalpara>
2731
2732 <formalpara>
2733 <title>libhipl assistant API</title>
2734 <para>
2735 <itemizedlist>
2736 <listitem>
2737 <para>
2738 <emphasis>hipl_add_peer_info()</emphasis> adds the peer's HIT and
2739 IP address mappings to the database of libhipl. The library can
2740 use this information to look up a peer's locator when a peer name
2741 is given.
2742 </para>
2743 </listitem>
2744
2745 <listitem>
2746 <para>
2747 <emphasis>hipl_lib_set_bex_feedback()</emphasis>: This function
2748 turns the step-by-step process of BEX on/off. By default, BEX
2749 feedback is off. When it is on, sending/receiving functions only
2750 process one BEX messages at a time, and give different return
2751 values to notify the progress of BEX.
2752 </para>
2753 </listitem>
2754 </itemizedlist>
2755 </para>
2756 </formalpara>
2757
2758 <formalpara>
2759 <title>Libhipl sample program</title>
2760 <para>
2761 For a libhipl usage example, refer to the
2762 <emphasis>check_libhipl</emphasis> program in the
2763 <emphasis>test</emphasis> directory.
2764 </para>
2765 </formalpara>
2766
2767 </section> <!-- ch_libhipl_usage -->
2662</chapter> <!-- ch_exp_extensions -->2768</chapter> <!-- ch_exp_extensions -->
26632769
2664</book>2770</book>
26652771
=== modified file 'hipd/main.c'
--- hipd/main.c 2012-05-12 06:54:33 +0000
+++ hipd/main.c 2012-05-12 14:17:22 +0000
@@ -1,5 +1,5 @@
1/*1/*
2 * Copyright (c) 2010-2011 Aalto University and RWTH Aachen University.2 * Copyright (c) 2010-2012 Aalto University and RWTH Aachen University.
3 *3 *
4 * Permission is hereby granted, free of charge, to any person4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation5 * obtaining a copy of this software and associated documentation
@@ -33,9 +33,9 @@
33#include <sys/types.h>33#include <sys/types.h>
3434
3535
36#include "hipd/hipd.h"
37#include "init.h"
38#include "libcore/debug.h"36#include "libcore/debug.h"
37#include "libhipl/hipd.h"
38#include "libhipl/init.h"
3939
4040
41/**41/**
4242
=== modified file 'hipfw/hipfw.c'
--- hipfw/hipfw.c 2012-05-12 06:54:33 +0000
+++ hipfw/hipfw.c 2012-05-12 14:17:22 +0000
@@ -75,7 +75,7 @@
75#include "libcore/performance.h"75#include "libcore/performance.h"
76#include "libcore/prefix.h"76#include "libcore/prefix.h"
77#include "libcore/util.h"77#include "libcore/util.h"
78#include "hipd/hipd.h"78#include "libhipl/hipd.h"
79#include "config.h"79#include "config.h"
80#include "cache.h"80#include "cache.h"
81#include "cert.h"81#include "cert.h"
8282
=== modified file 'libcore/hostid.c'
--- libcore/hostid.c 2012-05-12 06:54:33 +0000
+++ libcore/hostid.c 2012-05-12 14:17:22 +0000
@@ -1,5 +1,5 @@
1/*1/*
2 * Copyright (c) 2010-2011 Aalto University and RWTH Aachen University.2 * Copyright (c) 2010-2012 Aalto University and RWTH Aachen University.
3 *3 *
4 * Permission is hereby granted, free of charge, to any person4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation5 * obtaining a copy of this software and associated documentation
@@ -29,6 +29,7 @@
29 */29 */
3030
31#include <errno.h>31#include <errno.h>
32#include <libgen.h>
32#include <stdint.h>33#include <stdint.h>
33#include <stdlib.h>34#include <stdlib.h>
34#include <string.h>35#include <string.h>
@@ -678,6 +679,7 @@
678 int err = 0, dsa_key_rr_len = 0, rsa_key_rr_len = 0;679 int err = 0, dsa_key_rr_len = 0, rsa_key_rr_len = 0;
679 int dsa_pub_key_rr_len = 0, rsa_pub_key_rr_len = 0;680 int dsa_pub_key_rr_len = 0, rsa_pub_key_rr_len = 0;
680 hip_hdr numeric_action = 0;681 hip_hdr numeric_action = 0;
682 char *hi_file_dup = NULL;
681 char hostname[HIP_HOST_ID_HOSTNAME_LEN_MAX];683 char hostname[HIP_HOST_ID_HOSTNAME_LEN_MAX];
682 const char *rsa_filenamebase = DEFAULT_HOST_RSA_KEY_FILE_BASE DEFAULT_ANON_HI_FILE_NAME_SUFFIX;684 const char *rsa_filenamebase = DEFAULT_HOST_RSA_KEY_FILE_BASE DEFAULT_ANON_HI_FILE_NAME_SUFFIX;
683 const char *dsa_filenamebase = DEFAULT_HOST_DSA_KEY_FILE_BASE DEFAULT_ANON_HI_FILE_NAME_SUFFIX;685 const char *dsa_filenamebase = DEFAULT_HOST_DSA_KEY_FILE_BASE DEFAULT_ANON_HI_FILE_NAME_SUFFIX;
@@ -743,11 +745,16 @@
743 goto out_err;745 goto out_err;
744 }746 }
745 } else if (!use_default) {747 } else if (!use_default) {
748 hi_file_dup = strdup(hi_file);
749 if ((err = check_and_create_dir(dirname(hi_file_dup), HIP_DIR_MODE))) {
750 HIP_ERROR("Could not create directory for path: %s\n", hi_file);
751 goto out_err;
752 }
746 if (!strcmp(hi_fmt, "dsa")) {753 if (!strcmp(hi_fmt, "dsa")) {
747 dsa_key = create_dsa_key(dsa_key_bits);754 dsa_key = create_dsa_key(dsa_key_bits);
748 HIP_IFEL(!dsa_key, -EINVAL,755 HIP_IFEL(!dsa_key, -EINVAL,
749 "Creation of DSA key failed.\n");756 "Creation of DSA key failed.\n");
750 if ((err = save_dsa_private_key(dsa_filenamebase, dsa_key))) {757 if ((err = save_dsa_private_key(hi_file, dsa_key))) {
751 HIP_ERROR("Saving of DSA key failed.\n");758 HIP_ERROR("Saving of DSA key failed.\n");
752 goto out_err;759 goto out_err;
753 }760 }
@@ -756,7 +763,7 @@
756 ecdsa_key = create_ecdsa_key(ecdsa_nid);763 ecdsa_key = create_ecdsa_key(ecdsa_nid);
757 HIP_IFEL(!ecdsa_key, -EINVAL,764 HIP_IFEL(!ecdsa_key, -EINVAL,
758 "Creation of ECDSA key failed.\n");765 "Creation of ECDSA key failed.\n");
759 if ((err = save_ecdsa_private_key(ecdsa_filenamebase, ecdsa_key))) {766 if ((err = save_ecdsa_private_key(hi_file, ecdsa_key))) {
760 HIP_ERROR("Saving of ECDSA key failed.\n");767 HIP_ERROR("Saving of ECDSA key failed.\n");
761 goto out_err;768 goto out_err;
762 }769 }
@@ -765,7 +772,7 @@
765 rsa_key = create_rsa_key(rsa_key_bits);772 rsa_key = create_rsa_key(rsa_key_bits);
766 HIP_IFEL(!rsa_key, -EINVAL,773 HIP_IFEL(!rsa_key, -EINVAL,
767 "Creation of RSA key failed.\n");774 "Creation of RSA key failed.\n");
768 if ((err = save_rsa_private_key(rsa_filenamebase, rsa_key))) {775 if ((err = save_rsa_private_key(hi_file, rsa_key))) {
769 HIP_ERROR("Saving of RSA key failed.\n");776 HIP_ERROR("Saving of RSA key failed.\n");
770 goto out_err;777 goto out_err;
771 }778 }
@@ -1093,6 +1100,7 @@
1093 change_key_file_perms(rsa_filenamebase_pub);1100 change_key_file_perms(rsa_filenamebase_pub);
1094 }1101 }
10951102
1103 free(hi_file_dup);
1096 free(dsa_host_id);1104 free(dsa_host_id);
1097 free(dsa_pub_host_id);1105 free(dsa_pub_host_id);
1098 free(rsa_host_id);1106 free(rsa_host_id);
10991107
=== added directory 'libhipl'
=== renamed file 'hipd/accessor.c' => 'libhipl/accessor.c'
=== renamed file 'hipd/accessor.h' => 'libhipl/accessor.h'
--- hipd/accessor.h 2012-05-12 06:54:33 +0000
+++ libhipl/accessor.h 2012-05-12 14:17:22 +0000
@@ -23,8 +23,8 @@
23 * OTHER DEALINGS IN THE SOFTWARE.23 * OTHER DEALINGS IN THE SOFTWARE.
24 */24 */
2525
26#ifndef HIPL_HIPD_ACCESSOR_H26#ifndef HIPL_LIBHIPL_ACCESSOR_H
27#define HIPL_HIPD_ACCESSOR_H27#define HIPL_LIBHIPL_ACCESSOR_H
2828
29#include "libcore/protodefs.h"29#include "libcore/protodefs.h"
3030
@@ -47,4 +47,4 @@
47int hipd_get_flag(unsigned int);47int hipd_get_flag(unsigned int);
48void hipd_set_flag(unsigned int);48void hipd_set_flag(unsigned int);
4949
50#endif /* HIPL_HIPD_ACCESSOR_H */50#endif /* HIPL_LIBHIPL_ACCESSOR_H */
5151
=== renamed file 'hipd/cert.c' => 'libhipl/cert.c'
=== renamed file 'hipd/cert.h' => 'libhipl/cert.h'
--- hipd/cert.h 2012-05-12 06:54:33 +0000
+++ libhipl/cert.h 2012-05-12 14:17:22 +0000
@@ -29,8 +29,8 @@
29 * Certificate signing and verification functions.29 * Certificate signing and verification functions.
30 */30 */
3131
32#ifndef HIPL_HIPD_CERT_H32#ifndef HIPL_LIBHIPL_CERT_H
33#define HIPL_HIPD_CERT_H33#define HIPL_LIBHIPL_CERT_H
3434
35#include <openssl/rsa.h>35#include <openssl/rsa.h>
3636
@@ -45,4 +45,4 @@
45int hip_cert_x509v3_handle_request_to_sign(struct hip_common *);45int hip_cert_x509v3_handle_request_to_sign(struct hip_common *);
46int hip_cert_x509v3_handle_request_to_verify(struct hip_common *);46int hip_cert_x509v3_handle_request_to_verify(struct hip_common *);
4747
48#endif /* HIPL_HIPD_CERT_H */48#endif /* HIPL_LIBHIPL_CERT_H */
4949
=== renamed file 'hipd/close.c' => 'libhipl/close.c'
=== renamed file 'hipd/close.h' => 'libhipl/close.h'
--- hipd/close.h 2012-05-12 06:54:33 +0000
+++ libhipl/close.h 2012-05-12 14:17:22 +0000
@@ -23,8 +23,8 @@
23 * OTHER DEALINGS IN THE SOFTWARE.23 * OTHER DEALINGS IN THE SOFTWARE.
24 */24 */
2525
26#ifndef HIPL_HIPD_CLOSE_H26#ifndef HIPL_LIBHIPL_CLOSE_H
27#define HIPL_HIPD_CLOSE_H27#define HIPL_LIBHIPL_CLOSE_H
2828
29#include "libcore/protodefs.h"29#include "libcore/protodefs.h"
30#include "libcore/state.h"30#include "libcore/state.h"
@@ -48,4 +48,4 @@
48 struct hip_packet_context *ctx);48 struct hip_packet_context *ctx);
49int hip_purge_closing_ha(struct hip_hadb_state *ha, void *opaque);49int hip_purge_closing_ha(struct hip_hadb_state *ha, void *opaque);
5050
51#endif /* HIPL_HIPD_CLOSE_H */51#endif /* HIPL_LIBHIPL_CLOSE_H */
5252
=== renamed file 'hipd/configfilereader.c' => 'libhipl/configfilereader.c'
=== renamed file 'hipd/configfilereader.h' => 'libhipl/configfilereader.h'
--- hipd/configfilereader.h 2012-01-06 14:38:54 +0000
+++ libhipl/configfilereader.h 2012-05-12 14:17:22 +0000
@@ -22,8 +22,8 @@
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE O22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE O
23 */23 */
2424
25#ifndef HIPL_HIPD_CONFIGFILEREADER_H25#ifndef HIPL_LIBHIPL_CONFIGFILEREADER_H
26#define HIPL_HIPD_CONFIGFILEREADER_H26#define HIPL_LIBHIPL_CONFIGFILEREADER_H
2727
28#include <stdio.h>28#include <stdio.h>
2929
@@ -52,4 +52,4 @@
52 struct hip_configfile_value *current);52 struct hip_configfile_value *current);
53void print_node(struct hip_configfile_value *node);53void print_node(struct hip_configfile_value *node);
5454
55#endif /* HIPL_HIPD_CONFIGFILEREADER_H */55#endif /* HIPL_LIBHIPL_CONFIGFILEREADER_H */
5656
=== renamed file 'hipd/cookie.c' => 'libhipl/cookie.c'
=== renamed file 'hipd/cookie.h' => 'libhipl/cookie.h'
--- hipd/cookie.h 2012-05-12 06:54:33 +0000
+++ libhipl/cookie.h 2012-05-12 14:17:22 +0000
@@ -23,8 +23,8 @@
23 * OTHER DEALINGS IN THE SOFTWARE.23 * OTHER DEALINGS IN THE SOFTWARE.
24 */24 */
2525
26#ifndef HIPL_HIPD_COOKIE_H26#ifndef HIPL_LIBHIPL_COOKIE_H
27#define HIPL_HIPD_COOKIE_H27#define HIPL_LIBHIPL_COOKIE_H
2828
29#include <stdint.h>29#include <stdint.h>
30#include <netinet/in.h>30#include <netinet/in.h>
@@ -58,4 +58,4 @@
58int hip_get_puzzle_difficulty_msg(struct hip_common *msg);58int hip_get_puzzle_difficulty_msg(struct hip_common *msg);
59int hip_set_puzzle_difficulty_msg(struct hip_common *msg);59int hip_set_puzzle_difficulty_msg(struct hip_common *msg);
6060
61#endif /* HIPL_HIPD_COOKIE_H */61#endif /* HIPL_LIBHIPL_COOKIE_H */
6262
=== renamed file 'hipd/dh.c' => 'libhipl/dh.c'
=== renamed file 'hipd/dh.h' => 'libhipl/dh.h'
--- hipd/dh.h 2011-11-25 17:56:24 +0000
+++ libhipl/dh.h 2012-05-12 14:17:22 +0000
@@ -23,8 +23,8 @@
23 * OTHER DEALINGS IN THE SOFTWARE.23 * OTHER DEALINGS IN THE SOFTWARE.
24 */24 */
2525
26#ifndef HIPL_HIPD_DH_H26#ifndef HIPL_LIBHIPL_DH_H
27#define HIPL_HIPD_DH_H27#define HIPL_LIBHIPL_DH_H
2828
29#include <stdint.h>29#include <stdint.h>
3030
@@ -37,4 +37,4 @@
37 int bufsize);37 int bufsize);
38int hip_init_cipher(void);38int hip_init_cipher(void);
3939
40#endif /* HIPL_HIPD_DH_H */40#endif /* HIPL_LIBHIPL_DH_H */
4141
=== renamed file 'hipd/esp_prot_anchordb.c' => 'libhipl/esp_prot_anchordb.c'
=== renamed file 'hipd/esp_prot_anchordb.h' => 'libhipl/esp_prot_anchordb.h'
--- hipd/esp_prot_anchordb.h 2012-05-12 06:54:33 +0000
+++ libhipl/esp_prot_anchordb.h 2012-05-12 14:17:22 +0000
@@ -33,8 +33,8 @@
33 * @brief Stores anchor elements to be used for the esp protection33 * @brief Stores anchor elements to be used for the esp protection
34 */34 */
3535
36#ifndef HIPL_HIPD_ESP_PROT_ANCHORDB_H36#ifndef HIPL_LIBHIPL_ESP_PROT_ANCHORDB_H
37#define HIPL_HIPD_ESP_PROT_ANCHORDB_H37#define HIPL_LIBHIPL_ESP_PROT_ANCHORDB_H
3838
39#include <stdint.h>39#include <stdint.h>
4040
@@ -47,4 +47,4 @@
47int anchor_db_get_anchor_length(const uint8_t transform);47int anchor_db_get_anchor_length(const uint8_t transform);
48int anchor_db_get_hash_item_length(const uint8_t transform);48int anchor_db_get_hash_item_length(const uint8_t transform);
4949
50#endif /* HIPL_HIPD_ESP_PROT_ANCHORDB_H */50#endif /* HIPL_LIBHIPL_ESP_PROT_ANCHORDB_H */
5151
=== renamed file 'hipd/esp_prot_hipd_msg.c' => 'libhipl/esp_prot_hipd_msg.c'
=== renamed file 'hipd/esp_prot_hipd_msg.h' => 'libhipl/esp_prot_hipd_msg.h'
--- hipd/esp_prot_hipd_msg.h 2012-05-12 06:54:33 +0000
+++ libhipl/esp_prot_hipd_msg.h 2012-05-12 14:17:22 +0000
@@ -31,8 +31,8 @@
31 * @brief Messaging with hipfw and other HIP instances31 * @brief Messaging with hipfw and other HIP instances
32 */32 */
3333
34#ifndef HIPL_HIPD_ESP_PROT_HIPD_MSG_H34#ifndef HIPL_LIBHIPL_ESP_PROT_HIPD_MSG_H
35#define HIPL_HIPD_ESP_PROT_HIPD_MSG_H35#define HIPL_LIBHIPL_ESP_PROT_HIPD_MSG_H
3636
37#include <stdint.h>37#include <stdint.h>
3838
@@ -72,4 +72,4 @@
72 struct hip_hadb_state *entry,72 struct hip_hadb_state *entry,
73 uint32_t *spi);73 uint32_t *spi);
7474
75#endif /* HIPL_HIPD_ESP_PROT_HIPD_MSG_H */75#endif /* HIPL_LIBHIPL_ESP_PROT_HIPD_MSG_H */
7676
=== renamed file 'hipd/esp_prot_light_update.c' => 'libhipl/esp_prot_light_update.c'
=== renamed file 'hipd/esp_prot_light_update.h' => 'libhipl/esp_prot_light_update.h'
--- hipd/esp_prot_light_update.h 2012-05-12 06:54:33 +0000
+++ libhipl/esp_prot_light_update.h 2012-05-12 14:17:22 +0000
@@ -31,8 +31,8 @@
31 * @brief Messaging required for HHL-based anchor element updates31 * @brief Messaging required for HHL-based anchor element updates
32 */32 */
3333
34#ifndef HIPL_HIPD_ESP_PROT_LIGHT_UPDATE_H34#ifndef HIPL_LIBHIPL_ESP_PROT_LIGHT_UPDATE_H
35#define HIPL_HIPD_ESP_PROT_LIGHT_UPDATE_H35#define HIPL_LIBHIPL_ESP_PROT_LIGHT_UPDATE_H
3636
37#include "libcore/modularization.h"37#include "libcore/modularization.h"
38#include "libcore/protodefs.h"38#include "libcore/protodefs.h"
@@ -48,4 +48,4 @@
48 const enum hip_state ha_state,48 const enum hip_state ha_state,
49 struct hip_packet_context *ctx);49 struct hip_packet_context *ctx);
5050
51#endif /* HIPL_HIPD_ESP_PROT_LIGHT_UPDATE_H */51#endif /* HIPL_LIBHIPL_ESP_PROT_LIGHT_UPDATE_H */
5252
=== renamed file 'hipd/hadb.c' => 'libhipl/hadb.c'
--- hipd/hadb.c 2012-05-12 06:54:33 +0000
+++ libhipl/hadb.c 2012-05-12 14:17:22 +0000
@@ -610,9 +610,15 @@
610 HIP_HOST_ID_HOSTNAME_LEN_MAX - 1);610 HIP_HOST_ID_HOSTNAME_LEN_MAX - 1);
611 }611 }
612612
613 if (hip_select_source_address(&peer_map.our_addr, &peer_map.peer_addr)) {613 /* hip_select_source_address() is incompatible with libhipl, therefore we skip
614 * this function and set the source address in peer_map to the ANY source
615 * address.*/
616 if (!hipl_is_libhip_mode()
617 && hip_select_source_address(&peer_map.our_addr, &peer_map.peer_addr)) {
614 HIP_ERROR("Cannot find source address\n");618 HIP_ERROR("Cannot find source address\n");
615 return -1;619 return -1;
620 } else if (hipl_is_libhip_mode()) {
621 memset(&peer_map.our_addr, 0, sizeof(peer_map.our_addr));
616 }622 }
617623
618 if (hip_for_each_hi(hadb_add_peer_info_wrapper, &peer_map)) {624 if (hip_for_each_hi(hadb_add_peer_info_wrapper, &peer_map)) {
@@ -1453,6 +1459,10 @@
1453 */1459 */
1454void hip_delete_security_associations_and_sp(struct hip_hadb_state *const ha)1460void hip_delete_security_associations_and_sp(struct hip_hadb_state *const ha)
1455{1461{
1462 if (hipl_is_libhip_mode()) {
1463 return;
1464 }
1465
1456 // Delete previous security policies1466 // Delete previous security policies
1457 hip_delete_hit_sp_pair(&ha->hit_our, &ha->hit_peer, 1);1467 hip_delete_hit_sp_pair(&ha->hit_our, &ha->hit_peer, 1);
1458 hip_delete_hit_sp_pair(&ha->hit_peer, &ha->hit_our, 1);1468 hip_delete_hit_sp_pair(&ha->hit_peer, &ha->hit_our, 1);
@@ -1522,6 +1532,11 @@
1522 ha),1532 ha),
1523 -1, "Error while changing outbound security association\n");1533 -1, "Error while changing outbound security association\n");
15241534
1535 if (hipl_is_libhip_mode()) {
1536 HIP_DEBUG("No SP set up in library mode\n");
1537 goto out_err;
1538 }
1539
1525 // Create a new security policy pointing to SAs after SA setup1540 // Create a new security policy pointing to SAs after SA setup
1526 HIP_IFEL(hip_setup_hit_sp_pair(&ha->hit_peer,1541 HIP_IFEL(hip_setup_hit_sp_pair(&ha->hit_peer,
1527 &ha->hit_our,1542 &ha->hit_our,
15281543
=== renamed file 'hipd/hadb.h' => 'libhipl/hadb.h'
--- hipd/hadb.h 2012-05-12 06:54:33 +0000
+++ libhipl/hadb.h 2012-05-12 14:17:22 +0000
@@ -23,8 +23,8 @@
23 * OTHER DEALINGS IN THE SOFTWARE.23 * OTHER DEALINGS IN THE SOFTWARE.
24 */24 */
2525
26#ifndef HIPL_HIPD_HADB_H26#ifndef HIPL_LIBHIPL_HADB_H
27#define HIPL_HIPD_HADB_H27#define HIPL_LIBHIPL_HADB_H
2828
29#include <stdint.h>29#include <stdint.h>
3030
@@ -125,4 +125,4 @@
125 const struct in6_addr *const src_addr,125 const struct in6_addr *const src_addr,
126 const struct in6_addr *const dst_addr);126 const struct in6_addr *const dst_addr);
127127
128#endif /* HIPL_HIPD_HADB_H */128#endif /* HIPL_LIBHIPL_HADB_H */
129129
=== renamed file 'hipd/hidb.c' => 'libhipl/hidb.c'
--- hipd/hidb.c 2012-05-12 06:54:33 +0000
+++ libhipl/hidb.c 2012-05-12 14:17:22 +0000
@@ -596,13 +596,15 @@
596 hip_add_iface_local_route(&in6_lsi);596 hip_add_iface_local_route(&in6_lsi);
597597
598 /* Adding HITs and LSIs to the interface */598 /* Adding HITs and LSIs to the interface */
599 if (hip_add_iface_local_hit(&hit)) {599 if (!hipl_is_libhip_mode()) {
600 HIP_ERROR("Failed to add HIT to the device\n");600 if (hip_add_iface_local_hit(&hit)) {
601 return -1;601 HIP_ERROR("Failed to add HIT to the device\n");
602 }602 return -1;
603 if (hip_add_iface_local_hit(&in6_lsi)) {603 }
604 HIP_ERROR("Failed to add LSI to the device\n");604 if (hip_add_iface_local_hit(&in6_lsi)) {
605 return -1;605 HIP_ERROR("Failed to add LSI to the device\n");
606 return -1;
607 }
606 }608 }
607 }609 }
608610
609611
=== renamed file 'hipd/hidb.h' => 'libhipl/hidb.h'
--- hipd/hidb.h 2012-05-12 06:54:33 +0000
+++ libhipl/hidb.h 2012-05-12 14:17:22 +0000
@@ -23,8 +23,8 @@
23 * OTHER DEALINGS IN THE SOFTWARE.23 * OTHER DEALINGS IN THE SOFTWARE.
24 */24 */
2525
26#ifndef HIPL_HIPD_HIDB_H26#ifndef HIPL_LIBHIPL_HIDB_H
27#define HIPL_HIPD_HIDB_H27#define HIPL_LIBHIPL_HIDB_H
2828
29/**29/**
30 * @file30 * @file
@@ -75,4 +75,4 @@
75int hip_get_default_hit_msg(struct hip_common *msg);75int hip_get_default_hit_msg(struct hip_common *msg);
76int hip_get_default_lsi(struct in_addr *lsi);76int hip_get_default_lsi(struct in_addr *lsi);
7777
78#endif /* HIPL_HIPD_HIDB_H */78#endif /* HIPL_LIBHIPL_HIDB_H */
7979
=== renamed file 'hipd/hip_socket.c' => 'libhipl/hip_socket.c'
=== renamed file 'hipd/hip_socket.h' => 'libhipl/hip_socket.h'
--- hipd/hip_socket.h 2012-05-12 06:54:33 +0000
+++ libhipl/hip_socket.h 2012-05-12 14:17:22 +0000
@@ -23,8 +23,8 @@
23 * OTHER DEALINGS IN THE SOFTWARE.23 * OTHER DEALINGS IN THE SOFTWARE.
24 */24 */
2525
26#ifndef HIPL_HIPD_HIP_SOCKET_H26#ifndef HIPL_LIBHIPL_HIP_SOCKET_H
27#define HIPL_HIPD_HIP_SOCKET_H27#define HIPL_LIBHIPL_HIP_SOCKET_H
2828
29#include <stdint.h>29#include <stdint.h>
30#include <sys/select.h>30#include <sys/select.h>
@@ -50,4 +50,4 @@
5050
51void hip_run_socket_handles(fd_set *read_fdset, struct hip_packet_context *ctx);51void hip_run_socket_handles(fd_set *read_fdset, struct hip_packet_context *ctx);
5252
53#endif /* HIPL_HIPD_HIP_SOCKET_H */53#endif /* HIPL_LIBHIPL_HIP_SOCKET_H */
5454
=== renamed file 'hipd/hipd.c' => 'libhipl/hipd.c'
=== renamed file 'hipd/hipd.h' => 'libhipl/hipd.h'
--- hipd/hipd.h 2012-05-12 06:54:33 +0000
+++ libhipl/hipd.h 2012-05-12 14:17:22 +0000
@@ -23,10 +23,11 @@
23 * OTHER DEALINGS IN THE SOFTWARE.23 * OTHER DEALINGS IN THE SOFTWARE.
24 */24 */
2525
26#ifndef HIPL_HIPD_HIPD_H26#ifndef HIPL_LIBHIPL_HIPD_H
27#define HIPL_HIPD_HIPD_H27#define HIPL_LIBHIPL_HIPD_H
2828
29#include <netdb.h>29#include <netdb.h>
30#include <stdbool.h>
30#include <stdint.h>31#include <stdint.h>
31#include <netinet/in.h>32#include <netinet/in.h>
32#include <sys/types.h>33#include <sys/types.h>
@@ -80,4 +81,8 @@
80int hipd_parse_cmdline_opts(int argc, char *argv[], uint64_t * flags);81int hipd_parse_cmdline_opts(int argc, char *argv[], uint64_t * flags);
81int hipd_main(uint64_t flags);82int hipd_main(uint64_t flags);
8283
83#endif /* HIPL_HIPD_HIPD_H */84/* libhip_mode accessor */
85bool hipl_is_libhip_mode(void);
86void hipl_set_libhip_mode(void);
87
88#endif /* HIPL_LIBHIPL_HIPD_H */
8489
=== renamed file 'hipd/hiprelay.c' => 'libhipl/hiprelay.c'
=== renamed file 'hipd/hiprelay.h' => 'libhipl/hiprelay.h'
--- hipd/hiprelay.h 2012-05-12 06:54:33 +0000
+++ libhipl/hiprelay.h 2012-05-12 14:17:22 +0000
@@ -30,8 +30,8 @@
30 * draft-ietf-hip-nat-traversal-03</a>30 * draft-ietf-hip-nat-traversal-03</a>
31 */31 */
3232
33#ifndef HIPL_HIPD_HIPRELAY_H33#ifndef HIPL_LIBHIPL_HIPRELAY_H
34#define HIPL_HIPD_HIPRELAY_H34#define HIPL_LIBHIPL_HIPRELAY_H
3535
36#include <stdint.h>36#include <stdint.h>
37#include <netinet/in.h>37#include <netinet/in.h>
@@ -133,4 +133,4 @@
133 struct hip_relrec *rec,133 struct hip_relrec *rec,
134 const uint8_t type_hdr);134 const uint8_t type_hdr);
135135
136#endif /* HIPL_HIPD_HIPRELAY_H */136#endif /* HIPL_LIBHIPL_HIPRELAY_H */
137137
=== renamed file 'hipd/hit_to_ip.c' => 'libhipl/hit_to_ip.c'
=== renamed file 'hipd/hit_to_ip.h' => 'libhipl/hit_to_ip.h'
--- hipd/hit_to_ip.h 2012-05-12 06:54:33 +0000
+++ libhipl/hit_to_ip.h 2012-05-12 14:17:22 +0000
@@ -29,8 +29,8 @@
29 * @brief usually invoked by hip_map_id_to_addr29 * @brief usually invoked by hip_map_id_to_addr
30 */30 */
3131
32#ifndef HIPL_HIPD_HIT_TO_IP_H32#ifndef HIPL_LIBHIPL_HIT_TO_IP_H
33#define HIPL_HIPD_HIT_TO_IP_H33#define HIPL_LIBHIPL_HIT_TO_IP_H
3434
35#include <netinet/in.h>35#include <netinet/in.h>
3636
@@ -42,4 +42,4 @@
42int hip_get_hit_to_ip_status(void);42int hip_get_hit_to_ip_status(void);
43void hip_hit_to_ip_set(const char *zone);43void hip_hit_to_ip_set(const char *zone);
4444
45#endif /* HIPL_HIPD_HIT_TO_IP_H */45#endif /* HIPL_LIBHIPL_HIT_TO_IP_H */
4646
=== renamed file 'hipd/init.c' => 'libhipl/init.c'
--- hipd/init.c 2012-05-12 06:54:33 +0000
+++ libhipl/init.c 2012-05-12 14:17:22 +0000
@@ -34,7 +34,9 @@
3434
35#include <errno.h>35#include <errno.h>
36#include <limits.h>36#include <limits.h>
37#include <pwd.h>
37#include <signal.h>38#include <signal.h>
39#include <stdbool.h>
38#include <stdint.h>40#include <stdint.h>
39#include <stdlib.h>41#include <stdlib.h>
40#include <string.h>42#include <string.h>
@@ -118,6 +120,34 @@
118#endif120#endif
119/** end ICMPV6_FILTER related stuff */121/** end ICMPV6_FILTER related stuff */
120122
123#define HIPL_USER_DIR ".hipl"
124#define HIPL_USER_RSA_KEY_NAME "libhipl_rsa_key"
125
126/* Flag to show if hipl is running in libhip mode (=1) or normal mode (=0).
127 *
128 * This variable should NOT be accessed directly. Always use the accessor
129 * functions instead.
130 */
131static bool hipd_library_mode = false;
132
133/**
134 * Test if the library mode is on.
135 *
136 * @return true if the library mode is on, false otherwise.
137 */
138bool hipl_is_libhip_mode(void)
139{
140 return hipd_library_mode;
141}
142
143/**
144 * Turn on library mode.
145 */
146void hipl_set_libhip_mode(void)
147{
148 hipd_library_mode = true;
149}
150
121/* Startup flags of the HIPD. Keep the around, for they will be used at exit */151/* Startup flags of the HIPD. Keep the around, for they will be used at exit */
122static uint64_t sflags;152static uint64_t sflags;
123153
@@ -523,6 +553,199 @@
523 lmod_register_packet_type(HIP_LUPDATE, "HIP_LUPDATE");553 lmod_register_packet_type(HIP_LUPDATE, "HIP_LUPDATE");
524}554}
525555
556static int libhip_init_handle_functions(void)
557{
558 HIP_DEBUG("Initialize handle functions for libhip.\n");
559
560 hip_register_handle_function(HIP_I1, HIP_STATE_UNASSOCIATED, &hip_check_i1, 20000);
561 hip_register_handle_function(HIP_I1, HIP_STATE_UNASSOCIATED, &hip_handle_i1, 30000);
562 hip_register_handle_function(HIP_I1, HIP_STATE_UNASSOCIATED, &hip_update_retransmissions, 35000);
563 hip_register_handle_function(HIP_I1, HIP_STATE_UNASSOCIATED, &hip_send_r1, 40000);
564 hip_register_handle_function(HIP_I1, HIP_STATE_I1_SENT, &hip_check_i1, 20000);
565 hip_register_handle_function(HIP_I1, HIP_STATE_I1_SENT, &hip_handle_i1, 30000);
566 hip_register_handle_function(HIP_I1, HIP_STATE_I1_SENT, &hip_update_retransmissions, 35000);
567 hip_register_handle_function(HIP_I1, HIP_STATE_I1_SENT, &hip_send_r1, 40000);
568 hip_register_handle_function(HIP_I1, HIP_STATE_I2_SENT, &hip_check_i1, 20000);
569 hip_register_handle_function(HIP_I1, HIP_STATE_I2_SENT, &hip_handle_i1, 30000);
570 hip_register_handle_function(HIP_I1, HIP_STATE_I2_SENT, &hip_update_retransmissions, 35000);
571 hip_register_handle_function(HIP_I1, HIP_STATE_I2_SENT, &hip_send_r1, 40000);
572 hip_register_handle_function(HIP_I1, HIP_STATE_R2_SENT, &hip_check_i1, 20000);
573 hip_register_handle_function(HIP_I1, HIP_STATE_R2_SENT, &hip_handle_i1, 30000);
574 hip_register_handle_function(HIP_I1, HIP_STATE_R2_SENT, &hip_update_retransmissions, 35000);
575 hip_register_handle_function(HIP_I1, HIP_STATE_R2_SENT, &hip_send_r1, 40000);
576 hip_register_handle_function(HIP_I1, HIP_STATE_ESTABLISHED, &hip_check_i1, 20000);
577 hip_register_handle_function(HIP_I1, HIP_STATE_ESTABLISHED, &hip_handle_i1, 30000);
578 hip_register_handle_function(HIP_I1, HIP_STATE_ESTABLISHED, &hip_update_retransmissions, 35000);
579 hip_register_handle_function(HIP_I1, HIP_STATE_ESTABLISHED, &hip_send_r1, 40000);
580 hip_register_handle_function(HIP_I1, HIP_STATE_CLOSING, &hip_check_i1, 20000);
581 hip_register_handle_function(HIP_I1, HIP_STATE_CLOSING, &hip_handle_i1, 30000);
582 hip_register_handle_function(HIP_I1, HIP_STATE_CLOSING, &hip_update_retransmissions, 35000);
583 hip_register_handle_function(HIP_I1, HIP_STATE_CLOSING, &hip_send_r1, 40000);
584 hip_register_handle_function(HIP_I1, HIP_STATE_CLOSED, &hip_check_i1, 20000);
585 hip_register_handle_function(HIP_I1, HIP_STATE_CLOSED, &hip_handle_i1, 30000);
586 hip_register_handle_function(HIP_I1, HIP_STATE_CLOSED, &hip_update_retransmissions, 35000);
587 hip_register_handle_function(HIP_I1, HIP_STATE_CLOSED, &hip_send_r1, 40000);
588 hip_register_handle_function(HIP_I1, HIP_STATE_NONE, &hip_check_i1, 20000);
589 hip_register_handle_function(HIP_I1, HIP_STATE_NONE, &hip_handle_i1, 30000);
590 hip_register_handle_function(HIP_I1, HIP_STATE_NONE, &hip_update_retransmissions, 35000);
591 hip_register_handle_function(HIP_I1, HIP_STATE_NONE, &hip_send_r1, 40000);
592
593 hip_register_handle_function(HIP_I2, HIP_STATE_UNASSOCIATED, &hip_check_i2, 20000);
594 hip_register_handle_function(HIP_I2, HIP_STATE_UNASSOCIATED, &hip_handle_i2, 30000);
595 hip_register_handle_function(HIP_I2, HIP_STATE_UNASSOCIATED, &hip_update_retransmissions, 30250);
596 hip_register_handle_function(HIP_I2, HIP_STATE_UNASSOCIATED, &hip_create_r2, 40000);
597 hip_register_handle_function(HIP_I2, HIP_STATE_UNASSOCIATED, &hip_add_rvs_reg_from, 41000);
598 hip_register_handle_function(HIP_I2, HIP_STATE_UNASSOCIATED, &hip_hmac2_and_sign, 42000);
599 hip_register_handle_function(HIP_I2, HIP_STATE_UNASSOCIATED, &hip_add_rvs_relay_to, 43000);
600 hip_register_handle_function(HIP_I2, HIP_STATE_UNASSOCIATED, &hip_send_r2, 50000);
601 hip_register_handle_function(HIP_I2, HIP_STATE_I1_SENT, &hip_check_i2, 20000);
602 hip_register_handle_function(HIP_I2, HIP_STATE_I1_SENT, &hip_handle_i2, 30000);
603 hip_register_handle_function(HIP_I2, HIP_STATE_I1_SENT, &hip_update_retransmissions, 30250);
604 hip_register_handle_function(HIP_I2, HIP_STATE_I1_SENT, &hip_create_r2, 40000);
605 hip_register_handle_function(HIP_I2, HIP_STATE_I1_SENT, &hip_add_rvs_reg_from, 41000);
606 hip_register_handle_function(HIP_I2, HIP_STATE_I1_SENT, &hip_hmac2_and_sign, 42000);
607 hip_register_handle_function(HIP_I2, HIP_STATE_I1_SENT, &hip_add_rvs_relay_to, 43000);
608 hip_register_handle_function(HIP_I2, HIP_STATE_I1_SENT, &hip_send_r2, 50000);
609 hip_register_handle_function(HIP_I2, HIP_STATE_I2_SENT, &hip_check_i2, 20000);
610 hip_register_handle_function(HIP_I2, HIP_STATE_I2_SENT, &hip_handle_i2_in_i2_sent, 21000);
611 hip_register_handle_function(HIP_I2, HIP_STATE_I2_SENT, &hip_handle_i2, 30000);
612 hip_register_handle_function(HIP_I2, HIP_STATE_I2_SENT, &hip_update_retransmissions, 30250);
613 hip_register_handle_function(HIP_I2, HIP_STATE_I2_SENT, &hip_create_r2, 40000);
614 hip_register_handle_function(HIP_I2, HIP_STATE_I2_SENT, &hip_add_rvs_reg_from, 41000);
615 hip_register_handle_function(HIP_I2, HIP_STATE_I2_SENT, &hip_hmac2_and_sign, 42000);
616 hip_register_handle_function(HIP_I2, HIP_STATE_I2_SENT, &hip_add_rvs_relay_to, 43000);
617 hip_register_handle_function(HIP_I2, HIP_STATE_I2_SENT, &hip_send_r2, 50000);
618 hip_register_handle_function(HIP_I2, HIP_STATE_R2_SENT, &hip_check_i2, 20000);
619 hip_register_handle_function(HIP_I2, HIP_STATE_R2_SENT, &hip_handle_i2, 30000);
620 hip_register_handle_function(HIP_I2, HIP_STATE_R2_SENT, &hip_update_retransmissions, 30250);
621 hip_register_handle_function(HIP_I2, HIP_STATE_R2_SENT, &hip_create_r2, 40000);
622 hip_register_handle_function(HIP_I2, HIP_STATE_R2_SENT, &hip_add_rvs_reg_from, 41000);
623 hip_register_handle_function(HIP_I2, HIP_STATE_R2_SENT, &hip_hmac2_and_sign, 42000);
624 hip_register_handle_function(HIP_I2, HIP_STATE_R2_SENT, &hip_add_rvs_relay_to, 43000);
625 hip_register_handle_function(HIP_I2, HIP_STATE_R2_SENT, &hip_send_r2, 50000);
626 hip_register_handle_function(HIP_I2, HIP_STATE_ESTABLISHED, &hip_check_i2, 20000);
627 hip_register_handle_function(HIP_I2, HIP_STATE_ESTABLISHED, &hip_handle_i2, 30000);
628 hip_register_handle_function(HIP_I2, HIP_STATE_ESTABLISHED, &hip_update_retransmissions, 30250);
629 hip_register_handle_function(HIP_I2, HIP_STATE_ESTABLISHED, &hip_create_r2, 40000);
630 hip_register_handle_function(HIP_I2, HIP_STATE_ESTABLISHED, &hip_add_rvs_reg_from, 41000);
631 hip_register_handle_function(HIP_I2, HIP_STATE_ESTABLISHED, &hip_hmac2_and_sign, 42000);
632 hip_register_handle_function(HIP_I2, HIP_STATE_ESTABLISHED, &hip_add_rvs_relay_to, 43000);
633 hip_register_handle_function(HIP_I2, HIP_STATE_ESTABLISHED, &hip_send_r2, 50000);
634 hip_register_handle_function(HIP_I2, HIP_STATE_CLOSING, &hip_check_i2, 20000);
635 hip_register_handle_function(HIP_I2, HIP_STATE_CLOSING, &hip_handle_i2, 30000);
636 hip_register_handle_function(HIP_I2, HIP_STATE_CLOSING, &hip_update_retransmissions, 30250);
637 hip_register_handle_function(HIP_I2, HIP_STATE_CLOSING, &hip_create_r2, 40000);
638 hip_register_handle_function(HIP_I2, HIP_STATE_CLOSING, &hip_add_rvs_reg_from, 41000);
639 hip_register_handle_function(HIP_I2, HIP_STATE_CLOSING, &hip_hmac2_and_sign, 42000);
640 hip_register_handle_function(HIP_I2, HIP_STATE_CLOSING, &hip_add_rvs_relay_to, 43000);
641 hip_register_handle_function(HIP_I2, HIP_STATE_CLOSING, &hip_send_r2, 50000);
642 hip_register_handle_function(HIP_I2, HIP_STATE_CLOSED, &hip_check_i2, 20000);
643 hip_register_handle_function(HIP_I2, HIP_STATE_CLOSED, &hip_handle_i2, 30000);
644 hip_register_handle_function(HIP_I2, HIP_STATE_CLOSED, &hip_update_retransmissions, 30250);
645 hip_register_handle_function(HIP_I2, HIP_STATE_CLOSED, &hip_create_r2, 40000);
646 hip_register_handle_function(HIP_I2, HIP_STATE_CLOSED, &hip_add_rvs_reg_from, 41000);
647 hip_register_handle_function(HIP_I2, HIP_STATE_CLOSED, &hip_hmac2_and_sign, 42000);
648 hip_register_handle_function(HIP_I2, HIP_STATE_CLOSED, &hip_add_rvs_relay_to, 43000);
649 hip_register_handle_function(HIP_I2, HIP_STATE_CLOSED, &hip_send_r2, 50000);
650 hip_register_handle_function(HIP_I2, HIP_STATE_NONE, &hip_check_i2, 20000);
651 hip_register_handle_function(HIP_I2, HIP_STATE_NONE, &hip_handle_i2, 30000);
652 hip_register_handle_function(HIP_I2, HIP_STATE_NONE, &hip_update_retransmissions, 30250);
653 hip_register_handle_function(HIP_I2, HIP_STATE_NONE, &hip_create_r2, 40000);
654 hip_register_handle_function(HIP_I2, HIP_STATE_NONE, &hip_add_rvs_reg_from, 41000);
655 hip_register_handle_function(HIP_I2, HIP_STATE_NONE, &hip_hmac2_and_sign, 42000);
656 hip_register_handle_function(HIP_I2, HIP_STATE_NONE, &hip_add_rvs_relay_to, 43000);
657 hip_register_handle_function(HIP_I2, HIP_STATE_NONE, &hip_send_r2, 50000);
658
659 hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &hip_check_r1, 20000);
660 hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &hip_handle_r1, 30000);
661 hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &hip_update_retransmissions, 30500);
662 hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &hip_build_esp_info, 31000);
663 hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &hip_build_solution, 32000);
664 hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &hip_handle_diffie_hellman, 33000);
665 hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &esp_prot_r1_handle_transforms, 34000);
666 hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &hip_create_i2, 40000);
667 hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &hip_add_signed_echo_response, 41000);
668 hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &hip_mac_and_sign_handler, 42000);
669 hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &hip_add_unsigned_echo_response, 43000);
670 hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &hip_send_i2, 50000);
671 hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &hip_check_r1, 20000);
672 hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &hip_handle_r1, 30000);
673 hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &hip_update_retransmissions, 30500);
674 hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &hip_build_esp_info, 31000);
675 hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &hip_build_solution, 32000);
676 hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &hip_handle_diffie_hellman, 33000);
677 hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &esp_prot_r1_handle_transforms, 34000);
678 hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &hip_create_i2, 40000);
679 hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &hip_add_signed_echo_response, 41000);
680 hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &hip_mac_and_sign_handler, 42000);
681 hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &hip_add_unsigned_echo_response, 43000);
682 hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &hip_send_i2, 50000);
683 hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &hip_check_r1, 20000);
684 hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &hip_handle_r1, 30000);
685 hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &hip_update_retransmissions, 30500);
686 hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &hip_build_esp_info, 31000);
687 hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &hip_build_solution, 32000);
688 hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &hip_handle_diffie_hellman, 33000);
689 hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &esp_prot_r1_handle_transforms, 34000);
690 hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &hip_create_i2, 40000);
691 hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &hip_add_signed_echo_response, 41000);
692 hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &hip_mac_and_sign_handler, 42000);
693 hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &hip_add_unsigned_echo_response, 43000);
694 hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &hip_send_i2, 50000);
695 hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &hip_check_r1, 20000);
696 hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &hip_handle_r1, 30000);
697 hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &hip_update_retransmissions, 30500);
698 hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &hip_build_esp_info, 31000);
699 hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &hip_build_solution, 32000);
700 hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &hip_handle_diffie_hellman, 33000);
701 hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &esp_prot_r1_handle_transforms, 34000);
702 hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &hip_create_i2, 40000);
703 hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &hip_add_signed_echo_response, 41000);
704 hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &hip_mac_and_sign_handler, 42000);
705 hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &hip_add_unsigned_echo_response, 43000);
706 hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &hip_send_i2, 50000);
707
708 hip_register_handle_function(HIP_R2, HIP_STATE_I2_SENT, &hip_check_r2, 20000);
709 hip_register_handle_function(HIP_R2, HIP_STATE_I2_SENT, &hip_handle_r2, 30000);
710 hip_register_handle_function(HIP_R2, HIP_STATE_I2_SENT, &hip_update_retransmissions, 30250);
711
712 hip_register_handle_function(HIP_NOTIFY, HIP_STATE_I1_SENT, &hip_check_notify, 20000);
713 hip_register_handle_function(HIP_NOTIFY, HIP_STATE_I1_SENT, &hip_handle_notify, 30000);
714 hip_register_handle_function(HIP_NOTIFY, HIP_STATE_I2_SENT, &hip_check_notify, 20000);
715 hip_register_handle_function(HIP_NOTIFY, HIP_STATE_I2_SENT, &hip_handle_notify, 30000);
716 hip_register_handle_function(HIP_NOTIFY, HIP_STATE_R2_SENT, &hip_check_notify, 20000);
717 hip_register_handle_function(HIP_NOTIFY, HIP_STATE_R2_SENT, &hip_handle_notify, 30000);
718 hip_register_handle_function(HIP_NOTIFY, HIP_STATE_ESTABLISHED, &hip_check_notify, 20000);
719 hip_register_handle_function(HIP_NOTIFY, HIP_STATE_ESTABLISHED, &hip_handle_notify, 30000);
720 hip_register_handle_function(HIP_NOTIFY, HIP_STATE_CLOSING, &hip_check_notify, 20000);
721 hip_register_handle_function(HIP_NOTIFY, HIP_STATE_CLOSING, &hip_handle_notify, 30000);
722 hip_register_handle_function(HIP_NOTIFY, HIP_STATE_CLOSED, &hip_check_notify, 20000);
723 hip_register_handle_function(HIP_NOTIFY, HIP_STATE_CLOSED, &hip_handle_notify, 30000);
724
725 hip_register_handle_function(HIP_CLOSE, HIP_STATE_ESTABLISHED, &hip_close_check_packet, 20000);
726 hip_register_handle_function(HIP_CLOSE, HIP_STATE_ESTABLISHED, &hip_update_retransmissions, 25000);
727 hip_register_handle_function(HIP_CLOSE, HIP_STATE_ESTABLISHED, &hip_close_create_response, 30000);
728 hip_register_handle_function(HIP_CLOSE, HIP_STATE_ESTABLISHED, &hip_close_send_response, 40000);
729
730 hip_register_handle_function(HIP_CLOSE, HIP_STATE_CLOSING, &hip_close_check_packet, 20000);
731 hip_register_handle_function(HIP_CLOSE, HIP_STATE_CLOSING, &hip_update_retransmissions, 25000);
732 hip_register_handle_function(HIP_CLOSE, HIP_STATE_CLOSING, &hip_close_create_response, 30000);
733 hip_register_handle_function(HIP_CLOSE, HIP_STATE_CLOSING, &hip_close_send_response, 40000);
734
735 hip_register_handle_function(HIP_CLOSE_ACK, HIP_STATE_CLOSING, &hip_close_ack_check_packet, 20000);
736 hip_register_handle_function(HIP_CLOSE_ACK, HIP_STATE_CLOSING, &hip_update_retransmissions, 25000);
737 hip_register_handle_function(HIP_CLOSE_ACK, HIP_STATE_CLOSING, &hip_close_ack_handle_packet, 30000);
738
739 hip_register_handle_function(HIP_CLOSE_ACK, HIP_STATE_CLOSED, &hip_close_ack_check_packet, 20000);
740 hip_register_handle_function(HIP_CLOSE_ACK, HIP_STATE_CLOSED, &hip_update_retransmissions, 25000);
741 hip_register_handle_function(HIP_CLOSE_ACK, HIP_STATE_CLOSED, &hip_close_ack_handle_packet, 30000);
742
743 hip_register_handle_function(HIP_LUPDATE, HIP_STATE_ESTABLISHED, &esp_prot_handle_light_update, 20000);
744 hip_register_handle_function(HIP_LUPDATE, HIP_STATE_R2_SENT, &esp_prot_handle_light_update, 20000);
745
746 return 0;
747}
748
526static int init_handle_functions(void)749static int init_handle_functions(void)
527{750{
528 int err = 0;751 int err = 0;
@@ -1090,6 +1313,78 @@
1090}1313}
10911314
1092/**1315/**
1316 * Initialization function for libhipl.
1317 *
1318 * @param debug_level debug level for log output.
1319 * @return 0 on success, negative number on error.
1320 */
1321int hipl_lib_init(enum logdebug debug_level)
1322{
1323 int err = 0;
1324 int keypath_len = 0;
1325 char *key_path = NULL;
1326 struct hip_common *msg = NULL;
1327 struct passwd *pwd;
1328
1329 hipl_set_libhip_mode();
1330 hip_nat_status = 1;
1331
1332 /* disable hip_firewall */
1333 lsi_status = HIP_MSG_LSI_OFF;
1334
1335 hip_set_logdebug(debug_level);
1336
1337 hip_init_hadb();
1338 hip_init_hostid_db();
1339 hip_netdev_init_addresses();
1340 libhip_init_handle_functions();
1341
1342 /* Load default key from ~/.hipl/ */
1343 if ((pwd = getpwuid(getuid())) == NULL) {
1344 return -1;
1345 }
1346
1347 /* +3 because we need 2 slashes and a NULL for termination */
1348 keypath_len = strlen(pwd->pw_dir) +
1349 strlen(HIPL_USER_DIR) +
1350 strlen(HIPL_USER_RSA_KEY_NAME) +
1351 strlen(DEFAULT_PUB_HI_FILE_NAME_SUFFIX) + 3;
1352 if ((key_path = malloc(keypath_len)) == NULL) {
1353 HIP_ERROR("malloc() failed\n");
1354 return -ENOMEM;
1355 }
1356
1357 HIP_IFEL(snprintf(key_path, keypath_len, "%s/%s/%s%s", pwd->pw_dir,
1358 HIPL_USER_DIR,
1359 HIPL_USER_RSA_KEY_NAME,
1360 DEFAULT_PUB_HI_FILE_NAME_SUFFIX) < 0,
1361 -1, "snprintf() failed");
1362
1363 HIP_DEBUG("Using key: %s\n", key_path);
1364 HIP_IFEL(!(msg = hip_msg_alloc()), -ENOMEM, "hip_msg_alloc()");
1365 if (hip_serialize_host_id_action(msg, ACTION_ADD, 0, 0, "rsa",
1366 key_path, 0, 0, 0)) {
1367 hip_msg_init(msg);
1368 HIP_IFEL(hip_serialize_host_id_action(msg, ACTION_NEW, 0, 0, "rsa",
1369 key_path, RSA_KEY_DEFAULT_BITS,
1370 DSA_KEY_DEFAULT_BITS,
1371 ECDSA_DEFAULT_CURVE), -1,
1372 "Fail to create local key at %s.", key_path);
1373
1374 hip_msg_init(msg);
1375 HIP_IFEL(hip_serialize_host_id_action(msg, ACTION_ADD, 0, 0, "rsa",
1376 key_path, 0, 0, 0), -1,
1377 "Fail to load local key at %s.", key_path);
1378 }
1379 HIP_IFE(hip_handle_add_local_hi(msg), -1);
1380
1381out_err:
1382 free(msg);
1383 free(key_path);
1384 return err;
1385}
1386
1387/**
1093 * create a socket to handle UDP encapsulation of HIP control1388 * create a socket to handle UDP encapsulation of HIP control
1094 * packets1389 * packets
1095 *1390 *
10961391
=== renamed file 'hipd/init.h' => 'libhipl/init.h'
--- hipd/init.h 2011-11-25 17:56:24 +0000
+++ libhipl/init.h 2012-05-12 14:17:22 +0000
@@ -1,5 +1,5 @@
1/*1/*
2 * Copyright (c) 2010 Aalto University and RWTH Aachen University.2 * Copyright (c) 2010-2012 Aalto University and RWTH Aachen University.
3 *3 *
4 * Permission is hereby granted, free of charge, to any person4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation5 * obtaining a copy of this software and associated documentation
@@ -23,12 +23,14 @@
23 * OTHER DEALINGS IN THE SOFTWARE.23 * OTHER DEALINGS IN THE SOFTWARE.
24 */24 */
2525
26#ifndef HIPL_HIPD_INIT_H26#ifndef HIPL_LIBHIPL_INIT_H
27#define HIPL_HIPD_INIT_H27#define HIPL_LIBHIPL_INIT_H
2828
29#include <stdint.h>29#include <stdint.h>
30#include <netinet/in.h>30#include <netinet/in.h>
3131
32#include "libcore/debug.h"
33
3234
33/* startup flags options to be configured via the command line */35/* startup flags options to be configured via the command line */
34#define HIPD_START_FOREGROUND (1 << 0)36#define HIPD_START_FOREGROUND (1 << 0)
@@ -60,4 +62,6 @@
60 int is_output);62 int is_output);
61void hip_exit(void);63void hip_exit(void);
6264
63#endif /* HIPL_HIPD_INIT_H */65int hipl_lib_init(enum logdebug);
66
67#endif /* HIPL_LIBHIPL_INIT_H */
6468
=== renamed file 'hipd/input.c' => 'libhipl/input.c'
--- hipd/input.c 2012-05-12 06:54:33 +0000
+++ libhipl/input.c 2012-05-12 14:17:22 +0000
@@ -707,6 +707,11 @@
707 &ctx->src_addr);707 &ctx->src_addr);
708 }708 }
709709
710 if (hipl_is_libhip_mode()) {
711 ctx->msg_ports.src_port = ctx->hadb_entry->peer_udp_port;
712 ctx->msg_ports.dst_port = ctx->hadb_entry->local_udp_port;
713 }
714
710 hip_relay_add_rvs_to_ha(ctx->input_msg, ctx->hadb_entry);715 hip_relay_add_rvs_to_ha(ctx->input_msg, ctx->hadb_entry);
711716
712#ifdef CONFIG_HIP_RVS717#ifdef CONFIG_HIP_RVS
713718
=== renamed file 'hipd/input.h' => 'libhipl/input.h'
--- hipd/input.h 2012-05-12 06:54:33 +0000
+++ libhipl/input.h 2012-05-12 14:17:22 +0000
@@ -23,8 +23,8 @@
23 * OTHER DEALINGS IN THE SOFTWARE.23 * OTHER DEALINGS IN THE SOFTWARE.
24 */24 */
2525
26#ifndef HIPL_HIPD_INPUT_H26#ifndef HIPL_LIBHIPL_INPUT_H
27#define HIPL_HIPD_INPUT_H27#define HIPL_LIBHIPL_INPUT_H
2828
29#include <stdint.h>29#include <stdint.h>
30#include <netinet/in.h>30#include <netinet/in.h>
@@ -133,4 +133,4 @@
133 UNUSED const enum hip_state ha_state,133 UNUSED const enum hip_state ha_state,
134 struct hip_packet_context *const ctx);134 struct hip_packet_context *const ctx);
135135
136#endif /* HIPL_HIPD_INPUT_H */136#endif /* HIPL_LIBHIPL_INPUT_H */
137137
=== renamed file 'hipd/keymat.c' => 'libhipl/keymat.c'
=== renamed file 'hipd/keymat.h' => 'libhipl/keymat.h'
--- hipd/keymat.h 2012-05-12 06:54:33 +0000
+++ libhipl/keymat.h 2012-05-12 14:17:22 +0000
@@ -23,8 +23,8 @@
23 * OTHER DEALINGS IN THE SOFTWARE.23 * OTHER DEALINGS IN THE SOFTWARE.
24 */24 */
2525
26#ifndef HIPL_HIPD_KEYMAT_H26#ifndef HIPL_LIBHIPL_KEYMAT_H
27#define HIPL_HIPD_KEYMAT_H27#define HIPL_LIBHIPL_KEYMAT_H
2828
29#include <stdint.h>29#include <stdint.h>
30#include <netinet/in.h>30#include <netinet/in.h>
@@ -44,4 +44,4 @@
44 struct hip_keymat_keymat *keymat,44 struct hip_keymat_keymat *keymat,
45 int len);45 int len);
4646
47#endif /* HIPL_HIPD_KEYMAT_H */47#endif /* HIPL_LIBHIPL_KEYMAT_H */
4848
=== added file 'libhipl/lhipl.c'
--- libhipl/lhipl.c 1970-01-01 00:00:00 +0000
+++ libhipl/lhipl.c 2012-05-12 14:17:22 +0000
@@ -0,0 +1,536 @@
1/*
2 * Copyright (c) 2010-2012 Aalto University and RWTH Aachen University.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following
11 * conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26/**
27 * @file
28 * Provides the implementation of the libhipl public APIs.
29 */
30
31#define _BSD_SOURCE
32
33#include <errno.h>
34#include <fcntl.h>
35#include <string.h>
36#include <unistd.h>
37
38#include "libcore/builder.h"
39#include "libcore/ife.h"
40#include "libcore/prefix.h"
41#include "close.h"
42#include "hadb.h"
43#include "init.h"
44#include "lhipl_sock.h"
45#include "lhipl_operations.h"
46#include "netdev.h"
47#include "output.h"
48#include "lhipl.h"
49
50
51/* A switch to turn BEX feedback off/on.
52 *
53 * If it is on, when the BEX process is conducted during "recvmsg" and "sendmsg",
54 * the intermediate result is returned to the caller.
55 * If it is off, "recvmsg" and "sendmsg" will only return after the message gets
56 * sent or received.
57 */
58static bool libhipl_bex_feedback;
59
60/**
61 * build a @c sockaddr_in6 address to store a peer's HIT and port number from a
62 * string-based peer name and a port number.
63 *
64 * @param peername the string representation of a peer, only support HIT string
65 * currently.
66 * @param port the port of the peer.
67 * @param peer the @c sockaddr_in6 to hold the peer's HIT and port number.
68 * @return 0 on success, -1 on error.
69 */
70static int build_peer_hit(const char *peername, const uint16_t port,
71 struct sockaddr_in6 *peer)
72{
73 if (!peername || !peer) {
74 HIP_ERROR("Invalid input\n");
75 return -1;
76 }
77
78 if (inet_pton(AF_INET6, peername, &peer->sin6_addr) != 1) {
79 HIP_ERROR("Failed to translate %s into HIT\n", peername);
80 }
81 peer->sin6_port = htons(port);
82 peer->sin6_family = AF_INET6;
83 return 0;
84}
85
86/**
87 * Initialize libhipl library.
88 *
89 * This function should be called before using libhipl.
90 *
91 * @param debug_level the debug level of the libhipl library.
92 * @return 0 on success, negative number on error.
93 */
94int hipl_lib_init_all(enum hipl_lib_loglv debug_level)
95{
96 hipl_hsock_init();
97 switch (debug_level) {
98 case HIPL_LIB_LOG_DEBUG:
99 return hipl_lib_init(LOGDEBUG_ALL);
100 case HIPL_LIB_LOG_INFO:
101 return hipl_lib_init(LOGDEBUG_MEDIUM);
102 case HIPL_LIB_LOG_ERROR:
103 return hipl_lib_init(LOGDEBUG_LOW);
104 case HIPL_LIB_LOG_NONE:
105 return hipl_lib_init(LOGDEBUG_NONE);
106 }
107
108 return -1;
109}
110
111/**
112 * Check the current state of the BEX feedback switch.
113 *
114 * @return true if the switch is on, false if it is off.
115 */
116bool hipl_lib_bex_feedback(void)
117{
118 return libhipl_bex_feedback;
119}
120
121/**
122 * Turn the BEX feedback switch off/on.
123 *
124 * @param val true to turn the switch on, false to turn it off.
125 */
126void hipl_lib_set_bex_feedback(bool val)
127{
128 libhipl_bex_feedback = val;
129}
130
131/**
132 * Turn the non-blocking feature on/off for a libhipl socket.
133 *
134 * @param hsock_id ID of the libhipl socket.
135 * @param on true to turn non-blocking on, false otherwise.
136 * @return 0 on success, negative number on error.
137 */
138int hipl_lib_set_nonblock(const hipl_sock_id hsock_id, bool on)
139{
140 int flags;
141 struct hipl_sock *hsock = NULL;
142
143 if ((hsock = hipl_hsock_find(hsock_id)) == NULL) {
144 HIP_ERROR("ID %d is not a libhipl socket\n", hsock_id);
145 return -EBADF;
146 }
147
148 flags = fcntl(hsock->sock_fd, F_GETFL, 0);
149 if (on && !(flags & O_NONBLOCK)) {
150 fcntl(hsock->sock_fd, F_SETFL, flags | O_NONBLOCK);
151 } else if (!on && (flags & O_NONBLOCK)) {
152 fcntl(hsock->sock_fd, F_SETFL, flags & ~O_NONBLOCK);
153 }
154 return 0;
155}
156
157/**
158 * Return the corresponding socket file descriptor of a libhipl socket.
159 *
160 * @param hsock_id the ID of the libhipl socket
161 * @return socket file descriptor on success, -1 on error.
162 */
163int hipl_lib_get_sockfd(const hipl_sock_id hsock_id)
164{
165 struct hipl_sock *hsock = NULL;
166
167 if ((hsock = hipl_hsock_find(hsock_id)) == NULL) {
168 HIP_ERROR("ID %d is not a libhipl socket\n", hsock_id);
169 return -1;
170 }
171
172 return hsock->sock_fd;
173}
174
175/**
176 * Save peer's HIT-to-IP mapping.
177 *
178 * @param hit peer's hit.
179 * @param addr peer's IP address.
180 * @return 0 on success, -1 otherwise.
181 */
182int hipl_add_peer_info(const char *const hit, const char *const addr)
183{
184 struct in6_addr peer_hit, peer_addr6;
185 struct in_addr peer_addr4;
186
187 if (!hit || !addr) {
188 HIP_ERROR("Invalid argument\n");
189 return -1;
190 }
191 if (inet_pton(AF_INET6, hit, &peer_hit) != 1) {
192 HIP_ERROR("Invalid hit: %s\n", hit);
193 return -1;
194 }
195 if (!ipv6_addr_is_hit(&peer_hit)) {
196 HIP_ERROR("Invalid hit: %s\n", hit);
197 return -1;
198 }
199 if (inet_pton(AF_INET6, addr, &peer_addr6) != 1) {
200 if (inet_pton(AF_INET, addr, &peer_addr4) != 1) {
201 HIP_ERROR("Invalid address: %s\n", addr);
202 return -1;
203 }
204 IPV4_TO_IPV6_MAP(&peer_addr4, &peer_addr6);
205 }
206
207 return hip_hadb_add_peer_info(&peer_hit, &peer_addr6, NULL, NULL);
208}
209
210/**
211 * Create a libhipl socket.
212 *
213 * @param domain the domain of the libhipl socket (AF_INET / AF_INET6).
214 * @param type the type of the libhipl socket (SOCK_DGRAM / SOCK_STREAM).
215 * @param protocol the protocol (IPPROTO_UDP / IPPROTO_TCP).
216 * @return the ID of the new libhipl socket on success, negative
217 * number on error.
218 */
219int hipl_socket(const int domain, const int type, const int protocol)
220{
221 if (domain != AF_INET && domain != AF_INET6) {
222 HIP_ERROR("Invalid domain: %d\n", domain);
223 return -EINVAL;
224 }
225 if (type != SOCK_DGRAM && type != SOCK_STREAM) {
226 HIP_ERROR("Invalid type: %d\n", type);
227 return -EINVAL;
228 }
229 if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
230 HIP_ERROR("Invalid protocol: %d\n", protocol);
231 return -EINVAL;
232 }
233
234 return hipl_socket_internal(domain, type, protocol);
235}
236
237/**
238 * Close a socket.
239 *
240 * Send HIP CLOSE message to the associated peer and delete the libhipl
241 * socket information.
242 *
243 * @param hsock_id the ID of the libhipl socket to be closed.
244 * @return 0 on success, negative number on error.
245 */
246int hipl_close(const hipl_sock_id hsock_id)
247{
248 int ret = 0;
249 struct hip_common *msg = NULL;
250 struct hipl_sock *hsock = NULL;
251
252 if ((hsock = hipl_hsock_find(hsock_id)) == NULL) {
253 HIP_ERROR("ID %d is not a libhipl socket\n", hsock_id);
254 return -1;
255 }
256 if (!hsock->ha || hsock->ha->state == HIP_STATE_CLOSED) {
257 HIP_DEBUG("Not sending CLOSE.\n");
258 goto skip_close_msg;
259 }
260
261 /* Build HIP_CLOSE message and send it to the peer */
262 if ((msg = hip_msg_alloc()) == NULL) {
263 HIP_ERROR("hip_msg_alloc() failed");
264 ret = -ENOMEM;
265 goto skip_close_msg;
266 }
267 if ((ret = hip_build_param_contents(msg, &hsock->peer_hit,
268 HIP_PARAM_HIT,
269 sizeof(hsock->peer_hit))) < 0) {
270 HIP_ERROR("hip_build_param_contents() failed\n");
271 goto skip_close_msg;
272 }
273 if ((ret = hip_send_close(msg, 1)) < 0) {
274 HIP_ERROR("hip_send_close() failed\n");
275 }
276
277skip_close_msg:
278 if (hsock) {
279 close(hsock->sock_fd);
280 hipl_hsock_delete_and_free(hsock);
281 }
282 free(msg);
283 return ret;
284}
285
286/**
287 * Bind a libhipl socket to a local IP address.
288 *
289 * @param hsock_id the ID of the libhipl socket.
290 * @param address the IP address to be bound.
291 * @param address_len the length of the @c address.
292 * @return 0 on success, negative number on error.
293 */
294int hipl_bind(const hipl_sock_id hsock_id, const struct sockaddr *const address,
295 const socklen_t address_len)
296{
297 struct hipl_sock *hsock;
298
299 if ((hsock = hipl_hsock_find(hsock_id)) == NULL) {
300 HIP_ERROR("ID %d is not a libhipl socket\n", hsock_id);
301 return -1;
302 }
303
304 return hipl_bind_internal(hsock, address, address_len);
305}
306
307/**
308 * Switch a libhipl socket to listening mode.
309 *
310 * @param hsock_id the ID of the libhipl socket.
311 * @param backlog the max length of the queue for pending connections.
312 * @return zero on success, negative number on error.
313 */
314int hipl_listen(const hipl_sock_id hsock_id, const int backlog)
315{
316 struct hipl_sock *hsock;
317
318 if ((hsock = hipl_hsock_find(hsock_id)) == NULL) {
319 HIP_ERROR("ID %d is not a libhipl socket.\n", hsock_id);
320 return -1;
321 }
322
323 return listen(hsock->sock_fd, backlog);
324}
325
326/**
327 * Send data to a peer.
328 *
329 * Triggers base exchange if no HIP association exists.
330 * @note Data is currently sent unencrypted.
331 *
332 * @param hsock_id the ID of the libhipl socket for sending data.
333 * @param msg data to send.
334 * @param len size of the data.
335 * @param flags the flags of the socket function @c sendto().
336 * @param peername the string representation of the peer.
337 * @param port peer's port number.
338 * @return number of bytes sent on success, negative number on error.
339 */
340int hipl_sendto(const hipl_sock_id hsock_id, const void *const msg,
341 const size_t len, const int flags,
342 const char *const peername, uint16_t port)
343{
344 struct hipl_sock *hsock;
345 struct sockaddr_in6 peer;
346 struct msghdr params = { 0 };
347 struct iovec iov;
348 char *buf = NULL;
349 int err = 0;
350
351 if ((hsock = hipl_hsock_find(hsock_id)) == NULL) {
352 HIP_ERROR("ID %d is not a libhipl socket.\n", hsock_id);
353 return -1;
354 }
355 if (hsock->sock_proto == IPPROTO_UDP
356 && (peername == NULL || build_peer_hit(peername, port, &peer) < 0)) {
357 HIP_ERROR("Invalid argument: peername\n");
358 return -EINVAL;
359 }
360 if (hsock->sock_proto == IPPROTO_TCP
361 && hsock->peer_locator.ss_family == 0) {
362 HIP_ERROR("Not connected!\n");
363 return -ENOTCONN;
364 }
365 if (msg == NULL) {
366 HIP_ERROR("Invalid argument: msg\n");
367 return -EINVAL;
368 }
369
370 struct sockaddr_storage dst;
371 struct sockaddr_in6 *p6;
372 if (hsock->sock_proto == IPPROTO_UDP) {
373 hipl_build_addrstorage(&peer.sin6_addr, ntohs(peer.sin6_port), &dst);
374 } else {
375 dst = hsock->peer_locator;
376 p6 = (struct sockaddr_in6 *) &dst;
377 p6->sin6_addr = hsock->peer_hit;
378 p6->sin6_family = AF_INET6;
379 }
380 if ((buf = malloc(len)) == NULL) {
381 return -ENOMEM;
382 }
383 memcpy(buf, msg, len);
384 iov.iov_base = buf;
385 iov.iov_len = len;
386 params.msg_name = &dst;
387 params.msg_namelen = sizeof(dst);
388 params.msg_iovlen = 1;
389 params.msg_iov = &iov;
390
391 if (hipl_lib_bex_feedback()) {
392 err = hipl_sendmsg_internal(hsock, &params, flags);
393 } else {
394 fd_set fdset;
395 if (hipl_hsock_ha_state(hsock) == HIP_STATE_UNASSOCIATED) {
396 HIP_DEBUG("Sending via hsock %d, Triggering BEX.\n", hsock->sid);
397 err = hipl_sendmsg_internal(hsock, &params, flags);
398 HIP_IFEL(err != -EWAITBEX, -1, "hipl_sendmsg_internal() failed\n");
399 }
400 if (hipl_hsock_ha_state(hsock) == HIP_STATE_ESTABLISHED) {
401 HIP_DEBUG("Sending via hsock %d, HA established.\n", hsock->sid);
402 err = hipl_sendmsg_internal(hsock, &params, flags);
403 } else {
404 while (hipl_hsock_ha_state(hsock) != HIP_STATE_ESTABLISHED) {
405 FD_ZERO(&fdset);
406 FD_SET(hsock->sock_fd, &fdset);
407 HIP_DEBUG("Sending via hsock %d, Waiting BEX.\n", hsock->sid);
408 err = select(hsock->sock_fd + 1, &fdset, NULL, NULL, NULL);
409 HIP_IFEL(err < 0, -1, "select(): %s\n", strerror(errno));
410 err = hipl_sendmsg_internal(hsock, &params, flags);
411 HIP_IFEL(err < 0 && err != -EWAITBEX && err != -EBEXESTABLISHED,
412 -1, "hipl_sendmsg_internal() failed\n");
413 }
414 err = hipl_sendmsg_internal(hsock, &params, flags);
415 }
416 }
417
418out_err:
419 free(buf);
420 return err;
421}
422
423/**
424 * Receive data from a peer.
425 *
426 * Wait for base exchange if no host association exists.
427 * @note Data is currently sent unencrypted.
428 *
429 * @param hsock_id the ID of the libhipl socket for receiving data.
430 * @param buf buffer for received data.
431 * @param len the size of the @c buf.
432 * @param flags the flags of the socket function @c recvfrom().
433 * @param peername buffer for the HIT of the associated peer, the size of
434 * this buffer should be at least @c HIPL_MAX_PEERNAME.
435 * @param port buffer for the port of the associated peer.
436 * @return number of bytes received on success,
437 * negative number on error,
438 * 0 on end-of-file.
439 */
440int hipl_recvfrom(const hipl_sock_id hsock_id, void *const buf,
441 const size_t len, const int flags,
442 char *const peername, uint16_t *const port)
443{
444 struct hipl_sock *hsock = NULL;
445 struct msghdr params = { 0 };
446 struct iovec iov;
447 struct sockaddr_in6 hit;
448 int ret;
449
450 if ((hsock = hipl_hsock_find(hsock_id)) == NULL) {
451 HIP_ERROR("ID %d is not a libhipl socket.\n", hsock_id);
452 return -1;
453 }
454 if (hsock->sock_proto == IPPROTO_TCP
455 && hsock->peer_locator.ss_family == 0) {
456 HIP_ERROR("Not connected!\n");
457 return -ENOTCONN;
458 }
459
460 iov.iov_base = buf;
461 iov.iov_len = len;
462 params.msg_name = &hit;
463 params.msg_namelen = sizeof(hit);
464 params.msg_iovlen = 1;
465 params.msg_iov = &iov;
466
467 if (!hipl_lib_bex_feedback()) {
468 fd_set fdset;
469 while (hipl_hsock_ha_state(hsock) != HIP_STATE_ESTABLISHED) {
470 FD_ZERO(&fdset);
471 FD_SET(hsock->sock_fd, &fdset);
472 if (select(hsock->sock_fd + 1, &fdset, NULL, NULL, NULL) < 0) {
473 HIP_PERROR("select()");
474 }
475 ret = hipl_recvmsg_internal(hsock, &params, flags);
476 if (ret < 0 && ret != -EWAITBEX && ret != -EBEXESTABLISHED) {
477 HIP_ERROR("hipl_recvmsg_internal() failed()\n");
478 return ret;
479 }
480 }
481 }
482 ret = hipl_recvmsg_internal(hsock, &params, flags);
483
484 if (peername) {
485 inet_ntop(AF_INET6, &hit.sin6_addr, peername, HIPL_MAX_PEERNAME);
486 }
487 if (port) {
488 *port = ntohs(hit.sin6_port);
489 }
490 return ret;
491}
492
493/**
494 * Initiate a connection to a peer.
495 *
496 * @param hsock_id the ID of the libhipl socket to initiate a connection.
497 * @param peername the string representation of the peer.
498 * @param port the port number of the peer.
499 * @return 0 on success, negative number on error.
500 */
501int hipl_connect(const hipl_sock_id hsock_id, const char *peername,
502 const uint16_t port)
503{
504 struct hipl_sock *hsock = NULL;
505 struct sockaddr_in6 peer;
506
507 if (peername == NULL || build_peer_hit(peername, port, &peer) < 0) {
508 HIP_ERROR("Invalid argument: peername\n");
509 return -EINVAL;
510 }
511 if ((hsock = hipl_hsock_find(hsock_id)) == NULL) {
512 HIP_ERROR("ID %d is not a libhipl socket.\n", hsock_id);
513 return -1;
514 }
515
516 return hipl_connect_internal(hsock, &peer);
517}
518
519/**
520 * Wait for an incoming connection.
521 *
522 * @param hsock_id the ID of the libhipl socket for waiting connections.
523 * @return the ID of the accepted libhipl socket, negative number
524 * on error.
525 */
526int hipl_accept(const hipl_sock_id hsock_id)
527{
528 struct hipl_sock *hsock = NULL;
529
530 if ((hsock = hipl_hsock_find(hsock_id)) == NULL) {
531 HIP_ERROR("ID %d is not a libhipl socket.\n", hsock_id);
532 return -1;
533 }
534
535 return hipl_accept_internal(hsock);
536}
0537
=== added file 'libhipl/lhipl.h'
--- libhipl/lhipl.h 1970-01-01 00:00:00 +0000
+++ libhipl/lhipl.h 2012-05-12 14:17:22 +0000
@@ -0,0 +1,76 @@
1/*
2 * Copyright (c) 2010-2012 Aalto University and RWTH Aachen University.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following
11 * conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26#ifndef HIPL_LIB_HIPL_LHIPL_H
27#define HIPL_LIB_HIPL_LHIPL_H
28
29#include <sys/socket.h>
30#include <stdbool.h>
31#include <stdint.h>
32
33
34#define EWAITBEX 139000
35#define EBEXESTABLISHED 139001
36
37#define HIPL_MAX_PEERNAME 128
38
39enum hipl_lib_loglv { HIPL_LIB_LOG_DEBUG, HIPL_LIB_LOG_INFO,
40 HIPL_LIB_LOG_ERROR, HIPL_LIB_LOG_NONE };
41
42typedef uint16_t hipl_sock_id;
43
44int hipl_lib_init_all(enum hipl_lib_loglv);
45
46void hipl_lib_set_bex_feedback(bool val);
47bool hipl_lib_bex_feedback(void);
48
49int hipl_lib_set_nonblock(const hipl_sock_id hsock_id, bool on);
50int hipl_lib_get_sockfd(const hipl_sock_id hsock_id);
51
52int hipl_add_peer_info(const char *const hit, const char *const addr);
53
54int hipl_socket(const int domain, const int type, const int protocol);
55
56int hipl_close(const hipl_sock_id hsock_id);
57
58int hipl_listen(const hipl_sock_id hsock_id, const int backlog);
59
60int hipl_bind(const hipl_sock_id hsock_id, const struct sockaddr *const address,
61 const socklen_t address_len);
62
63int hipl_sendto(const hipl_sock_id hsock_id, const void *const msg,
64 const size_t len, const int flags,
65 const char *const peername, const uint16_t port);
66
67int hipl_recvfrom(const hipl_sock_id hsock_id, void *const buf,
68 const size_t len, const int flags,
69 char *const peername, uint16_t *const port);
70
71int hipl_connect(const hipl_sock_id hsock_id, const char *const peername,
72 const uint16_t port);
73
74int hipl_accept(const hipl_sock_id hsock_id);
75
76#endif /* HIPL_LIB_HIPL_LHIPL_H */
077
=== added file 'libhipl/lhipl_operations.c'
--- libhipl/lhipl_operations.c 1970-01-01 00:00:00 +0000
+++ libhipl/lhipl_operations.c 2012-05-12 14:17:22 +0000
@@ -0,0 +1,774 @@
1/*
2 * Copyright (c) 2012 Aalto University and RWTH Aachen University.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following
11 * conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26/**
27 * @file
28 * Provides the internal implementation of the libhipl socket related
29 * operations.
30 */
31
32#define _BSD_SOURCE
33
34#include <sys/socket.h>
35#include <errno.h>
36#include <fcntl.h>
37#include <stdbool.h>
38#include <string.h>
39#include <unistd.h>
40
41#include "libcore/builder.h"
42#include "libcore/hip_udp.h"
43#include "libcore/ife.h"
44#include "libcore/prefix.h"
45#include "lhipl.h"
46#include "lhipl_sock.h"
47#include "hadb.h"
48#include "hidb.h"
49#include "input.h"
50#include "netdev.h"
51#include "output.h"
52#include "lhipl_operations.h"
53
54
55/**
56 * Automatically bind to a port for a libhipl socket.
57 *
58 * @param hsock the libhipl socket for port binding.
59 * @return 0 on success, -1 on error.
60 */
61static int auto_bind(struct hipl_sock *const hsock)
62{
63 struct sockaddr_storage ss = { 0 };
64 struct sockaddr_in *addr4;
65 struct sockaddr_in6 *addr6;
66
67 if (hsock->src_port != 0) {
68 HIP_DEBUG("A bound port exists, auto_bind stops\n");
69 return 0;
70 }
71
72 if (hsock->sock_family == AF_INET) {
73 ss.ss_family = AF_INET;
74 addr4 = (struct sockaddr_in *) &ss;
75 addr4->sin_port = 0;
76 return hipl_bind_internal(hsock, (struct sockaddr *) addr4,
77 sizeof(ss));
78 } else {
79 ss.ss_family = AF_INET6;
80 addr6 = (struct sockaddr_in6 *) &ss;
81 addr6->sin6_port = 0;
82 return hipl_bind_internal(hsock, (struct sockaddr *) addr6,
83 sizeof(ss));
84 }
85}
86
87static uint16_t get_port_from_saddr(const struct sockaddr *const addr)
88{
89 const struct sockaddr_in *addr4 = (const struct sockaddr_in *) addr;
90 const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *) addr;
91
92 if (addr->sa_family == AF_INET) {
93 return addr4->sin_port;
94 } else {
95 return addr6->sin6_port;
96 }
97}
98
99/**
100 * Set global variables in order to inter-operate with libhipdeamon.
101 *
102 * @param sock_fd the socket file descriptor for sending message.
103 * @param local_port the local port for sending message.
104 * @param remote_port the remote port for receiving message.
105 */
106static void set_hip_connection_parameters(const int sock_fd,
107 const int local_port,
108 const int remote_port)
109{
110 HIP_DEBUG("Set connection params: fd = %d, lport = %d, rport= %d\n",
111 sock_fd, local_port, remote_port);
112 hip_nat_sock_output_udp = sock_fd;
113 hip_raw_sock_output_v4 = sock_fd;
114 hip_raw_sock_output_v6 = sock_fd;
115 hip_set_local_nat_udp_port(local_port);
116 hip_set_peer_nat_udp_port(remote_port);
117}
118
119/**
120 * Check whether a received packet is a HIP control packet.
121 *
122 * @param buf buffer containing the received packet.
123 * @param len size of the @c buf.
124 * @param hsock the libhipl socket receiving the packet.
125 * @return 1 on a control message, 0 on a user message,
126 * negative number on error.
127 */
128static int hipl_is_control_msg(char *const buf, unsigned int len,
129 struct hipl_sock *const hsock)
130{
131 char udp_pad[HIP_UDP_ZERO_BYTES_LEN] = { 0 };
132 struct hip_common *msg;
133 struct sockaddr_storage src = { 0 };
134 socklen_t srclen = sizeof(src);
135
136 if (len < sizeof(struct hip_common)) {
137 return 0;
138 }
139
140 if (!memcmp(udp_pad, buf, HIP_UDP_ZERO_BYTES_LEN)) {
141 HIP_DEBUG("Message is padded\n");
142 msg = (struct hip_common *) (buf + HIP_UDP_ZERO_BYTES_LEN);
143 len -= HIP_UDP_ZERO_BYTES_LEN;
144 } else {
145 msg = (struct hip_common *) buf;
146 }
147
148 if (getsockname(hsock->sock_fd, (struct sockaddr *) &src, &srclen) < 0) {
149 HIP_PERROR("getsockname()");
150 return true;
151 }
152
153 return !hip_verify_network_header(msg, (struct sockaddr *) &src,
154 (struct sockaddr *) &hsock->peer_locator,
155 len);
156}
157
158static void build_packet_context(struct hip_packet_context *const ctx,
159 struct sockaddr *ctx_dst, struct sockaddr *ctx_src)
160{
161 struct sockaddr_in *s4;
162 struct sockaddr_in6 *s6;
163
164 if (ctx_dst->sa_family == AF_INET) {
165 s4 = (struct sockaddr_in *) ctx_dst;
166 IPV4_TO_IPV6_MAP(&s4->sin_addr, &ctx->dst_addr);
167 ctx->msg_ports.dst_port = ntohs(s4->sin_port);
168 } else if (ctx_dst->sa_family == AF_INET6) {
169 s6 = (struct sockaddr_in6 *) ctx_dst;
170 ctx->dst_addr = s6->sin6_addr;
171 ctx->msg_ports.dst_port = ntohs(s6->sin6_port);
172 }
173
174 if (ctx_src->sa_family == AF_INET) {
175 s4 = (struct sockaddr_in *) ctx_src;
176 IPV4_TO_IPV6_MAP(&s4->sin_addr, &ctx->src_addr);
177 ctx->msg_ports.src_port = ntohs(s4->sin_port);
178 } else if (ctx_src->sa_family == AF_INET6) {
179 s6 = (struct sockaddr_in6 *) ctx_src;
180 ctx->src_addr = s6->sin6_addr;
181 ctx->msg_ports.src_port = ntohs(s6->sin6_port);
182 }
183}
184
185/**
186 * Receive and pre-process an incoming message.
187 *
188 * This function discards UDP packet from an unknown peer, identifies
189 * user/control packet, eliminates zero padding in control packets and
190 * builds the packet context for handling control packets.
191 *
192 * @param hsock the libhipl socket for receiving message.
193 * @param msg buffer to hold the incoming message.
194 * @param flags the flags of socket @c recvmsg().
195 * @param ctx the HIP packet context to be built.
196 * @param is_user_msg true if the message is a user message, false otherwise.
197 * @return negative number on errors, 0 on end-of-file, number of
198 * bytes received otherwise.
199 */
200static int recv_msg_wrapper(struct hipl_sock *const hsock,
201 struct msghdr *const msg, const int flags,
202 struct hip_packet_context *const ctx,
203 bool *is_user_msg)
204{
205 int ret;
206 struct sockaddr_storage our_locator = { 0 };
207 socklen_t sslen = sizeof(our_locator);
208
209 *is_user_msg = true;
210 if ((ret = recvmsg(hsock->sock_fd, msg, flags)) < 0) {
211 HIP_PERROR("recvmsg()");
212 return -1;
213 }
214
215 //in UDP mode, we don't know the peer locator until we receive the
216 //first message from the peer. Once we get the peer locator, save it
217 //to 'hsock'. We should also fill the packet context for UDP.
218 if (hsock->sock_proto == IPPROTO_UDP) {
219 if (msg->msg_name != NULL && hsock->peer_locator.ss_family == 0) {
220 memcpy(&hsock->peer_locator, msg->msg_name,
221 sizeof(hsock->peer_locator));
222 }
223 if (ctx != NULL) {
224 if (getsockname(hsock->sock_fd, (struct sockaddr *) &our_locator,
225 &sslen) < 0) {
226 HIP_PERROR("getsockname()");
227 return -1;
228 }
229 build_packet_context(ctx, (struct sockaddr *) &our_locator,
230 (struct sockaddr *) &hsock->peer_locator);
231 }
232 }
233
234 char *buf = msg->msg_iov->iov_base;
235 if (hipl_is_control_msg(buf, ret, hsock)) {
236 memmove(buf, buf + HIP_UDP_ZERO_BYTES_LEN,
237 HIP_MAX_PACKET - HIP_UDP_ZERO_BYTES_LEN);
238 ret -= HIP_UDP_ZERO_BYTES_LEN;
239 *is_user_msg = false;
240 }
241
242 return ret;
243}
244
245static int nonblock_result_check(const int ret, const int err)
246{
247 if (ret < 0 && err != EWOULDBLOCK && err != EAGAIN) {
248 HIP_ERROR("BEX failed, errno = %d, errstr = %s\n", err,
249 strerror(err));
250 return -1;
251 }
252 if (ret < 0 && (err == EWOULDBLOCK || err == EAGAIN)) {
253 HIP_DEBUG("BEX returns EWOULDBLOCK or EAGAIN\n");
254 return -EWAITBEX;
255 }
256
257 return 0;
258}
259
260/**
261 * Wait for a HIP I1 packet and continue performing base exchange.
262 *
263 * @param hsock the libhipl socket waiting for the I1 packet.
264 * @param ctx the HIP packet context for HIP packet processing.
265 * @return 0 on success, -1 on error, -EWAITBEX when BEX is pending
266 */
267static int nonblock_await_bex(struct hipl_sock *const hsock,
268 struct hip_packet_context *const ctx)
269{
270 struct msghdr params = { 0 };
271 struct iovec iov;
272 struct sockaddr_storage ss;
273 bool is_user_msg;
274 int ret = 0;
275 int flag;
276
277 params.msg_name = &ss;
278 params.msg_namelen = sizeof(ss);
279 params.msg_iovlen = 1;
280 iov.iov_base = ctx->input_msg;
281 iov.iov_len = HIP_MAX_PACKET;
282 params.msg_iov = &iov;
283
284 flag = fcntl(hsock->sock_fd, F_GETFL, 0);
285 fcntl(hsock->sock_fd, F_SETFL, flag | O_NONBLOCK);
286
287 set_hip_connection_parameters(hsock->sock_fd, hsock->src_port, 0);
288 ret = recv_msg_wrapper(hsock, &params, 0, ctx, &is_user_msg);
289 if ((ret = nonblock_result_check(ret, errno)) != 0) {
290 HIP_DEBUG("returns %d\n", ret);
291 goto out;
292 }
293
294 if (is_user_msg || hip_receive_control_packet(ctx) < 0) {
295 HIP_ERROR("hip_receive_control_packet() failed\n");
296 ret = -1;
297 }
298
299out:
300 fcntl(hsock->sock_fd, F_SETFL, flag);
301 return ret;
302}
303
304/**
305 * Trigger BEX in a non-blocking way.
306 *
307 * @param hsock the libhipl socket to trigger BEX.
308 * @param src_hit the source HIT for base exchange.
309 * @param dst_hit the destination HIT for base exchange.
310 * @param dst_port the destination port.
311 * @return -1 on error, -EWAITBEX when the BEX is pending, 0 if sending
312 * BEX trigger message successfully.
313 */
314static int nonblock_trigger_bex(struct hipl_sock *hsock,
315 const hip_hit_t *src_hit,
316 const hip_hit_t *dst_hit,
317 const int dst_port)
318{
319 struct in6_addr dst_addr;
320 int err = 0, flag;
321
322 flag = fcntl(hsock->sock_fd, F_GETFL, 0);
323 fcntl(hsock->sock_fd, F_SETFL, flag | O_NONBLOCK);
324
325 err = hip_map_id_to_addr(dst_hit, NULL, &dst_addr);
326 HIP_IFEL(err < 0, -1, "failed to match hit to IP\n");
327 HIP_IFEL(ipv6_addr_any(&dst_addr), -1, "Couldn't map HIT to IP\n");
328
329 set_hip_connection_parameters(hsock->sock_fd, hsock->src_port, dst_port);
330 err = netdev_trigger_bex(src_hit, dst_hit, NULL, NULL, NULL, &dst_addr);
331 HIP_DEBUG("netdev_trigger_bex returns %d, errno = %d\n", err, errno);
332 err = nonblock_result_check(err, errno);
333 if (err == 0) {
334 hsock->ha = hip_hadb_find_byhits(src_hit, dst_hit);
335 }
336
337out_err:
338 fcntl(hsock->sock_fd, F_SETFL, flag);
339 return err;
340}
341
342/**
343 * Handle BEX for a libhipl socket.
344 *
345 * If param @c peer_hit is given, current libhipl socket will be the initiator
346 * and trigger the BEX. otherwise, it acts as a responder and waits for an I1
347 * message.
348 *
349 * @param hsock the libhipl socket to handle BEX.
350 * @param peer_hit the peer's hit and port.
351 * @return -1 on error, -EWAITBEX when the BEX is pending, and
352 * -EBEXESTABLISHED when BEX finishes.
353 */
354static int handle_bex(struct hipl_sock *hsock, struct sockaddr_in6 *peer_hit)
355{
356 int err = 0;
357 struct hip_packet_context ctx = { 0 };
358
359 // We are the initiator, send I1
360 if (hipl_hsock_ha_state(hsock) == HIP_STATE_UNASSOCIATED && peer_hit) {
361 hsock->peer_hit = peer_hit->sin6_addr;
362
363 err = nonblock_trigger_bex(hsock, &hsock->src_hit,
364 &peer_hit->sin6_addr,
365 ntohs(peer_hit->sin6_port));
366 // send I1 successfully, return -EWAITBEX.
367 if (err == 0) {
368 err = -EWAITBEX;
369 }
370 return err;
371 }
372
373 // waiting for and handle control messages
374 hsock->ha = hip_hadb_find_byhits(&hsock->src_hit, &hsock->peer_hit);
375 if (hipl_hsock_ha_state(hsock) != HIP_STATE_ESTABLISHED) {
376 ctx.input_msg = hip_msg_alloc();
377 ctx.output_msg = hip_msg_alloc();
378 HIP_IFEL(!ctx.input_msg || !ctx.output_msg, -ENOMEM,
379 "hip_msg_alloc() failed\n");
380
381 err = nonblock_await_bex(hsock, &ctx);
382 HIP_DEBUG("nonb_await_bex returns %d\n", err);
383 if (err < 0) { /* -1 or -EWAITBEX */
384 goto out_err;
385 }
386 hsock->peer_hit = ctx.input_msg->hit_sender;
387 hsock->ha = hip_hadb_find_byhits(&hsock->src_hit, &hsock->peer_hit);
388 }
389
390 if (hipl_hsock_ha_state(hsock) == HIP_STATE_ESTABLISHED) {
391 err = -EBEXESTABLISHED;
392 } else {
393 err = -EWAITBEX;
394 }
395
396out_err:
397 free(ctx.input_msg);
398 free(ctx.output_msg);
399 return err;
400}
401
402static int validate_udp_peer_addr(const struct hipl_sock *const hsock,
403 const struct sockaddr_storage *const saddr)
404{
405 const struct in6_addr *paddr;
406 struct in6_addr peer_addr;
407
408 if (hsock->sock_proto != IPPROTO_UDP) {
409 return 0;
410 }
411
412 switch (saddr->ss_family) {
413 case AF_INET:
414 IPV4_TO_IPV6_MAP(&((const struct sockaddr_in *) saddr)->sin_addr,
415 &peer_addr);
416 paddr = &peer_addr;
417 break;
418
419 case AF_INET6:
420 paddr = &((const struct sockaddr_in6 *) saddr)->sin6_addr;
421 break;
422
423 default:
424 HIP_DEBUG("Unsupported family: %d\n", saddr->ss_family);
425 return -1;
426 }
427
428 if (ipv6_addr_cmp(&hsock->ha->peer_addr, paddr)) {
429 HIP_DEBUG("Packet not from associated address. Dropping.\n");
430 HIP_DEBUG_IN6ADDR("expected", &hsock->ha->peer_addr);
431 HIP_DEBUG_IN6ADDR("got", paddr);
432 return -1;
433 }
434
435 return 0;
436}
437
438/**
439 * Build a @c sockaddr_storage from a given IPv6 address and a port number.
440 *
441 * If the address is V4MAPPED, the storage family will be @c AF_INET,
442 * otherwise the storage family will be @c AF_INET6.
443 *
444 * @param addr a V6 address or a V4MAPPED address.
445 * @param port the port number.
446 * @param ss the @c sockaddr_storage to be filled.
447 */
448void hipl_build_addrstorage(const struct in6_addr *const addr,
449 const uint16_t port,
450 struct sockaddr_storage *const ss)
451{
452 HIP_ASSERT(addr && ss);
453 memset(ss, 0, sizeof(*ss));
454
455 if (IN6_IS_ADDR_V4MAPPED(addr)) {
456 struct sockaddr_in *const in = (struct sockaddr_in *) ss;
457 in->sin_family = AF_INET;
458 IPV6_TO_IPV4_MAP(addr, &in->sin_addr);
459 in->sin_port = htons(port);
460 } else {
461 struct sockaddr_in6 *const in6 = (struct sockaddr_in6 *) ss;
462 in6->sin6_family = AF_INET6;
463 ipv6_addr_copy(&in6->sin6_addr, addr);
464 in6->sin6_port = htons(port);
465 }
466}
467
468/**
469 * Create a libhipl socket.
470 *
471 * @param family the communications domain of the libhipl socket
472 * @param type the socket type of the libhipl socket
473 * @param protocol the protocol of the libhipl socket
474 * @return the ID of the new libhipl socket on success, negative
475 * number otherwise
476 */
477int hipl_socket_internal(const int family, const int type, const int protocol)
478{
479 int sock;
480 int on = 1, off = 0;
481 struct hipl_sock *hsock = NULL;
482
483 //TODO support IPV6
484 if (family == AF_INET6) {
485 HIP_ERROR("No IPv6 support yet.\n");
486 return -ENOTSUP;
487 }
488
489 sock = socket(family, type, protocol);
490 if (family == AF_INET) {
491 setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
492 setsockopt(sock, IPPROTO_IP, IP_PKTINFO, &on, sizeof(on));
493 setsockopt(sock, IPPROTO_IP, IP_RECVERR, &off, sizeof(off));
494 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
495 } else {
496 setsockopt(sock, IPPROTO_IPV6, IPV6_RECVERR, &off, sizeof(off));
497 setsockopt(sock, IPPROTO_IPV6, IPV6_2292PKTINFO, &on, sizeof(on));
498 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
499 }
500 if ((hsock = hipl_hsock_new(family, type, protocol)) == NULL) {
501 HIP_ERROR("Failed to create libhipl socket.\n");
502 close(sock);
503 return -1;
504 }
505 hsock->sock_fd = sock;
506 hip_get_default_hit(&hsock->src_hit);
507
508 return hsock->sid;
509}
510
511/**
512 * Bind a libhipl socket to a local address.
513 *
514 * @param hsock the libhipl socket for address binding.
515 * @param address the IP address to be bound.
516 * @param address_len the length of the @c address.
517 * @return 0 on success, -1 on error.
518 */
519int hipl_bind_internal(struct hipl_sock *const hsock,
520 const struct sockaddr *const address,
521 const socklen_t address_len)
522{
523 struct sockaddr_storage laddr = { 0 };
524 socklen_t laddr_len = sizeof(laddr);
525 uint16_t request_port;
526
527 request_port = get_port_from_saddr(address);
528 if (bind(hsock->sock_fd, address, address_len) < 0) {
529 HIP_PERROR("bind error:");
530 return -1;
531 }
532
533 /* Ask OS for the assigned port number */
534 if (request_port == 0) {
535 if (0 > getsockname(hsock->sock_fd, (struct sockaddr *) &laddr,
536 &laddr_len)) {
537 HIP_PERROR("getsockname() error: ");
538 return -1;
539 }
540 request_port = get_port_from_saddr((struct sockaddr *) &laddr);
541 }
542
543 hsock->src_port = ntohs(request_port);
544 HIP_DEBUG("bind to port %d\n", hsock->src_port);
545
546 return 0;
547}
548
549/**
550 * Initiate a connection to a peer.
551 *
552 * @param hsock the libhipl socket to initiate the connection.
553 * @param peer the peer's HIT and port number.
554 * @return 0 on success, -1 on error.
555 */
556int hipl_connect_internal(struct hipl_sock *const hsock,
557 const struct sockaddr_in6 *const peer)
558{
559 struct in6_addr dst_addr = { { { 0 } } };
560 struct sockaddr_storage ss;
561
562 if (ipv6_addr_any(&peer->sin6_addr)) {
563 HIP_ERROR("Invalid argument: dst_hit.\n");
564 return -1;
565 }
566
567 if (hip_map_id_to_addr(&peer->sin6_addr, NULL, &dst_addr) < 0) {
568 return -1;
569 }
570 if (ipv6_addr_any(&dst_addr)) {
571 HIP_ERROR("Couldn't map HIT to IP\n");
572 return -1;
573 }
574
575 HIP_DEBUG_IN6ADDR("Dest locator is: ", &dst_addr);
576 HIP_DEBUG("Dest locator is V4MAPPED: %d\n", IN6_IS_ADDR_V4MAPPED(&dst_addr));
577 hipl_build_addrstorage(&dst_addr, ntohs(peer->sin6_port), &ss);
578
579 if (connect(hsock->sock_fd, (struct sockaddr *) &ss, sizeof(ss)) < 0) {
580 HIP_ERROR("connect(): %s\n", strerror(errno));
581 return -1;
582 }
583
584 /* Save related information into hsock */
585 hsock->peer_hit = peer->sin6_addr;
586 hsock->peer_locator = ss;
587 if (hsock->src_port == 0) {
588 socklen_t taddr_len = sizeof(ss);
589
590 if (getsockname(hsock->sock_fd, (struct sockaddr *) &ss,
591 &taddr_len) < 0) {
592 HIP_PERROR("getsockname() error: ");
593 return -1;
594 }
595 hsock->src_port = ntohs(get_port_from_saddr((struct sockaddr *) &ss));
596 }
597
598 return 0;
599}
600
601/**
602 * Wait for an incoming connection on a libhipl socket.
603 *
604 * @param hsock the libhipl socket waiting for the connection.
605 * @return the ID of the accepted libhipl socket on success, -1 on error.
606 */
607int hipl_accept_internal(struct hipl_sock *const hsock)
608{
609 int new_fd;
610 struct hipl_sock *hsock_new = NULL;
611 struct sockaddr_storage ss = { 0 };
612 socklen_t ss_len = sizeof(ss);
613
614 new_fd = accept(hsock->sock_fd, (struct sockaddr *) &ss, &ss_len);
615 if (new_fd < 0) {
616 HIP_PERROR("accept(): ");
617 return -1;
618 }
619
620 hsock_new = hipl_hsock_new(hsock->sock_family, hsock->sock_type,
621 hsock->sock_proto);
622 hsock_new->src_port = hsock->src_port;
623 hsock_new->src_hit = hsock->src_hit;
624 hsock_new->sock_fd = new_fd;
625 hsock_new->peer_locator = ss;
626
627 return hsock_new->sid;
628}
629
630/**
631 * Receive data from a remote peer.
632 *
633 * Wait for base exchange if no host association exists.
634 * @note Data is currently sent unencrypted.
635 * @note Scatter read is not supported yet.
636 *
637 * @param hsock the libhipl socket for receiving data.
638 * @param msg the buffer to hold data and peer information.
639 * @param flags the flags of the socket function @c recvmsg().
640 * @return number of bytes received on success,
641 * 0 on end-of-file,
642 * -EWAITBEX when the BEX is pending,
643 * -EBEXESTABLISHED when BEX finishes,
644 * other negative numbers on error.
645 */
646ssize_t hipl_recvmsg_internal(struct hipl_sock *const hsock,
647 struct msghdr *const msg,
648 const int flags)
649{
650 struct hip_packet_context ctx = { 0 };
651 struct msghdr params = { 0 };
652 struct iovec iov;
653 struct sockaddr_storage recv_remote_addr;
654 int err = 0;
655 bool is_user_msg;
656
657 if (msg->msg_iovlen != 1) {
658 HIP_ERROR("Invalid iovlen: %d, scatter read is not supported yet\n",
659 msg->msg_iovlen);
660 return -ENOTSUP;
661 }
662
663 /* Bind to an ephemeral port if the src port hasn't been bound yet */
664 if (auto_bind(hsock)) {
665 HIP_ERROR("Fail to bind the hip socket.\n");
666 return -1;
667 }
668
669 /* Handle BEX if HA hasn't established */
670 if (hipl_hsock_ha_state(hsock) != HIP_STATE_ESTABLISHED
671 && hipl_hsock_ha_state(hsock) != HIP_STATE_CLOSING) {
672 return handle_bex(hsock, NULL);
673 }
674
675 ctx.input_msg = hip_msg_alloc();
676 ctx.output_msg = hip_msg_alloc();
677 HIP_IFEL(!ctx.input_msg || !ctx.output_msg, -ENOMEM,
678 "hip_msg_alloc() failed\n");
679 params.msg_name = &recv_remote_addr;
680 params.msg_namelen = sizeof(recv_remote_addr);
681 params.msg_iovlen = 1;
682 iov.iov_base = ctx.input_msg;
683 iov.iov_len = HIP_MAX_PACKET;
684 params.msg_iov = &iov;
685
686 err = recv_msg_wrapper(hsock, &params, flags, &ctx, &is_user_msg);
687 HIP_IFEL(err < 0, -1, "recv_msg_wrapper() failed\n");
688
689 if (validate_udp_peer_addr(hsock, &recv_remote_addr) < 0) {
690 HIP_IFEL(true, -EAGAIN,
691 "Received a packet with invalid peer address, dropping.\n");
692 }
693 if (!is_user_msg) {
694 HIP_DEBUG("receive a hip control message.\n");
695 hip_receive_control_packet(&ctx);
696 if (hipl_hsock_ha_state(hsock) != HIP_STATE_ESTABLISHED) {
697 HIP_DEBUG("HA state change to %d, return 0.\n", hsock->ha->state);
698 return 0;
699 }
700 } else {
701 HIP_DEBUG("receive a user message.\n");
702 // TODO, if buffer size is too small, we should save it to a internal buffer
703 // and only return content with length specified by user's buffer (TCP).
704 // return error for UDP in this case.
705 struct iovec *iovp;
706 struct sockaddr_in6 *hitp;
707 hitp = msg->msg_name;
708 iovp = msg->msg_iov;
709 HIP_IFEL(iovp->iov_len < (unsigned int) err, -1,
710 "buffer size too small\n");
711 HIP_DEBUG_HIT("ha->hit_peer:", &hsock->ha->hit_peer);
712 hitp->sin6_family = AF_INET6;
713 hitp->sin6_port = get_port_from_saddr((const struct sockaddr *) &hsock->peer_locator);
714 HIP_DEBUG("hitp port: %d\n", ntohs(hitp->sin6_port));
715 hitp->sin6_addr = hsock->ha->hit_peer;
716 memcpy(iovp->iov_base, ctx.input_msg, err);
717 }
718
719out_err:
720 free(ctx.input_msg);
721 free(ctx.output_msg);
722 return err;
723}
724
725/**
726 * Send data to a peer.
727 *
728 * Trigger base exchange if no host association exists.
729 * @note Data is currently sent unencrypted.
730 * @note Gather write is not supported yet.
731 *
732 * @param hsock the libhipl socket for sending data.
733 * @param msg containing data, and peer information.
734 * @param flags the flags of the socket function @c sendmsg().
735 * @return number of bytes sent on success,
736 * -EWAITBEX if the BEX is pending,
737 * -EBEXESTABLISHED if the BEX finishes,
738 * other negative number on error.
739 */
740ssize_t hipl_sendmsg_internal(struct hipl_sock *const hsock,
741 struct msghdr *const msg,
742 const int flags)
743{
744 int fd = hsock->sock_fd;
745
746 /* Gather write is not supported yet */
747 if (msg->msg_iovlen > 1) {
748 HIP_ERROR("Invalid iovlen: %d, gather write is not supported\n",
749 msg->msg_iovlen);
750 return -ENOTSUP;
751 }
752
753 /* Bind to an ephemeral port if the src port hasn't been bound yet */
754 if (auto_bind(hsock)) {
755 HIP_ERROR("Fail to bind the hip socket.\n");
756 return -1;
757 }
758
759 /* Start BEX if HA hasn't established */
760 if (hipl_hsock_ha_state(hsock) != HIP_STATE_ESTABLISHED) {
761 return handle_bex(hsock, msg->msg_name);
762 }
763
764 /* Determine peer's locator and send out the message */
765 HIP_DEBUG("BEX ok, start to send user data\n");
766 if (hsock->peer_locator.ss_family == 0) {
767 hipl_build_addrstorage(&hsock->ha->peer_addr,
768 hsock->ha->peer_udp_port,
769 &hsock->peer_locator);
770 }
771 msg->msg_namelen = sizeof(hsock->peer_locator);
772 memcpy(msg->msg_name, &hsock->peer_locator, msg->msg_namelen);
773 return sendmsg(fd, msg, flags);
774}
0775
=== added file 'libhipl/lhipl_operations.h'
--- libhipl/lhipl_operations.h 1970-01-01 00:00:00 +0000
+++ libhipl/lhipl_operations.h 2012-05-12 14:17:22 +0000
@@ -0,0 +1,55 @@
1/*
2 * Copyright (c) 2012 Aalto University and RWTH Aachen University.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following
11 * conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26#ifndef HIPL_LIB_HIPL_LHIPL_OPERATIONS_H
27#define HIPL_LIB_HIPL_LHIPL_OPERATIONS_H
28
29#include "lhipl_sock.h"
30
31
32int hipl_socket_internal(const int family, const int type, const int protocol);
33
34int hipl_bind_internal(struct hipl_sock *const hsock,
35 const struct sockaddr *const address,
36 const socklen_t address_len);
37
38int hipl_connect_internal(struct hipl_sock *const hsock,
39 const struct sockaddr_in6 *const addr);
40
41int hipl_accept_internal(struct hipl_sock *const hsock);
42
43ssize_t hipl_recvmsg_internal(struct hipl_sock *const hsock,
44 struct msghdr *const msg,
45 const int flags);
46
47ssize_t hipl_sendmsg_internal(struct hipl_sock *const hsock,
48 struct msghdr *const msg,
49 const int flags);
50
51void hipl_build_addrstorage(const struct in6_addr *const addr,
52 const uint16_t port,
53 struct sockaddr_storage *const ss);
54
55#endif /* HIPL_LIB_HIPL_LHIPL_OPERATIONS_H */
056
=== added file 'libhipl/lhipl_sock.c'
--- libhipl/lhipl_sock.c 1970-01-01 00:00:00 +0000
+++ libhipl/lhipl_sock.c 2012-05-12 14:17:22 +0000
@@ -0,0 +1,150 @@
1/*
2 * Copyright (c) 2012 Aalto University and RWTH Aachen University.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following
11 * conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26/**
27 * @file
28 * Provides functions for maintaining libhipl sockets.
29 */
30
31#define _BSD_SOURCE
32
33#include <sys/socket.h>
34#include <string.h>
35#include <unistd.h>
36
37#include "libcore/debug.h"
38#include "libcore/hashtable.h"
39#include "lhipl_sock.h"
40
41
42/* A hashtable to record all opening libhipl sockets */
43static HIP_HASHTABLE *hsocks = NULL;
44
45static unsigned long hipl_sk_hash(const struct hipl_sock *hsock)
46{
47 return hsock->sid;
48}
49
50STATIC_IMPLEMENT_LHASH_HASH_FN(hipl_sk, struct hipl_sock)
51
52static int hipl_sk_cmp(const struct hipl_sock *hsock1,
53 const struct hipl_sock *hsock2)
54{
55 return memcmp(&hsock1->sid, &hsock2->sid, sizeof(hsock1->sid));
56}
57
58STATIC_IMPLEMENT_LHASH_COMP_FN(hipl_sk, struct hipl_sock)
59
60static uint32_t hsock_generate_id(void)
61{
62 static uint32_t id_generator = HIPL_LIB_HSOCK_ID_MIN;
63
64 if (id_generator == HIPL_LIB_HSOCK_ID_MAX) {
65 id_generator = HIPL_LIB_HSOCK_ID_MIN;
66 } else {
67 id_generator += 1;
68 }
69
70 return id_generator;
71}
72
73/**
74 * Initialize the libhipl socket hashtable.
75 */
76void hipl_hsock_init(void)
77{
78 hsocks = hip_ht_init(LHASH_HASH_FN(hipl_sk), LHASH_COMP_FN(hipl_sk));
79}
80
81/**
82 * Create a new libhipl socket and save it to the libhipl socket hashtable.
83 *
84 * @param family the address family of the libhipl socket (INET or INET6).
85 * @param type the type of the protocol.
86 * @param protocol the protocol of the libhipl socket (TCP or UDP).
87 * @return pointer to the created libhipl socket on success, NULL on
88 * error.
89 */
90struct hipl_sock *hipl_hsock_new(const int family, const int type,
91 const int protocol)
92{
93 struct hipl_sock *hsock = NULL;
94
95 hsock = calloc(sizeof(struct hipl_sock), sizeof(uint8_t));
96 if (hsock == NULL) {
97 HIP_ERROR("calloc() failed.\n");
98 return NULL;
99 }
100
101 hsock->sid = hsock_generate_id();
102 hsock->sock_family = family;
103 hsock->sock_type = type;
104 hsock->sock_proto = protocol;
105 hip_ht_add(hsocks, hsock);
106 return hsock;
107}
108
109/**
110 * Get a libhipl socket by its ID.
111 *
112 * @param hsock_id the ID of the libhipl socket.
113 * @return pointer to the libhipl socket on success, or NULL if the
114 * given ID doesn't match any record.
115 */
116struct hipl_sock *hipl_hsock_find(const uint16_t hsock_id)
117{
118 struct hipl_sock hsock;
119
120 hsock.sid = hsock_id;
121 return hip_ht_find(hsocks, &hsock);
122}
123
124/**
125 * Delete a libhipl socket and free the memory it occupies.
126 *
127 * @param hsock pointer to the libhipl socket to be deleted.
128 */
129void hipl_hsock_delete_and_free(struct hipl_sock *const hsock)
130{
131 struct hipl_sock *deleted_item;
132
133 deleted_item = hip_ht_delete(hsocks, hsock);
134 free(deleted_item);
135}
136
137/**
138 * Get the HIP association state of a given libhipl socket.
139 *
140 * @param hsock the libhipl socket.
141 * @return the HIP association state of the libhipl socket.
142 */
143enum hip_state hipl_hsock_ha_state(const struct hipl_sock *const hsock)
144{
145 if (!hsock->ha) {
146 return HIP_STATE_UNASSOCIATED;
147 } else {
148 return hsock->ha->state;
149 }
150}
0151
=== added file 'libhipl/lhipl_sock.h'
--- libhipl/lhipl_sock.h 1970-01-01 00:00:00 +0000
+++ libhipl/lhipl_sock.h 2012-05-12 14:17:22 +0000
@@ -0,0 +1,67 @@
1/*
2 * Copyright (c) 2012 Aalto University and RWTH Aachen University.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following
11 * conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26#ifndef HIPL_LIB_HIPL_LHIPL_SOCK_H
27#define HIPL_LIB_HIPL_LHIPL_SOCK_H
28
29#include <limits.h>
30
31#include "libcore/protodefs.h"
32#include "libcore/state.h"
33
34
35#define HIPL_LIB_HSOCK_ID_MIN 1
36#define HIPL_LIB_HSOCK_ID_MAX USHRT_MAX
37#define HIPL_LIB_HSOCK_MAX 1024
38
39/* The internal information about each libhipl socket.
40 *
41 * It is generated when a new libhipl socket is created.
42 */
43struct hipl_sock {
44 uint16_t sid; /* libhipl socket ID */
45 struct hip_hadb_state *ha;
46 hip_hit_t peer_hit;
47 struct sockaddr_storage peer_locator;
48 hip_hit_t src_hit; /* our HIT */
49 int src_port; /* our port number */
50 int sock_fd; /* underlying socket */
51 int sock_family;
52 int sock_type;
53 int sock_proto;
54};
55
56void hipl_hsock_init(void);
57
58enum hip_state hipl_hsock_ha_state(const struct hipl_sock *const hsock);
59
60struct hipl_sock *hipl_hsock_new(const int family, const int type,
61 const int protocol);
62
63struct hipl_sock *hipl_hsock_find(const uint16_t hsock_id);
64
65void hipl_hsock_delete_and_free(struct hipl_sock *const hsock);
66
67#endif /* HIPL_LIB_HIPL_LHIPL_SOCK_H */
068
=== renamed file 'hipd/lsidb.c' => 'libhipl/lsidb.c'
=== renamed file 'hipd/lsidb.h' => 'libhipl/lsidb.h'
--- hipd/lsidb.h 2012-05-12 06:54:33 +0000
+++ libhipl/lsidb.h 2012-05-12 14:17:22 +0000
@@ -30,8 +30,8 @@
30 * host and which LSIs are still available.30 * host and which LSIs are still available.
31 */31 */
3232
33#ifndef HIPL_HIPD_LSIDB_H33#ifndef HIPL_LIBHIPL_LSIDB_H
34#define HIPL_HIPD_LSIDB_H34#define HIPL_LIBHIPL_LSIDB_H
3535
36#include <stdbool.h>36#include <stdbool.h>
3737
@@ -40,4 +40,4 @@
40bool lsidb_allocate_lsi(hip_lsi_t *const lsi);40bool lsidb_allocate_lsi(hip_lsi_t *const lsi);
41bool lsidb_free_lsi(const hip_lsi_t const lsi);41bool lsidb_free_lsi(const hip_lsi_t const lsi);
4242
43#endif /* HIPL_HIPD_LSIDB_H */43#endif /* HIPL_LIBHIPL_LSIDB_H */
4444
=== renamed file 'hipd/maintenance.c' => 'libhipl/maintenance.c'
=== renamed file 'hipd/maintenance.h' => 'libhipl/maintenance.h'
--- hipd/maintenance.h 2012-03-20 21:45:03 +0000
+++ libhipl/maintenance.h 2012-05-12 14:17:22 +0000
@@ -23,8 +23,8 @@
23 * OTHER DEALINGS IN THE SOFTWARE.23 * OTHER DEALINGS IN THE SOFTWARE.
24 */24 */
2525
26#ifndef HIPL_HIPD_MAINTENANCE_H26#ifndef HIPL_LIBHIPL_MAINTENANCE_H
27#define HIPL_HIPD_MAINTENANCE_H27#define HIPL_LIBHIPL_MAINTENANCE_H
2828
29#include <stdint.h>29#include <stdint.h>
30#include <netinet/in.h>30#include <netinet/in.h>
@@ -43,4 +43,4 @@
43 struct in6_addr *hit_r);43 struct in6_addr *hit_r);
44int hipfw_set_esp_relay(int action);44int hipfw_set_esp_relay(int action);
4545
46#endif /* HIPL_HIPD_MAINTENANCE_H */46#endif /* HIPL_LIBHIPL_MAINTENANCE_H */
4747
=== renamed file 'hipd/nat.c' => 'libhipl/nat.c'
=== renamed file 'hipd/nat.h' => 'libhipl/nat.h'
--- hipd/nat.h 2012-05-12 06:54:33 +0000
+++ libhipl/nat.h 2012-05-12 14:17:22 +0000
@@ -35,8 +35,8 @@
35 * @note All Doxygen comments have been added in version 1.1.35 * @note All Doxygen comments have been added in version 1.1.
36 */36 */
3737
38#ifndef HIPL_HIPD_NAT_H38#ifndef HIPL_LIBHIPL_NAT_H
39#define HIPL_HIPD_NAT_H39#define HIPL_LIBHIPL_NAT_H
4040
41#include "libcore/protodefs.h"41#include "libcore/protodefs.h"
4242
@@ -52,4 +52,4 @@
52int hip_nat_refresh_port(void);52int hip_nat_refresh_port(void);
53int hip_user_nat_mode(int nat_mode);53int hip_user_nat_mode(int nat_mode);
5454
55#endif /* HIPL_HIPD_NAT_H */55#endif /* HIPL_LIBHIPL_NAT_H */
5656
=== renamed file 'hipd/netdev.c' => 'libhipl/netdev.c'
=== renamed file 'hipd/netdev.h' => 'libhipl/netdev.h'
--- hipd/netdev.h 2012-05-12 06:54:33 +0000
+++ libhipl/netdev.h 2012-05-12 14:17:22 +0000
@@ -29,8 +29,8 @@
29 * events over netlink from the kernel.29 * events over netlink from the kernel.
30 */30 */
3131
32#ifndef HIPL_HIPD_NETDEV_H32#ifndef HIPL_LIBHIPL_NETDEV_H
33#define HIPL_HIPD_NETDEV_H33#define HIPL_LIBHIPL_NETDEV_H
3434
35#include <stdbool.h>35#include <stdbool.h>
36#include <netinet/in.h>36#include <netinet/in.h>
@@ -73,4 +73,4 @@
73int hip_map_id_to_addr(const hip_hit_t *hit, const hip_lsi_t *lsi,73int hip_map_id_to_addr(const hip_hit_t *hit, const hip_lsi_t *lsi,
74 struct in6_addr *addr);74 struct in6_addr *addr);
7575
76#endif /* HIPL_HIPD_NETDEV_H */76#endif /* HIPL_LIBHIPL_NETDEV_H */
7777
=== renamed file 'hipd/nsupdate.c' => 'libhipl/nsupdate.c'
=== renamed file 'hipd/nsupdate.h' => 'libhipl/nsupdate.h'
--- hipd/nsupdate.h 2011-11-25 17:56:24 +0000
+++ libhipl/nsupdate.h 2012-05-12 14:17:22 +0000
@@ -33,12 +33,12 @@
33 * @brief and nsupdate by hip_send_locators_to_all_peers and hipd_init33 * @brief and nsupdate by hip_send_locators_to_all_peers and hipd_init
34 */34 */
3535
36#ifndef HIPL_HIPD_NSUPDATE_H36#ifndef HIPL_LIBHIPL_NSUPDATE_H
37#define HIPL_HIPD_NSUPDATE_H37#define HIPL_LIBHIPL_NSUPDATE_H
3838
39void hip_set_nsupdate_status(int status);39void hip_set_nsupdate_status(int status);
40int hip_get_nsupdate_status(void);40int hip_get_nsupdate_status(void);
4141
42int nsupdate(int start);42int nsupdate(int start);
4343
44#endif /* HIPL_HIPD_NSUPDATE_H */44#endif /* HIPL_LIBHIPL_NSUPDATE_H */
4545
=== renamed file 'hipd/opp_mode.c' => 'libhipl/opp_mode.c'
=== renamed file 'hipd/opp_mode.h' => 'libhipl/opp_mode.h'
--- hipd/opp_mode.h 2012-05-12 06:54:33 +0000
+++ libhipl/opp_mode.h 2012-05-12 14:17:22 +0000
@@ -31,8 +31,8 @@
31 * @brief Interface of the HIP opportunistic mode31 * @brief Interface of the HIP opportunistic mode
32 */32 */
3333
34#ifndef HIPL_HIPD_OPP_MODE_H34#ifndef HIPL_LIBHIPL_OPP_MODE_H
35#define HIPL_HIPD_OPP_MODE_H35#define HIPL_LIBHIPL_OPP_MODE_H
3636
37#include <netinet/in.h>37#include <netinet/in.h>
3838
@@ -43,4 +43,4 @@
43 const struct in6_addr *const src_addr);43 const struct in6_addr *const src_addr);
44int hip_handle_opp_r1(struct hip_packet_context *ctx);44int hip_handle_opp_r1(struct hip_packet_context *ctx);
4545
46#endif /* HIPL_HIPD_OPP_MODE_H */46#endif /* HIPL_LIBHIPL_OPP_MODE_H */
4747
=== renamed file 'hipd/output.c' => 'libhipl/output.c'
--- hipd/output.c 2012-05-12 06:54:33 +0000
+++ libhipl/output.c 2012-05-12 14:17:22 +0000
@@ -848,7 +848,8 @@
848 HIP_ASSERT(!hit_is_opportunistic_hit(&ctx->input_msg->hit_receiver));848 HIP_ASSERT(!hit_is_opportunistic_hit(&ctx->input_msg->hit_receiver));
849849
850 /* Case: I ----->IPv4---> RVS ---IPv6---> R */850 /* Case: I ----->IPv4---> RVS ---IPv6---> R */
851 if (IN6_IS_ADDR_V4MAPPED(r1_src_addr) !=851 if (!hipl_is_libhip_mode() &&
852 IN6_IS_ADDR_V4MAPPED(r1_src_addr) !=
852 IN6_IS_ADDR_V4MAPPED(r1_dst_addr)) {853 IN6_IS_ADDR_V4MAPPED(r1_dst_addr)) {
853 HIP_DEBUG_IN6ADDR("r1_src_addr", r1_src_addr);854 HIP_DEBUG_IN6ADDR("r1_src_addr", r1_src_addr);
854 HIP_DEBUG_IN6ADDR("r1_dst_addr", r1_dst_addr);855 HIP_DEBUG_IN6ADDR("r1_dst_addr", r1_dst_addr);
@@ -1217,6 +1218,9 @@
1217 goto out_err;1218 goto out_err;
1218 }1219 }
12191220
1221 if (hipl_is_libhip_mode()) {
1222 udp = 1;
1223 }
1220 dst_is_ipv4 = IN6_IS_ADDR_V4MAPPED(peer_addr);1224 dst_is_ipv4 = IN6_IS_ADDR_V4MAPPED(peer_addr);
1221 len = hip_get_msg_total_len(msg);1225 len = hip_get_msg_total_len(msg);
12221226
@@ -1245,13 +1249,16 @@
1245 if (local_addr) {1249 if (local_addr) {
1246 HIP_DEBUG("local address given\n");1250 HIP_DEBUG("local address given\n");
1247 memcpy(&my_addr, local_addr, sizeof(struct in6_addr));1251 memcpy(&my_addr, local_addr, sizeof(struct in6_addr));
1248 } else {1252 } else if (!hipl_is_libhip_mode()) {
1249 HIP_DEBUG("no local address, selecting one\n");1253 HIP_DEBUG("no local address, selecting one\n");
1250 HIP_IFEL(hip_select_source_address(&my_addr, peer_addr), -1,1254 HIP_IFEL(hip_select_source_address(&my_addr, peer_addr), -1,
1251 "Cannot find source address\n");1255 "Cannot find source address\n");
1256 } else {
1257 memset(&my_addr, 0, sizeof(my_addr));
1252 }1258 }
12531259
1254 src_is_ipv4 = IN6_IS_ADDR_V4MAPPED(&my_addr);1260 src_is_ipv4 = IN6_IS_ADDR_V4MAPPED(&my_addr) ||
1261 (dst_is_ipv4 && ipv6_addr_any(&my_addr));
12551262
1256 if (src_is_ipv4) {1263 if (src_is_ipv4) {
1257 IPV6_TO_IPV4_MAP(&my_addr, &src4->sin_addr);1264 IPV6_TO_IPV4_MAP(&my_addr, &src4->sin_addr);
@@ -1305,9 +1312,10 @@
13051312
1306 /* Handover may cause e.g. on-link duplicate address detection1313 /* Handover may cause e.g. on-link duplicate address detection
1307 * which may cause bind to fail. */1314 * which may cause bind to fail. */
13081315 if (!hipl_is_libhip_mode()) {
1309 HIP_IFEL(bind(hip_raw_sock_output, (struct sockaddr *) &src, sa_size),1316 HIP_IFEL(bind(hip_raw_sock_output, (struct sockaddr *) &src, sa_size),
1310 -1, "Binding to raw sock failed\n");1317 -1, "Binding to raw sock failed\n");
1318 }
13111319
1312#if (HIP_SIMULATE_PACKET_LOSS_PROBABILITY > 0)1320#if (HIP_SIMULATE_PACKET_LOSS_PROBABILITY > 0)
1313 if (HIP_SIMULATE_PACKET_LOSS && HIP_SIMULATE_PACKET_IS_LOST()) {1321 if (HIP_SIMULATE_PACKET_LOSS && HIP_SIMULATE_PACKET_IS_LOST()) {
@@ -1324,18 +1332,25 @@
1324 len = hip_get_msg_total_len(msg);1332 len = hip_get_msg_total_len(msg);
13251333
1326 if (udp) {1334 if (udp) {
1327 struct udphdr *uh = (struct udphdr *) msg;1335 if (!hipl_is_libhip_mode()) {
13281336 /* Insert 32 bits of zero bytes between UDP and HIP */
1329 /* Insert 32 bits of zero bytes between UDP and HIP */1337 memmove((char *) msg + HIP_UDP_ZERO_BYTES_LEN + sizeof(struct udphdr), msg, len);
1330 memmove((char *) msg + HIP_UDP_ZERO_BYTES_LEN + sizeof(struct udphdr), msg, len);1338 memset(msg, 0, HIP_UDP_ZERO_BYTES_LEN + sizeof(struct udphdr));
1331 memset(msg, 0, HIP_UDP_ZERO_BYTES_LEN + sizeof(struct udphdr));1339 len += HIP_UDP_ZERO_BYTES_LEN + sizeof(struct udphdr);
1332 len += HIP_UDP_ZERO_BYTES_LEN + sizeof(struct udphdr);1340
13331341 struct udphdr *uh = (struct udphdr *) msg;
1334 uh->source = htons(src_port);1342 uh->source = htons(src_port);
1335 uh->dest = htons(dst_port);1343 uh->dest = htons(dst_port);
1336 uh->len = htons(len);1344 uh->len = htons(len);
1337 uh->check = 0;1345 uh->check = 0;
1338 memmoved = 1;1346 } else {
1347 memmove((char *) msg + HIP_UDP_ZERO_BYTES_LEN, msg, len);
1348 memset(msg, 0, HIP_UDP_ZERO_BYTES_LEN);
1349 len += HIP_UDP_ZERO_BYTES_LEN;
1350
1351 dst4->sin_port = htons(dst_port);
1352 }
1353 memmoved = 1;
1339 }1354 }
13401355
1341 sent = sendto(hip_raw_sock_output, msg, len, 0,1356 sent = sendto(hip_raw_sock_output, msg, len, 0,
13421357
=== renamed file 'hipd/output.h' => 'libhipl/output.h'
--- hipd/output.h 2012-05-12 06:54:33 +0000
+++ libhipl/output.h 2012-05-12 14:17:22 +0000
@@ -23,8 +23,8 @@
23 * OTHER DEALINGS IN THE SOFTWARE.23 * OTHER DEALINGS IN THE SOFTWARE.
24 */24 */
2525
26#ifndef HIPL_HIPD_OUTPUT_H26#ifndef HIPL_LIBHIPL_OUTPUT_H
27#define HIPL_HIPD_OUTPUT_H27#define HIPL_LIBHIPL_OUTPUT_H
2828
29#include <netinet/in.h>29#include <netinet/in.h>
3030
@@ -116,4 +116,4 @@
116 in_port_t src_port, in_port_t dst_port,116 in_port_t src_port, in_port_t dst_port,
117 const void *msg, int length);117 const void *msg, int length);
118118
119#endif /* HIPL_HIPD_OUTPUT_H */119#endif /* HIPL_LIBHIPL_OUTPUT_H */
120120
=== renamed file 'hipd/pkt_handling.c' => 'libhipl/pkt_handling.c'
=== renamed file 'hipd/pkt_handling.h' => 'libhipl/pkt_handling.h'
--- hipd/pkt_handling.h 2012-05-12 06:54:33 +0000
+++ libhipl/pkt_handling.h 2012-05-12 14:17:22 +0000
@@ -23,8 +23,8 @@
23 * OTHER DEALINGS IN THE SOFTWARE.23 * OTHER DEALINGS IN THE SOFTWARE.
24 */24 */
2525
26#ifndef HIPL_HIPD_PKT_HANDLING_H26#ifndef HIPL_LIBHIPL_PKT_HANDLING_H
27#define HIPL_HIPD_PKT_HANDLING_H27#define HIPL_LIBHIPL_PKT_HANDLING_H
2828
29#include <stdint.h>29#include <stdint.h>
3030
@@ -44,4 +44,4 @@
4444
45void hip_uninit_handle_functions(void);45void hip_uninit_handle_functions(void);
4646
47#endif /* HIPL_HIPD_PKT_HANDLING_H */47#endif /* HIPL_LIBHIPL_PKT_HANDLING_H */
4848
=== renamed file 'hipd/registration.c' => 'libhipl/registration.c'
=== renamed file 'hipd/registration.h' => 'libhipl/registration.h'
--- hipd/registration.h 2012-05-12 06:54:33 +0000
+++ libhipl/registration.h 2012-05-12 14:17:22 +0000
@@ -31,8 +31,8 @@
31 * @see hiprelay.h31 * @see hiprelay.h
32 */32 */
3333
34#ifndef HIPL_HIPD_REGISTRATION_H34#ifndef HIPL_LIBHIPL_REGISTRATION_H
35#define HIPL_HIPD_REGISTRATION_H35#define HIPL_LIBHIPL_REGISTRATION_H
3636
37#include <stdint.h>37#include <stdint.h>
38#include <sys/types.h>38#include <sys/types.h>
@@ -80,4 +80,4 @@
80int hip_handle_reg_from(struct hip_hadb_state *entry, struct hip_common *msg);80int hip_handle_reg_from(struct hip_hadb_state *entry, struct hip_common *msg);
81int hip_handle_req_user_msg(const struct hip_common *const msg);81int hip_handle_req_user_msg(const struct hip_common *const msg);
8282
83#endif /* HIPL_HIPD_REGISTRATION_H */83#endif /* HIPL_LIBHIPL_REGISTRATION_H */
8484
=== renamed file 'hipd/user.c' => 'libhipl/user.c'
=== renamed file 'hipd/user.h' => 'libhipl/user.h'
--- hipd/user.h 2012-05-12 06:54:33 +0000
+++ libhipl/user.h 2012-05-12 14:17:22 +0000
@@ -23,8 +23,8 @@
23 * OTHER DEALINGS IN THE SOFTWARE.23 * OTHER DEALINGS IN THE SOFTWARE.
24 */24 */
2525
26#ifndef HIPL_HIPD_USER_H26#ifndef HIPL_LIBHIPL_USER_H
27#define HIPL_HIPD_USER_H27#define HIPL_LIBHIPL_USER_H
2828
29#include <netinet/in.h>29#include <netinet/in.h>
3030
@@ -43,4 +43,4 @@
43int hip_handle_user_msg(struct hip_common *msg,43int hip_handle_user_msg(struct hip_common *msg,
44 struct sockaddr_in6 *src);44 struct sockaddr_in6 *src);
4545
46#endif /* HIPL_HIPD_USER_H */46#endif /* HIPL_LIBHIPL_USER_H */
4747
=== renamed file 'hipd/user_ipsec_hipd_msg.c' => 'libhipl/user_ipsec_hipd_msg.c'
=== renamed file 'hipd/user_ipsec_hipd_msg.h' => 'libhipl/user_ipsec_hipd_msg.h'
--- hipd/user_ipsec_hipd_msg.h 2012-05-12 06:54:33 +0000
+++ libhipl/user_ipsec_hipd_msg.h 2012-05-12 14:17:22 +0000
@@ -30,8 +30,8 @@
30 * @brief userspace IPsec hipd <-> hipfw communication30 * @brief userspace IPsec hipd <-> hipfw communication
31 */31 */
3232
33#ifndef HIPL_HIPD_USER_IPSEC_HIPD_MSG_H33#ifndef HIPL_LIBHIPL_USER_IPSEC_HIPD_MSG_H
34#define HIPL_HIPD_USER_IPSEC_HIPD_MSG_H34#define HIPL_LIBHIPL_USER_IPSEC_HIPD_MSG_H
3535
36#include <stdint.h>36#include <stdint.h>
37#include <netinet/in.h>37#include <netinet/in.h>
@@ -57,4 +57,4 @@
57 const int dst_port);57 const int dst_port);
58struct hip_common *create_flush_all_sa_msg(void);58struct hip_common *create_flush_all_sa_msg(void);
5959
60#endif /* HIPL_HIPD_USER_IPSEC_HIPD_MSG_H */60#endif /* HIPL_LIBHIPL_USER_IPSEC_HIPD_MSG_H */
6161
=== renamed file 'hipd/user_ipsec_sadb_api.c' => 'libhipl/user_ipsec_sadb_api.c'
=== renamed file 'hipd/user_ipsec_sadb_api.h' => 'libhipl/user_ipsec_sadb_api.h'
--- hipd/user_ipsec_sadb_api.h 2012-05-12 06:54:33 +0000
+++ libhipl/user_ipsec_sadb_api.h 2012-05-12 14:17:22 +0000
@@ -31,8 +31,8 @@
31 * @brief API used by the hipd to set up and maintain userspace IPsec state31 * @brief API used by the hipd to set up and maintain userspace IPsec state
32 */32 */
3333
34#ifndef HIPL_HIPD_USER_IPSEC_SADB_API_H34#ifndef HIPL_LIBHIPL_USER_IPSEC_SADB_API_H
35#define HIPL_HIPD_USER_IPSEC_SADB_API_H35#define HIPL_LIBHIPL_USER_IPSEC_SADB_API_H
3636
37#include <stdint.h>37#include <stdint.h>
38#include <netinet/in.h>38#include <netinet/in.h>
@@ -52,4 +52,4 @@
5252
53int hip_userspace_ipsec_setup_default_sp_prefix_pair(void);53int hip_userspace_ipsec_setup_default_sp_prefix_pair(void);
5454
55#endif /* HIPL_HIPD_USER_IPSEC_SADB_API_H */55#endif /* HIPL_LIBHIPL_USER_IPSEC_SADB_API_H */
5656
=== modified file 'modules/cert/hipd/cert.c'
--- modules/cert/hipd/cert.c 2012-05-12 06:54:33 +0000
+++ modules/cert/hipd/cert.c 2012-05-12 14:17:22 +0000
@@ -33,13 +33,13 @@
33#include <stdlib.h>33#include <stdlib.h>
34#include <string.h>34#include <string.h>
3535
36#include "hipd/hipd.h"
37#include "hipd/pkt_handling.h"
38#include "libcore/builder.h"36#include "libcore/builder.h"
39#include "libcore/cert.h"37#include "libcore/cert.h"
40#include "libcore/debug.h"38#include "libcore/debug.h"
41#include "libcore/ife.h"39#include "libcore/ife.h"
42#include "libcore/protodefs.h"40#include "libcore/protodefs.h"
41#include "libhipl/hipd.h"
42#include "libhipl/pkt_handling.h"
43#include "modules/midauth/hipd/midauth.h"43#include "modules/midauth/hipd/midauth.h"
44#include "modules/update/hipd/update.h"44#include "modules/update/hipd/update.h"
45#include "cert.h"45#include "cert.h"
4646
=== modified file 'modules/heartbeat/hipd/heartbeat.c'
--- modules/heartbeat/hipd/heartbeat.c 2012-05-12 06:54:33 +0000
+++ modules/heartbeat/hipd/heartbeat.c 2012-05-12 14:17:22 +0000
@@ -65,15 +65,6 @@
65#include <sys/types.h>65#include <sys/types.h>
66#include <sys/socket.h>66#include <sys/socket.h>
6767
68#include "hipd/close.h"
69#include "hipd/hadb.h"
70#include "hipd/init.h"
71#include "hipd/hip_socket.h"
72#include "hipd/maintenance.h"
73#include "hipd/nat.h"
74#include "hipd/output.h"
75#include "hipd/pkt_handling.h"
76#include "hipd/user.h"
77#include "libcore/common.h"68#include "libcore/common.h"
78#include "libcore/debug.h"69#include "libcore/debug.h"
79#include "libcore/icomm.h"70#include "libcore/icomm.h"
@@ -84,6 +75,15 @@
84#include "libcore/straddr.h"75#include "libcore/straddr.h"
85#include "libcore/modularization.h"76#include "libcore/modularization.h"
86#include "libcore/gpl/nlink.h"77#include "libcore/gpl/nlink.h"
78#include "libhipl/close.h"
79#include "libhipl/hadb.h"
80#include "libhipl/hip_socket.h"
81#include "libhipl/init.h"
82#include "libhipl/maintenance.h"
83#include "libhipl/nat.h"
84#include "libhipl/output.h"
85#include "libhipl/pkt_handling.h"
86#include "libhipl/user.h"
87#include "heartbeat.h"87#include "heartbeat.h"
8888
89#define HIP_MAX_ICMP_PACKET 51289#define HIP_MAX_ICMP_PACKET 512
9090
=== modified file 'modules/heartbeat_update/hipd/hb_update.c'
--- modules/heartbeat_update/hipd/hb_update.c 2012-05-12 06:54:33 +0000
+++ modules/heartbeat_update/hipd/hb_update.c 2012-05-12 14:17:22 +0000
@@ -1,5 +1,5 @@
1/*1/*
2 * Copyright (c) 2010 Aalto University and RWTH Aachen University.2 * Copyright (c) 2010, 2012 Aalto University and RWTH Aachen University.
3 *3 *
4 * Permission is hereby granted, free of charge, to any person4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation5 * obtaining a copy of this software and associated documentation
@@ -40,12 +40,12 @@
40#include <stdint.h>40#include <stdint.h>
41#include <stdlib.h>41#include <stdlib.h>
4242
43#include "hipd/hadb.h"
44#include "hipd/maintenance.h"
45#include "libcore/builder.h"43#include "libcore/builder.h"
46#include "libcore/common.h"44#include "libcore/common.h"
47#include "libcore/debug.h"45#include "libcore/debug.h"
48#include "libcore/protodefs.h"46#include "libcore/protodefs.h"
47#include "libhipl/hadb.h"
48#include "libhipl/maintenance.h"
49#include "modules/update/hipd/update.h"49#include "modules/update/hipd/update.h"
50#include "modules/update/hipd/update_locator.h"50#include "modules/update/hipd/update_locator.h"
51#include "hb_update.h"51#include "hb_update.h"
5252
=== modified file 'modules/midauth/hipd/midauth.c'
--- modules/midauth/hipd/midauth.c 2012-05-12 06:54:33 +0000
+++ modules/midauth/hipd/midauth.c 2012-05-12 14:17:22 +0000
@@ -33,8 +33,6 @@
33#include <stdint.h>33#include <stdint.h>
34#include <string.h>34#include <string.h>
3535
36#include "hipd/hidb.h"
37#include "hipd/pkt_handling.h"
38#include "libcore/builder.h"36#include "libcore/builder.h"
39#include "libcore/common.h"37#include "libcore/common.h"
40#include "libcore/ife.h"38#include "libcore/ife.h"
@@ -42,6 +40,8 @@
42#include "libcore/protodefs.h"40#include "libcore/protodefs.h"
43#include "libcore/solve.h"41#include "libcore/solve.h"
44#include "libcore/state.h"42#include "libcore/state.h"
43#include "libhipl/hidb.h"
44#include "libhipl/pkt_handling.h"
45#include "modules/midauth/lib/midauth_builder.h"45#include "modules/midauth/lib/midauth_builder.h"
46#include "modules/update/hipd/update.h"46#include "modules/update/hipd/update.h"
47#include "midauth.h"47#include "midauth.h"
4848
=== modified file 'modules/update/hipd/update.c'
--- modules/update/hipd/update.c 2012-05-12 06:54:33 +0000
+++ modules/update/hipd/update.c 2012-05-12 14:17:22 +0000
@@ -37,17 +37,6 @@
37#include <string.h>37#include <string.h>
3838
39#include "config.h"39#include "config.h"
40#include "hipd/cookie.h"
41#include "hipd/hadb.h"
42#include "hipd/hidb.h"
43#include "hipd/hipd.h"
44#include "hipd/input.h"
45#include "hipd/maintenance.h"
46#include "hipd/netdev.h"
47#include "hipd/nsupdate.h"
48#include "hipd/output.h"
49#include "hipd/pkt_handling.h"
50#include "hipd/user.h"
51#include "libcore/builder.h"40#include "libcore/builder.h"
52#include "libcore/debug.h"41#include "libcore/debug.h"
53#include "libcore/hip_udp.h"42#include "libcore/hip_udp.h"
@@ -57,6 +46,17 @@
57#include "libcore/prefix.h"46#include "libcore/prefix.h"
58#include "libcore/state.h"47#include "libcore/state.h"
59#include "libcore/performance.h"48#include "libcore/performance.h"
49#include "libhipl/cookie.h"
50#include "libhipl/hadb.h"
51#include "libhipl/hidb.h"
52#include "libhipl/hipd.h"
53#include "libhipl/input.h"
54#include "libhipl/maintenance.h"
55#include "libhipl/netdev.h"
56#include "libhipl/nsupdate.h"
57#include "libhipl/output.h"
58#include "libhipl/pkt_handling.h"
59#include "libhipl/user.h"
60#include "update_builder.h"60#include "update_builder.h"
61#include "update_locator.h"61#include "update_locator.h"
62#include "update_param_handling.h"62#include "update_param_handling.h"
6363
=== modified file 'modules/update/hipd/update_builder.c'
--- modules/update/hipd/update_builder.c 2012-05-12 06:54:33 +0000
+++ modules/update/hipd/update_builder.c 2012-05-12 14:17:22 +0000
@@ -34,12 +34,12 @@
34#include <string.h>34#include <string.h>
35#include <errno.h>35#include <errno.h>
3636
37#include "hipd/hadb.h"
38#include "hipd/netdev.h"
39#include "libcore/builder.h"37#include "libcore/builder.h"
40#include "libcore/ife.h"38#include "libcore/ife.h"
41#include "libcore/list.h"39#include "libcore/list.h"
42#include "libcore/prefix.h"40#include "libcore/prefix.h"
41#include "libhipl/hadb.h"
42#include "libhipl/netdev.h"
43#include "update_builder.h"43#include "update_builder.h"
4444
45enum hip_locator_traffic_type {45enum hip_locator_traffic_type {
4646
=== modified file 'modules/update/hipd/update_locator.c'
--- modules/update/hipd/update_locator.c 2012-05-12 06:54:33 +0000
+++ modules/update/hipd/update_locator.c 2012-05-12 14:17:22 +0000
@@ -1,5 +1,5 @@
1/*1/*
2 * Copyright (c) 2010 Aalto University and RWTH Aachen University.2 * Copyright (c) 2010, 2012 Aalto University and RWTH Aachen University.
3 *3 *
4 * Permission is hereby granted, free of charge, to any person4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation5 * obtaining a copy of this software and associated documentation
@@ -34,11 +34,11 @@
34#include <string.h>34#include <string.h>
35#include <openssl/lhash.h>35#include <openssl/lhash.h>
3636
37#include "hipd/maintenance.h"
38#include "libcore/builder.h"37#include "libcore/builder.h"
39#include "libcore/debug.h"38#include "libcore/debug.h"
40#include "libcore/ife.h"39#include "libcore/ife.h"
41#include "libcore/protodefs.h"40#include "libcore/protodefs.h"
41#include "libhipl/maintenance.h"
42#include "update_builder.h"42#include "update_builder.h"
43#include "update.h"43#include "update.h"
44#include "update_locator.h"44#include "update_locator.h"
4545
=== added file 'test/check_libhipl.c'
--- test/check_libhipl.c 1970-01-01 00:00:00 +0000
+++ test/check_libhipl.c 2012-05-12 14:17:22 +0000
@@ -0,0 +1,221 @@
1/*
2 * Copyright (c) 2012 Aalto University and RWTH Aachen University.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following
11 * conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26/**
27 * @file
28 * @brief Tests of libhipl on localhost (see doc/HACKING on unit tests).
29 */
30
31#include <arpa/inet.h>
32#include <check.h>
33#include <errno.h>
34#include <stdbool.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <sys/select.h>
38#include <unistd.h>
39
40#include "libhipl/hidb.h"
41#include "libhipl/lhipl.h"
42
43#ifndef max
44#define max(a, b) (((a) > (b)) ? (a) : (b))
45#endif
46
47#define LO_IP "127.0.0.1"
48#define TEST_MSG "Hello Sailor!"
49#define SEND_PORT 22345
50#define RECV_PORT 22300
51
52static struct in6_addr lo_hit;
53static char peername[HIPL_MAX_PEERNAME];
54static struct sockaddr_in send_addr;
55static struct sockaddr_in recv_addr;
56
57static int sender;
58static int receiver;
59
60static void test_libhipl_lo_init(int proto)
61{
62 if (hipl_lib_init_all(HIPL_LIB_LOG_NONE) < 0) {
63 fail("hipl_lib_init_all");
64 }
65
66 fail_if(hip_get_default_hit(&lo_hit) < 0, "Failed to load local hit");
67 inet_ntop(AF_INET6, &lo_hit, peername, HIPL_MAX_PEERNAME);
68 fail_if(hipl_add_peer_info(peername, LO_IP) < 0,
69 "Failed to insert peer info");
70
71 send_addr.sin_family = AF_INET;
72 inet_pton(AF_INET, LO_IP, &send_addr.sin_addr);
73 send_addr.sin_port = htons(SEND_PORT);
74 recv_addr.sin_family = AF_INET;
75 inet_pton(AF_INET, LO_IP, &recv_addr.sin_addr);
76 recv_addr.sin_port = htons(RECV_PORT);
77
78 if (proto == IPPROTO_TCP) {
79 sender = hipl_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
80 receiver = hipl_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
81 } else if (proto == IPPROTO_UDP) {
82 sender = hipl_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
83 receiver = hipl_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
84 }
85 fail_if(sender <= 0 || receiver <= 0);
86
87 if (hipl_bind(sender, (struct sockaddr *) &send_addr,
88 sizeof(send_addr)) < 0) {
89 fail("hipl_bind() for sender");
90 }
91 if (hipl_bind(receiver, (struct sockaddr *) &recv_addr,
92 sizeof(recv_addr)) < 0) {
93 fail("hipl_bind() for receiver");
94 }
95
96 hipl_lib_set_bex_feedback(true);
97}
98
99static void test_libhipl_lo_main_loop(int send_hid, int recv_hid)
100{
101 char peer[HIPL_MAX_PEERNAME];
102 uint16_t peer_port;
103 char buf[1024];
104 fd_set rset;
105 int ret = 0;
106 int maxfd = max(hipl_lib_get_sockfd(send_hid),
107 hipl_lib_get_sockfd(recv_hid));
108
109 //trigger BEX
110 ret = hipl_sendto(send_hid, TEST_MSG, strlen(TEST_MSG), 0,
111 peername, RECV_PORT);
112 fail_if(ret != -EWAITBEX);
113
114 while (1) {
115 FD_ZERO(&rset);
116 FD_SET(hipl_lib_get_sockfd(send_hid), &rset);
117 FD_SET(hipl_lib_get_sockfd(recv_hid), &rset);
118 fail_if(select(maxfd + 1, &rset, NULL, NULL, NULL) < 0);
119
120 if (FD_ISSET(hipl_lib_get_sockfd(recv_hid), &rset)) {
121 ret = hipl_recvfrom(recv_hid, buf, 1024, 0, peer, &peer_port);
122 fail_if(ret < 0 && ret != -EWAITBEX && ret != -EBEXESTABLISHED);
123 fail_if(ret > 0 && ret != strlen(TEST_MSG));
124
125 if (ret == strlen(TEST_MSG)) {
126 buf[ret] = '\0';
127 fail_if(strcmp(buf, TEST_MSG) != 0);
128 fail_if(strcmp(peername, peer) != 0);
129 fail_if(peer_port != SEND_PORT);
130 // Finish test
131 break;
132 }
133 }
134 if (FD_ISSET(hipl_lib_get_sockfd(send_hid), &rset)) {
135 ret = hipl_sendto(send_hid, TEST_MSG, strlen(TEST_MSG), 0,
136 peername, RECV_PORT);
137 fail_if(ret < 0 && ret != -EWAITBEX && ret != -EBEXESTABLISHED);
138 fail_if(ret > 0 && ret != strlen(TEST_MSG));
139 }
140 }
141
142 hipl_close(send_hid);
143 hipl_close(recv_hid);
144}
145
146START_TEST(test_libhipl_lo_tcp)
147{
148 fd_set wset, rset;
149 int maxfd, recv_slave = 0;
150
151 test_libhipl_lo_init(IPPROTO_TCP);
152
153 // Setup TCP connection
154 maxfd = max(hipl_lib_get_sockfd(sender), hipl_lib_get_sockfd(receiver));
155 if (hipl_listen(receiver, 5) < 0) {
156 fail("hipl_listen()");
157 }
158 fail_if(hipl_lib_set_nonblock(sender, true) < 0);
159 fail_if(hipl_lib_set_nonblock(receiver, true) < 0);
160 hipl_accept(receiver);
161 hipl_connect(sender, peername, RECV_PORT);
162 while (recv_slave <= 0) {
163 FD_ZERO(&wset);
164 FD_ZERO(&rset);
165 FD_SET(hipl_lib_get_sockfd(sender), &wset);
166 FD_SET(hipl_lib_get_sockfd(receiver), &rset);
167 fail_if(select(maxfd + 1, &rset, &wset, NULL, NULL) < 0);
168 if (FD_ISSET(hipl_lib_get_sockfd(receiver), &rset)) {
169 if ((recv_slave = hipl_accept(receiver)) < 0) {
170 fail("hipl_accept(), %s", strerror(errno));
171 }
172 }
173 if (FD_ISSET(hipl_lib_get_sockfd(sender), &wset)) {
174 if (hipl_connect(sender, peername, RECV_PORT) < 0
175 && errno != EINPROGRESS && errno != EISCONN) {
176 fail("hipl_connect() %s", strerror(errno));
177 }
178 }
179 }
180 fail_if(hipl_connect(sender, peername, RECV_PORT) < 0 && errno != EISCONN);
181 fail_if(hipl_lib_set_nonblock(sender, false) < 0);
182
183 // Process base exchange and user data
184 test_libhipl_lo_main_loop(sender, recv_slave);
185 hipl_close(receiver);
186}
187END_TEST
188
189START_TEST(test_libhipl_lo_udp)
190{
191 test_libhipl_lo_init(IPPROTO_UDP);
192 test_libhipl_lo_main_loop(sender, receiver);
193}
194END_TEST
195
196static Suite *hipnc_suite(void)
197{
198 Suite *s = suite_create("libhipl");
199
200 TCase *tc_libhipl_lo = tcase_create("libhipl_lo");
201 tcase_add_test(tc_libhipl_lo, test_libhipl_lo_udp);
202 tcase_add_test(tc_libhipl_lo, test_libhipl_lo_tcp);
203 suite_add_tcase(s, tc_libhipl_lo);
204
205 return s;
206}
207
208int main(void)
209{
210 int number_failed;
211 Suite *s = hipnc_suite();
212 SRunner *sr = srunner_create(NULL);
213
214 srunner_add_suite(sr, s);
215 srunner_run_all(sr, CK_NORMAL);
216
217 number_failed = srunner_ntests_failed(sr);
218 srunner_free(sr);
219
220 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
221}
0222
=== modified file 'test/hipd/lsidb.c'
--- test/hipd/lsidb.c 2012-01-18 21:09:47 +0000
+++ test/hipd/lsidb.c 2012-05-12 14:17:22 +0000
@@ -26,7 +26,7 @@
26#include <check.h>26#include <check.h>
27#include <stdlib.h>27#include <stdlib.h>
2828
29#include "hipd/lsidb.c"29#include "libhipl/lsidb.c"
30#include "test_suites.h"30#include "test_suites.h"
3131
32START_TEST(test_lsidb_allocate_lsi_valid)32START_TEST(test_lsidb_allocate_lsi_valid)

Subscribers

People subscribed via source and target branches

to all changes: