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: 2989 lines (+2255/-120)
34 files modified
.bzrignore (+1/-0)
Makefile.am (+59/-44)
doc/HOWTO.xml.in (+136/-0)
hipd/hipd.c (+6/-7)
hipd/main.c (+2/-2)
hipfw/hipfw.c (+1/-1)
lib/core/crypto.h (+2/-2)
lib/core/hostid.c (+11/-4)
lib/core/linkedlist.c (+46/-1)
lib/core/linkedlist.h (+2/-0)
lib/core/message.c (+72/-1)
lib/core/message.h (+1/-0)
lib/hipdaemon/hadb.c (+15/-1)
lib/hipdaemon/hidb.c (+9/-7)
lib/hipdaemon/hipd.h (+4/-0)
lib/hipdaemon/init.c (+290/-1)
lib/hipdaemon/init.h (+3/-1)
lib/hipdaemon/input.c (+6/-1)
lib/hipdaemon/maintenance.c (+1/-0)
lib/hipdaemon/output.c (+33/-18)
lib/hipdaemon/socket_wrapper.c (+811/-0)
lib/hipdaemon/socket_wrapper.h (+48/-0)
modules/heartbeat/hipd/heartbeat.c (+8/-8)
modules/heartbeat_update/hipd/hb_update.c (+2/-2)
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 (+1/-1)
test/check_hipnetcat.c (+201/-0)
test/check_lib_core.c (+2/-1)
test/hipd/lsidb.c (+1/-1)
test/hipnetcat.c (+344/-0)
test/lib/core/linkedlist.c (+120/-0)
test/lib/core/test_suites.h (+2/-1)
To merge this branch: bzr merge lp:~hipl-core/hipl/libhip
Reviewer Review Type Date Requested Status
Christof Mroz Needs Fixing
Diego Biurrun Disapprove
Miika Komu Pending
Xin Pending
Review via email: mp+92480@code.launchpad.net

This proposal supersedes a proposal from 2012-02-03.

Description of the change

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

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

Libhip merge proposal:

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

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

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

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

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

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

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

I am advocating this approach, so I think I'll need abstain myself. Feel free to comment both the design and implementation style. Also, I should repeat the point of this merge proposal is the system testing - we did this intermediate step intentionally to avoid an overly long merge proposal.

review: Abstain
Revision history for this message
Diego Biurrun (diego-biurrun) wrote : Posted in a previous version of this proposal
Download full text (11.6 KiB)

 review needs-fixing

On Fri, Feb 03, 2012 at 10:58:21AM +0000, Xin wrote:
> 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.

Hmmmmmmmmmmmmmmmmmmmmmmm, I'm sceptical. Much of this sounds as if it
were not necessarily tied to librarizing hipd. For example, the system
level test could be done without it. Or maybe I don't yet understand
well enough.

Below are some quick comments on the implementation. Look out for similar
stuff in the other code you add. There is some work ahead before this is
fit for merging.

> --- Makefile.am 2012-01-30 12:28:31 +0000
> +++ Makefile.am 2012-02-03 10:57:21 +0000
> @@ -72,12 +73,15 @@
> test/check_hipfw \
> test/check_lib_core \
> test/check_lib_tool \
> - test/check_modules_midauth
> + test/check_modules_midauth \
> + test/check_hipnetcat
> +
> check_PROGRAMS = test/check_hipd \
> test/check_hipfw \
> test/check_lib_core \
> test/check_lib_tool \
> - test/check_modules_midauth
> + test/check_modules_midauth \
> + test/check_hipnetcat
> endif

That stuff used to be in alphabetical order.

There's more stuff I could complain about in this file, but there's no
point in reviewing at this level yet I th...

review: Needs Fixing
Revision history for this message
Miika Komu (miika-iki) wrote : Posted in a previous version of this proposal

> Hmmmmmmmmmmmmmmmmmmmmmmm, I'm sceptical. Much of this sounds as if it
> were not necessarily tied to librarizing hipd. For example, the system
> level test could be done without it.

It could be but we have clearly identified another hidden design goal to boost the adoption of HIP ;)

Thanks for you comments, Xin will work on them to improve the code.

Revision history for this message
Miika Komu (miika-iki) wrote : Posted in a previous version of this proposal

Hi,

On 02/07/2012 02:01 PM, Xin Gu wrote:
> On 06/02/12 19:44, Diego Biurrun wrote:
>>> @@ -866,7 +1074,7 @@
>>> * @param signum signal the signal hipd received from OS
>>> */
>>> -static void hip_close(int signum)
>>> +static void hipd_close(int signum)
>>> {
>>> @@ -928,8 +1136,8 @@
>>>
>>> /* Register signal handlers */
>>> - signal(SIGINT, hip_close);
>>> - signal(SIGTERM, hip_close);
>>> + signal(SIGINT, hipd_close);
>>> + signal(SIGTERM, hipd_close);
>>> signal(SIGCHLD, sig_chld);
>> Push that separately.
>>
>>
> Could you explain more here? I didn't get your point.

I believe Diego meant that this should be pushed directly to trunk.

>>> +int hip_bind(int fd, const struct sockaddr *address, socklen_t
>>> address_len)
>>> +{
>>> + int err = 0;
>>> + struct hip_fd_info *fd_info;
>>> + struct sockaddr_storage laddr;
>>> + socklen_t laddr_len = sizeof(laddr);
>>> + uint16_t request_port;
>>> +
>>> + fd_info = hip_socket_get_info(fd);
>>> + HIP_IFEL(!fd_info, -1, "Fd %d is not a hip socket, exiting.\n", fd);
>>> +
>>> + request_port = get_port_from_saddr(address);
>>> + if ((err = bind(fd, address, address_len)) == 0) {
>>> + if (request_port == 0) {
>>> + HIP_IFEL(getsockname(fd, (struct sockaddr *)&laddr,&laddr_len),
>>> + -1, "getsockname() failed\n");
>>> + request_port = get_port_from_saddr((struct sockaddr *)&laddr);
>>> + }
>>> + fd_info->bound_port = ntohs(request_port);
>>> + } else {
>>> + HIP_PERROR("bind error:");
>>> + }
>>> +
>>> + HIP_DEBUG("bind to port %d\n", fd_info->bound_port);
>>> +
>>> +out_err:
>>> + return err;
>>> +}
>> HIP_IFEL abuse, more below
>>
> As a new developer, I am not sure what kind of usage will be defined as
> an abuse. Basically I use HIP_IFEL as a shortcut for code like below:
> if (func()) {
> HIP_ERROR("msg");
> return -1;
> }
> In current code base I can also find similar usage. In HACKING doc there
> is a sample for malloc(). Probably I am wrong, could you also explain
> how to use this macro properly? Thanks.

unless the HIP_IFEL is used for memory deallocation, it should not be
used as a direct "return" is sufficient. Sorry that I forgot mention
about this.

P.S. Please remember to cc the launchpad bug id.

Revision history for this message
Xin (eric-nevup) wrote : Posted in a previous version of this proposal
Download full text (7.3 KiB)

On 08/02/12 20:00, Diego Biurrun wrote:
> review needs-fixing
>
> On Wed, Feb 08, 2012 at 08:43:18AM +0000, Xin wrote:
>> You have been requested to review the proposed merge of lp:~hipl-core/hipl/libhip into lp:hipl.
>>
>> === modified file 'Makefile.am'
>> --- Makefile.am 2012-01-30 12:28:31 +0000
>> +++ Makefile.am 2012-02-07 15:20:56 +0000
>> @@ -64,19 +65,22 @@
>>
>> ### libraries ###
>> lib_LTLIBRARIES = lib/core/libhipcore.la
>> -
>> +lib_LTLIBRARIES += lib/hipdaemon/libhipdaemon.la
>>
>> ### tests ###
> Oh, the poor empty line ...
Fixed, I didn't notice that there is a style for empty lines
>
>> if HIP_UNITTESTS
>> -TESTS = test/check_hipd \
>> - test/check_hipfw \
>> - test/check_lib_core \
>> - test/check_lib_tool \
>> +TESTS = test/check_hipd \
>> + test/check_hipfw \
>> + test/check_hipnetcat \
>> + test/check_lib_core \
>> + test/check_lib_tool \
>> test/check_modules_midauth
>> -check_PROGRAMS = test/check_hipd \
>> - test/check_hipfw \
>> - test/check_lib_core \
>> - test/check_lib_tool \
>> +
>> +check_PROGRAMS = test/check_hipd \
>> + test/check_hipfw \
>> + test/check_hipnetcat \
>> + test/check_lib_core \
>> + test/check_lib_tool \
>> test/check_modules_midauth
>> endif
> Having to realign all those backslashes is suboptimal. I'll move them
> all to a sensible position in trunk.
I merged your newest change, which align to column 72, but some new
contents in libhip are long than 72. I align those longer lines to 84
>
>> @@ -86,55 +90,17 @@
>> test_auth_performance_SOURCES = test/auth_performance.c
>> test_certteststub_SOURCES = test/certteststub.c
>> test_dh_performance_SOURCES = test/dh_performance.c
>> -test_fw_port_bindings_performance_SOURCES = test/fw_port_bindings_performance.c \
>> - hipfw/file_buffer.c \
>> +test_fw_port_bindings_performance_SOURCES = hipfw/file_buffer.c \
>> hipfw/line_parser.c \
>> - hipfw/port_bindings.c
>> + hipfw/port_bindings.c \
>> + test/fw_port_bindings_performance.c
> This is unrelated, push to trunk right away.
Pushed
>> @@ -225,6 +233,8 @@
>> test/hipfw/rewrite.c \
>> $(hipfw_hipfw_sources)
>>
>> +test_check_hipnetcat_SOURCES = test/check_hipnetcat.c
>> +
>> test_check_lib_core_SOURCES = test/check_lib_core.c \
>> test/lib/core/crypto.c \
>> test/lib/core/hit.c \
>> @@ -241,21 +251,35 @@
>> test/modu...

Read more...

Revision history for this message
Diego Biurrun (diego-biurrun) wrote : Posted in a previous version of this proposal

On Thu, Feb 09, 2012 at 04:54:32PM +0200, Xin Gu wrote:
> On 08/02/12 20:00, Diego Biurrun wrote:
> >On Wed, Feb 08, 2012 at 08:43:18AM +0000, Xin wrote:
> >>
> >>--- Makefile.am 2012-01-30 12:28:31 +0000
> >>+++ Makefile.am 2012-02-07 15:20:56 +0000
> >>@@ -64,19 +65,22 @@
> >>
> >> ### libraries ###
> >> lib_LTLIBRARIES = lib/core/libhipcore.la
> >>-
> >>+lib_LTLIBRARIES += lib/hipdaemon/libhipdaemon.la
> >>
> >> ### tests ###
> >Oh, the poor empty line ...
> Fixed, I didn't notice that there is a style for empty lines

You shouldn't unintentionally mess with the style of a file.

> >> if HIP_UNITTESTS
> >>-TESTS = test/check_hipd \
> >>- test/check_hipfw \
> >>- test/check_lib_core \
> >>- test/check_lib_tool \
> >>+TESTS = test/check_hipd \
> >>+ test/check_hipfw \
> >>+ test/check_hipnetcat \
> >>+ test/check_lib_core \
> >>+ test/check_lib_tool \
> >> test/check_modules_midauth
> >>-check_PROGRAMS = test/check_hipd \
> >>- test/check_hipfw \
> >>- test/check_lib_core \
> >>- test/check_lib_tool \
> >>+
> >>+check_PROGRAMS = test/check_hipd \
> >>+ test/check_hipfw \
> >>+ test/check_hipnetcat \
> >>+ test/check_lib_core \
> >>+ test/check_lib_tool \
> >> test/check_modules_midauth
> >> endif
> >Having to realign all those backslashes is suboptimal. I'll move them
> >all to a sensible position in trunk.
> I merged your newest change, which align to column 72, but some new
> contents in libhip are long than 72. I align those longer lines to 84

Try to keep lines below 80 characters where easily possible.

And please leave empty lines before and after your text, your mails
are currently unnecessarily hard to read.

Diego

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

 review disapprove

On Fri, Feb 10, 2012 at 02:24:28PM +0000, Xin wrote:
> Xin has proposed merging lp:~hipl-core/hipl/libhip into lp:hipl.
>
> Requested reviews:
> Diego Biurrun (diego-biurrun)
> Xin (eric-nevup)
> Miika Komu (miika-iki)

You are requesting review from yourself and not all of hipl-core?

> For more details, see:
> https://code.launchpad.net/~hipl-core/hipl/libhip/+merge/92480
>
> Feb 10, 2012 code revised based on Diego's review
>
> --- Makefile.am 2012-02-09 09:30:59 +0000
> +++ Makefile.am 2012-02-10 14:23:36 +0000
> @@ -64,17 +65,21 @@
>
> ### libraries ###
> lib_LTLIBRARIES = lib/core/libhipcore.la
> +lib_LTLIBRARIES += lib/hipdaemon/libhipdaemon.la

OK, time to start with the design review I guess: I don't like the
naming. We previously had this empty lib/ directory that only contains
subdirs and I previously discussed renaming it. Your merge request
makes this all the more urgent.

> @@ -83,58 +88,20 @@
>
> ### source declarations ###
>
> -test_auth_performance_SOURCES = test/auth_performance.c
> -test_certteststub_SOURCES = test/certteststub.c
> -test_dh_performance_SOURCES = test/dh_performance.c
> -test_fw_port_bindings_performance_SOURCES = hipfw/file_buffer.c \
> - hipfw/line_parser.c \
> - hipfw/port_bindings.c \
> - test/fw_port_bindings_performance.c
> -test_hc_performance_SOURCES = test/hc_performance.c
> +test_auth_performance_SOURCES = test/auth_performance.c
> +test_certteststub_SOURCES = test/certteststub.c
> +test_dh_performance_SOURCES = test/dh_performance.c
> +test_fw_port_bindings_performance_SOURCES = hipfw/file_buffer.c \
> + hipfw/line_parser.c \
> + hipfw/port_bindings.c \
> + test/fw_port_bindings_performance.c
> +test_hc_performance_SOURCES = test/hc_performance.c
> +test_hipnetcat_SOURCES = test/hipnetcat.c

This was better before. Please avoid senseless and unrelated cosmetic
changes.

> @@ -164,8 +131,7 @@
> hipfw/user_ipsec_api.c \
> hipfw/user_ipsec_esp.c \
> hipfw/user_ipsec_fw_msg.c \
> - hipfw/user_ipsec_sadb.c \
> - modules/midauth/lib/midauth_builder.c
> + hipfw/user_ipsec_sadb.c

This reminds me of this whole modularity nonsense - not related to your
merge request of course...

> @@ -244,21 +255,24 @@
>
> ### static library dependencies ###
>
> -hipd_hipd_LDADD = lib/core/libhipcore.la
> -hipfw_hipfw_LDADD = lib/core/libhipcore.la
> +hipd_hipd_LDADD = lib/hipdaemon/libhipdaemon.la
> +hipfw_hipfw_LDADD ...

review: Disapprove
lp:~hipl-core/hipl/libhip updated
4869. By Xin

Code revise based on Diego's 2nd review.

4870. By Xin

Revise document style.

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

On 12/02/12 15:25, Diego Biurrun wrote:
> review reject
>
> On Fri, Feb 10, 2012 at 02:24:28PM +0000, Xin wrote:
>> Xin has proposed merging lp:~hipl-core/hipl/libhip into lp:hipl.
>>
>> Requested reviews:
>> Diego Biurrun (diego-biurrun)
>> Xin (eric-nevup)
>> Miika Komu (miika-iki)
> You are requesting review from yourself and not all of hipl-core?

I explained why I have done this in previous mail, anyway, i will setup
a new review next time. I am still learning the behaviors of Launchpad...

>> @@ -83,58 +88,20 @@
>>
>> ### source declarations ###
>>
>> -test_auth_performance_SOURCES = test/auth_performance.c
>> -test_certteststub_SOURCES = test/certteststub.c
>> -test_dh_performance_SOURCES = test/dh_performance.c
>> -test_fw_port_bindings_performance_SOURCES = hipfw/file_buffer.c \
>> - hipfw/line_parser.c \
>> - hipfw/port_bindings.c \
>> - test/fw_port_bindings_performance.c
>> -test_hc_performance_SOURCES = test/hc_performance.c
>> +test_auth_performance_SOURCES = test/auth_performance.c
>> +test_certteststub_SOURCES = test/certteststub.c
>> +test_dh_performance_SOURCES = test/dh_performance.c
>> +test_fw_port_bindings_performance_SOURCES = hipfw/file_buffer.c \
>> + hipfw/line_parser.c \
>> + hipfw/port_bindings.c \
>> + test/fw_port_bindings_performance.c
>> +test_hc_performance_SOURCES = test/hc_performance.c
>> +test_hipnetcat_SOURCES = test/hipnetcat.c
> This was better before. Please avoid senseless and unrelated cosmetic
> changes.

Fixed

>> @@ -244,21 +255,24 @@
>>
>> ### static library dependencies ###
>>
>> -hipd_hipd_LDADD = lib/core/libhipcore.la
>> -hipfw_hipfw_LDADD = lib/core/libhipcore.la
>> +hipd_hipd_LDADD = lib/hipdaemon/libhipdaemon.la
>> +hipfw_hipfw_LDADD = lib/hipdaemon/libhipdaemon.la
> Why does the firewall depend on libhipdaemon?

Because modules/midauth/hipd/midauth.c is used both for building fw and
libdaemon. Well, I am not an autotool expert, perhaps it is not the best
solution.

>
>> --- doc/HOWTO.xml.in 2012-01-25 10:44:48 +0000
>> +++ doc/HOWTO.xml.in 2012-02-10 14:23:36 +0000
>> @@ -764,6 +764,58 @@
>> </para>
>> </section> <!-- handover -->
>>
>> +<section id="ch_hipnetcat">
>> +<title>Test HIPL by the hipnetcat program</title>
> s/by/with/
>
>> +<para>
>> + The hipnetcat can be used to test the base exchange functionality of the HIPL without
> s/The //
>
>> + installing the HIPL binary. This section explains its usage in detail.
> HIPL is the name of the project, not of the binary.
>
>> +<para>
>> + The hipnetcat program takes similar parameters as the normal netcat program. In
>> + order to check the functionality of...

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

On Sun, Feb 12, 2012 at 10:33:00PM +0200, Xin Gu wrote:
> On 12/02/12 15:25, Diego Biurrun wrote:
> >
> >On Fri, Feb 10, 2012 at 02:24:28PM +0000, Xin wrote:
> >>Xin has proposed merging lp:~hipl-core/hipl/libhip into lp:hipl.
> >>
> >>@@ -244,21 +255,24 @@
> >>
> >> ### static library dependencies ###
> >>
> >>-hipd_hipd_LDADD = lib/core/libhipcore.la
> >>-hipfw_hipfw_LDADD = lib/core/libhipcore.la
> >>+hipd_hipd_LDADD = lib/hipdaemon/libhipdaemon.la
> >>+hipfw_hipfw_LDADD = lib/hipdaemon/libhipdaemon.la
> >Why does the firewall depend on libhipdaemon?
>
> Because modules/midauth/hipd/midauth.c is used both for building fw
> and libdaemon. Well, I am not an autotool expert, perhaps it is not
> the best solution.

This is not related to autotools at all. Suddenly you need to link
the firewall against all of hipd. What symbols did you move and why
did you move it to the libhipdaemon code and not to the common code.

> >>--- hipd/esp_prot_hipd_msg.c 2011-12-16 13:37:33 +0000
> >>+++ lib/hipdaemon/esp_prot_hipd_msg.c 2012-02-10 14:23:36 +0000
> >>@@ -57,6 +57,10 @@
> >>
> >>+int esp_prot_active = 0;
> >>+int esp_prot_num_transforms = 0;
> >>+long esp_prot_num_parallel_hchains = 0;
> >This looks suspicious. Why are you moving these global variables, but
> >not the extern declarations?
> >
> >Why is this part of this merge request and split off and done separately?
> >The same applies to all the other similar changes. I'm not convinced you
> >need to do them (here).
>
> Because HIPL daemon and libhip both link to libhipdaemon.la, but
> hipd.c is not part of libhipdaemon, so those global variables have
> been moved to other files otherwise libhip can not access them.

Why did you move them to these files in the first place?

> >>--- hipd/hadb.c 2012-01-25 20:45:27 +0000
> >>+++ lib/hipdaemon/hadb.c 2012-02-10 14:23:36 +0000
> >>@@ -105,6 +105,23 @@
> >>
> >>+/* Flag to show if hipl is running in libhip mode (=1) or normal mode (=0).
> >>+ * This variable should NOT be accessed directly. Always use the accessor
> >>+ * functions instead.
> >>+ */
> >>+static int hipd_library_mode = 0;
> >>+
> >>+int is_libhip_mode()
> >>+{
> >>+ return hipd_library_mode;
> >>+}
> >>+
> >>+int set_libhip_mode()
> >>+{
> >>+ hipd_library_mode = 1;
> >>+ return 0;
> >>+}
> >Why in this file?
>
> I moved them to init.c, also rename them with a "hip_" prefix since
> they are global.

Slightly unrelated, but hip_ is not a good prefix, it should be hipl_
if at all.

> >>--- hipd/init.c 2012-01-18 21:21:26 +0000
> >>+++ lib/hipdaemon/init.c 2012-02-10 14:23:36 +0000
> >>@@ -74,12 +75,14 @@
> >> #include "accessor.h"
> >> #include "close.h"
> >> #include "dh.h"
> >>+#include "esp_prot_hipd_msg.h"
> >> #include "esp_prot_light_update.h"
> >> #include "hadb.h"
> >> #include "hidb.h"
> >> #include "hip_socket.h"
> >> #include "hipd.h"
> >> #include "hiprelay.h"
> >>+#include "init.h"
> >> #include "input.h"
> >> #include "maintenance.h"
> >> #include "nat.h"
> >>@@ -88,9 +91,8 @@
> >> #include "output.h"
> >> #include "pkt_handling...

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

On 13/02/12 00:26, Diego Biurrun wrote:
> On Sun, Feb 12, 2012 at 10:33:00PM +0200, Xin Gu wrote:
>> On 12/02/12 15:25, Diego Biurrun wrote:
>>> On Fri, Feb 10, 2012 at 02:24:28PM +0000, Xin wrote:
>>>> Xin has proposed merging lp:~hipl-core/hipl/libhip into lp:hipl.
>>>>
>>>> @@ -244,21 +255,24 @@
>>>>
>>>> ### static library dependencies ###
>>>>
>>>> -hipd_hipd_LDADD = lib/core/libhipcore.la
>>>> -hipfw_hipfw_LDADD = lib/core/libhipcore.la
>>>> +hipd_hipd_LDADD = lib/hipdaemon/libhipdaemon.la
>>>> +hipfw_hipfw_LDADD = lib/hipdaemon/libhipdaemon.la
>>> Why does the firewall depend on libhipdaemon?
>> Because modules/midauth/hipd/midauth.c is used both for building fw
>> and libdaemon. Well, I am not an autotool expert, perhaps it is not
>> the best solution.
> This is not related to autotools at all. Suddenly you need to link
> the firewall against all of hipd. What symbols did you move and why
> did you move it to the libhipdaemon code and not to the common code.

In trunk, the modules/midauth/hipd/midauth.c is used to build both hipd
and hipfw. They are both executable, so it is fine. Now since this file
goes to libhipdeamon, a share library, autotools will complain conflict
because it is used both for share lib and executable.

>>>> --- hipd/esp_prot_hipd_msg.c 2011-12-16 13:37:33 +0000
>>>> +++ lib/hipdaemon/esp_prot_hipd_msg.c 2012-02-10 14:23:36 +0000
>>>> @@ -57,6 +57,10 @@
>>>>
>>>> +int esp_prot_active = 0;
>>>> +int esp_prot_num_transforms = 0;
>>>> +long esp_prot_num_parallel_hchains = 0;
>>> This looks suspicious. Why are you moving these global variables, but
>>> not the extern declarations?
>>>
>>> Why is this part of this merge request and split off and done separately?
>>> The same applies to all the other similar changes. I'm not convinced you
>>> need to do them (here).
>> Because HIPL daemon and libhip both link to libhipdaemon.la, but
>> hipd.c is not part of libhipdaemon, so those global variables have
>> been moved to other files otherwise libhip can not access them.
> Why did you move them to these files in the first place?

Those moves come from previous work in libhip branch. I think it is
reasonable, if you consider to share code between hipd and libhip.

>
>>>> --- hipd/hadb.c 2012-01-25 20:45:27 +0000
>>>> +++ lib/hipdaemon/hadb.c 2012-02-10 14:23:36 +0000
>>>> @@ -105,6 +105,23 @@
>>>>
>>>> +/* Flag to show if hipl is running in libhip mode (=1) or normal mode (=0).
>>>> + * This variable should NOT be accessed directly. Always use the accessor
>>>> + * functions instead.
>>>> + */
>>>> +static int hipd_library_mode = 0;
>>>> +
>>>> +int is_libhip_mode()
>>>> +{
>>>> + return hipd_library_mode;
>>>> +}
>>>> +
>>>> +int set_libhip_mode()
>>>> +{
>>>> + hipd_library_mode = 1;
>>>> + return 0;
>>>> +}
>>> Why in this file?
>> I moved them to init.c, also rename them with a "hip_" prefix since
>> they are global.
> Slightly unrelated, but hip_ is not a good prefix, it should be hipl_
> if at all.

Ok, I will change all of those prefix staff related to this merge then.

>
>>>> @@ -8...

Read more...

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

> if ((pid = fork())> 0) {
> serv_pid = pid;
> printf("server pid: %d\n", serv_pid);
> }
>
> if (pid == 0) {
> if (execv("test/hipnetcat", serv_argv)) {
> perror("execv");
> return;
> }
> }
>
> you mean&& this two if statements? I am confused, they are 2 processes
> here I don't know how can you do that...
>
Forget about what I am saying here, I get your point now :)

Xin

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

On Mon, Feb 13, 2012 at 10:39:38AM +0200, Xin Gu wrote:
> On 13/02/12 00:26, Diego Biurrun wrote:
> >On Sun, Feb 12, 2012 at 10:33:00PM +0200, Xin Gu wrote:
> >>On 12/02/12 15:25, Diego Biurrun wrote:
> >>>On Fri, Feb 10, 2012 at 02:24:28PM +0000, Xin wrote:
> >>>>Xin has proposed merging lp:~hipl-core/hipl/libhip into lp:hipl.
> >>>>
> >>>>@@ -244,21 +255,24 @@
> >>>>
> >>>> ### static library dependencies ###
> >>>>
> >>>>-hipd_hipd_LDADD = lib/core/libhipcore.la
> >>>>-hipfw_hipfw_LDADD = lib/core/libhipcore.la
> >>>>+hipd_hipd_LDADD = lib/hipdaemon/libhipdaemon.la
> >>>>+hipfw_hipfw_LDADD = lib/hipdaemon/libhipdaemon.la
> >>>Why does the firewall depend on libhipdaemon?
> >>Because modules/midauth/hipd/midauth.c is used both for building fw
> >>and libdaemon. Well, I am not an autotool expert, perhaps it is not
> >>the best solution.
> >This is not related to autotools at all. Suddenly you need to link
> >the firewall against all of hipd. What symbols did you move and why
> >did you move it to the libhipdaemon code and not to the common code.
>
> In trunk, the modules/midauth/hipd/midauth.c is used to build both
> hipd and hipfw.

No.

> >>>>--- hipd/esp_prot_hipd_msg.c 2011-12-16 13:37:33 +0000
> >>>>+++ lib/hipdaemon/esp_prot_hipd_msg.c 2012-02-10 14:23:36 +0000
> >>>>@@ -57,6 +57,10 @@
> >>>>
> >>>>+int esp_prot_active = 0;
> >>>>+int esp_prot_num_transforms = 0;
> >>>>+long esp_prot_num_parallel_hchains = 0;
> >>>This looks suspicious. Why are you moving these global variables, but
> >>>not the extern declarations?
> >>>
> >>>Why is this part of this merge request and split off and done separately?
> >>>The same applies to all the other similar changes. I'm not convinced you
> >>>need to do them (here).
> >>Because HIPL daemon and libhip both link to libhipdaemon.la, but
> >>hipd.c is not part of libhipdaemon, so those global variables have
> >>been moved to other files otherwise libhip can not access them.
> >Why did you move them to these files in the first place?
>
> Those moves come from previous work in libhip branch. I think it is
> reasonable, if you consider to share code between hipd and libhip.

If those moves make sense at all, then they make sense outside of this
merge request and should be submitted separately.

> >>>>@@ -1090,6 +1298,68 @@
> >>>>
> >>>>+int libhipd_init(void)
> >>>>+{
> >>>>+ set_libhip_mode();
> >>>>+ hip_nat_status = 1;
> >>>>+#ifdef CONFIG_HIP_FIREWALL
> >>>>+ hipfw_status = 0;
> >>>>+#endif
> >>>You never tested in a setup with the firewall enabled.
> >>>
> >>>I'm getting more and more sceptical of this whole merge proposal.
> >>>It has obviously seen little to no testing, at least outside of
> >>>the very basic standard configuration that it worked in.
> >>>
> >>>It seems that the previous iteration was never compiled. What sort of
> >>>testing did you do? Does this pass 'make alltests'? Does it pass the
> >>>autobuilder?
> >>This part of code is unchanged since I started to work on the libhip
> >>branch. The reason of this #ifdef is that hipfw cannot handle the
> ...

Read more...

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

On 13/02/12 11:25, Diego Biurrun wrote:
> On Mon, Feb 13, 2012 at 10:39:38AM +0200, Xin Gu wrote:
>> On 13/02/12 00:26, Diego Biurrun wrote:
>>> On Sun, Feb 12, 2012 at 10:33:00PM +0200, Xin Gu wrote:
>>>> On 12/02/12 15:25, Diego Biurrun wrote:
>>>>> On Fri, Feb 10, 2012 at 02:24:28PM +0000, Xin wrote:
>>>>>> Xin has proposed merging lp:~hipl-core/hipl/libhip into lp:hipl.
>>>>>>
>>>>>> @@ -244,21 +255,24 @@
>>>>>>
>>>>>> ### static library dependencies ###
>>>>>>
>>>>>> -hipd_hipd_LDADD = lib/core/libhipcore.la
>>>>>> -hipfw_hipfw_LDADD = lib/core/libhipcore.la
>>>>>> +hipd_hipd_LDADD = lib/hipdaemon/libhipdaemon.la
>>>>>> +hipfw_hipfw_LDADD = lib/hipdaemon/libhipdaemon.la
>>>>> Why does the firewall depend on libhipdaemon?
>>>> Because modules/midauth/hipd/midauth.c is used both for building fw
>>>> and libdaemon. Well, I am not an autotool expert, perhaps it is not
>>>> the best solution.
>>> This is not related to autotools at all. Suddenly you need to link
>>> the firewall against all of hipd. What symbols did you move and why
>>> did you move it to the libhipdaemon code and not to the common code.
>> In trunk, the modules/midauth/hipd/midauth.c is used to build both
>> hipd and hipfw.
> No.

My unreliable memory... The file causes the conflict should be:
modules/midauth/lib/midauth_builder.c, sorry

>>>>>> --- hipd/esp_prot_hipd_msg.c 2011-12-16 13:37:33 +0000
>>>>>> +++ lib/hipdaemon/esp_prot_hipd_msg.c 2012-02-10 14:23:36 +0000
>>>>>> @@ -57,6 +57,10 @@
>>>>>>
>>>>>> +int esp_prot_active = 0;
>>>>>> +int esp_prot_num_transforms = 0;
>>>>>> +long esp_prot_num_parallel_hchains = 0;
>>>>> This looks suspicious. Why are you moving these global variables, but
>>>>> not the extern declarations?
>>>>>
>>>>> Why is this part of this merge request and split off and done separately?
>>>>> The same applies to all the other similar changes. I'm not convinced you
>>>>> need to do them (here).
>>>> Because HIPL daemon and libhip both link to libhipdaemon.la, but
>>>> hipd.c is not part of libhipdaemon, so those global variables have
>>>> been moved to other files otherwise libhip can not access them.
>>> Why did you move them to these files in the first place?
>> Those moves come from previous work in libhip branch. I think it is
>> reasonable, if you consider to share code between hipd and libhip.
> If those moves make sense at all, then they make sense outside of this
> merge request and should be submitted separately.
>

But before we have this libhip staff, we didn't have the requirement to
share those codes. IMO keeping them in merge can help us to track why we
move those variables in the future.

Xin

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

On Mon, Feb 13, 2012 at 12:23:03PM +0200, Xin Gu wrote:
> On 13/02/12 11:25, Diego Biurrun wrote:
> >On Mon, Feb 13, 2012 at 10:39:38AM +0200, Xin Gu wrote:
> >>On 13/02/12 00:26, Diego Biurrun wrote:
> >>>On Sun, Feb 12, 2012 at 10:33:00PM +0200, Xin Gu wrote:
> >>>>On 12/02/12 15:25, Diego Biurrun wrote:
> >>>>>On Fri, Feb 10, 2012 at 02:24:28PM +0000, Xin wrote:
> >>>>>>Xin has proposed merging lp:~hipl-core/hipl/libhip into lp:hipl.
> >>>>>>
> >>>>>>@@ -244,21 +255,24 @@
> >>>>>>
> >>>>>> ### static library dependencies ###
> >>>>>>
> >>>>>>-hipd_hipd_LDADD = lib/core/libhipcore.la
> >>>>>>-hipfw_hipfw_LDADD = lib/core/libhipcore.la
> >>>>>>+hipd_hipd_LDADD = lib/hipdaemon/libhipdaemon.la
> >>>>>>+hipfw_hipfw_LDADD = lib/hipdaemon/libhipdaemon.la
> >>>>>Why does the firewall depend on libhipdaemon?
> >>>>Because modules/midauth/hipd/midauth.c is used both for building fw
> >>>>and libdaemon. Well, I am not an autotool expert, perhaps it is not
> >>>>the best solution.
> >>>This is not related to autotools at all. Suddenly you need to link
> >>>the firewall against all of hipd. What symbols did you move and why
> >>>did you move it to the libhipdaemon code and not to the common code.
> >>In trunk, the modules/midauth/hipd/midauth.c is used to build both
> >>hipd and hipfw.
> >No.
>
> My unreliable memory... The file causes the conflict should be:
> modules/midauth/lib/midauth_builder.c, sorry

And that was a *huge* bug that you amplified a hundredfold in this
merge request. Fixed. hipfw does not and must not depend on hipd
code. Your build system changes must reflect this and you must not
make hipfw depend on hipd with "autotools" handwaving.

> >>>>>>--- hipd/esp_prot_hipd_msg.c 2011-12-16 13:37:33 +0000
> >>>>>>+++ lib/hipdaemon/esp_prot_hipd_msg.c 2012-02-10 14:23:36 +0000
> >>>>>>@@ -57,6 +57,10 @@
> >>>>>>
> >>>>>>+int esp_prot_active = 0;
> >>>>>>+int esp_prot_num_transforms = 0;
> >>>>>>+long esp_prot_num_parallel_hchains = 0;
> >>>>>This looks suspicious. Why are you moving these global variables, but
> >>>>>not the extern declarations?
> >>>>>
> >>>>>Why is this part of this merge request and split off and done separately?
> >>>>>The same applies to all the other similar changes. I'm not convinced you
> >>>>>need to do them (here).
> >>>>Because HIPL daemon and libhip both link to libhipdaemon.la, but
> >>>>hipd.c is not part of libhipdaemon, so those global variables have
> >>>>been moved to other files otherwise libhip can not access them.
> >>>Why did you move them to these files in the first place?
> >>Those moves come from previous work in libhip branch. I think it is
> >>reasonable, if you consider to share code between hipd and libhip.
> >If those moves make sense at all, then they make sense outside of this
> >merge request and should be submitted separately.
>
> But before we have this libhip staff, we didn't have the requirement
> to share those codes. IMO keeping them in merge can help us to track
> why we move those variables in the future.

We have Bazaar to track why me make changes.

I can ...

Read more...

Revision history for this message
Christof Mroz (christof-mroz) wrote :
Download full text (19.5 KiB)

Implementation looks good overall, and splitting the code should make HIPL more flexible for users.
You may ignore my comments about code that was not newly written but just moved, of course.

> === modified file 'lib/core/hostid.c'
> --- lib/core/hostid.c 2011-11-10 10:35:47 +0000
> +++ lib/core/hostid.c 2012-02-12 20:26:22 +0000
> @@ -743,11 +744,16 @@
> goto out_err;
> }
> } else if (!use_default) {
> + char *hi_file_dup = strdup(hi_file);
> + if ((err = check_and_create_dir(dirname(hi_file_dup), HIP_DIR_MODE))) {
> + HIP_ERROR("Could not create direcory for path: %s\n", hi_file);
> + goto out_err;
> + }

You allocate a copy using strdup(), but I don't see where you free() it.

> === modified file 'lib/core/linkedlist.c'
> --- lib/core/linkedlist.c 2011-08-15 14:11:56 +0000
> +++ lib/core/linkedlist.c 2012-02-12 20:26:22 +0000
> @@ -298,6 +298,31 @@
> }
>
> /**
> + * Deletes the first node in a list with the given element as its data.
> + * If there is no match, does nothing.
> + *
> + * @param linkedlist list to remoe an element from
> + * @param ptr pointer by which to identify the node
> + * @param free_element a function pointer to a function for freeing the memory
> + * allocated for an element at a node or NULL if the element
> + * itself is not to be freed.
> + */
> +void hip_ll_del_by_ptr(struct hip_ll *linkedlist, void *ptr,
> + free_elem_fn free_element)
> +{
> + int index = 0;
> + const struct hip_ll_node *current = NULL;
> +
> + while ((current = hip_ll_iterate(linkedlist, current))) {
> + if (current->ptr == ptr) {
> + hip_ll_del(linkedlist, index, free_element);
> + return;
> + }
> + index++;
> + }
> +}

You found the link already, so traversing the list again, using an integer index, is redundant.

> === modified file 'lib/core/message.c'
> --- lib/core/message.c 2011-11-08 15:25:41 +0000
> +++ lib/core/message.c 2012-02-12 20:26:22 +0000
> @@ -1,5 +1,5 @@
> /*
> - * Copyright (c) 2010 Aalto University and RWTH Aachen University.
> + * Copyright (c) 2010-2012 Aalto University and RWTH Aachen University.
> *
> * Permission is hereby granted, free of charge, to any person
> * obtaining a copy of this software and associated documentation
> @@ -686,6 +686,79 @@
> }
>
> /**
> + * Read a control message over TCP socket.
> + *
> + * @param sockfd a socket file descriptor
> + * @param ctx a pointer to the packet context
> + * @return -1 in case of an error, 0 otherwise.
> + */
> +int hip_read_control_msg_tcp(int sockfd, struct hip_packet_context *ctx)
> +{
> + int len, is_ipv4;
> + struct sockaddr dst_addr = { 0 };
> + struct sockaddr src_addr = { 0 };
> + struct sockaddr_in *saddr4;
> + struct sockaddr_in6 *saddr6;
> + socklen_t saddr_len = sizeof(struct sockaddr);
> +
> + hip_msg_init(ctx->input_msg);
> +
> + len = recv(sockfd, ctx->input_msg, HIP_MAX_PACKET, 0);
> + ...

review: Needs Fixing
Revision history for this message
Christof Mroz (christof-mroz) wrote :
Download full text (5.0 KiB)

On 14.02.2012 21:34, Christof Mroz wrote:
> +/**
> > + * Receive data from the peer associated with a socket.
> > + * Waits for base exchange if no host association exists.
> > + *
> > + * @note Data is currently sent unencrypted.
> > + *
> > + * @param fd file descriptor of the socket to receive from
> > + * @param buf buffer for received data
> > + * @param len size of buf
> > + * @param flags recvfrom() flags
> > + * @param addr buffer for the associated peer HIT
> > + * @param addr_len size of dst_hit
> > + * @return number of bytes received on success, -1 otherwise
> > + */
> > +int hip_recvfrom(int fd, void *buf, size_t len, int flags,
> > + struct sockaddr *addr, socklen_t *addr_len)
> > +{
> > + int err = 0;
> > + socklen_t socklen = *addr_len;
> > + struct sockaddr_in6 *peer_hit = (struct sockaddr_in6 *) addr;
> > + struct in6_addr peer_addr = { { { 0 } } };
> > + struct in6_addr *peer_addr6;
> > + struct in_addr *peer_addr4;
> > + struct hip_fd_info *fd_info = NULL;
> > + struct hip_packet_context ctx = { 0 };
> > + int (*read_control_msg)(int, struct hip_packet_context *, int) = NULL;
> > +
> > +
> > + if ((fd_info = hip_socket_get_info(fd)) == NULL) {
> > + HIP_ERROR("Fd %d is not a hip socket, exiting.\n", fd);
> > + return -1;
> > + }
> > +
> > + /* Bind to a ephemeral port if the src port hasn't been bound yet */
> > + if (fd_info->bound_port == 0) {
> > + if (auto_bind(fd_info)) {
> > + HIP_ERROR("Fail to bind the hip socket.\n");
> > + return -1;
> > + }
> > + }
> > +
> > + /* Handle BEX if HA hasn't establised */
> > + if (!fd_info->ha) {
> > + if (hip_await_bex(fd_info, addr)) {
> > + HIP_ERROR("Base exchange not successful.\n");
> > + return -1;
> > + }
> > + }
> > +
> > + ctx.input_msg = hip_msg_alloc();
> > + ctx.output_msg = hip_msg_alloc();
> > + read_control_msg = fd_info->family == AF_INET ? hip_read_control_msg_v4
> > + : hip_read_control_msg_v6;
> > +
> > + /* Loop until we get a non-control packet or a CLOSE packet */
> > + while (fd_info->ha->state == HIP_STATE_ESTABLISHED) {
> > + err = recvfrom(fd, buf, len, flags | MSG_PEEK, addr,&socklen);
> > + HIP_DEBUG("Peek packet len: %d\n", err);
> > + HIP_DEBUG("peer sockaddr: AF = %d, socklen = %d\n", addr->sa_family, socklen);
> > + if (err< 0) {
> > + perror("recvfrom");
> > + }
> > +
> > + /* Drop the packet if it doesn't come from the address associated
> > + * with the correct peer. */
> > + if (fd_info->proto == IPPROTO_UDP) {
> > + if (addr->sa_family == AF_INET) {
> > + peer_addr4 =&((struct sockaddr_in *) addr)->sin_addr;
> > + IPV4_TO_IPV6_MAP(peer_addr4,&peer_addr);
> > + peer_addr6 =&peer_addr;
> > + } else {
> > ...

Read more...

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

On 13/02/12 13:33, Diego Biurrun wrote:
> And that was a *huge* bug that you amplified a hundredfold in this
> merge request. Fixed. hipfw does not and must not depend on hipd
> code. Your build system changes must reflect this and you must not
> make hipfw depend on hipd with "autotools" handwaving.
>
>

The diff result below shows one approach I find for this issue.
The idea is building midauth_builder.c into an intermediate lib and
later linking it to both libhipdaemon.la and hipfw.
Do you think it is ok?

=== modified file 'Makefile.am'
--- Makefile.am 2012-02-12 20:20:06 +0000
+++ Makefile.am 2012-02-15 13:53:36 +0000
@@ -66,6 +66,7 @@
  ### libraries ###
  lib_LTLIBRARIES = lib/core/libhipcore.la
  lib_LTLIBRARIES += lib/hipdaemon/libhipdaemon.la
+noinst_LTLIBRARIES = libmidauthbuilder.la

  ### tests ###
@@ -208,7 +209,6 @@

lib/hipdaemon/user_ipsec_sadb_api.c \

modules/heartbeat/hipd/heartbeat.c \

modules/heartbeat_update/hipd/hb_update.c \
-
modules/midauth/lib/midauth_builder.c \

modules/update/hipd/update.c \

modules/update/hipd/update_builder.c \

modules/update/hipd/update_locator.c \
@@ -217,7 +217,10 @@
  lib_hipdaemon_libhipdaemon_la_SOURCES =
$(lib_hipdaemon_libhipdaemon_la_sources) \
                                          modules/midauth/hipd/midauth.c

-lib_hipdaemon_libhipdaemon_la_LIBADD = lib/core/libhipcore.la
+libmidauthbuilder_la_SOURCES = modules/midauth/lib/midauth_builder.c
+
+lib_hipdaemon_libhipdaemon_la_LIBADD = lib/core/libhipcore.la \
+ libmidauthbuilder.la

  test_check_hipd_SOURCES = test/check_hipd.c \
                                test/hipd/lsidb.c
@@ -255,7 +258,8 @@
  ### static library dependencies ###

  hipd_hipd_LDADD = lib/hipdaemon/libhipdaemon.la
-hipfw_hipfw_LDADD = lib/hipdaemon/libhipdaemon.la
+hipfw_hipfw_LDADD = lib/core/libhipcore.la \
+ libmidauthbuilder.la
  test_auth_performance_LDADD = lib/core/libhipcore.la
  test_check_hipd_LDADD = lib/hipdaemon/libhipdaemon.la
  test_check_hipfw_LDADD = lib/hipdaemon/libhipdaemon.la

Xin

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

Remove useless declarations and definitions.
Found during merge process

4872. By Xin

Change prefix of new API to "hipl_"

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

On Wed, Feb 15, 2012 at 04:03:24PM +0200, Xin Gu wrote:
> On 13/02/12 13:33, Diego Biurrun wrote:
> >And that was a *huge* bug that you amplified a hundredfold in this
> >merge request. Fixed. hipfw does not and must not depend on hipd
> >code. Your build system changes must reflect this and you must not
> >make hipfw depend on hipd with "autotools" handwaving.
>
> The diff result below shows one approach I find for this issue.
> The idea is building midauth_builder.c into an intermediate lib and
> later linking it to both libhipdaemon.la and hipfw.
> Do you think it is ok?

No, it does not. Now there is one extra, completeley senseless, library
containing only one object file. Do you realize we already build a library
with the common code (libhipcore)? That's where common code should go, not
in second library for common code. Also, I already fixed the issue.

Diego

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

On 16/02/12 13:02, Diego Biurrun wrote:
> On Wed, Feb 15, 2012 at 04:03:24PM +0200, Xin Gu wrote:
>> On 13/02/12 13:33, Diego Biurrun wrote:
>>> And that was a *huge* bug that you amplified a hundredfold in this
>>> merge request. Fixed. hipfw does not and must not depend on hipd
>>> code. Your build system changes must reflect this and you must not
>>> make hipfw depend on hipd with "autotools" handwaving.
>> The diff result below shows one approach I find for this issue.
>> The idea is building midauth_builder.c into an intermediate lib and
>> later linking it to both libhipdaemon.la and hipfw.
>> Do you think it is ok?
> No, it does not. Now there is one extra, completeley senseless, library
> containing only one object file. Do you realize we already build a library
> with the common code (libhipcore)? That's where common code should go, not
> in second library for common code. Also, I already fixed the issue.

Could you tell me where can I find your fix? There is no any new commit
from you in the trunk.

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

Improve efficiency of the new list del function
Reduce 2 times of iteration to 1.
Add test method.

4874. By Xin

Code revising based on the comments of Christof.

4875. By Xin

Sync with trunk 6283.

4876. By Xin

Code revising to align to trunk code.

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

On 14/02/12 22:34, Christof Mroz wrote:
> Review: Needs Fixing
>
> Implementation looks good overall, and splitting the code should make HIPL more flexible for users.
> You may ignore my comments about code that was not newly written but just moved, of course.
>
>> +int hip_accept(int fd, struct sockaddr *new_peer_hit, socklen_t *hit_len)
>> +{
>> + int new_fd;
>> + struct hip_fd_info *fd_info = NULL;
>> + struct hip_fd_info *fd_info_new = NULL;
>> + struct sockaddr_storage ss = { 0 };
>> + socklen_t ss_len = sizeof(struct sockaddr_storage);
>> + struct sockaddr_in6 *phit = NULL;
>> +
>> + if (*hit_len< sizeof(struct sockaddr_in6)) {
>> + return -1;
>> + }
>> +
>> + if ((fd_info = hip_socket_get_info(fd)) == NULL) {
>> + HIP_ERROR("Fd %d is not a hip socket, exiting.\n", fd);
>> + return -1;
>> + }
>> +
>> + new_fd = accept(fd, (struct sockaddr *)&ss,&ss_len);
>> + if (new_fd< 0) {
>> + HIP_PERROR("accept(): ");
>> + return -1;
>> + }
>> +
>> + fd_info_new = create_new_fd_info(new_fd, fd_info->bound_port,
>> + fd_info->family, fd_info->proto);
>> +
>> + if (hip_await_bex(fd_info_new, (struct sockaddr *)&ss)< 0) {
>> + HIP_ERROR("Base exchange not successful.\n");
>> + return -1;
>> + }
>> +
>> + if (new_peer_hit) {
>> + phit = (struct sockaddr_in6 *) new_peer_hit;
>> + memset(phit, 0, *hit_len);
>> + memcpy(&phit->sin6_addr,&fd_info_new->ha->hit_peer,
>> + sizeof(struct in6_addr));
>> + phit->sin6_port = get_port_from_saddr((struct sockaddr *)&ss);
>> + *hit_len = sizeof(struct sockaddr_in6);
>> + }
>> +
>> + return new_fd;
>> +}
> fd_info_new is not freed.

Those fd_infoS are maintained in a global list, so cannot be freed here.
the function hipl_close (previously hip_close) handle the case of free
fd_info.

>
>> + /* check server& client status */
>> + for (i = 0; i< TEST_HIPNC_TIMEOUT; i++) {
>> + tv.tv_sec = 1;
>> + tv.tv_usec = 0;
>> + select(0, NULL, NULL, NULL,&tv);
>> + round = remain_cld;
>> + for (j = 0; j< round; j++) {
>> + pid = waitpid(-1,&status, WNOHANG);
>> + fail_if(pid> 0&& status != 0, "hipnetcat failed");
>> + if (pid> 0&& status == 0) {
>> + remain_cld--;
>> + if (remain_cld == 0) {
>> + break;
>> + }
>> + }
>> + }
>> + }
>> +
>> + fail_if(remain_cld> 0, "hipnetcat test timeout!");
>> +}
> I guess that the daemon and firewall store much (all?) of their state statically, and this forces you to spawn separate processes even though that's an ugly thing to do in a test (which should be self-contained without side effects), correct?
>
> For completeness' sake I'd suggest an OpenGL-ish approach: simply provide two functions that retrieve or store the global state, respectively. Only those portions relevant to library users, of course.
> Then you can restructure the test as follows
>
> void *...

Read more...

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

On Thu, Feb 16, 2012 at 01:39:53PM +0200, Xin Gu wrote:
> On 16/02/12 13:02, Diego Biurrun wrote:
> >On Wed, Feb 15, 2012 at 04:03:24PM +0200, Xin Gu wrote:
> >>On 13/02/12 13:33, Diego Biurrun wrote:
> >>>And that was a *huge* bug that you amplified a hundredfold in this
> >>>merge request. Fixed. hipfw does not and must not depend on hipd
> >>>code. Your build system changes must reflect this and you must not
> >>>make hipfw depend on hipd with "autotools" handwaving.
> >>The diff result below shows one approach I find for this issue.
> >>The idea is building midauth_builder.c into an intermediate lib and
> >>later linking it to both libhipdaemon.la and hipfw.
> >>Do you think it is ok?
> >No, it does not. Now there is one extra, completeley senseless, library
> >containing only one object file. Do you realize we already build a library
> >with the common code (libhipcore)? That's where common code should go, not
> >in second library for common code. Also, I already fixed the issue.
>
> Could you tell me where can I find your fix? There is no any new
> commit from you in the trunk.

I committed it, but forgot to push. Done now.

Diego

Revision history for this message
Christof Mroz (christof-mroz) wrote :

On 17.02.2012 17:49, Xin wrote:
> On 14/02/12 22:34, Christof Mroz wrote:
>>> + fd_info_new = create_new_fd_info(new_fd, fd_info->bound_port,
>>> + fd_info->family, fd_info->proto);
>>> +
>>> + if (hip_await_bex(fd_info_new, (struct sockaddr *)&ss)< 0) {
>>> + HIP_ERROR("Base exchange not successful.\n");
>>> + return -1;
>>> + }
>>> +
>>> + if (new_peer_hit) {
>>> + phit = (struct sockaddr_in6 *) new_peer_hit;
>>> + memset(phit, 0, *hit_len);
>>> + memcpy(&phit->sin6_addr,&fd_info_new->ha->hit_peer,
>>> + sizeof(struct in6_addr));
>>> + phit->sin6_port = get_port_from_saddr((struct sockaddr *)&ss);
>>> + *hit_len = sizeof(struct sockaddr_in6);
>>> + }
>>> +
>>> + return new_fd;
>>> +}
>> fd_info_new is not freed.
>
> Those fd_infoS are maintained in a global list, so cannot be freed here.
> the function hipl_close (previously hip_close) handle the case of free
> fd_info.

OK, my bad

> This test can verify both libhipl and hipnetcat. The hipnetcat cannot
> act as client and server at the same time (like normal netcat), that's
> why we have 2 processes.

Ah I see now. At first I thought that hipnetcast was written for the
sole purpose of unit testing, and not conceived as a tool for
distribution...
If you want to test hipnetcat itself, the cleanest way would involve
mock functions, both for the sender and listener case. But this would
take some more effort that just forking of course. As I don't know about
the auto builder internals and such, I will let others decide whether
the fork() is acceptable.

In this case I don't see a need for the context management that I
proposed anymore. Unless you think a library user might want to juggle
multiple HIP sockets, and you don't want to rewrite anything so that it
carries the static data inside the socket handle (which would be the
cleanest solution, I guess).

> Your advice gives me a good hint for the improvement of the libhipl API.
> There are still many things to explore, and my graduation relies on it :)

Hopefully I helped rather than distracting you :) there are endless
possibilities for tweaking an API.

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

Sync with trunk 6285

4878. By Xin

cosmetics: revert removed empty lines.

Unmerged revisions

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file '.bzrignore'
2--- .bzrignore 2012-02-13 11:20:22 +0000
3+++ .bzrignore 2012-02-20 08:12:22 +0000
4@@ -61,6 +61,7 @@
5 test/dh_performance
6 test/fw_port_bindings_performance
7 test/hc_performance
8+test/hipnetcat
9 tools/hipconf
10 tools/hipdnskeyparse/hipdnskeyparse
11 tools/hipdnsproxy/hipdnsproxy
12
13=== modified file 'Makefile.am'
14--- Makefile.am 2012-02-13 11:20:22 +0000
15+++ Makefile.am 2012-02-20 08:12:22 +0000
16@@ -35,12 +35,13 @@
17 EXTRA_DIST += $(wildcard $(addprefix $(srcdir)/tools/,*.cfg *.pl *.sh *.xml))
18 EXTRA_DIST += $(wildcard $(addprefix $(srcdir)/hipfw/,*.cfg))
19 EXTRA_DIST += $(HIPL_HEADER_LIST)
20-EXTRA_DIST += hipd/pisa.c hipfw/pisa.c hipfw/pisa_cert.c
21+EXTRA_DIST += lib/hipdaemon/pisa.c hipfw/pisa.c hipfw/pisa_cert.c
22
23 ### user programs ###
24 bin_PROGRAMS = test/auth_performance \
25 test/certteststub \
26- test/hc_performance
27+ test/hc_performance \
28+ test/hipnetcat
29
30 if HIP_PERFORMANCE
31 bin_PROGRAMS += test/dh_performance
32@@ -64,17 +65,21 @@
33
34 ### libraries ###
35 lib_LTLIBRARIES = lib/core/libhipcore.la
36+lib_LTLIBRARIES += lib/hipdaemon/libhipdaemon.la
37
38
39 ### tests ###
40 if HIP_UNITTESTS
41 TESTS = test/check_hipd \
42 test/check_hipfw \
43+ test/check_hipnetcat \
44 test/check_lib_core \
45 test/check_lib_tool \
46 test/check_modules_midauth
47+
48 check_PROGRAMS = test/check_hipd \
49 test/check_hipfw \
50+ test/check_hipnetcat \
51 test/check_lib_core \
52 test/check_lib_tool \
53 test/check_modules_midauth
54@@ -91,49 +96,12 @@
55 hipfw/port_bindings.c \
56 test/fw_port_bindings_performance.c
57 test_hc_performance_SOURCES = test/hc_performance.c
58+test_hipnetcat_SOURCES = test/hipnetcat.c
59
60 tools_hipconf_SOURCES = tools/hipconf.c
61 tools_pisacert_SOURCES = tools/pisacert.c
62
63-hipd_hipd_sources = hipd/accessor.c \
64- hipd/cert.c \
65- hipd/close.c \
66- hipd/configfilereader.c \
67- hipd/cookie.c \
68- hipd/dh.c \
69- hipd/esp_prot_anchordb.c \
70- hipd/esp_prot_hipd_msg.c \
71- hipd/esp_prot_light_update.c \
72- hipd/hadb.c \
73- hipd/hidb.c \
74- hipd/hip_socket.c \
75- hipd/hipd.c \
76- hipd/hiprelay.c \
77- hipd/hit_to_ip.c \
78- hipd/init.c \
79- hipd/input.c \
80- hipd/keymat.c \
81- hipd/lsidb.c \
82- hipd/maintenance.c \
83- hipd/nat.c \
84- hipd/netdev.c \
85- hipd/nsupdate.c \
86- hipd/opp_mode.c \
87- hipd/output.c \
88- hipd/pkt_handling.c \
89- hipd/registration.c \
90- hipd/user.c \
91- hipd/user_ipsec_hipd_msg.c \
92- hipd/user_ipsec_sadb_api.c \
93- modules/heartbeat/hipd/heartbeat.c \
94- modules/heartbeat_update/hipd/hb_update.c \
95- modules/update/hipd/update.c \
96- modules/update/hipd/update_builder.c \
97- modules/update/hipd/update_locator.c \
98- modules/update/hipd/update_param_handling.c
99-
100-hipd_hipd_SOURCES = $(hipd_hipd_sources) \
101- modules/midauth/hipd/midauth.c \
102+hipd_hipd_SOURCES = hipd/hipd.c \
103 hipd/main.c
104
105 dist_sysconf_DATA = hipd/hipd.conf \
106@@ -209,6 +177,47 @@
107 lib_core_libhipcore_la_SOURCES += lib/core/performance.c
108 endif
109
110+lib_hipdaemon_libhipdaemon_la_sources = lib/hipdaemon/accessor.c \
111+ lib/hipdaemon/cert.c \
112+ lib/hipdaemon/close.c \
113+ lib/hipdaemon/configfilereader.c \
114+ lib/hipdaemon/cookie.c \
115+ lib/hipdaemon/dh.c \
116+ lib/hipdaemon/esp_prot_anchordb.c \
117+ lib/hipdaemon/esp_prot_hipd_msg.c \
118+ lib/hipdaemon/esp_prot_light_update.c \
119+ lib/hipdaemon/hadb.c \
120+ lib/hipdaemon/hidb.c \
121+ lib/hipdaemon/hip_socket.c \
122+ lib/hipdaemon/hiprelay.c \
123+ lib/hipdaemon/hit_to_ip.c \
124+ lib/hipdaemon/init.c \
125+ lib/hipdaemon/input.c \
126+ lib/hipdaemon/keymat.c \
127+ lib/hipdaemon/lsidb.c \
128+ lib/hipdaemon/maintenance.c \
129+ lib/hipdaemon/nat.c \
130+ lib/hipdaemon/netdev.c \
131+ lib/hipdaemon/nsupdate.c \
132+ lib/hipdaemon/opp_mode.c \
133+ lib/hipdaemon/output.c \
134+ lib/hipdaemon/pkt_handling.c \
135+ lib/hipdaemon/registration.c \
136+ lib/hipdaemon/socket_wrapper.c \
137+ lib/hipdaemon/user.c \
138+ lib/hipdaemon/user_ipsec_hipd_msg.c \
139+ lib/hipdaemon/user_ipsec_sadb_api.c \
140+ modules/heartbeat/hipd/heartbeat.c \
141+ modules/heartbeat_update/hipd/hb_update.c \
142+ modules/update/hipd/update.c \
143+ modules/update/hipd/update_builder.c \
144+ modules/update/hipd/update_locator.c \
145+ modules/update/hipd/update_param_handling.c
146+
147+lib_hipdaemon_libhipdaemon_la_SOURCES = $(lib_hipdaemon_libhipdaemon_la_sources) \
148+ modules/midauth/hipd/midauth.c
149+
150+lib_hipdaemon_libhipdaemon_la_LIBADD = lib/core/libhipcore.la
151
152 test_check_hipd_SOURCES = test/check_hipd.c \
153 test/hipd/lsidb.c
154@@ -224,10 +233,13 @@
155 test/hipfw/rewrite.c \
156 $(hipfw_hipfw_sources)
157
158+test_check_hipnetcat_SOURCES = test/check_hipnetcat.c
159+
160 test_check_lib_core_SOURCES = test/check_lib_core.c \
161 test/lib/core/crypto.c \
162 test/lib/core/hit.c \
163 test/lib/core/hostid.c \
164+ test/lib/core/linkedlist.c \
165 test/lib/core/solve.c \
166 test/lib/core/straddr.c
167
168@@ -243,21 +255,24 @@
169
170 ### static library dependencies ###
171
172-hipd_hipd_LDADD = lib/core/libhipcore.la
173+hipd_hipd_LDADD = lib/hipdaemon/libhipdaemon.la
174 hipfw_hipfw_LDADD = lib/core/libhipcore.la
175 test_auth_performance_LDADD = lib/core/libhipcore.la
176-test_check_hipd_LDADD = lib/core/libhipcore.la
177+test_check_hipd_LDADD = lib/hipdaemon/libhipdaemon.la
178 test_check_hipfw_LDADD = lib/core/libhipcore.la
179+test_check_hipnetcat_LDADD = lib/hipdaemon/libhipdaemon.la
180 test_check_lib_core_LDADD = lib/core/libhipcore.la
181 test_check_lib_tool_LDADD = lib/core/libhipcore.la
182-test_check_modules_midauth_LDADD = lib/core/libhipcore.la
183+test_check_modules_midauth_LDADD = lib/hipdaemon/libhipdaemon.la
184 test_certteststub_LDADD = lib/core/libhipcore.la
185 test_dh_performance_LDADD = lib/core/libhipcore.la
186 test_fw_port_bindings_performance_LDADD = lib/core/libhipcore.la
187 test_hc_performance_LDADD = lib/core/libhipcore.la
188+test_hipnetcat_LDADD = lib/hipdaemon/libhipdaemon.la
189 tools_hipconf_LDADD = lib/core/libhipcore.la
190 tools_pisacert_LDADD = lib/core/libhipcore.la
191
192+
193 ### dynamic library dependencies ###
194
195 test_check_hipfw_LDFLAGS = -ldl -Wl,-z,muldefs
196
197=== modified file 'doc/HOWTO.xml.in'
198--- doc/HOWTO.xml.in 2012-01-25 10:44:48 +0000
199+++ doc/HOWTO.xml.in 2012-02-20 08:12:22 +0000
200@@ -764,6 +764,80 @@
201 </para>
202 </section> <!-- handover -->
203
204+ <section id="ch_hipnetcat">
205+ <title>Test HIPL with the hipnetcat program</title>
206+ <para>
207+ Hipnetcat can be used to test the base exchange functionality of HIPL.
208+ This section explains its usage in detail.
209+ </para>
210+ <para>
211+ Hipnetcat program takes similar parameters as the normal netcat program.
212+ In order to check the functionality of the base exchange, a hipnetcat
213+ server and a hipnetcat client are required.
214+ </para>
215+ <para>
216+ The usage of hipnetcat is shown as follow:
217+ <programlisting>
218+ Usage: hipnetcat [-hlt] [-p source_port] [-s source_ip_address]
219+ [-d dest_port] [peer_identifier[s]]
220+
221+ -h: help
222+ -l: Listening mode, hip netcat acts as the server side.
223+ -t: Using TCP as transportation protocol, otherwise UDP will be used.
224+ </programlisting>
225+ The last parameter peer_identifiers supports multiple peer indentities
226+ with different types. The types includes: HIT, IP address and host name.
227+ host name.
228+ </para>
229+
230+ <itemizedlist>
231+ <listitem><para>
232+ Example 1: localhost hipnetcat connection via TCP. The server listens on
233+ 127.0.0.1:22300 and the client connects from 127.0.0.1:22345 with HIT
234+ 2001:1c:809e:244a:c33:78fb:45e3:d132.
235+ <programlisting>
236+ The server:
237+ hipnetcat -l -t -s 127.0.0.1 -p 22300
238+
239+ The client:
240+ hipnetcat -t -s 127.0.0.1 -p 22345 -d 22300 \
241+ 127.0.0.1 2001:1c:809e:244a:c33:78fb:45e3:d132
242+ </programlisting>
243+ </para></listitem>
244+ <listitem><para>
245+ Example 2: localhost hipnetcat connection via UDP. The server listens on
246+ 127.0.0.1:22300 and the client connects from 127.0.0.1:22345 with HIT
247+ 2001:1c:809e:244a:c33:78fb:45e3:d132.
248+ <programlisting>
249+ The server:
250+ hipnetcat -l -s 127.0.0.1 -p 22300
251+
252+ The client:
253+ hipnetcat -s 127.0.0.1 -p 22345 -d 22300 \
254+ 127.0.0.1 2001:1c:809e:244a:c33:78fb:45e3:d132
255+ </programlisting>
256+ </para></listitem>
257+ </itemizedlist>
258+
259+ <para>
260+ Example 2: localhost hipnetcat connection via UDP. The server listens on
261+ 127.0.0.1:22300 and the client connects from 127.0.0.1:22345 with HIT
262+ 2001:1c:809e:244a:c33:78fb:45e3:d132.
263+ <programlisting>
264+ The server:
265+ hipnetcat -l -s 127.0.0.1 -p 22300
266+
267+ The client:
268+ hipnetcat -s 127.0.0.1 -p 22345 -d 22300 \
269+ 127.0.0.1 2001:1c:809e:244a:c33:78fb:45e3:d132
270+ </programlisting>
271+ </para>
272+
273+ <para>
274+ If the hipnetcat execution successes, the server will receive one message
275+ from the client, and then both sides exit without any error.
276+ </para>
277+ </section> <!-- hipnetcat -->
278 </chapter> <!-- ch_prebuilt -->
279
280 <chapter id="ch_how_to_use_hip">
281@@ -2584,6 +2658,68 @@
282
283 </section>
284
285+ <section id="ch_libhipl_usage">
286+ <title>The Libhipl Extension</title>
287+
288+ <para>
289+ Libhipl provides HIP functionality as a library for upper layer
290+ applications and it does not require the presence of normal HIPL daemon.
291+ Instead, the HIP control messages are transmitted on top of TCP/UDP.
292+ From the applications' point of view, they get similar API set as normal
293+ socket API. This section describes the usage of libhipl API.
294+ </para>
295+
296+ <para>
297+ Libhipl API set is mainly located in
298+ <emphasis>"socket_wrapper.h"</emphasis>. The initialization function:
299+ <emphasis>libhipd_init(void)</emphasis> is declared in
300+ <emphasis>"init.h"</emphasis>
301+ </para>
302+
303+ <para>
304+ Libhipl requires user to initialize it before calling any socket wrapper
305+ function which starts with "hip_" prefix. To initialize libhipl, call the
306+ <emphasis>libhipd_init</emphasis> function in "init.h"
307+ </para>
308+
309+ <para>
310+ Once the libhipl initialization finishes, user can use functions provided
311+ by socket_wrapper.h and it works like normal socket function except that:
312+ <itemizedlist>
313+ <listitem>
314+ <para>
315+ In <emphasis>hip_sendto</emphasis> and
316+ <emphasis>hip_connect</emphasis>, the destination address should be a
317+ HIT, not an IP address. The destination port should also be included
318+ in the same structure.
319+ </para>
320+ </listitem>
321+ <listitem>
322+ <para>
323+ In <emphasis>hip_recvfrom</emphasis>, the remote peer address should
324+ also be a HIT, not an IP address.The peer's port should also be
325+ included in the same structure.
326+ </para>
327+ </listitem>
328+ <listitem>
329+ <para>
330+ In <emphasis>hip_accept</emphasis>, after a new peer is accepted, the
331+ <emphasis>addr</emphasis> parameter returns a structure which
332+ contains peer's HIT and port (NOT IP). The
333+ <emphasis>addr_len</emphasis> parameter returns the length of this
334+ structure. The user then can use this structure for further function
335+ calls such as <emphasis>hip_recvfrom</emphasis>.
336+ </para>
337+ </listitem>
338+ </itemizedlist>
339+ </para>
340+
341+ <para>
342+ For more detail usage of the libhipl, please refer the hipnetcat program
343+ in the <emphasis>test</emphasis> directory.
344+ </para>
345+
346+ </section> <!-- ch_libhipl_usage -->
347 </chapter> <!-- ch_exp_extensions -->
348
349 </book>
350
351=== modified file 'hipd/hipd.c'
352--- hipd/hipd.c 2012-02-15 17:37:10 +0000
353+++ hipd/hipd.c 2012-02-20 08:12:22 +0000
354@@ -58,13 +58,12 @@
355 #include "lib/core/protodefs.h"
356 #include "lib/core/straddr.h"
357 #include "lib/core/util.h"
358-#include "config.h"
359-#include "accessor.h"
360-#include "hip_socket.h"
361-#include "init.h"
362-#include "maintenance.h"
363-#include "netdev.h"
364-#include "hipd.h"
365+#include "lib/hipdaemon/accessor.h"
366+#include "lib/hipdaemon/hip_socket.h"
367+#include "lib/hipdaemon/init.h"
368+#include "lib/hipdaemon/maintenance.h"
369+#include "lib/hipdaemon/netdev.h"
370+#include "lib/hipdaemon/hipd.h"
371
372
373 /** For receiving netlink IPsec events (acquire, expire, etc) */
374
375=== modified file 'hipd/main.c'
376--- hipd/main.c 2011-11-03 09:21:12 +0000
377+++ hipd/main.c 2012-02-20 08:12:22 +0000
378@@ -33,8 +33,8 @@
379 #include <sys/types.h>
380
381
382-#include "hipd/hipd.h"
383-#include "init.h"
384+#include "lib/hipdaemon/hipd.h"
385+#include "lib/hipdaemon/init.h"
386 #include "lib/core/debug.h"
387
388
389
390=== modified file 'hipfw/hipfw.c'
391--- hipfw/hipfw.c 2011-11-25 16:40:40 +0000
392+++ hipfw/hipfw.c 2012-02-20 08:12:22 +0000
393@@ -75,7 +75,7 @@
394 #include "lib/core/performance.h"
395 #include "lib/core/prefix.h"
396 #include "lib/core/util.h"
397-#include "hipd/hipd.h"
398+#include "lib/hipdaemon/hipd.h"
399 #include "config.h"
400 #include "cache.h"
401 #include "common_types.h"
402
403=== modified file 'lib/core/crypto.h'
404--- lib/core/crypto.h 2011-11-25 17:56:24 +0000
405+++ lib/core/crypto.h 2012-02-20 08:12:22 +0000
406@@ -65,9 +65,9 @@
407 #define RSA_KEY_DEFAULT_BITS 1024
408 #define ECDSA_DEFAULT_CURVE NID_X9_62_prime256v1
409
410-
411+#define DEFAULT_HOST_RSA_KEY_FILE_NAME "hip_host_rsa_key"
412 #define DEFAULT_HOST_DSA_KEY_FILE_BASE HIPL_SYSCONFDIR "/hip_host_dsa_key"
413-#define DEFAULT_HOST_RSA_KEY_FILE_BASE HIPL_SYSCONFDIR "/hip_host_rsa_key"
414+#define DEFAULT_HOST_RSA_KEY_FILE_BASE HIPL_SYSCONFDIR "/" DEFAULT_HOST_RSA_KEY_FILE_NAME
415 #define DEFAULT_HOST_ECDSA_KEY_FILE_BASE HIPL_SYSCONFDIR "/hip_host_ecdsa_key"
416 #define DEFAULT_PUB_FILE_SUFFIX ".pub"
417
418
419=== modified file 'lib/core/hostid.c'
420--- lib/core/hostid.c 2011-11-10 10:35:47 +0000
421+++ lib/core/hostid.c 2012-02-20 08:12:22 +0000
422@@ -1,5 +1,5 @@
423 /*
424- * Copyright (c) 2010-2011 Aalto University and RWTH Aachen University.
425+ * Copyright (c) 2010-2012 Aalto University and RWTH Aachen University.
426 *
427 * Permission is hereby granted, free of charge, to any person
428 * obtaining a copy of this software and associated documentation
429@@ -29,6 +29,7 @@
430 */
431
432 #include <errno.h>
433+#include <libgen.h>
434 #include <stdint.h>
435 #include <stdlib.h>
436 #include <string.h>
437@@ -743,11 +744,17 @@
438 goto out_err;
439 }
440 } else if (!use_default) {
441+ char hi_file_dup[strlen(hi_file) + 1];
442+ strcpy(hi_file_dup, hi_file);
443+ if ((err = check_and_create_dir(dirname(hi_file_dup), HIP_DIR_MODE))) {
444+ HIP_ERROR("Could not create direcory for path: %s\n", hi_file);
445+ goto out_err;
446+ }
447 if (!strcmp(hi_fmt, "dsa")) {
448 dsa_key = create_dsa_key(dsa_key_bits);
449 HIP_IFEL(!dsa_key, -EINVAL,
450 "Creation of DSA key failed.\n");
451- if ((err = save_dsa_private_key(dsa_filenamebase, dsa_key))) {
452+ if ((err = save_dsa_private_key(hi_file, dsa_key))) {
453 HIP_ERROR("Saving of DSA key failed.\n");
454 goto out_err;
455 }
456@@ -756,7 +763,7 @@
457 ecdsa_key = create_ecdsa_key(ecdsa_nid);
458 HIP_IFEL(!ecdsa_key, -EINVAL,
459 "Creation of ECDSA key failed.\n");
460- if ((err = save_ecdsa_private_key(ecdsa_filenamebase, ecdsa_key))) {
461+ if ((err = save_ecdsa_private_key(hi_file, ecdsa_key))) {
462 HIP_ERROR("Saving of ECDSA key failed.\n");
463 goto out_err;
464 }
465@@ -765,7 +772,7 @@
466 rsa_key = create_rsa_key(rsa_key_bits);
467 HIP_IFEL(!rsa_key, -EINVAL,
468 "Creation of RSA key failed.\n");
469- if ((err = save_rsa_private_key(rsa_filenamebase, rsa_key))) {
470+ if ((err = save_rsa_private_key(hi_file, rsa_key))) {
471 HIP_ERROR("Saving of RSA key failed.\n");
472 goto out_err;
473 }
474
475=== modified file 'lib/core/linkedlist.c'
476--- lib/core/linkedlist.c 2011-08-15 14:11:56 +0000
477+++ lib/core/linkedlist.c 2012-02-20 08:12:22 +0000
478@@ -1,5 +1,5 @@
479 /*
480- * Copyright (c) 2010 Aalto University and RWTH Aachen University.
481+ * Copyright (c) 2010-2012 Aalto University and RWTH Aachen University.
482 *
483 * Permission is hereby granted, free of charge, to any person
484 * obtaining a copy of this software and associated documentation
485@@ -298,6 +298,51 @@
486 }
487
488 /**
489+ * Deletes the first node from list which has the same ptr given.
490+ * If there is no match, does nothing.
491+ *
492+ * @param linkedlist list to remoe an element from
493+ * @param ptr pointer by which to identify the node
494+ * @param free_element a function pointer to a function for freeing the memory
495+ * allocated for an element at a node or NULL if the element
496+ * itself is not to be freed.
497+ */
498+void hip_ll_del_by_ptr(struct hip_ll *linkedlist, void *ptr,
499+ free_elem_fn free_element)
500+{
501+ struct hip_ll_node *curr;
502+ struct hip_ll_node *tmp;
503+
504+ /* match first list node */
505+ if (linkedlist != NULL && linkedlist->element_count > 0
506+ && linkedlist->head->ptr == ptr) {
507+ tmp = linkedlist->head;
508+ linkedlist->head = tmp->next;
509+ linkedlist->element_count--;
510+ if (free_element != NULL) {
511+ free_element(tmp->ptr);
512+ }
513+ free(tmp);
514+ return;
515+ }
516+
517+ /* match the rest list */
518+ tmp = linkedlist->head;
519+ for (curr = tmp->next; curr != NULL; curr = curr->next) {
520+ if (curr->ptr == ptr) {
521+ tmp->next = curr->next;
522+ linkedlist->element_count--;
523+ if (free_element != NULL) {
524+ free_element(curr->ptr);
525+ }
526+ free(curr);
527+ return;
528+ }
529+ tmp = tmp->next;
530+ }
531+}
532+
533+/**
534 * Deletes the first node from a linked list. If there are no nodes in the list,
535 * no action will be taken. If @c free_element is non-NULL the memory allocated
536 * for the element itself is also freed. When @c free_element is non-NULL, make
537
538=== modified file 'lib/core/linkedlist.h'
539--- lib/core/linkedlist.h 2011-11-25 17:56:24 +0000
540+++ lib/core/linkedlist.h 2012-02-20 08:12:22 +0000
541@@ -69,6 +69,8 @@
542 void *hip_ll_del(struct hip_ll *linkedlist, const unsigned int index,
543 free_elem_fn free_element);
544 void *hip_ll_del_first(struct hip_ll *linkedlist, free_elem_fn free_element);
545+void hip_ll_del_by_ptr(struct hip_ll *linkedlist, void *ptr,
546+ free_elem_fn free_element);
547 void *hip_ll_get(const struct hip_ll *const linkedlist, const unsigned int index);
548 const struct hip_ll_node *hip_ll_iterate(const struct hip_ll *const linkedlist,
549 const struct hip_ll_node *const current);
550
551=== modified file 'lib/core/message.c'
552--- lib/core/message.c 2011-11-08 15:25:41 +0000
553+++ lib/core/message.c 2012-02-20 08:12:22 +0000
554@@ -1,5 +1,5 @@
555 /*
556- * Copyright (c) 2010 Aalto University and RWTH Aachen University.
557+ * Copyright (c) 2010-2012 Aalto University and RWTH Aachen University.
558 *
559 * Permission is hereby granted, free of charge, to any person
560 * obtaining a copy of this software and associated documentation
561@@ -686,6 +686,77 @@
562 }
563
564 /**
565+ * Read a control message over TCP socket.
566+ *
567+ * @param sockfd a socket file descriptor
568+ * @param ctx a pointer to the packet context
569+ * @return -1 in case of an error, 0 otherwise.
570+ */
571+int hip_read_control_msg_tcp(int sockfd, struct hip_packet_context *ctx)
572+{
573+ int len, is_ipv4;
574+ struct sockaddr dst_addr = { 0 };
575+ struct sockaddr src_addr = { 0 };
576+ struct sockaddr_in *saddr4;
577+ struct sockaddr_in6 *saddr6;
578+ socklen_t saddr_len = sizeof(struct sockaddr);
579+
580+ len = recv(sockfd, ctx->input_msg, HIP_MAX_PACKET, 0);
581+ if (len < 0) {
582+ HIP_PERROR("recvfrom(): ");
583+ return -1;
584+ }
585+
586+ /* Get peer address */
587+ if (getpeername(sockfd, &src_addr, &saddr_len) < 0) {
588+ HIP_PERROR("getpeername(): ");
589+ return -1;
590+ }
591+
592+ is_ipv4 = (src_addr.sa_family == AF_INET);
593+ if (is_ipv4) {
594+ saddr4 = (struct sockaddr_in *) &src_addr;
595+ IPV4_TO_IPV6_MAP(&saddr4->sin_addr, &ctx->src_addr);
596+ ctx->msg_ports.src_port = ntohs(saddr4->sin_port);
597+ } else {
598+ saddr6 = (struct sockaddr_in6 *) &src_addr;
599+ memcpy(&ctx->dst_addr, &saddr6->sin6_addr, sizeof(struct in6_addr));
600+ ctx->msg_ports.src_port = ntohs(saddr6->sin6_port);
601+ }
602+
603+ /* Get local (bound) address */
604+ if (getsockname(sockfd, &dst_addr, &saddr_len) < 0) {
605+ HIP_PERROR("getsockname(): ");
606+ return -1;
607+ }
608+ if (is_ipv4) {
609+ saddr4 = (struct sockaddr_in *) &dst_addr;
610+ IPV4_TO_IPV6_MAP(&saddr4->sin_addr, &ctx->dst_addr);
611+ ctx->msg_ports.dst_port = saddr4->sin_port;
612+ } else {
613+ saddr6 = (struct sockaddr_in6 *) &dst_addr;
614+ memcpy(&ctx->dst_addr, &saddr6->sin6_addr, sizeof(struct in6_addr));
615+ ctx->msg_ports.dst_port = saddr6->sin6_port;
616+ }
617+
618+ HIP_DEBUG_IN6ADDR("src", &ctx->src_addr);
619+ HIP_DEBUG_IN6ADDR("dst", &ctx->dst_addr);
620+
621+ memmove(ctx->input_msg,
622+ ((char *) ctx->input_msg) + HIP_UDP_ZERO_BYTES_LEN,
623+ HIP_MAX_PACKET - HIP_UDP_ZERO_BYTES_LEN);
624+ len -= HIP_UDP_ZERO_BYTES_LEN;
625+
626+ if (hip_verify_network_header(ctx->input_msg, &src_addr,
627+ &dst_addr, len) < 0) {
628+ HIP_ERROR("verifying network header failed\n");
629+ return -1;
630+ }
631+
632+ return 0;
633+}
634+
635+/**
636 * Read an IPv6 control message.
637 *
638 * @param sockfd a socket file descriptor
639
640=== modified file 'lib/core/message.h'
641--- lib/core/message.h 2011-11-25 17:56:24 +0000
642+++ lib/core/message.h 2012-02-20 08:12:22 +0000
643@@ -46,6 +46,7 @@
644 int hip_send_recv_daemon_info(struct hip_common *msg,
645 int send_only,
646 int opt_socket);
647+int hip_read_control_msg_tcp(int sockfd, struct hip_packet_context *ctx);
648 int hip_send_recv_firewall_info(struct hip_common *const msg);
649
650 #endif /* HIPL_LIB_CORE_MESSAGE_H */
651
652=== added directory 'lib/hipdaemon'
653=== renamed file 'hipd/accessor.c' => 'lib/hipdaemon/accessor.c'
654=== renamed file 'hipd/accessor.h' => 'lib/hipdaemon/accessor.h'
655=== renamed file 'hipd/cert.c' => 'lib/hipdaemon/cert.c'
656=== renamed file 'hipd/cert.h' => 'lib/hipdaemon/cert.h'
657=== renamed file 'hipd/close.c' => 'lib/hipdaemon/close.c'
658=== renamed file 'hipd/close.h' => 'lib/hipdaemon/close.h'
659=== renamed file 'hipd/configfilereader.c' => 'lib/hipdaemon/configfilereader.c'
660=== renamed file 'hipd/configfilereader.h' => 'lib/hipdaemon/configfilereader.h'
661=== renamed file 'hipd/cookie.c' => 'lib/hipdaemon/cookie.c'
662=== renamed file 'hipd/cookie.h' => 'lib/hipdaemon/cookie.h'
663=== renamed file 'hipd/dh.c' => 'lib/hipdaemon/dh.c'
664=== renamed file 'hipd/dh.h' => 'lib/hipdaemon/dh.h'
665=== renamed file 'hipd/esp_prot_anchordb.c' => 'lib/hipdaemon/esp_prot_anchordb.c'
666=== renamed file 'hipd/esp_prot_anchordb.h' => 'lib/hipdaemon/esp_prot_anchordb.h'
667=== renamed file 'hipd/esp_prot_hipd_msg.c' => 'lib/hipdaemon/esp_prot_hipd_msg.c'
668=== renamed file 'hipd/esp_prot_hipd_msg.h' => 'lib/hipdaemon/esp_prot_hipd_msg.h'
669=== renamed file 'hipd/esp_prot_light_update.c' => 'lib/hipdaemon/esp_prot_light_update.c'
670=== renamed file 'hipd/esp_prot_light_update.h' => 'lib/hipdaemon/esp_prot_light_update.h'
671=== renamed file 'hipd/hadb.c' => 'lib/hipdaemon/hadb.c'
672--- hipd/hadb.c 2012-02-17 10:45:47 +0000
673+++ lib/hipdaemon/hadb.c 2012-02-20 08:12:22 +0000
674@@ -616,7 +616,12 @@
675
676 if (hip_select_source_address(&peer_map.our_addr, &peer_map.peer_addr)) {
677 HIP_ERROR("Cannot find source address\n");
678- return -1;
679+ if (hipl_is_libhip_mode()) {
680+ memset(&peer_map.our_addr, 0, sizeof(peer_map.our_addr));
681+ HIP_DEBUG("Using ANY for source address\n");
682+ } else {
683+ return -1;
684+ }
685 }
686
687 if (hip_for_each_hi(hadb_add_peer_info_wrapper, &peer_map)) {
688@@ -1456,6 +1461,10 @@
689 */
690 void hip_delete_security_associations_and_sp(struct hip_hadb_state *const ha)
691 {
692+ if (hipl_is_libhip_mode()) {
693+ return;
694+ }
695+
696 // Delete previous security policies
697 hip_delete_hit_sp_pair(&ha->hit_our, &ha->hit_peer, 1);
698 hip_delete_hit_sp_pair(&ha->hit_peer, &ha->hit_our, 1);
699@@ -1525,6 +1534,11 @@
700 ha),
701 -1, "Error while changing outbound security association\n");
702
703+ if (hipl_is_libhip_mode()) {
704+ HIP_DEBUG("No SP set up in library mode\n");
705+ goto out_err;
706+ }
707+
708 // Create a new security policy pointing to SAs after SA setup
709 HIP_IFEL(hip_setup_hit_sp_pair(&ha->hit_peer,
710 &ha->hit_our,
711
712=== renamed file 'hipd/hadb.h' => 'lib/hipdaemon/hadb.h'
713=== renamed file 'hipd/hidb.c' => 'lib/hipdaemon/hidb.c'
714--- hipd/hidb.c 2012-01-25 20:45:27 +0000
715+++ lib/hipdaemon/hidb.c 2012-02-20 08:12:22 +0000
716@@ -600,13 +600,15 @@
717 hip_add_iface_local_route(&in6_lsi);
718
719 /* Adding HITs and LSIs to the interface */
720- if (hip_add_iface_local_hit(&hit)) {
721- HIP_ERROR("Failed to add HIT to the device\n");
722- return -1;
723- }
724- if (hip_add_iface_local_hit(&in6_lsi)) {
725- HIP_ERROR("Failed to add LSI to the device\n");
726- return -1;
727+ if (!hipl_is_libhip_mode()) {
728+ if (hip_add_iface_local_hit(&hit)) {
729+ HIP_ERROR("Failed to add HIT to the device\n");
730+ return -1;
731+ }
732+ if (hip_add_iface_local_hit(&in6_lsi)) {
733+ HIP_ERROR("Failed to add LSI to the device\n");
734+ return -1;
735+ }
736 }
737 }
738
739
740=== renamed file 'hipd/hidb.h' => 'lib/hipdaemon/hidb.h'
741=== renamed file 'hipd/hip_socket.c' => 'lib/hipdaemon/hip_socket.c'
742=== renamed file 'hipd/hip_socket.h' => 'lib/hipdaemon/hip_socket.h'
743=== renamed file 'hipd/hipd.h' => 'lib/hipdaemon/hipd.h'
744--- hipd/hipd.h 2012-02-15 17:37:10 +0000
745+++ lib/hipdaemon/hipd.h 2012-02-20 08:12:22 +0000
746@@ -83,4 +83,8 @@
747 int hipd_parse_cmdline_opts(int argc, char *argv[], uint64_t * flags);
748 int hipd_main(uint64_t flags);
749
750+/* libhip_mode accessor */
751+int hipl_is_libhip_mode(void);
752+void hipl_set_libhip_mode(void);
753+
754 #endif /* HIPL_HIPD_HIPD_H */
755
756=== renamed file 'hipd/hiprelay.c' => 'lib/hipdaemon/hiprelay.c'
757=== renamed file 'hipd/hiprelay.h' => 'lib/hipdaemon/hiprelay.h'
758=== renamed file 'hipd/hit_to_ip.c' => 'lib/hipdaemon/hit_to_ip.c'
759=== renamed file 'hipd/hit_to_ip.h' => 'lib/hipdaemon/hit_to_ip.h'
760=== renamed file 'hipd/init.c' => 'lib/hipdaemon/init.c'
761--- hipd/init.c 2012-01-18 21:21:26 +0000
762+++ lib/hipdaemon/init.c 2012-02-20 08:12:22 +0000
763@@ -34,6 +34,7 @@
764
765 #include <errno.h>
766 #include <limits.h>
767+#include <pwd.h>
768 #include <signal.h>
769 #include <stdint.h>
770 #include <stdlib.h>
771@@ -74,6 +75,7 @@
772 #include "accessor.h"
773 #include "close.h"
774 #include "dh.h"
775+#include "esp_prot_hipd_msg.h"
776 #include "esp_prot_light_update.h"
777 #include "hadb.h"
778 #include "hidb.h"
779@@ -88,9 +90,9 @@
780 #include "output.h"
781 #include "pkt_handling.h"
782 #include "registration.h"
783+#include "socket_wrapper.h"
784 #include "user.h"
785 #include "init.h"
786-#include "hipd/esp_prot_hipd_msg.h"
787
788
789 /**
790@@ -118,6 +120,31 @@
791 #endif
792 /** end ICMPV6_FILTER related stuff */
793
794+#define HIP_USER_DIR ".hipl/"
795+
796+/* Flag to show if hipl is running in libhip mode (=1) or normal mode (=0).
797+ * This variable should NOT be accessed directly. Always use the accessor
798+ * functions instead.
799+ */
800+static int hipd_library_mode = 0;
801+
802+/**
803+ * Test if HIPL is running on library mode.
804+ * @return 1 on library mode, 0 otherwise.
805+ */
806+int hipl_is_libhip_mode()
807+{
808+ return hipd_library_mode;
809+}
810+
811+/**
812+ * Set HIPL to run on library mode.
813+ */
814+void hipl_set_libhip_mode()
815+{
816+ hipd_library_mode = 1;
817+}
818+
819 /* Startup flags of the HIPD. Keep the around, for they will be used at exit */
820 static uint64_t sflags;
821
822@@ -523,6 +550,208 @@
823 lmod_register_packet_type(HIP_LUPDATE, "HIP_LUPDATE");
824 }
825
826+static int libhip_init_handle_functions(void)
827+{
828+ HIP_DEBUG("Initialize handle functions for libhip.\n");
829+
830+ hip_register_handle_function(HIP_I1, HIP_STATE_UNASSOCIATED, &hip_check_i1, 20000);
831+ hip_register_handle_function(HIP_I1, HIP_STATE_UNASSOCIATED, &hip_handle_i1, 30000);
832+ hip_register_handle_function(HIP_I1, HIP_STATE_UNASSOCIATED, &hip_update_retransmissions, 35000);
833+ hip_register_handle_function(HIP_I1, HIP_STATE_UNASSOCIATED, &hip_send_r1, 40000);
834+ hip_register_handle_function(HIP_I1, HIP_STATE_I1_SENT, &hip_check_i1, 20000);
835+ hip_register_handle_function(HIP_I1, HIP_STATE_I1_SENT, &hip_handle_i1, 30000);
836+ hip_register_handle_function(HIP_I1, HIP_STATE_I1_SENT, &hip_update_retransmissions, 35000);
837+ hip_register_handle_function(HIP_I1, HIP_STATE_I1_SENT, &hip_send_r1, 40000);
838+ hip_register_handle_function(HIP_I1, HIP_STATE_I2_SENT, &hip_check_i1, 20000);
839+ hip_register_handle_function(HIP_I1, HIP_STATE_I2_SENT, &hip_handle_i1, 30000);
840+ hip_register_handle_function(HIP_I1, HIP_STATE_I2_SENT, &hip_update_retransmissions, 35000);
841+ hip_register_handle_function(HIP_I1, HIP_STATE_I2_SENT, &hip_send_r1, 40000);
842+ hip_register_handle_function(HIP_I1, HIP_STATE_R2_SENT, &hip_check_i1, 20000);
843+ hip_register_handle_function(HIP_I1, HIP_STATE_R2_SENT, &hip_handle_i1, 30000);
844+ hip_register_handle_function(HIP_I1, HIP_STATE_R2_SENT, &hip_update_retransmissions, 35000);
845+ hip_register_handle_function(HIP_I1, HIP_STATE_R2_SENT, &hip_send_r1, 40000);
846+ hip_register_handle_function(HIP_I1, HIP_STATE_ESTABLISHED, &hip_check_i1, 20000);
847+ hip_register_handle_function(HIP_I1, HIP_STATE_ESTABLISHED, &hip_handle_i1, 30000);
848+ hip_register_handle_function(HIP_I1, HIP_STATE_ESTABLISHED, &hip_update_retransmissions, 35000);
849+ hip_register_handle_function(HIP_I1, HIP_STATE_ESTABLISHED, &hip_send_r1, 40000);
850+ hip_register_handle_function(HIP_I1, HIP_STATE_CLOSING, &hip_check_i1, 20000);
851+ hip_register_handle_function(HIP_I1, HIP_STATE_CLOSING, &hip_handle_i1, 30000);
852+ hip_register_handle_function(HIP_I1, HIP_STATE_CLOSING, &hip_update_retransmissions, 35000);
853+ hip_register_handle_function(HIP_I1, HIP_STATE_CLOSING, &hip_send_r1, 40000);
854+ hip_register_handle_function(HIP_I1, HIP_STATE_CLOSED, &hip_check_i1, 20000);
855+ hip_register_handle_function(HIP_I1, HIP_STATE_CLOSED, &hip_handle_i1, 30000);
856+ hip_register_handle_function(HIP_I1, HIP_STATE_CLOSED, &hip_update_retransmissions, 35000);
857+ hip_register_handle_function(HIP_I1, HIP_STATE_CLOSED, &hip_send_r1, 40000);
858+ hip_register_handle_function(HIP_I1, HIP_STATE_NONE, &hip_check_i1, 20000);
859+ hip_register_handle_function(HIP_I1, HIP_STATE_NONE, &hip_handle_i1, 30000);
860+ hip_register_handle_function(HIP_I1, HIP_STATE_NONE, &hip_update_retransmissions, 35000);
861+ hip_register_handle_function(HIP_I1, HIP_STATE_NONE, &hip_send_r1, 40000);
862+
863+ hip_register_handle_function(HIP_I2, HIP_STATE_UNASSOCIATED, &hip_check_i2, 20000);
864+ hip_register_handle_function(HIP_I2, HIP_STATE_UNASSOCIATED, &hip_handle_i2, 30000);
865+ hip_register_handle_function(HIP_I2, HIP_STATE_UNASSOCIATED, &hip_update_retransmissions, 30250);
866+ //hip_register_handle_function(HIP_I2, HIP_STATE_UNASSOCIATED, &hip_setup_ipsec_sa, 30500);
867+ hip_register_handle_function(HIP_I2, HIP_STATE_UNASSOCIATED, &hip_create_r2, 40000);
868+ hip_register_handle_function(HIP_I2, HIP_STATE_UNASSOCIATED, &hip_add_rvs_reg_from, 41000);
869+ hip_register_handle_function(HIP_I2, HIP_STATE_UNASSOCIATED, &hip_hmac2_and_sign, 42000);
870+ hip_register_handle_function(HIP_I2, HIP_STATE_UNASSOCIATED, &hip_add_rvs_relay_to, 43000);
871+ hip_register_handle_function(HIP_I2, HIP_STATE_UNASSOCIATED, &hip_send_r2, 50000);
872+ hip_register_handle_function(HIP_I2, HIP_STATE_I1_SENT, &hip_check_i2, 20000);
873+ hip_register_handle_function(HIP_I2, HIP_STATE_I1_SENT, &hip_handle_i2, 30000);
874+ hip_register_handle_function(HIP_I2, HIP_STATE_I1_SENT, &hip_update_retransmissions, 30250);
875+ //hip_register_handle_function(HIP_I2, HIP_STATE_I1_SENT, &hip_setup_ipsec_sa, 30500);
876+ hip_register_handle_function(HIP_I2, HIP_STATE_I1_SENT, &hip_create_r2, 40000);
877+ hip_register_handle_function(HIP_I2, HIP_STATE_I1_SENT, &hip_add_rvs_reg_from, 41000);
878+ hip_register_handle_function(HIP_I2, HIP_STATE_I1_SENT, &hip_hmac2_and_sign, 42000);
879+ hip_register_handle_function(HIP_I2, HIP_STATE_I1_SENT, &hip_add_rvs_relay_to, 43000);
880+ hip_register_handle_function(HIP_I2, HIP_STATE_I1_SENT, &hip_send_r2, 50000);
881+ hip_register_handle_function(HIP_I2, HIP_STATE_I2_SENT, &hip_check_i2, 20000);
882+ hip_register_handle_function(HIP_I2, HIP_STATE_I2_SENT, &hip_handle_i2_in_i2_sent, 21000);
883+ hip_register_handle_function(HIP_I2, HIP_STATE_I2_SENT, &hip_handle_i2, 30000);
884+ hip_register_handle_function(HIP_I2, HIP_STATE_I2_SENT, &hip_update_retransmissions, 30250);
885+ //hip_register_handle_function(HIP_I2, HIP_STATE_I2_SENT, &hip_setup_ipsec_sa, 30500);
886+ hip_register_handle_function(HIP_I2, HIP_STATE_I2_SENT, &hip_create_r2, 40000);
887+ hip_register_handle_function(HIP_I2, HIP_STATE_I2_SENT, &hip_add_rvs_reg_from, 41000);
888+ hip_register_handle_function(HIP_I2, HIP_STATE_I2_SENT, &hip_hmac2_and_sign, 42000);
889+ hip_register_handle_function(HIP_I2, HIP_STATE_I2_SENT, &hip_add_rvs_relay_to, 43000);
890+ hip_register_handle_function(HIP_I2, HIP_STATE_I2_SENT, &hip_send_r2, 50000);
891+ hip_register_handle_function(HIP_I2, HIP_STATE_R2_SENT, &hip_check_i2, 20000);
892+ hip_register_handle_function(HIP_I2, HIP_STATE_R2_SENT, &hip_handle_i2, 30000);
893+ hip_register_handle_function(HIP_I2, HIP_STATE_R2_SENT, &hip_update_retransmissions, 30250);
894+ //hip_register_handle_function(HIP_I2, HIP_STATE_R2_SENT, &hip_setup_ipsec_sa, 30500);
895+ hip_register_handle_function(HIP_I2, HIP_STATE_R2_SENT, &hip_create_r2, 40000);
896+ hip_register_handle_function(HIP_I2, HIP_STATE_R2_SENT, &hip_add_rvs_reg_from, 41000);
897+ hip_register_handle_function(HIP_I2, HIP_STATE_R2_SENT, &hip_hmac2_and_sign, 42000);
898+ hip_register_handle_function(HIP_I2, HIP_STATE_R2_SENT, &hip_add_rvs_relay_to, 43000);
899+ hip_register_handle_function(HIP_I2, HIP_STATE_R2_SENT, &hip_send_r2, 50000);
900+ hip_register_handle_function(HIP_I2, HIP_STATE_ESTABLISHED, &hip_check_i2, 20000);
901+ hip_register_handle_function(HIP_I2, HIP_STATE_ESTABLISHED, &hip_handle_i2, 30000);
902+ hip_register_handle_function(HIP_I2, HIP_STATE_ESTABLISHED, &hip_update_retransmissions, 30250);
903+ //hip_register_handle_function(HIP_I2, HIP_STATE_ESTABLISHED, &hip_setup_ipsec_sa, 30500);
904+ hip_register_handle_function(HIP_I2, HIP_STATE_ESTABLISHED, &hip_create_r2, 40000);
905+ hip_register_handle_function(HIP_I2, HIP_STATE_ESTABLISHED, &hip_add_rvs_reg_from, 41000);
906+ hip_register_handle_function(HIP_I2, HIP_STATE_ESTABLISHED, &hip_hmac2_and_sign, 42000);
907+ hip_register_handle_function(HIP_I2, HIP_STATE_ESTABLISHED, &hip_add_rvs_relay_to, 43000);
908+ hip_register_handle_function(HIP_I2, HIP_STATE_ESTABLISHED, &hip_send_r2, 50000);
909+ hip_register_handle_function(HIP_I2, HIP_STATE_CLOSING, &hip_check_i2, 20000);
910+ hip_register_handle_function(HIP_I2, HIP_STATE_CLOSING, &hip_handle_i2, 30000);
911+ hip_register_handle_function(HIP_I2, HIP_STATE_CLOSING, &hip_update_retransmissions, 30250);
912+ //hip_register_handle_function(HIP_I2, HIP_STATE_CLOSING, &hip_setup_ipsec_sa, 30500);
913+ hip_register_handle_function(HIP_I2, HIP_STATE_CLOSING, &hip_create_r2, 40000);
914+ hip_register_handle_function(HIP_I2, HIP_STATE_CLOSING, &hip_add_rvs_reg_from, 41000);
915+ hip_register_handle_function(HIP_I2, HIP_STATE_CLOSING, &hip_hmac2_and_sign, 42000);
916+ hip_register_handle_function(HIP_I2, HIP_STATE_CLOSING, &hip_add_rvs_relay_to, 43000);
917+ hip_register_handle_function(HIP_I2, HIP_STATE_CLOSING, &hip_send_r2, 50000);
918+ hip_register_handle_function(HIP_I2, HIP_STATE_CLOSED, &hip_check_i2, 20000);
919+ hip_register_handle_function(HIP_I2, HIP_STATE_CLOSED, &hip_handle_i2, 30000);
920+ hip_register_handle_function(HIP_I2, HIP_STATE_CLOSED, &hip_update_retransmissions, 30250);
921+ //hip_register_handle_function(HIP_I2, HIP_STATE_CLOSED, &hip_setup_ipsec_sa, 30500);
922+ hip_register_handle_function(HIP_I2, HIP_STATE_CLOSED, &hip_create_r2, 40000);
923+ hip_register_handle_function(HIP_I2, HIP_STATE_CLOSED, &hip_add_rvs_reg_from, 41000);
924+ hip_register_handle_function(HIP_I2, HIP_STATE_CLOSED, &hip_hmac2_and_sign, 42000);
925+ hip_register_handle_function(HIP_I2, HIP_STATE_CLOSED, &hip_add_rvs_relay_to, 43000);
926+ hip_register_handle_function(HIP_I2, HIP_STATE_CLOSED, &hip_send_r2, 50000);
927+ hip_register_handle_function(HIP_I2, HIP_STATE_NONE, &hip_check_i2, 20000);
928+ hip_register_handle_function(HIP_I2, HIP_STATE_NONE, &hip_handle_i2, 30000);
929+ hip_register_handle_function(HIP_I2, HIP_STATE_NONE, &hip_update_retransmissions, 30250);
930+ //hip_register_handle_function(HIP_I2, HIP_STATE_NONE, &hip_setup_ipsec_sa, 30500);
931+ hip_register_handle_function(HIP_I2, HIP_STATE_NONE, &hip_create_r2, 40000);
932+ hip_register_handle_function(HIP_I2, HIP_STATE_NONE, &hip_add_rvs_reg_from, 41000);
933+ hip_register_handle_function(HIP_I2, HIP_STATE_NONE, &hip_hmac2_and_sign, 42000);
934+ hip_register_handle_function(HIP_I2, HIP_STATE_NONE, &hip_add_rvs_relay_to, 43000);
935+ hip_register_handle_function(HIP_I2, HIP_STATE_NONE, &hip_send_r2, 50000);
936+
937+ hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &hip_check_r1, 20000);
938+ hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &hip_handle_r1, 30000);
939+ hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &hip_update_retransmissions, 30500);
940+ hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &hip_build_esp_info, 31000);
941+ hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &hip_build_solution, 32000);
942+ hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &hip_handle_diffie_hellman, 33000);
943+ hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &esp_prot_r1_handle_transforms, 34000);
944+ hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &hip_create_i2, 40000);
945+ hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &hip_add_signed_echo_response, 41000);
946+ hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &hip_mac_and_sign_handler, 42000);
947+ hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &hip_add_unsigned_echo_response, 43000);
948+ hip_register_handle_function(HIP_R1, HIP_STATE_I1_SENT, &hip_send_i2, 50000);
949+ hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &hip_check_r1, 20000);
950+ hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &hip_handle_r1, 30000);
951+ hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &hip_update_retransmissions, 30500);
952+ hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &hip_build_esp_info, 31000);
953+ hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &hip_build_solution, 32000);
954+ hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &hip_handle_diffie_hellman, 33000);
955+ hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &esp_prot_r1_handle_transforms, 34000);
956+ hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &hip_create_i2, 40000);
957+ hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &hip_add_signed_echo_response, 41000);
958+ hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &hip_mac_and_sign_handler, 42000);
959+ hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &hip_add_unsigned_echo_response, 43000);
960+ hip_register_handle_function(HIP_R1, HIP_STATE_I2_SENT, &hip_send_i2, 50000);
961+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &hip_check_r1, 20000);
962+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &hip_handle_r1, 30000);
963+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &hip_update_retransmissions, 30500);
964+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &hip_build_esp_info, 31000);
965+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &hip_build_solution, 32000);
966+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &hip_handle_diffie_hellman, 33000);
967+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &esp_prot_r1_handle_transforms, 34000);
968+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &hip_create_i2, 40000);
969+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &hip_add_signed_echo_response, 41000);
970+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &hip_mac_and_sign_handler, 42000);
971+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &hip_add_unsigned_echo_response, 43000);
972+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSING, &hip_send_i2, 50000);
973+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &hip_check_r1, 20000);
974+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &hip_handle_r1, 30000);
975+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &hip_update_retransmissions, 30500);
976+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &hip_build_esp_info, 31000);
977+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &hip_build_solution, 32000);
978+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &hip_handle_diffie_hellman, 33000);
979+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &esp_prot_r1_handle_transforms, 34000);
980+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &hip_create_i2, 40000);
981+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &hip_add_signed_echo_response, 41000);
982+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &hip_mac_and_sign_handler, 42000);
983+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &hip_add_unsigned_echo_response, 43000);
984+ hip_register_handle_function(HIP_R1, HIP_STATE_CLOSED, &hip_send_i2, 50000);
985+
986+ hip_register_handle_function(HIP_R2, HIP_STATE_I2_SENT, &hip_check_r2, 20000);
987+ hip_register_handle_function(HIP_R2, HIP_STATE_I2_SENT, &hip_handle_r2, 30000);
988+ hip_register_handle_function(HIP_R2, HIP_STATE_I2_SENT, &hip_update_retransmissions, 30250);
989+ //hip_register_handle_function(HIP_R2, HIP_STATE_I2_SENT, &hip_setup_ipsec_sa, 30500);
990+
991+ hip_register_handle_function(HIP_NOTIFY, HIP_STATE_I1_SENT, &hip_check_notify, 20000);
992+ hip_register_handle_function(HIP_NOTIFY, HIP_STATE_I1_SENT, &hip_handle_notify, 30000);
993+ hip_register_handle_function(HIP_NOTIFY, HIP_STATE_I2_SENT, &hip_check_notify, 20000);
994+ hip_register_handle_function(HIP_NOTIFY, HIP_STATE_I2_SENT, &hip_handle_notify, 30000);
995+ hip_register_handle_function(HIP_NOTIFY, HIP_STATE_R2_SENT, &hip_check_notify, 20000);
996+ hip_register_handle_function(HIP_NOTIFY, HIP_STATE_R2_SENT, &hip_handle_notify, 30000);
997+ hip_register_handle_function(HIP_NOTIFY, HIP_STATE_ESTABLISHED, &hip_check_notify, 20000);
998+ hip_register_handle_function(HIP_NOTIFY, HIP_STATE_ESTABLISHED, &hip_handle_notify, 30000);
999+ hip_register_handle_function(HIP_NOTIFY, HIP_STATE_CLOSING, &hip_check_notify, 20000);
1000+ hip_register_handle_function(HIP_NOTIFY, HIP_STATE_CLOSING, &hip_handle_notify, 30000);
1001+ hip_register_handle_function(HIP_NOTIFY, HIP_STATE_CLOSED, &hip_check_notify, 20000);
1002+ hip_register_handle_function(HIP_NOTIFY, HIP_STATE_CLOSED, &hip_handle_notify, 30000);
1003+
1004+ hip_register_handle_function(HIP_CLOSE, HIP_STATE_ESTABLISHED, &hip_close_check_packet, 20000);
1005+ hip_register_handle_function(HIP_CLOSE, HIP_STATE_ESTABLISHED, &hip_update_retransmissions, 25000);
1006+ hip_register_handle_function(HIP_CLOSE, HIP_STATE_ESTABLISHED, &hip_close_create_response, 30000);
1007+ hip_register_handle_function(HIP_CLOSE, HIP_STATE_ESTABLISHED, &hip_close_send_response, 40000);
1008+
1009+ hip_register_handle_function(HIP_CLOSE, HIP_STATE_CLOSING, &hip_close_check_packet, 20000);
1010+ hip_register_handle_function(HIP_CLOSE, HIP_STATE_CLOSING, &hip_update_retransmissions, 25000);
1011+ hip_register_handle_function(HIP_CLOSE, HIP_STATE_CLOSING, &hip_close_create_response, 30000);
1012+ hip_register_handle_function(HIP_CLOSE, HIP_STATE_CLOSING, &hip_close_send_response, 40000);
1013+
1014+ hip_register_handle_function(HIP_CLOSE_ACK, HIP_STATE_CLOSING, &hip_close_ack_check_packet, 20000);
1015+ hip_register_handle_function(HIP_CLOSE_ACK, HIP_STATE_CLOSING, &hip_update_retransmissions, 25000);
1016+ hip_register_handle_function(HIP_CLOSE_ACK, HIP_STATE_CLOSING, &hip_close_ack_handle_packet, 30000);
1017+
1018+ hip_register_handle_function(HIP_CLOSE_ACK, HIP_STATE_CLOSED, &hip_close_ack_check_packet, 20000);
1019+ hip_register_handle_function(HIP_CLOSE_ACK, HIP_STATE_CLOSED, &hip_update_retransmissions, 25000);
1020+ hip_register_handle_function(HIP_CLOSE_ACK, HIP_STATE_CLOSED, &hip_close_ack_handle_packet, 30000);
1021+
1022+ hip_register_handle_function(HIP_LUPDATE, HIP_STATE_ESTABLISHED, &esp_prot_handle_light_update, 20000);
1023+ hip_register_handle_function(HIP_LUPDATE, HIP_STATE_R2_SENT, &esp_prot_handle_light_update, 20000);
1024+
1025+ return 0;
1026+}
1027+
1028 static int init_handle_functions(void)
1029 {
1030 int err = 0;
1031@@ -1090,6 +1319,66 @@
1032 return err;
1033 }
1034
1035+int hipl_lib_init(void)
1036+{
1037+ int err = 0;
1038+ int keypath_len = 0;
1039+ struct hip_common *msg = NULL;
1040+ struct passwd *pwd;
1041+
1042+ hipl_set_libhip_mode();
1043+ hip_nat_status = 1;
1044+#ifdef CONFIG_HIP_FIREWALL
1045+ hipfw_status = 0;
1046+#endif
1047+
1048+ hip_init_hadb();
1049+ hip_init_hostid_db();
1050+ hip_netdev_init_addresses();
1051+ libhip_init_handle_functions();
1052+
1053+ /* Load default key from ~/.hipl/ */
1054+ if ((pwd = getpwuid(getuid())) == NULL) {
1055+ return -1;
1056+ }
1057+
1058+ /* +2 because we need a slash after pwd and a NULL for termination */
1059+ keypath_len = strlen(pwd->pw_dir) +
1060+ strlen(HIP_USER_DIR) +
1061+ strlen(DEFAULT_HOST_RSA_KEY_FILE_NAME) +
1062+ strlen(DEFAULT_PUB_HI_FILE_NAME_SUFFIX) + 2;
1063+ char key_path[keypath_len];
1064+
1065+ HIP_IFEL(snprintf(key_path, keypath_len, "%s/%s%s%s", pwd->pw_dir,
1066+ HIP_USER_DIR,
1067+ DEFAULT_HOST_RSA_KEY_FILE_NAME,
1068+ DEFAULT_PUB_HI_FILE_NAME_SUFFIX) < 0,
1069+ -1, "snprintf() failed");
1070+
1071+ HIP_DEBUG("Using key: %s\n", key_path);
1072+ HIP_IFEL(!(msg = hip_msg_alloc()), -1, "hip_msg_alloc()");
1073+ if (hip_serialize_host_id_action(msg, ACTION_ADD, 0, 0, "rsa",
1074+ key_path, 0, 0, 0)) {
1075+ free(msg);
1076+ HIP_IFEL(!(msg = hip_msg_alloc()), -1, "hip_msg_alloc()");
1077+ HIP_IFEL(hip_serialize_host_id_action(msg, ACTION_NEW, 0, 0, "rsa",
1078+ key_path, RSA_KEY_DEFAULT_BITS,
1079+ DSA_KEY_DEFAULT_BITS,
1080+ ECDSA_DEFAULT_CURVE), -1,
1081+ "Fail to create local key at %s.", key_path);
1082+ free(msg);
1083+ HIP_IFE(!(msg = hip_msg_alloc()), -1);
1084+ HIP_IFEL(hip_serialize_host_id_action(msg, ACTION_ADD, 0, 0, "rsa",
1085+ key_path, 0, 0, 0), -1,
1086+ "Fail to load local key at %s.", key_path);
1087+ }
1088+ HIP_IFE(hip_handle_add_local_hi(msg), -1);
1089+
1090+out_err:
1091+ free(msg);
1092+ return err;
1093+}
1094+
1095 /**
1096 * create a socket to handle UDP encapsulation of HIP control
1097 * packets
1098
1099=== renamed file 'hipd/init.h' => 'lib/hipdaemon/init.h'
1100--- hipd/init.h 2011-11-25 17:56:24 +0000
1101+++ lib/hipdaemon/init.h 2012-02-20 08:12:22 +0000
1102@@ -1,5 +1,5 @@
1103 /*
1104- * Copyright (c) 2010 Aalto University and RWTH Aachen University.
1105+ * Copyright (c) 2010-2012 Aalto University and RWTH Aachen University.
1106 *
1107 * Permission is hereby granted, free of charge, to any person
1108 * obtaining a copy of this software and associated documentation
1109@@ -60,4 +60,6 @@
1110 int is_output);
1111 void hip_exit(void);
1112
1113+int hipl_lib_init(void);
1114+
1115 #endif /* HIPL_HIPD_INIT_H */
1116
1117=== renamed file 'hipd/input.c' => 'lib/hipdaemon/input.c'
1118--- hipd/input.c 2012-02-17 10:45:47 +0000
1119+++ lib/hipdaemon/input.c 2012-02-20 08:12:22 +0000
1120@@ -1,5 +1,5 @@
1121 /*
1122- * Copyright (c) 2010-2011 Aalto University and RWTH Aachen University.
1123+ * Copyright (c) 2010-2012 Aalto University and RWTH Aachen University.
1124 *
1125 * Permission is hereby granted, free of charge, to any person
1126 * obtaining a copy of this software and associated documentation
1127@@ -709,6 +709,11 @@
1128 &ctx->src_addr);
1129 }
1130
1131+ if (hipl_is_libhip_mode()) {
1132+ ctx->msg_ports.src_port = ctx->hadb_entry->peer_udp_port;
1133+ ctx->msg_ports.dst_port = ctx->hadb_entry->local_udp_port;
1134+ }
1135+
1136 hip_relay_add_rvs_to_ha(ctx->input_msg, ctx->hadb_entry);
1137
1138 #ifdef CONFIG_HIP_RVS
1139
1140=== renamed file 'hipd/input.h' => 'lib/hipdaemon/input.h'
1141=== renamed file 'hipd/keymat.c' => 'lib/hipdaemon/keymat.c'
1142=== renamed file 'hipd/keymat.h' => 'lib/hipdaemon/keymat.h'
1143=== renamed file 'hipd/lsidb.c' => 'lib/hipdaemon/lsidb.c'
1144=== renamed file 'hipd/lsidb.h' => 'lib/hipdaemon/lsidb.h'
1145=== renamed file 'hipd/maintenance.c' => 'lib/hipdaemon/maintenance.c'
1146--- hipd/maintenance.c 2012-02-15 17:37:10 +0000
1147+++ lib/hipdaemon/maintenance.c 2012-02-20 08:12:22 +0000
1148@@ -58,6 +58,7 @@
1149 #include "cookie.h"
1150 #include "hadb.h"
1151 #include "hidb.h"
1152+#include "hip_socket.h"
1153 #include "hipd.h"
1154 #include "init.h"
1155 #include "input.h"
1156
1157=== renamed file 'hipd/maintenance.h' => 'lib/hipdaemon/maintenance.h'
1158=== renamed file 'hipd/nat.c' => 'lib/hipdaemon/nat.c'
1159=== renamed file 'hipd/nat.h' => 'lib/hipdaemon/nat.h'
1160=== renamed file 'hipd/netdev.c' => 'lib/hipdaemon/netdev.c'
1161=== renamed file 'hipd/netdev.h' => 'lib/hipdaemon/netdev.h'
1162=== renamed file 'hipd/nsupdate.c' => 'lib/hipdaemon/nsupdate.c'
1163=== renamed file 'hipd/nsupdate.h' => 'lib/hipdaemon/nsupdate.h'
1164=== renamed file 'hipd/opp_mode.c' => 'lib/hipdaemon/opp_mode.c'
1165=== renamed file 'hipd/opp_mode.h' => 'lib/hipdaemon/opp_mode.h'
1166=== renamed file 'hipd/output.c' => 'lib/hipdaemon/output.c'
1167--- hipd/output.c 2012-02-15 17:37:10 +0000
1168+++ lib/hipdaemon/output.c 2012-02-20 08:12:22 +0000
1169@@ -850,7 +850,8 @@
1170 HIP_ASSERT(!hit_is_opportunistic_hit(&ctx->input_msg->hitr));
1171
1172 /* Case: I ----->IPv4---> RVS ---IPv6---> R */
1173- if (IN6_IS_ADDR_V4MAPPED(r1_src_addr) !=
1174+ if (!hipl_is_libhip_mode() &&
1175+ IN6_IS_ADDR_V4MAPPED(r1_src_addr) !=
1176 IN6_IS_ADDR_V4MAPPED(r1_dst_addr)) {
1177 HIP_DEBUG_IN6ADDR("r1_src_addr", r1_src_addr);
1178 HIP_DEBUG_IN6ADDR("r1_dst_addr", r1_dst_addr);
1179@@ -1217,6 +1218,9 @@
1180 goto out_err;
1181 }
1182
1183+ if (hipl_is_libhip_mode()) {
1184+ udp = 1;
1185+ }
1186 dst_is_ipv4 = IN6_IS_ADDR_V4MAPPED(peer_addr);
1187 len = hip_get_msg_total_len(msg);
1188
1189@@ -1245,13 +1249,16 @@
1190 if (local_addr) {
1191 HIP_DEBUG("local address given\n");
1192 memcpy(&my_addr, local_addr, sizeof(struct in6_addr));
1193- } else {
1194+ } else if (!hipl_is_libhip_mode()) {
1195 HIP_DEBUG("no local address, selecting one\n");
1196 HIP_IFEL(hip_select_source_address(&my_addr, peer_addr), -1,
1197 "Cannot find source address\n");
1198+ } else {
1199+ memset(&my_addr, 0, sizeof(my_addr));
1200 }
1201
1202- src_is_ipv4 = IN6_IS_ADDR_V4MAPPED(&my_addr);
1203+ src_is_ipv4 = IN6_IS_ADDR_V4MAPPED(&my_addr) ||
1204+ (dst_is_ipv4 && ipv6_addr_any(&my_addr));
1205
1206 if (src_is_ipv4) {
1207 IPV6_TO_IPV4_MAP(&my_addr, &src4->sin_addr);
1208@@ -1305,9 +1312,10 @@
1209
1210 /* Handover may cause e.g. on-link duplicate address detection
1211 * which may cause bind to fail. */
1212-
1213- HIP_IFEL(bind(hip_raw_sock_output, (struct sockaddr *) &src, sa_size),
1214- -1, "Binding to raw sock failed\n");
1215+ if (!hipl_is_libhip_mode()) {
1216+ HIP_IFEL(bind(hip_raw_sock_output, (struct sockaddr *) &src, sa_size),
1217+ -1, "Binding to raw sock failed\n");
1218+ }
1219
1220 #if (HIP_SIMULATE_PACKET_LOSS_PROBABILITY > 0)
1221 if (HIP_SIMULATE_PACKET_LOSS && HIP_SIMULATE_PACKET_IS_LOST()) {
1222@@ -1324,18 +1332,25 @@
1223 len = hip_get_msg_total_len(msg);
1224
1225 if (udp) {
1226- struct udphdr *uh = (struct udphdr *) msg;
1227-
1228- /* Insert 32 bits of zero bytes between UDP and HIP */
1229- memmove((char *) msg + HIP_UDP_ZERO_BYTES_LEN + sizeof(struct udphdr), msg, len);
1230- memset(msg, 0, HIP_UDP_ZERO_BYTES_LEN + sizeof(struct udphdr));
1231- len += HIP_UDP_ZERO_BYTES_LEN + sizeof(struct udphdr);
1232-
1233- uh->source = htons(src_port);
1234- uh->dest = htons(dst_port);
1235- uh->len = htons(len);
1236- uh->check = 0;
1237- memmoved = 1;
1238+ if (!hipl_is_libhip_mode()) {
1239+ /* Insert 32 bits of zero bytes between UDP and HIP */
1240+ memmove((char *) msg + HIP_UDP_ZERO_BYTES_LEN + sizeof(struct udphdr), msg, len);
1241+ memset(msg, 0, HIP_UDP_ZERO_BYTES_LEN + sizeof(struct udphdr));
1242+ len += HIP_UDP_ZERO_BYTES_LEN + sizeof(struct udphdr);
1243+
1244+ struct udphdr *uh = (struct udphdr *) msg;
1245+ uh->source = htons(src_port);
1246+ uh->dest = htons(dst_port);
1247+ uh->len = htons(len);
1248+ uh->check = 0;
1249+ } else {
1250+ memmove((char *) msg + HIP_UDP_ZERO_BYTES_LEN, msg, len);
1251+ memset(msg, 0, HIP_UDP_ZERO_BYTES_LEN);
1252+ len += HIP_UDP_ZERO_BYTES_LEN;
1253+
1254+ dst4->sin_port = htons(dst_port);
1255+ }
1256+ memmoved = 1;
1257 }
1258
1259 sent = sendto(hip_raw_sock_output, msg, len, 0,
1260
1261=== renamed file 'hipd/output.h' => 'lib/hipdaemon/output.h'
1262=== renamed file 'hipd/pisa.c' => 'lib/hipdaemon/pisa.c'
1263=== renamed file 'hipd/pisa.h' => 'lib/hipdaemon/pisa.h'
1264=== renamed file 'hipd/pkt_handling.c' => 'lib/hipdaemon/pkt_handling.c'
1265=== renamed file 'hipd/pkt_handling.h' => 'lib/hipdaemon/pkt_handling.h'
1266=== renamed file 'hipd/registration.c' => 'lib/hipdaemon/registration.c'
1267=== renamed file 'hipd/registration.h' => 'lib/hipdaemon/registration.h'
1268=== added file 'lib/hipdaemon/socket_wrapper.c'
1269--- lib/hipdaemon/socket_wrapper.c 1970-01-01 00:00:00 +0000
1270+++ lib/hipdaemon/socket_wrapper.c 2012-02-20 08:12:22 +0000
1271@@ -0,0 +1,811 @@
1272+/*
1273+ * Copyright (c) 2010-2012 Aalto University and RWTH Aachen University.
1274+ *
1275+ * Permission is hereby granted, free of charge, to any person
1276+ * obtaining a copy of this software and associated documentation
1277+ * files (the "Software"), to deal in the Software without
1278+ * restriction, including without limitation the rights to use,
1279+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
1280+ * copies of the Software, and to permit persons to whom the
1281+ * Software is furnished to do so, subject to the following
1282+ * conditions:
1283+ *
1284+ * The above copyright notice and this permission notice shall be
1285+ * included in all copies or substantial portions of the Software.
1286+ *
1287+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1288+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
1289+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
1290+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
1291+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
1292+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
1293+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
1294+ * OTHER DEALINGS IN THE SOFTWARE.
1295+ */
1296+
1297+/**
1298+ * @file
1299+ * This file contains the implementation of libhipl API.
1300+ */
1301+
1302+#define _BSD_SOURCE
1303+
1304+#include <string.h>
1305+#include <unistd.h>
1306+
1307+#include "lib/core/builder.h"
1308+#include "lib/core/conf.h"
1309+#include "lib/core/crypto.h"
1310+#include "lib/core/hip_udp.h"
1311+#include "lib/core/linkedlist.h"
1312+#include "lib/core/message.h"
1313+#include "lib/core/prefix.h"
1314+#include "close.h"
1315+#include "hadb.h"
1316+#include "hidb.h"
1317+#include "init.h"
1318+#include "input.h"
1319+#include "netdev.h"
1320+#include "output.h"
1321+#include "socket_wrapper.h"
1322+
1323+struct hipl_fd_info {
1324+ int fd;
1325+ int bound_port;
1326+ int family;
1327+ int proto;
1328+ struct hip_hadb_state *ha;
1329+};
1330+
1331+static struct hip_ll socket_list;
1332+static struct in6_addr default_hit;
1333+
1334+static uint16_t get_port_from_saddr(const struct sockaddr *addr)
1335+{
1336+ const struct sockaddr_in *addr4;
1337+ const struct sockaddr_in6 *addr6;
1338+
1339+ if (addr->sa_family == AF_INET) {
1340+ addr4 = (const struct sockaddr_in *) addr;
1341+ return addr4->sin_port;
1342+ } else {
1343+ addr6 = (const struct sockaddr_in6 *) addr;
1344+ return addr6->sin6_port;
1345+ }
1346+}
1347+
1348+/**
1349+ * Build a suitable sockaddr_storage.
1350+ * If the address is V4MAPPED, the storage family will be INET4.
1351+ * Otherwise the storage family will be INET6.
1352+ *
1353+ * @param addr a V6 address or V4MAPPED address
1354+ * @param port port number (in NETWORK byte order)
1355+ * @param ss the sockaddr_storage to be filled.
1356+ */
1357+static void build_sockaddr(struct in6_addr *const addr, const uint16_t port,
1358+ struct sockaddr_storage *const ss)
1359+{
1360+ HIP_ASSERT(addr && ss);
1361+ memset(ss, 0, sizeof(struct sockaddr_storage));
1362+
1363+ if (IN6_IS_ADDR_V4MAPPED(addr)) {
1364+ struct sockaddr_in *const in = (struct sockaddr_in *) ss;
1365+ in->sin_family = AF_INET;
1366+ IPV6_TO_IPV4_MAP(addr, &in->sin_addr);
1367+ in->sin_port = port;
1368+ } else {
1369+ struct sockaddr_in6 *const in6 = (struct sockaddr_in6 *) ss;
1370+ in6->sin6_family = AF_INET6;
1371+ ipv6_addr_copy(&in6->sin6_addr, addr);
1372+ in6->sin6_port = port;
1373+ }
1374+}
1375+
1376+/**
1377+ * Create a new HIP socket info struct and insert it into global list.
1378+ *
1379+ * @param fd file descriptor of the socket
1380+ * @param bound_port the port number which this fd bound to (in HOST order)
1381+ * @param family the address family of this fd (INET or INET6)
1382+ * @param proto the protocol of this fd (TCP or UDP)
1383+ * @return pointer of the new struct, NULL if we fail to create it.
1384+ */
1385+static struct hipl_fd_info *create_new_fd_info(const int fd,
1386+ const uint16_t bound_port,
1387+ const int family,
1388+ const int proto)
1389+{
1390+ int err = 0;
1391+ struct hipl_fd_info *fd_info_new = NULL;
1392+
1393+ if ((fd_info_new = calloc(sizeof(struct hipl_fd_info), 1)) == NULL) {
1394+ HIP_PERROR("calloc()");
1395+ return NULL;
1396+ }
1397+
1398+ HIP_IFEL(hip_ll_add_last(&socket_list, fd_info_new),
1399+ -1, "Error adding FD info struct to list\n");
1400+ fd_info_new->fd = fd;
1401+ fd_info_new->bound_port = bound_port;
1402+ fd_info_new->family = family;
1403+ fd_info_new->proto = proto;
1404+
1405+out_err:
1406+ if (err) {
1407+ free(fd_info_new);
1408+ fd_info_new = NULL;
1409+ }
1410+ return fd_info_new;
1411+}
1412+
1413+/**
1414+ * Get information on a HIP socket by its file descritor.
1415+ *
1416+ * @param fd file descriptor of the HIP socket
1417+ * @return a structure containing information on the status of the socket
1418+ * or NULL if none is found
1419+ */
1420+static struct hipl_fd_info *hipl_socket_get_info(int fd)
1421+{
1422+ const struct hip_ll_node *current = NULL;
1423+ struct hipl_fd_info *fd_info;
1424+
1425+ while ((current = hip_ll_iterate(&socket_list, current))) {
1426+ fd_info = current->ptr;
1427+ if (fd_info->fd == fd) {
1428+ return fd_info;
1429+ }
1430+ }
1431+
1432+ return NULL;
1433+}
1434+
1435+/**
1436+ * Automatically bind to a port for hip_fd_info struct.
1437+ *
1438+ * @param info hip_fd_info struct
1439+ * @return 0 if it successes, -1 on error
1440+ */
1441+static int auto_bind(const struct hipl_fd_info *info)
1442+{
1443+ struct sockaddr_storage ss = { 0 };
1444+ struct sockaddr_in *addr4;
1445+ struct sockaddr_in6 *addr6;
1446+
1447+ if (info->bound_port != 0) {
1448+ HIP_ERROR("A bound port exists, auto_bind stops\n");
1449+ return -1;
1450+ }
1451+
1452+ if (info->family == AF_INET) {
1453+ ss.ss_family = AF_INET;
1454+ addr4 = (struct sockaddr_in *) &ss;
1455+ addr4->sin_port = 0;
1456+ return hipl_bind(info->fd, (struct sockaddr *) addr4, sizeof(ss));
1457+ } else {
1458+ ss.ss_family = AF_INET6;
1459+ addr6 = (struct sockaddr_in6 *) &ss;
1460+ addr6->sin6_port = 0;
1461+ return hipl_bind(info->fd, (struct sockaddr *) addr6, sizeof(ss));
1462+ }
1463+}
1464+
1465+/**
1466+ * Add peer's hit-to-addr mapping to hadb.
1467+ * @param peer_hit peer's hit
1468+ * @param peer_addr peer's addr, v4 addr should be mapped.
1469+ * @return 0 on success, -1 otherwise.
1470+ */
1471+int hipl_add_peer_info(const hip_hit_t *peer_hit,
1472+ const struct in6_addr *peer_addr)
1473+{
1474+ return hip_hadb_add_peer_info(peer_hit, peer_addr, NULL, NULL);
1475+}
1476+
1477+/**
1478+ * Set global variables in order to interoperate with libhipdeamon.
1479+ * @param sock_fd the sock fd used for sending message.
1480+ * @param local_port the local port for sending message.
1481+ * @param remote_port the remote port for receiving message.
1482+ */
1483+static void set_hip_connection_parameters(int sock_fd, int local_port,
1484+ int remote_port)
1485+{
1486+ HIP_DEBUG("fd = %d, lport = %d, rport= %d\n", sock_fd, local_port, remote_port);
1487+ hip_nat_sock_output_udp = sock_fd;
1488+ hip_raw_sock_output_v4 = sock_fd;
1489+ hip_raw_sock_output_v6 = sock_fd;
1490+ hip_set_local_nat_udp_port(local_port);
1491+ hip_set_peer_nat_udp_port(remote_port);
1492+}
1493+
1494+/**
1495+ * Check whether a received packet is a HIP control packet
1496+ *
1497+ * @param buf buffer containing the received data
1498+ * @param len size of buf
1499+ * @param fd_info information of the socket which the data was received from
1500+ * @return
1501+ */
1502+static int hipl_is_control_msg(char *buf, unsigned int len,
1503+ struct hipl_fd_info *fd_info)
1504+{
1505+ char udp_pad[HIP_UDP_ZERO_BYTES_LEN] = { 0 };
1506+ struct hip_common *msg;
1507+ struct sockaddr_storage src = { 0 };
1508+ struct sockaddr_storage dst = { 0 };
1509+
1510+ if (len < sizeof(struct hip_common)) {
1511+ return 0;
1512+ }
1513+
1514+ if (!memcmp(udp_pad, buf, HIP_UDP_ZERO_BYTES_LEN)) {
1515+ HIP_DEBUG("Message is padded\n");
1516+ msg = (struct hip_common *) (buf + HIP_UDP_ZERO_BYTES_LEN);
1517+ len -= HIP_UDP_ZERO_BYTES_LEN;
1518+ } else {
1519+ msg = (struct hip_common *) buf;
1520+ }
1521+
1522+ src.ss_family = fd_info->family;
1523+ dst.ss_family = fd_info->family;
1524+
1525+ if (fd_info->family == AF_INET) {
1526+ struct sockaddr_in *src4 = (struct sockaddr_in *) &src;
1527+ struct sockaddr_in *dst4 = (struct sockaddr_in *) &dst;
1528+
1529+ src4->sin_port = fd_info->ha->peer_udp_port;
1530+ dst4->sin_port = fd_info->ha->local_udp_port;
1531+
1532+ IPV6_TO_IPV4_MAP(&fd_info->ha->peer_addr, &src4->sin_addr);
1533+ IPV6_TO_IPV4_MAP(&fd_info->ha->our_addr, &dst4->sin_addr);
1534+
1535+ HIP_DEBUG_INADDR("src", &src4->sin_addr);
1536+ HIP_DEBUG_INADDR("dst", &dst4->sin_addr);
1537+ } else if (fd_info->family == AF_INET6) {
1538+ struct sockaddr_in6 *src6 = (struct sockaddr_in6 *) &src;
1539+ struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *) &dst;
1540+
1541+ src6->sin6_port = fd_info->ha->peer_udp_port;
1542+ dst6->sin6_port = fd_info->ha->local_udp_port;
1543+
1544+ memcpy(&src6->sin6_addr, &fd_info->ha->peer_addr,
1545+ sizeof(struct in6_addr));
1546+ memcpy(&dst6->sin6_addr, &fd_info->ha->our_addr,
1547+ sizeof(struct in6_addr));
1548+
1549+ HIP_DEBUG_IN6ADDR("src", &src6->sin6_addr);
1550+ HIP_DEBUG_IN6ADDR("dst", &dst6->sin6_addr);
1551+ }
1552+
1553+ return !hip_verify_network_header(msg, (struct sockaddr *) &src,
1554+ (struct sockaddr *) &dst, len);
1555+}
1556+
1557+/**
1558+ * Initiate and perform a base exchange with a peer.
1559+ *
1560+ * @param fd file descriptor
1561+ * @param dst_hit_sockaddr hit of the peer to be contacted
1562+ * @return 0 on success, -1 otherwise
1563+ */
1564+static int hipl_do_bex(int fd, const struct sockaddr_in6 *dst_hit_sockaddr)
1565+{
1566+ struct in6_addr dst_hit;
1567+ struct in6_addr dst_addr;
1568+ struct hip_packet_context ctx = { 0 };
1569+ struct hipl_fd_info *fd_info;
1570+
1571+ if ((fd_info = hipl_socket_get_info(fd)) == NULL) {
1572+ HIP_ERROR("No socket info found.\n");
1573+ return -1;
1574+ }
1575+
1576+ hip_get_default_hit(&default_hit);
1577+
1578+ memcpy(&dst_hit, &dst_hit_sockaddr->sin6_addr, sizeof(dst_hit));
1579+ if (hip_map_id_to_addr(&dst_hit, NULL, &dst_addr) < 0) {
1580+ return -1;
1581+ }
1582+ if (ipv6_addr_any(&dst_addr)) {
1583+ HIP_ERROR("Couldn't map HIT to IP\n");
1584+ return -1;
1585+ }
1586+
1587+ set_hip_connection_parameters(fd, fd_info->bound_port,
1588+ ntohs(dst_hit_sockaddr->sin6_port));
1589+ if (netdev_trigger_bex(&default_hit, &dst_hit,
1590+ NULL, NULL, NULL, &dst_addr) < 0) {
1591+ HIP_ERROR("Couldn't start base exchange.\n");
1592+ return -1;
1593+ }
1594+
1595+ ctx.input_msg = hip_msg_alloc();
1596+ ctx.output_msg = hip_msg_alloc();
1597+
1598+ fd_info->ha = hip_hadb_find_byhits(&default_hit, &dst_hit);
1599+
1600+ do {
1601+ if (fd_info->proto == IPPROTO_TCP) {
1602+ if (!hip_read_control_msg_tcp(fd, &ctx)) {
1603+ hip_receive_control_packet(&ctx);
1604+ }
1605+ } else if (fd_info->family == AF_INET) {
1606+ if (!hip_read_control_msg_v4(fd, &ctx, HIP_UDP_ZERO_BYTES_LEN)) {
1607+ hip_receive_control_packet(&ctx);
1608+ }
1609+ } else {
1610+ if (!hip_read_control_msg_v6(fd, &ctx, HIP_UDP_ZERO_BYTES_LEN)) {
1611+ hip_receive_control_packet(&ctx);
1612+ }
1613+ }
1614+ } while (fd_info->ha->state != HIP_STATE_ESTABLISHED);
1615+
1616+ fd_info->ha->local_udp_port = fd_info->bound_port;
1617+
1618+ free(ctx.input_msg);
1619+ free(ctx.output_msg);
1620+ return 0;
1621+}
1622+
1623+/**
1624+ * Wait for an I1 packet and perform base exchange.
1625+ *
1626+ * @param fd_info fd information of the socket for incoming BEX msg.
1627+ * @param peer_addr peer's locator
1628+ * @return 0 on success, -1 otherwise
1629+ */
1630+static int hipl_await_bex(struct hipl_fd_info *fd_info,
1631+ struct sockaddr *peer_addr)
1632+{
1633+ int fd = fd_info->fd;
1634+ struct hip_packet_context ctx = { 0 };
1635+
1636+ ctx.input_msg = hip_msg_alloc();
1637+ ctx.output_msg = hip_msg_alloc();
1638+
1639+ set_hip_connection_parameters(fd, fd_info->bound_port,
1640+ ntohs(get_port_from_saddr(peer_addr)));
1641+
1642+ do {
1643+ if (fd_info->proto == IPPROTO_TCP) {
1644+ if (!hip_read_control_msg_tcp(fd, &ctx) &&
1645+ !hip_receive_control_packet(&ctx)) {
1646+ fd_info->ha = hip_hadb_find_byhits(&ctx.input_msg->hitr,
1647+ &ctx.input_msg->hits);
1648+ }
1649+ } else if (fd_info->family == AF_INET) {
1650+ if (!hip_read_control_msg_v4(fd, &ctx, HIP_UDP_ZERO_BYTES_LEN) &&
1651+ !hip_receive_control_packet(&ctx)) {
1652+ fd_info->ha = hip_hadb_find_byhits(&ctx.input_msg->hitr,
1653+ &ctx.input_msg->hits);
1654+ }
1655+ } else {
1656+ if (!hip_read_control_msg_v6(fd, &ctx, HIP_UDP_ZERO_BYTES_LEN) &&
1657+ !hip_receive_control_packet(&ctx)) {
1658+ fd_info->ha = hip_hadb_find_byhits(&ctx.input_msg->hitr,
1659+ &ctx.input_msg->hits);
1660+ }
1661+ }
1662+ } while (!fd_info->ha || fd_info->ha->state != HIP_STATE_ESTABLISHED);
1663+
1664+ fd_info->ha->local_udp_port = fd_info->bound_port;
1665+
1666+ free(ctx.input_msg);
1667+ free(ctx.output_msg);
1668+ return 0;
1669+}
1670+
1671+/**
1672+ * Create a socket and an information entry for it
1673+ *
1674+ * @param domain Communications domain for the socket
1675+ * @param type Socket type
1676+ * @param protocol Socket protocol
1677+ * @return file descriptor of the new socket on success, -1 otherwise
1678+ */
1679+int hipl_socket(int domain, int type, int protocol)
1680+{
1681+ int sock;
1682+ int on = 1, off = 0, err = 0;
1683+ struct hipl_fd_info *fd_info = NULL;
1684+
1685+ sock = socket(domain, type, protocol);
1686+
1687+ if (domain == AF_INET) {
1688+ setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
1689+ setsockopt(sock, IPPROTO_IP, IP_PKTINFO, &on, sizeof(on));
1690+ setsockopt(sock, IPPROTO_IP, IP_RECVERR, &off, sizeof(off));
1691+ setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
1692+ } else {
1693+ setsockopt(sock, IPPROTO_IPV6, IPV6_RECVERR, &off, sizeof(off));
1694+ setsockopt(sock, IPPROTO_IPV6, IPV6_2292PKTINFO, &on, sizeof(on));
1695+ setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
1696+ }
1697+
1698+ //TODO support IPV6
1699+ HIP_IFEL(domain == AF_INET6, -1, "No support for IPv6.\n");
1700+
1701+ fd_info = create_new_fd_info(sock, 0, domain, protocol);
1702+ HIP_IFEL(!fd_info, -1, "Fail to create hip socket info for fd: %d\n", sock);
1703+
1704+out_err:
1705+ if (err) {
1706+ close(sock);
1707+ return err;
1708+ }
1709+
1710+ return sock;
1711+}
1712+
1713+/**
1714+ * Close a socket. Sends CLOSE to the associated peer and deletes
1715+ * the socket information.
1716+ *
1717+ * @param fd file descriptor of the socket to be closed
1718+ * @return 0 if the socket was successfully closed, -1 otherwise
1719+ */
1720+int hipl_close(int fd)
1721+{
1722+ int err = 0;
1723+ struct hip_common *msg = NULL;
1724+ struct hipl_fd_info *fd_info = NULL;
1725+
1726+ fd_info = hipl_socket_get_info(fd);
1727+ if (!fd_info || !fd_info->ha || fd_info->ha->state == HIP_STATE_CLOSED) {
1728+ HIP_DEBUG("Not sending CLOSE.\n");
1729+ goto skip_close_msg;
1730+ }
1731+
1732+ if (!(msg = hip_msg_alloc()) ||
1733+ hip_build_param_contents(msg, &fd_info->ha->hit_peer,
1734+ HIP_PARAM_HIT, sizeof(struct in6_addr)) ||
1735+ hip_send_close(msg, 1)) {
1736+ HIP_ERROR("Sending CLOSE failed\n");
1737+ goto skip_close_msg;
1738+ }
1739+
1740+skip_close_msg:
1741+ err = close(fd);
1742+
1743+ if (fd_info) {
1744+ hip_ll_del_by_ptr(&socket_list, fd_info, free);
1745+ }
1746+ free(msg);
1747+ return err;
1748+}
1749+
1750+/**
1751+ * Bind a socket to a local address
1752+ *
1753+ * @param fd file descriptor of the socket
1754+ * @param address address to bind to
1755+ * @param address_len length of address
1756+ * @return 0 on success, -1 otherwise
1757+ */
1758+int hipl_bind(int fd, const struct sockaddr *address, socklen_t address_len)
1759+{
1760+ struct hipl_fd_info *fd_info;
1761+ struct sockaddr_storage laddr = { 0 };
1762+ socklen_t laddr_len = sizeof(laddr);
1763+ uint16_t request_port;
1764+
1765+ if ((fd_info = hipl_socket_get_info(fd)) == NULL) {
1766+ HIP_ERROR("Fd %d is not a hip socket, exiting.\n", fd);
1767+ return -1;
1768+ }
1769+
1770+ request_port = get_port_from_saddr(address);
1771+ if (bind(fd, address, address_len) < 0) {
1772+ HIP_PERROR("bind error:");
1773+ return -1;
1774+ }
1775+
1776+ /* Ask OS to get assigned port number */
1777+ if (request_port == 0) {
1778+ if (getsockname(fd, (struct sockaddr *) &laddr, &laddr_len) < 0) {
1779+ HIP_PERROR("getsockname() error: ");
1780+ return -1;
1781+ }
1782+ request_port = get_port_from_saddr((struct sockaddr *) &laddr);
1783+ }
1784+
1785+ fd_info->bound_port = ntohs(request_port);
1786+ HIP_DEBUG("bind to port %d\n", fd_info->bound_port);
1787+
1788+ return 0;
1789+}
1790+
1791+/**
1792+ * Send data to the peer associated with a socket. Triggers base exchange
1793+ * if no host association exists.
1794+ *
1795+ * @note Data is currently sent unencrypted.
1796+ *
1797+ * @param fd file descriptor of the socket to send from
1798+ * @param msg data to send
1799+ * @param len size of data
1800+ * @param flags sendto() flags
1801+ * @param dst_hit HIT of the peer to send to
1802+ * @param dst_len size of dst_hit
1803+ * @return number of bytes sent on success, -1 otherwise
1804+ */
1805+int hipl_sendto(int fd, const void *msg, size_t len,
1806+ int flags, const struct sockaddr *dst_hit,
1807+ socklen_t dst_len)
1808+{
1809+ struct sockaddr_storage dst_addr;
1810+ uint16_t dst_port;
1811+ struct hipl_fd_info *fd_info;
1812+
1813+ dst_port = ((const struct sockaddr_in6 *) dst_hit)->sin6_port;
1814+
1815+ if (dst_len != sizeof(struct sockaddr_in6)) {
1816+ HIP_ERROR("Only HITs are supported\n");
1817+ return -1;
1818+ }
1819+
1820+ if ((fd_info = hipl_socket_get_info(fd)) == NULL) {
1821+ HIP_ERROR("Fd %d is not a hip socket, exiting.\n", fd);
1822+ return -1;
1823+ }
1824+
1825+ /* Bind to a ephemeral port if the src port hasn't been bound yet */
1826+ if (fd_info->bound_port == 0) {
1827+ if (auto_bind(fd_info)) {
1828+ HIP_ERROR("Fail to bind the hip socket.\n");
1829+ return -1;
1830+ }
1831+ }
1832+
1833+ /* Start BEX if HA hasn't establised */
1834+ if (!fd_info->ha) {
1835+ if (hipl_do_bex(fd, (const struct sockaddr_in6 *) dst_hit)) {
1836+ HIP_ERROR("Base exchange not successful.\n");
1837+ return -1;
1838+ }
1839+ }
1840+
1841+ /* Send user data */
1842+ build_sockaddr(&fd_info->ha->peer_addr, dst_port, &dst_addr);
1843+ return sendto(fd, msg, len, flags,
1844+ (struct sockaddr *) &dst_addr, sizeof(dst_addr));
1845+}
1846+
1847+/**
1848+ * Receive data from the peer associated with a socket.
1849+ * Waits for base exchange if no host association exists.
1850+ *
1851+ * @note Data is currently sent unencrypted.
1852+ *
1853+ * @param fd file descriptor of the socket to receive from
1854+ * @param buf buffer for received data
1855+ * @param len size of buf
1856+ * @param flags recvfrom() flags
1857+ * @param addr buffer for the associated peer HIT
1858+ * @param addr_len size of dst_hit
1859+ * @return number of bytes received on success, -1 otherwise
1860+ */
1861+int hipl_recvfrom(int fd, void *buf, size_t len, int flags,
1862+ struct sockaddr *addr, socklen_t *addr_len)
1863+{
1864+ int err = 0;
1865+ socklen_t socklen = *addr_len;
1866+ struct sockaddr_in6 *peer_hit = (struct sockaddr_in6 *) addr;
1867+ struct in6_addr peer_addr = { { { 0 } } };
1868+ struct in6_addr *peer_addr6;
1869+ struct in_addr *peer_addr4;
1870+ struct hipl_fd_info *fd_info = NULL;
1871+ struct hip_packet_context ctx = { 0 };
1872+ int (*read_control_msg)(int, struct hip_packet_context *, int) = NULL;
1873+
1874+
1875+ if ((fd_info = hipl_socket_get_info(fd)) == NULL) {
1876+ HIP_ERROR("Fd %d is not a hip socket, exiting.\n", fd);
1877+ return -1;
1878+ }
1879+
1880+ /* Bind to a ephemeral port if the src port hasn't been bound yet */
1881+ if (fd_info->bound_port == 0) {
1882+ if (auto_bind(fd_info)) {
1883+ HIP_ERROR("Fail to bind the hip socket.\n");
1884+ return -1;
1885+ }
1886+ }
1887+
1888+ /* Handle BEX if HA hasn't establised */
1889+ if (!fd_info->ha) {
1890+ if (hipl_await_bex(fd_info, addr)) {
1891+ HIP_ERROR("Base exchange not successful.\n");
1892+ return -1;
1893+ }
1894+ }
1895+
1896+ ctx.input_msg = hip_msg_alloc();
1897+ ctx.output_msg = hip_msg_alloc();
1898+ read_control_msg = fd_info->family == AF_INET ? hip_read_control_msg_v4
1899+ : hip_read_control_msg_v6;
1900+
1901+ /* Loop until we get a non-control packet or a CLOSE packet */
1902+ while (fd_info->ha->state == HIP_STATE_ESTABLISHED) {
1903+ err = recvfrom(fd, buf, len, flags | MSG_PEEK, addr, &socklen);
1904+ HIP_DEBUG("Peek packet len: %d\n", err);
1905+ HIP_DEBUG("peer sockaddr: AF = %d, socklen = %d\n", addr->sa_family, socklen);
1906+ if (err < 0) {
1907+ perror("recvfrom");
1908+ }
1909+
1910+ /* Drop the packet if it doesn't come from the address associated
1911+ * with the correct peer. */
1912+ if (fd_info->proto == IPPROTO_UDP) {
1913+ if (addr->sa_family == AF_INET) {
1914+ peer_addr4 = &((struct sockaddr_in *) addr)->sin_addr;
1915+ IPV4_TO_IPV6_MAP(peer_addr4, &peer_addr);
1916+ peer_addr6 = &peer_addr;
1917+ } else {
1918+ peer_addr6 = &((struct sockaddr_in6 *) addr)->sin6_addr;
1919+ }
1920+ if (ipv6_addr_cmp(&fd_info->ha->peer_addr, peer_addr6)) {
1921+ HIP_DEBUG("Packet not from associated address. Dropping.\n");
1922+ HIP_DEBUG_IN6ADDR("expected", &fd_info->ha->peer_addr);
1923+ HIP_DEBUG_IN6ADDR("got", peer_addr6);
1924+ err = recvfrom(fd, buf, 1, flags, addr, &socklen);
1925+ HIP_IFEL(err < 0, err, "recvfrom()\n");
1926+ continue;
1927+ }
1928+ }
1929+
1930+ /* Receive message */
1931+ if (hipl_is_control_msg(buf, err, fd_info)) {
1932+ HIP_DEBUG("receive a hip control message.\n");
1933+ if (fd_info->proto == IPPROTO_TCP) {
1934+ if (!hip_read_control_msg_tcp(fd, &ctx)) {
1935+ hip_receive_control_packet(&ctx);
1936+ }
1937+ } else if (!read_control_msg(fd, &ctx, HIP_UDP_ZERO_BYTES_LEN)) {
1938+ hip_receive_control_packet(&ctx);
1939+ } else {
1940+ HIP_ERROR("Error reading control packet\n");
1941+ }
1942+ err = 0;
1943+ } else {
1944+ HIP_DEBUG("receive a non hip control message.\n");
1945+ err = recvfrom(fd, buf, len, flags, addr, &socklen);
1946+ HIP_IFEL(err < 0, err, "recvfrom() error\n");
1947+ break;
1948+ }
1949+ }
1950+
1951+ if (peer_hit) {
1952+ memset(peer_hit, 0, *addr_len);
1953+ memcpy(&peer_hit->sin6_addr, &fd_info->ha->hit_peer,
1954+ sizeof(struct in6_addr));
1955+ peer_hit->sin6_port = fd_info->ha->peer_udp_port;
1956+ *addr_len = sizeof(struct sockaddr_in6);
1957+ }
1958+
1959+out_err:
1960+ free(ctx.input_msg);
1961+ free(ctx.output_msg);
1962+ return err;
1963+}
1964+
1965+/**
1966+ * Initiate a connection on a socket and perform base exchange as Initiator
1967+ *
1968+ * @param fd file descriptor of the socket
1969+ * @param addr peer HIT to connect to
1970+ * @param addr_len size of addr
1971+ * @return 0 on success, -1 otherwise
1972+ */
1973+int hipl_connect(int fd, const struct sockaddr *addr, socklen_t addr_len)
1974+{
1975+ uint16_t dst_port;
1976+ struct hipl_fd_info *fd_info = NULL;
1977+ struct in6_addr dst_hit;
1978+ struct in6_addr dst_addr = { { { 0 } } };
1979+ struct sockaddr_storage ss;
1980+
1981+ if ((fd_info = hipl_socket_get_info(fd)) == NULL) {
1982+ HIP_ERROR("Fd %d is not a hip socket, exiting.\n", fd);
1983+ return -1;
1984+ }
1985+
1986+ if (addr_len != sizeof(struct sockaddr_in6)) {
1987+ HIP_ERROR("Invalid argument, Only HITs supported\n");
1988+ return -1;
1989+ }
1990+
1991+ dst_port = ((const struct sockaddr_in6 *) addr)->sin6_port;
1992+ ipv6_addr_copy(&dst_hit, &((const struct sockaddr_in6 *) addr)->sin6_addr);
1993+ if (ipv6_addr_any(&dst_hit)) {
1994+ HIP_ERROR("Invalid argument: The ANY dest address is not supported.\n");
1995+ return -1;
1996+ }
1997+
1998+ if (hip_map_id_to_addr(&dst_hit, NULL, &dst_addr) < 0) {
1999+ return -1;
2000+ }
2001+ if (ipv6_addr_any(&dst_addr)) {
2002+ HIP_ERROR("Couldn't map HIT to IP\n");
2003+ return -1;
2004+ }
2005+
2006+ HIP_DEBUG_IN6ADDR("Dest locator is: ", &dst_addr);
2007+ HIP_DEBUG("Dest locator is V4MAPPED: %d\n", IN6_IS_ADDR_V4MAPPED(&dst_addr));
2008+ build_sockaddr(&dst_addr, dst_port, &ss);
2009+
2010+ if (connect(fd, (struct sockaddr *) &ss, sizeof(ss)) < 0) {
2011+ HIP_PERROR("connect():");
2012+ return -1;
2013+ }
2014+
2015+ /* Bind to a ephemeral port if the src port hasn't been bound yet */
2016+ if (fd_info->bound_port == 0) {
2017+ if (auto_bind(fd_info)) {
2018+ HIP_ERROR("Fail to bind the hip socket.\n");
2019+ return -1;
2020+ }
2021+ }
2022+
2023+ if (hipl_do_bex(fd, (const struct sockaddr_in6 *) addr) < 0) {
2024+ HIP_ERROR("Base exchange not successful.\n");
2025+ return -1;
2026+ }
2027+
2028+ return 0;
2029+}
2030+
2031+/**
2032+ * Wait for an incoming connection on a socket and perform base exchange as
2033+ * Responder
2034+ *
2035+ * @param fd file descriptor of the socket
2036+ * @param new_peer_hit buffer to store peer hit and port
2037+ * @param hit_len size of new_peer_hit
2038+ * @return file descriptor of accepted socket, -1 on error
2039+ */
2040+int hipl_accept(int fd, struct sockaddr *new_peer_hit, socklen_t *hit_len)
2041+{
2042+ int new_fd;
2043+ struct hipl_fd_info *fd_info = NULL;
2044+ struct hipl_fd_info *fd_info_new = NULL;
2045+ struct sockaddr_storage ss = { 0 };
2046+ socklen_t ss_len = sizeof(struct sockaddr_storage);
2047+ struct sockaddr_in6 *phit = NULL;
2048+
2049+ if (*hit_len < sizeof(struct sockaddr_in6)) {
2050+ return -1;
2051+ }
2052+
2053+ if ((fd_info = hipl_socket_get_info(fd)) == NULL) {
2054+ HIP_ERROR("Fd %d is not a hip socket, exiting.\n", fd);
2055+ return -1;
2056+ }
2057+
2058+ new_fd = accept(fd, (struct sockaddr *) &ss, &ss_len);
2059+ if (new_fd < 0) {
2060+ HIP_PERROR("accept(): ");
2061+ return -1;
2062+ }
2063+
2064+ fd_info_new = create_new_fd_info(new_fd, fd_info->bound_port,
2065+ fd_info->family, fd_info->proto);
2066+
2067+ if (hipl_await_bex(fd_info_new, (struct sockaddr *) &ss) < 0) {
2068+ HIP_ERROR("Base exchange not successful.\n");
2069+ return -1;
2070+ }
2071+
2072+ if (new_peer_hit) {
2073+ phit = (struct sockaddr_in6 *) new_peer_hit;
2074+ memset(phit, 0, *hit_len);
2075+ memcpy(&phit->sin6_addr, &fd_info_new->ha->hit_peer,
2076+ sizeof(struct in6_addr));
2077+ phit->sin6_port = get_port_from_saddr((struct sockaddr *) &ss);
2078+ *hit_len = sizeof(struct sockaddr_in6);
2079+ }
2080+
2081+ return new_fd;
2082+}
2083
2084=== added file 'lib/hipdaemon/socket_wrapper.h'
2085--- lib/hipdaemon/socket_wrapper.h 1970-01-01 00:00:00 +0000
2086+++ lib/hipdaemon/socket_wrapper.h 2012-02-20 08:12:22 +0000
2087@@ -0,0 +1,48 @@
2088+/*
2089+ * Copyright (c) 2010-2012 Aalto University and RWTH Aachen University.
2090+ *
2091+ * Permission is hereby granted, free of charge, to any person
2092+ * obtaining a copy of this software and associated documentation
2093+ * files (the "Software"), to deal in the Software without
2094+ * restriction, including without limitation the rights to use,
2095+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
2096+ * copies of the Software, and to permit persons to whom the
2097+ * Software is furnished to do so, subject to the following
2098+ * conditions:
2099+ *
2100+ * The above copyright notice and this permission notice shall be
2101+ * included in all copies or substantial portions of the Software.
2102+ *
2103+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2104+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
2105+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2106+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
2107+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
2108+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2109+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2110+ * OTHER DEALINGS IN THE SOFTWARE.
2111+ */
2112+
2113+#ifndef HIPL_LIB_HIPDAEMON_SOCKET_WRAPPER_H
2114+#define HIPL_LIB_HIPDAEMON_SOCKET_WRAPPER_H
2115+
2116+#include <sys/socket.h>
2117+
2118+#include "lib/core/protodefs.h"
2119+
2120+
2121+int hipl_add_peer_info(const hip_hit_t *peer_hit,
2122+ const struct in6_addr *peer_addr);
2123+
2124+int hipl_socket(int domain, int type, int protocol);
2125+int hipl_close(int fd);
2126+int hipl_bind(int fd, const struct sockaddr *address, socklen_t address_len);
2127+int hipl_sendto(int fd, const void *msg, size_t len,
2128+ int flags, const struct sockaddr *dst_hit,
2129+ socklen_t dst_len);
2130+int hipl_recvfrom(int fd, void *buf, size_t len, int flags,
2131+ struct sockaddr *addr, socklen_t *addr_len);
2132+int hipl_connect(int fd, const struct sockaddr *addr, socklen_t addr_len);
2133+int hipl_accept(int fd, struct sockaddr *addr, socklen_t *addr_len);
2134+
2135+#endif /* HIPL_LIB_HIPDAEMON_SOCKET_WRAPPER_H */
2136
2137=== renamed file 'hipd/user.c' => 'lib/hipdaemon/user.c'
2138=== renamed file 'hipd/user.h' => 'lib/hipdaemon/user.h'
2139=== renamed file 'hipd/user_ipsec_hipd_msg.c' => 'lib/hipdaemon/user_ipsec_hipd_msg.c'
2140=== renamed file 'hipd/user_ipsec_hipd_msg.h' => 'lib/hipdaemon/user_ipsec_hipd_msg.h'
2141=== renamed file 'hipd/user_ipsec_sadb_api.c' => 'lib/hipdaemon/user_ipsec_sadb_api.c'
2142=== renamed file 'hipd/user_ipsec_sadb_api.h' => 'lib/hipdaemon/user_ipsec_sadb_api.h'
2143=== modified file 'modules/heartbeat/hipd/heartbeat.c'
2144--- modules/heartbeat/hipd/heartbeat.c 2011-10-25 21:14:16 +0000
2145+++ modules/heartbeat/hipd/heartbeat.c 2012-02-20 08:12:22 +0000
2146@@ -66,14 +66,14 @@
2147 #include <sys/types.h>
2148 #include <sys/socket.h>
2149
2150-#include "hipd/hadb.h"
2151-#include "hipd/init.h"
2152-#include "hipd/hip_socket.h"
2153-#include "hipd/maintenance.h"
2154-#include "hipd/nat.h"
2155-#include "hipd/output.h"
2156-#include "hipd/pkt_handling.h"
2157-#include "hipd/user.h"
2158+#include "lib/hipdaemon/hadb.h"
2159+#include "lib/hipdaemon/init.h"
2160+#include "lib/hipdaemon/hip_socket.h"
2161+#include "lib/hipdaemon/maintenance.h"
2162+#include "lib/hipdaemon/nat.h"
2163+#include "lib/hipdaemon/output.h"
2164+#include "lib/hipdaemon/pkt_handling.h"
2165+#include "lib/hipdaemon/user.h"
2166 #include "lib/core/common.h"
2167 #include "lib/core/debug.h"
2168 #include "lib/core/icomm.h"
2169
2170=== modified file 'modules/heartbeat_update/hipd/hb_update.c'
2171--- modules/heartbeat_update/hipd/hb_update.c 2011-10-25 21:14:16 +0000
2172+++ modules/heartbeat_update/hipd/hb_update.c 2012-02-20 08:12:22 +0000
2173@@ -40,8 +40,8 @@
2174 #include <stdint.h>
2175 #include <stdlib.h>
2176
2177-#include "hipd/hadb.h"
2178-#include "hipd/maintenance.h"
2179+#include "lib/hipdaemon/hadb.h"
2180+#include "lib/hipdaemon/maintenance.h"
2181 #include "lib/core/builder.h"
2182 #include "lib/core/common.h"
2183 #include "lib/core/debug.h"
2184
2185=== modified file 'modules/midauth/hipd/midauth.c'
2186--- modules/midauth/hipd/midauth.c 2012-01-16 22:06:09 +0000
2187+++ modules/midauth/hipd/midauth.c 2012-02-20 08:12:22 +0000
2188@@ -33,8 +33,8 @@
2189 #include <stdint.h>
2190 #include <string.h>
2191
2192-#include "hipd/hidb.h"
2193-#include "hipd/pkt_handling.h"
2194+#include "lib/hipdaemon/hidb.h"
2195+#include "lib/hipdaemon/pkt_handling.h"
2196 #include "lib/core/builder.h"
2197 #include "lib/core/common.h"
2198 #include "lib/core/ife.h"
2199
2200=== modified file 'modules/update/hipd/update.c'
2201--- modules/update/hipd/update.c 2012-01-17 08:34:18 +0000
2202+++ modules/update/hipd/update.c 2012-02-20 08:12:22 +0000
2203@@ -37,17 +37,17 @@
2204 #include <string.h>
2205
2206 #include "config.h"
2207-#include "hipd/cookie.h"
2208-#include "hipd/hadb.h"
2209-#include "hipd/hidb.h"
2210-#include "hipd/hipd.h"
2211-#include "hipd/input.h"
2212-#include "hipd/maintenance.h"
2213-#include "hipd/netdev.h"
2214-#include "hipd/nsupdate.h"
2215-#include "hipd/output.h"
2216-#include "hipd/pkt_handling.h"
2217-#include "hipd/user.h"
2218+#include "lib/hipdaemon/cookie.h"
2219+#include "lib/hipdaemon/hadb.h"
2220+#include "lib/hipdaemon/hidb.h"
2221+#include "lib/hipdaemon/hipd.h"
2222+#include "lib/hipdaemon/input.h"
2223+#include "lib/hipdaemon/maintenance.h"
2224+#include "lib/hipdaemon/netdev.h"
2225+#include "lib/hipdaemon/nsupdate.h"
2226+#include "lib/hipdaemon/output.h"
2227+#include "lib/hipdaemon/pkt_handling.h"
2228+#include "lib/hipdaemon/user.h"
2229 #include "lib/core/builder.h"
2230 #include "lib/core/debug.h"
2231 #include "lib/core/hip_udp.h"
2232
2233=== modified file 'modules/update/hipd/update_builder.c'
2234--- modules/update/hipd/update_builder.c 2012-02-15 17:37:10 +0000
2235+++ modules/update/hipd/update_builder.c 2012-02-20 08:12:22 +0000
2236@@ -34,8 +34,8 @@
2237 #include <string.h>
2238 #include <errno.h>
2239
2240-#include "hipd/hadb.h"
2241-#include "hipd/netdev.h"
2242+#include "lib/hipdaemon/hadb.h"
2243+#include "lib/hipdaemon/netdev.h"
2244 #include "lib/core/builder.h"
2245 #include "lib/core/ife.h"
2246 #include "lib/core/list.h"
2247
2248=== modified file 'modules/update/hipd/update_locator.c'
2249--- modules/update/hipd/update_locator.c 2011-12-12 14:18:05 +0000
2250+++ modules/update/hipd/update_locator.c 2012-02-20 08:12:22 +0000
2251@@ -34,7 +34,7 @@
2252 #include <string.h>
2253 #include <openssl/lhash.h>
2254
2255-#include "hipd/maintenance.h"
2256+#include "lib/hipdaemon/maintenance.h"
2257 #include "lib/core/builder.h"
2258 #include "lib/core/debug.h"
2259 #include "lib/core/ife.h"
2260
2261=== added file 'test/check_hipnetcat.c'
2262--- test/check_hipnetcat.c 1970-01-01 00:00:00 +0000
2263+++ test/check_hipnetcat.c 2012-02-20 08:12:22 +0000
2264@@ -0,0 +1,201 @@
2265+/*
2266+ * Copyright (c) 2010-2012 Aalto University and RWTH Aachen University.
2267+ *
2268+ * Permission is hereby granted, free of charge, to any person
2269+ * obtaining a copy of this software and associated documentation
2270+ * files (the "Software"), to deal in the Software without
2271+ * restriction, including without limitation the rights to use,
2272+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
2273+ * copies of the Software, and to permit persons to whom the
2274+ * Software is furnished to do so, subject to the following
2275+ * conditions:
2276+ *
2277+ * The above copyright notice and this permission notice shall be
2278+ * included in all copies or substantial portions of the Software.
2279+ *
2280+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2281+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
2282+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2283+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
2284+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
2285+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2286+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2287+ * OTHER DEALINGS IN THE SOFTWARE.
2288+ */
2289+
2290+/**
2291+ * @file
2292+ * @brief Tests of libhip (see doc/HACKING on unit tests).
2293+ */
2294+
2295+#include <arpa/inet.h>
2296+#include <check.h>
2297+#include <stdio.h>
2298+#include <stdlib.h>
2299+#include <sys/select.h>
2300+#include <sys/wait.h>
2301+#include <unistd.h>
2302+
2303+#include "lib/hipdaemon/hidb.h"
2304+#include "lib/hipdaemon/init.h"
2305+
2306+
2307+#define TEST_HIPNC_TIMEOUT 10
2308+
2309+static void hipnc_test_start(char *serv_argv[], char *client_argv[])
2310+{
2311+ pid_t serv_pid, client_pid, pid;
2312+ int status, i, j, round;
2313+ struct timeval tv;
2314+ int remain_cld = 2;
2315+
2316+ /* start hipnetcat server */
2317+ if ((pid = fork()) > 0) {
2318+ serv_pid = pid;
2319+ printf("server pid: %d\n", serv_pid);
2320+ }
2321+
2322+ if (pid == 0 && execv("test/hipnetcat", serv_argv)) {
2323+ perror("execv");
2324+ return;
2325+ }
2326+
2327+ /* start hipnetcat client */
2328+ sleep(1);
2329+ if ((pid = fork()) > 0) {
2330+ client_pid = pid;
2331+ printf("client_pid: %d\n", client_pid);
2332+ }
2333+
2334+ if (pid == 0 && execv("test/hipnetcat", client_argv)) {
2335+ perror("execv");
2336+ return;
2337+ }
2338+
2339+ /* check server & client status */
2340+ for (i = 0; i < TEST_HIPNC_TIMEOUT; i++) {
2341+ tv.tv_sec = 1;
2342+ tv.tv_usec = 0;
2343+ select(0, NULL, NULL, NULL, &tv);
2344+ round = remain_cld;
2345+ for (j = 0; j < round; j++) {
2346+ pid = waitpid(-1, &status, WNOHANG);
2347+ fail_if(pid > 0 && status != 0, "hipnetcat failed");
2348+ if (pid > 0 && status == 0) {
2349+ remain_cld--;
2350+ if (remain_cld == 0) {
2351+ break;
2352+ }
2353+ }
2354+ }
2355+ }
2356+
2357+ fail_if(remain_cld > 0, "hipnetcat test timeout!");
2358+}
2359+
2360+START_TEST(test_hipnc_lo_tcp)
2361+{
2362+ char *serv_argv[16];
2363+ char *client_argv[16];
2364+ struct in6_addr local_hit = { { { 0 } } };
2365+ char hit_buf[128];
2366+ int idx = 0;
2367+
2368+ /* init */
2369+ fail_if(hipl_lib_init(), "Failed to init libhip");
2370+ fail_if(hip_get_default_hit(&local_hit),
2371+ "Failed to load local hit for hipnetcat startup");
2372+ fail_if(inet_ntop(AF_INET6, &local_hit, hit_buf, 128) == NULL,
2373+ "Failed to parse hit to string.");
2374+
2375+ serv_argv[idx++] = strdup("hipnetcat");
2376+ serv_argv[idx++] = strdup("-l");
2377+ serv_argv[idx++] = strdup("-p");
2378+ serv_argv[idx++] = strdup("22300");
2379+ serv_argv[idx++] = strdup("-s");
2380+ serv_argv[idx++] = strdup("127.0.0.1");
2381+ serv_argv[idx++] = strdup("-t");
2382+ serv_argv[idx++] = NULL;
2383+
2384+ idx = 0;
2385+ client_argv[idx++] = strdup("hipnetcat");
2386+ client_argv[idx++] = strdup("-p");
2387+ client_argv[idx++] = strdup("22345");
2388+ client_argv[idx++] = strdup("-s");
2389+ client_argv[idx++] = strdup("127.0.0.1");
2390+ client_argv[idx++] = strdup("-d");
2391+ client_argv[idx++] = strdup("22300");
2392+ client_argv[idx++] = strdup("-t");
2393+ client_argv[idx++] = strdup("127.0.0.1");
2394+ client_argv[idx++] = hit_buf;
2395+ client_argv[idx++] = NULL;
2396+
2397+ hipnc_test_start(serv_argv, client_argv);
2398+}
2399+END_TEST
2400+
2401+START_TEST(test_hipnc_lo_udp)
2402+{
2403+ char *serv_argv[16];
2404+ char *client_argv[16];
2405+ struct in6_addr local_hit = { { { 0 } } };
2406+ char hit_buf[128];
2407+ int idx = 0;
2408+
2409+ /* init */
2410+ fail_if(hipl_lib_init(), "Failed to init libhip");
2411+ fail_if(hip_get_default_hit(&local_hit),
2412+ "Failed to load local hit for hipnetcat startup");
2413+ fail_if(inet_ntop(AF_INET6, &local_hit, hit_buf, 128) == NULL,
2414+ "Failed to parse hit to string.");
2415+
2416+ serv_argv[idx++] = strdup("hipnetcat");
2417+ serv_argv[idx++] = strdup("-l");
2418+ serv_argv[idx++] = strdup("-p");
2419+ serv_argv[idx++] = strdup("22300");
2420+ serv_argv[idx++] = strdup("-s");
2421+ serv_argv[idx++] = strdup("127.0.0.1");
2422+ serv_argv[idx++] = NULL;
2423+
2424+ idx = 0;
2425+ client_argv[idx++] = strdup("hipnetcat");
2426+ client_argv[idx++] = strdup("-p");
2427+ client_argv[idx++] = strdup("22345");
2428+ client_argv[idx++] = strdup("-s");
2429+ client_argv[idx++] = strdup("127.0.0.1");
2430+ client_argv[idx++] = strdup("-d");
2431+ client_argv[idx++] = strdup("22300");
2432+ client_argv[idx++] = strdup("127.0.0.1");
2433+ client_argv[idx++] = hit_buf;
2434+ client_argv[idx++] = NULL;
2435+
2436+ hipnc_test_start(serv_argv, client_argv);
2437+}
2438+END_TEST
2439+
2440+static Suite *hipnc_suite(void)
2441+{
2442+ Suite *s = suite_create("hipnc");
2443+
2444+ TCase *tc_hipnc_lo = tcase_create("hipnc_lo");
2445+ tcase_add_test(tc_hipnc_lo, test_hipnc_lo_udp);
2446+ tcase_add_test(tc_hipnc_lo, test_hipnc_lo_tcp);
2447+ tcase_set_timeout(tc_hipnc_lo, 20);
2448+ suite_add_tcase(s, tc_hipnc_lo);
2449+
2450+ return s;
2451+}
2452+
2453+int main(void)
2454+{
2455+ int number_failed;
2456+
2457+ Suite *s = hipnc_suite();
2458+ SRunner *sr = srunner_create(s);
2459+ srunner_run_all(sr, CK_NORMAL);
2460+
2461+ number_failed = srunner_ntests_failed(sr);
2462+ srunner_free(sr);
2463+
2464+ return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
2465+}
2466
2467=== modified file 'test/check_lib_core.c'
2468--- test/check_lib_core.c 2011-11-10 21:57:55 +0000
2469+++ test/check_lib_core.c 2012-02-20 08:12:22 +0000
2470@@ -1,5 +1,5 @@
2471 /*
2472- * Copyright (c) 2010-2011 Aalto University and RWTH Aachen University.
2473+ * Copyright (c) 2010-2012 Aalto University and RWTH Aachen University.
2474 *
2475 * Permission is hereby granted, free of charge, to any person
2476 * obtaining a copy of this software and associated documentation
2477@@ -43,6 +43,7 @@
2478 srunner_add_suite(sr, lib_core_crypto());
2479 #endif /* HAVE_EC_CRYPTO */
2480 srunner_add_suite(sr, lib_core_hostid());
2481+ srunner_add_suite(sr, lib_core_linkedlist());
2482
2483 srunner_run_all(sr, CK_NORMAL);
2484 number_failed = srunner_ntests_failed(sr);
2485
2486=== modified file 'test/hipd/lsidb.c'
2487--- test/hipd/lsidb.c 2012-01-18 21:09:47 +0000
2488+++ test/hipd/lsidb.c 2012-02-20 08:12:22 +0000
2489@@ -26,7 +26,7 @@
2490 #include <check.h>
2491 #include <stdlib.h>
2492
2493-#include "hipd/lsidb.c"
2494+#include "lib/hipdaemon/lsidb.c"
2495 #include "test_suites.h"
2496
2497 START_TEST(test_lsidb_allocate_lsi_valid)
2498
2499=== added file 'test/hipnetcat.c'
2500--- test/hipnetcat.c 1970-01-01 00:00:00 +0000
2501+++ test/hipnetcat.c 2012-02-20 08:12:22 +0000
2502@@ -0,0 +1,344 @@
2503+/*
2504+ * Copyright (c) 2010-2012 Aalto University and RWTH Aachen University.
2505+ *
2506+ * Permission is hereby granted, free of charge, to any person
2507+ * obtaining a copy of this software and associated documentation
2508+ * files (the "Software"), to deal in the Software without
2509+ * restriction, including without limitation the rights to use,
2510+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
2511+ * copies of the Software, and to permit persons to whom the
2512+ * Software is furnished to do so, subject to the following
2513+ * conditions:
2514+ *
2515+ * The above copyright notice and this permission notice shall be
2516+ * included in all copies or substantial portions of the Software.
2517+ *
2518+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2519+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
2520+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2521+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
2522+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
2523+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2524+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2525+ * OTHER DEALINGS IN THE SOFTWARE.
2526+ */
2527+
2528+/**
2529+ * @file
2530+ * The hipnetcat sample program using libhip.
2531+ */
2532+
2533+#define _BSD_SOURCE
2534+
2535+#include <arpa/inet.h>
2536+#include <errno.h>
2537+#include <netdb.h>
2538+#include <stdio.h>
2539+#include <stdlib.h>
2540+#include <string.h>
2541+#include <unistd.h>
2542+
2543+#include "lib/core/debug.h"
2544+#include "lib/core/hip_udp.h"
2545+#include "lib/core/ife.h"
2546+#include "lib/core/linkedlist.h"
2547+#include "lib/core/prefix.h"
2548+#include "lib/hipdaemon/init.h"
2549+#include "lib/hipdaemon/socket_wrapper.h"
2550+
2551+#define HIPNC_SERVER_MODE 0x0001
2552+#define HIPNC_UDP_MODE 0x0002
2553+#define HIPNC_IP6 0x0004
2554+
2555+static int dst_port = HIP_NAT_UDP_PORT;
2556+static unsigned int hipnc_flag = HIPNC_UDP_MODE & ~HIPNC_SERVER_MODE & ~HIPNC_IP6;
2557+static struct sockaddr_storage local_ss;
2558+static struct hip_ll hit_list;
2559+static struct hip_ll locator_list;
2560+
2561+
2562+static int create_hip_socket(unsigned int flag)
2563+{
2564+ return hipl_socket((flag & HIPNC_IP6) ? AF_INET6 : AF_INET,
2565+ (flag & HIPNC_UDP_MODE) ? SOCK_DGRAM : SOCK_STREAM,
2566+ (flag & HIPNC_UDP_MODE) ? IPPROTO_UDP : IPPROTO_TCP);
2567+}
2568+
2569+static int hipnc_run_client(const unsigned int flag,
2570+ struct sockaddr *local_addr, socklen_t addr_len,
2571+ struct sockaddr_in6 *peer_hit_saddr)
2572+{
2573+ int fd, err = 0;
2574+ unsigned int len = 0;
2575+ static char buf[256] = { 0 };
2576+
2577+ if ((fd = create_hip_socket(flag)) < 0) {
2578+ HIP_PERROR("Failed to create hip socket.");
2579+ return -1;
2580+ }
2581+
2582+ HIP_IFEL(hipl_bind(fd, local_addr, addr_len), -1,
2583+ "hipl_bind(): %s\n", strerror(errno));
2584+
2585+ if (!(flag & HIPNC_UDP_MODE)) {
2586+ HIP_IFEL(hipl_connect(fd, (struct sockaddr *) peer_hit_saddr,
2587+ sizeof(struct sockaddr_in6)),
2588+ -1, "hipl_connect(): %s\n", strerror(errno));
2589+ }
2590+
2591+ sprintf(buf, "Hello, sailor!");
2592+ len = hipl_sendto(fd, buf, sizeof(buf), 0,
2593+ (struct sockaddr *) peer_hit_saddr,
2594+ sizeof(struct sockaddr_in6));
2595+
2596+ HIP_INFO("Sent %d bytes\n", len);
2597+ if (len < sizeof(buf)) {
2598+ err = -1;
2599+ }
2600+
2601+out_err:
2602+ hipl_close(fd);
2603+
2604+ return err;
2605+}
2606+
2607+static int hipnc_run_server(const unsigned int flag,
2608+ struct sockaddr *local_addr, socklen_t addr_len)
2609+{
2610+ int fd;
2611+ int err = 0, len = 0;
2612+ int new_fd = 0;
2613+ struct sockaddr_in6 peer_hit_saddr = { 0 };
2614+ socklen_t socklen = sizeof(peer_hit_saddr);
2615+ char buf[256] = { 0 };
2616+
2617+ if ((fd = create_hip_socket(flag)) < 0) {
2618+ HIP_PERROR("Failed to create hip socket");
2619+ return -1;
2620+ }
2621+
2622+ HIP_IFEL(hipl_bind(fd, local_addr, addr_len), -1,
2623+ "hipl_bind(): %s\n", strerror(errno));
2624+
2625+ if (!(flag & HIPNC_UDP_MODE)) {
2626+ HIP_IFEL(listen(fd, 0), -1, "listen(): %s\n", strerror(errno));
2627+ new_fd = hipl_accept(fd, (struct sockaddr *) &peer_hit_saddr, &socklen);
2628+ HIP_IFEL(new_fd < 0, -1, "hipl_accept(): %s\n", strerror(errno));
2629+ } else {
2630+ new_fd = fd;
2631+ }
2632+
2633+ while ((len = hipl_recvfrom(new_fd, buf, sizeof(buf), 0,
2634+ (struct sockaddr *) &peer_hit_saddr,
2635+ &socklen)) > 0) {
2636+ HIP_INFO("Received %d bytes\n", len);
2637+ printf("%.256s\n", buf);
2638+ HIP_INFO_IN6ADDR("Peer HIT", &peer_hit_saddr.sin6_addr);
2639+ }
2640+
2641+ if (len < 0) {
2642+ perror("hip_recvfrom");
2643+ err = len;
2644+ }
2645+
2646+out_err:
2647+ if (new_fd > 0) {
2648+ close(new_fd);
2649+ }
2650+ hipl_close(fd);
2651+
2652+ return err;
2653+}
2654+
2655+static void usage(void)
2656+{
2657+ printf("Hipnetcat program.\n"
2658+ "Usage: hipnetcat [-hlt] [-p source_port] [-s source_ip_address]\n"
2659+ " [-d dest_port] [peer_identifier[s]]\n\n"
2660+ " -h: help\n"
2661+ " -l: Listening mode, hipnetcat acts as the server side.\n"
2662+ " -t: Use TCP as transport protocol, otherwise UDP will be used.\n");
2663+}
2664+
2665+static int parse_source_to_ss(const char *ip, int port, struct sockaddr_storage *ss)
2666+{
2667+ struct sockaddr_in *sa4;
2668+ struct sockaddr_in6 *sa6;
2669+
2670+ sa4 = (struct sockaddr_in *) ss;
2671+ sa6 = (struct sockaddr_in6 *) ss;
2672+
2673+ /*try to parse ip to v4 format */
2674+ ss->ss_family = AF_INET;
2675+ sa4->sin_port = htons(port);
2676+
2677+ if (inet_pton(AF_INET, ip, &sa4->sin_addr) > 0) {
2678+ hipnc_flag &= ~HIPNC_IP6;
2679+ return 0;
2680+ }
2681+
2682+ /* v4 parse failed, try v6 format */
2683+ memset(ss, 0, sizeof(*ss));
2684+ ss->ss_family = AF_INET6;
2685+ sa6->sin6_port = htons(port);
2686+
2687+ if (inet_pton(AF_INET6, ip, &sa6->sin6_addr) > 0) {
2688+ hipnc_flag |= HIPNC_IP6;
2689+ return 0;
2690+ }
2691+
2692+ /* Both v4 and v6 failed, return error */
2693+ return -1;
2694+}
2695+
2696+static int parse_arguments(int argc, char *argv[])
2697+{
2698+ int err = 0;
2699+ char opt = 0, *arg = NULL;
2700+ const char *src_ip = "0.0.0.0";
2701+ int src_port = HIP_NAT_UDP_PORT;
2702+ struct in6_addr *paddr6 = NULL;
2703+
2704+ while ((opt = getopt(argc, argv, "hltp:d:s:")) != -1) {
2705+ switch (opt) {
2706+ case 'l':
2707+ hipnc_flag |= HIPNC_SERVER_MODE;
2708+ break;
2709+ case 'p':
2710+ src_port = atoi(optarg);
2711+ break;
2712+ case 'd':
2713+ dst_port = atoi(optarg);
2714+ break;
2715+ case 's':
2716+ src_ip = optarg;
2717+ break;
2718+ case 't':
2719+ hipnc_flag &= ~HIPNC_UDP_MODE;
2720+ break;
2721+ case 'u':
2722+ hipnc_flag |= HIPNC_UDP_MODE;
2723+ break;
2724+ case 'h':
2725+ usage();
2726+ exit(EXIT_SUCCESS);
2727+ default:
2728+ HIP_ERROR("Invalid option.\n");
2729+ return -1;
2730+ }
2731+ }
2732+
2733+ if (parse_source_to_ss(src_ip, src_port, &local_ss) < 0) {
2734+ HIP_ERROR("Failed to parse source ip address.\n");
2735+ return -1;
2736+ }
2737+
2738+ if (!(hipnc_flag & HIPNC_SERVER_MODE) && optind == argc) {
2739+ HIP_ERROR("No identifier has been provided.\n");
2740+ return -1;
2741+ }
2742+
2743+ /* parse identifiers */
2744+ while (optind < argc) {
2745+ struct addrinfo *res;
2746+ struct addrinfo addr_hint = { 0 };
2747+ struct sockaddr_in *sa4;
2748+ struct sockaddr_in6 *sa6;
2749+
2750+ arg = argv[optind++];
2751+ addr_hint.ai_family = AF_UNSPEC;
2752+
2753+ HIP_DEBUG("Parse identifier: %s\n", arg);
2754+ err = getaddrinfo(arg, NULL, &addr_hint, &res);
2755+ HIP_IFEL(err, -1, "failed to parse: %s, %s\n", arg, gai_strerror(err));
2756+ for (; res != NULL; res = res->ai_next) {
2757+ paddr6 = malloc(sizeof(struct in6_addr));
2758+ HIP_IFEL(!paddr6, -1, "malloc() failed\n");
2759+ if (res->ai_family == AF_INET) {
2760+ sa4 = (struct sockaddr_in *) res->ai_addr;
2761+ IPV4_TO_IPV6_MAP(&sa4->sin_addr, paddr6);
2762+ hip_ll_add_last(&locator_list, paddr6);
2763+ } else if (res->ai_family == AF_INET6) {
2764+ sa6 = (struct sockaddr_in6 *) res->ai_addr;
2765+ memcpy(paddr6, &sa6->sin6_addr, sizeof(struct in6_addr));
2766+ if (ipv6_addr_is_hit(paddr6)) {
2767+ hip_ll_add_last(&hit_list, paddr6);
2768+ } else {
2769+ hip_ll_add_last(&locator_list, paddr6);
2770+ }
2771+ }
2772+ }
2773+ freeaddrinfo(res);
2774+ }
2775+
2776+ if (!(hipnc_flag & HIPNC_SERVER_MODE) && locator_list.element_count == 0) {
2777+ HIP_ERROR("No locator is provided.\n");
2778+ return -1;
2779+ }
2780+
2781+out_err:
2782+ if (err) {
2783+ free(paddr6);
2784+ }
2785+ return err;
2786+}
2787+
2788+int main(int argc, char *argv[])
2789+{
2790+ int err = 0;
2791+ struct sockaddr_in6 peer_hit_saddr = { 0 };
2792+
2793+ if (parse_arguments(argc, argv)) {
2794+ usage();
2795+ exit(EXIT_FAILURE);
2796+ }
2797+
2798+ if (hipl_lib_init() < 0) {
2799+ HIP_ERROR("libhipd init failed.\n");
2800+ return -1;
2801+ }
2802+
2803+ /* add peer hit-addr mapping */
2804+ if (hit_list.element_count != 0) {
2805+ const struct hip_ll_node *hit_i = NULL;
2806+ const struct hip_ll_node *locator_i = NULL;
2807+ struct in6_addr *p_hit;
2808+ struct in6_addr *p_locator;
2809+ while ((hit_i = hip_ll_iterate(&hit_list, hit_i))) {
2810+ p_hit = (struct in6_addr *) hit_i->ptr;
2811+ while ((locator_i = hip_ll_iterate(&locator_list, locator_i))) {
2812+ p_locator = (struct in6_addr *) locator_i->ptr;
2813+ HIP_DEBUG_HIT("HIPNC input hit:", p_hit);
2814+ hipl_add_peer_info(p_hit, p_locator);
2815+ }
2816+ }
2817+ }
2818+
2819+ HIP_DEBUG("hipnc_flag = %08x\n", hipnc_flag);
2820+
2821+ if (hipnc_flag & HIPNC_SERVER_MODE) {
2822+ return hipnc_run_server(hipnc_flag, (struct sockaddr *) &local_ss,
2823+ sizeof(local_ss));
2824+ } else if (hit_list.element_count != 0) {
2825+ const struct hip_ll_node *hit_i = NULL;
2826+ while ((hit_i = hip_ll_iterate(&hit_list, hit_i))) {
2827+ memcpy(&peer_hit_saddr.sin6_addr, hit_i->ptr, sizeof(struct in6_addr));
2828+ peer_hit_saddr.sin6_port = htons(dst_port);
2829+ HIP_DEBUG_HIT("Connect to hit", &peer_hit_saddr.sin6_addr);
2830+ HIP_DEBUG("Connect to port: %d\n", dst_port);
2831+
2832+ err = hipnc_run_client(hipnc_flag, (struct sockaddr *) &local_ss,
2833+ sizeof(local_ss), &peer_hit_saddr);
2834+
2835+ /* if no errors, finish execution, otherwise we try another HIT */
2836+ if (err == 0) {
2837+ return 0;
2838+ }
2839+ }
2840+ return err;
2841+ } else {
2842+ //TODO opportunistic mode
2843+ HIP_ERROR("opportunistic mode not supported. Please provide a HIT\n");
2844+ return 0;
2845+ }
2846+}
2847
2848=== added file 'test/lib/core/linkedlist.c'
2849--- test/lib/core/linkedlist.c 1970-01-01 00:00:00 +0000
2850+++ test/lib/core/linkedlist.c 2012-02-20 08:12:22 +0000
2851@@ -0,0 +1,120 @@
2852+/*
2853+ * Copyright (c) 2012 Aalto University and RWTH Aachen University.
2854+ *
2855+ * Permission is hereby granted, free of charge, to any person
2856+ * obtaining a copy of this software and associated documentation
2857+ * files (the "Software"), to deal in the Software without
2858+ * restriction, including without limitation the rights to use,
2859+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
2860+ * copies of the Software, and to permit persons to whom the
2861+ * Software is furnished to do so, subject to the following
2862+ * conditions:
2863+ *
2864+ * The above copyright notice and this permission notice shall be
2865+ * included in all copies or substantial portions of the Software.
2866+ *
2867+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2868+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
2869+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2870+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
2871+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
2872+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2873+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2874+ * OTHER DEALINGS IN THE SOFTWARE.
2875+ */
2876+
2877+#include <check.h>
2878+#include <stdlib.h>
2879+#include <stdio.h>
2880+
2881+#include "lib/core/linkedlist.h"
2882+#include "test_suites.h"
2883+
2884+
2885+static struct hip_ll *create_list(int item_count)
2886+{
2887+ struct hip_ll *const list = malloc(sizeof(struct hip_ll));
2888+ int i;
2889+ unsigned int *value = NULL;
2890+
2891+ hip_ll_init(list);
2892+ for (i = 0; i < item_count; i++) {
2893+ value = malloc(sizeof(int));
2894+ *value = i;
2895+ hip_ll_add_last(list, value);
2896+ }
2897+
2898+ return list;
2899+}
2900+
2901+START_TEST(test_hip_ll_del_by_ptr_first_elem_deleted)
2902+{
2903+ unsigned int list_size = 5;
2904+ struct hip_ll *list = create_list(list_size);
2905+ unsigned int *value;
2906+
2907+ fail_unless(list != NULL && list->element_count == list_size);
2908+
2909+ value = hip_ll_get(list, 0);
2910+ fail_unless(value != NULL && *value == 0);
2911+
2912+ hip_ll_del_by_ptr(list, value, free);
2913+ list_size--;
2914+ fail_unless(list->element_count == list_size);
2915+ value = hip_ll_get(list, 0);
2916+ fail_unless(value != NULL && *value == 1);
2917+}
2918+END_TEST
2919+
2920+START_TEST(test_hip_ll_del_by_ptr_middle_elem_deleted)
2921+{
2922+ unsigned int list_size = 5;
2923+ struct hip_ll *list = create_list(list_size);
2924+ unsigned int *value;
2925+ unsigned int idx = list_size / 2;
2926+
2927+ fail_unless(list != NULL && list->element_count == list_size);
2928+
2929+ value = hip_ll_get(list, idx);
2930+ fail_unless(value != NULL && *value == idx);
2931+
2932+ hip_ll_del_by_ptr(list, value, free);
2933+ list_size--;
2934+ fail_unless(list->element_count == list_size);
2935+ value = hip_ll_get(list, idx);
2936+ fail_unless(value != NULL && *value == idx + 1);
2937+}
2938+END_TEST
2939+
2940+START_TEST(test_hip_ll_del_by_ptr_last_elem_deleted)
2941+{
2942+ unsigned int list_size = 5;
2943+ struct hip_ll *list = create_list(list_size);
2944+ unsigned int *value;
2945+ unsigned int idx = list_size - 1;
2946+
2947+ fail_unless(list != NULL && list->element_count == list_size);
2948+
2949+ value = hip_ll_get(list, idx);
2950+ fail_unless(value != NULL && *value == idx);
2951+
2952+ hip_ll_del_by_ptr(list, value, free);
2953+ list_size--;
2954+ fail_unless(list->element_count == list_size);
2955+ value = hip_ll_get(list, idx - 1);
2956+ fail_unless(value != NULL && *value == idx - 1);
2957+}
2958+END_TEST
2959+
2960+Suite *lib_core_linkedlist(void)
2961+{
2962+ Suite *s = suite_create("lib/core/linkedlist");
2963+
2964+ TCase *tc_core = tcase_create("Core");
2965+ tcase_add_test(tc_core, test_hip_ll_del_by_ptr_first_elem_deleted);
2966+ tcase_add_test(tc_core, test_hip_ll_del_by_ptr_middle_elem_deleted);
2967+ tcase_add_test(tc_core, test_hip_ll_del_by_ptr_last_elem_deleted);
2968+ suite_add_tcase(s, tc_core);
2969+
2970+ return s;
2971+}
2972
2973=== modified file 'test/lib/core/test_suites.h'
2974--- test/lib/core/test_suites.h 2011-07-18 13:10:26 +0000
2975+++ test/lib/core/test_suites.h 2012-02-20 08:12:22 +0000
2976@@ -1,5 +1,5 @@
2977 /*
2978- * Copyright (c) 2010-2011 Aalto University and RWTH Aachen University.
2979+ * Copyright (c) 2010-2012 Aalto University and RWTH Aachen University.
2980 *
2981 * Permission is hereby granted, free of charge, to any person
2982 * obtaining a copy of this software and associated documentation
2983@@ -33,5 +33,6 @@
2984 Suite *lib_core_hostid(void);
2985 Suite *lib_core_solve(void);
2986 Suite *lib_core_straddr(void);
2987+Suite *lib_core_linkedlist(void);
2988
2989 #endif /* HIP_TEST_LIB_CORE_TEST_SUITES_H */

Subscribers

People subscribed via source and target branches

to all changes: