summaryrefslogtreecommitdiff
path: root/shared/n-dhcp4/src/n-dhcp4-c-connection.c
blob: 3dd053567885b796f562ddff749bee7eec137e2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
/*
 * DHCPv4 Client Connection
 *
 * XXX
 */

#include <assert.h>
#include <c-stdaux.h>
#include <errno.h>
#include <limits.h>
#include <sys/socket.h> /* needed by linux/netdevice.h */
#include <linux/netdevice.h>
#include <net/if_arp.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/epoll.h>
#include "n-dhcp4-private.h"
#include "util/packet.h"

/**
 * n_dhcp4_c_connection_init() - initialize client connection
 * @connection:                 connection to operate on
 * @client_config:              client configuration to use
 * @probe_config:               client probe configuration to use
 * @fd_epoll:                   epoll context to attach to, or -1
 *
 * This initializes a new client connection using the configuration given in
 * @client_config and @probe_config.
 *
 * The client-configuration given as @client_config must survive the lifetime
 * of @connection. It is pinned in the connection and used all over the place.
 * The caller must guarantee that the configuration is not deallocated in the
 * meantime. Same is true for @probe_config.
 *
 * The new connection automatically attaches to the epoll context given as
 * @fd_epoll. The epoll FD is retained in the connection and the caller must
 * guarantee that it lives as long as the connection.
 * The caller is explicitly allowed to pass -1 as @fd_epoll, in which case the
 * connection will initialize correctly, but will not be in a usable state.
 * That is, any call to n_dhcp4_c_connection_listen() will fail, since it will
 * be unable to attach to the epoll context. Such a connection can be used to
 * get a detached object that behaves sound, but provides no runtime.
 *
 * Return: 0 on success, negative error code on failure.
 */
int n_dhcp4_c_connection_init(NDhcp4CConnection *connection,
                              NDhcp4ClientConfig *client_config,
                              NDhcp4ClientProbeConfig *probe_config,
                              int fd_epoll) {
        *connection = (NDhcp4CConnection)N_DHCP4_C_CONNECTION_NULL(*connection);
        connection->client_config = client_config;
        connection->probe_config = probe_config;
        connection->fd_epoll = fd_epoll;

        /*
         * We explicitly allow initializing connections with an invalid
         * epoll-fd. The resulting connection immediately transitions into the
         * CLOSED state. This allows the caller to create dummy connections
         * useful to provide asynchronous constructor-feedback in the API.
         *
         * The effect of this is as if you immediately call
         * n_dhcp4_c_connection_close() on the new connection. However, by
         * directly passing -1 in the constructor, you are guaranteed not even
         * the constructor can ever mess with your epoll-set.
         */
        if (connection->fd_epoll < 0)
                connection->state = N_DHCP4_C_CONNECTION_STATE_CLOSED;

        return 0;
}

/**
 * n_dhcp4_c_connection_deinit() - deinitialize client connection
 * @connection:                 connection to operate on
 *
 * This deinitializes a connection that was previously initialized via
 * n_dhcp4_c_connection_init(). It will tear down all allocated state and
 * release it.
 *
 * Once this function returns, @connection is re-initialized to
 * N_DHCP4_C_CONNECTION_NULL. If this function is called on a deinitialized
 * connection, it is a no-op.
 */
void n_dhcp4_c_connection_deinit(NDhcp4CConnection *connection) {
        n_dhcp4_c_connection_close(connection);
        n_dhcp4_outgoing_free(connection->request);
        *connection = (NDhcp4CConnection)N_DHCP4_C_CONNECTION_NULL(*connection);
}

static void n_dhcp4_c_connection_outgoing_set_secs(NDhcp4Outgoing *message) {
        uint64_t secs;

        /*
         * This function sets the `secs` field for outgoing messages. It
         * expects the base-time and start-time to be already set by the
         * caller.
         * For a given outgoing message, its `secs` field describes the time
         * (in seconds) between the start of the transaction this message is
         * part of and the start of the operational process (also called the
         * base time here).
         *
         * The operational process in the DHCP sense describes the entire
         * process of requesting a lease and acquiring it. That is, it starts
         * with the caller's intent to request a lease, and it ends when we
         * got granted a lease. The act of refreshing a lease is, in itself, a
         * new operational process. The base-time describes the start-time
         * recorded when such a process as initiated.
         *
         * A transaction in the DHCP sense describes a request+reply
         * combination, in most cases. That is, the time a request is sent is
         * the start-time of a transaction. In the ideal case, the start-time
         * of the first transaction in an operational process matches the
         * base-time. However, transactions are often delayed with a randomized
         * offset to reduce traffic during network bursts.
         * In some cases, however, transactions are composed out of multiple
         * requests+reply combinations. This includes, for instance, the SELECT
         * message following an OFFER. The specification clearly says that
         * those must be considered a single transaction and thus share the
         * transaction start-time.
         *
         * The `secs` field, thus, describes how long a client has been busy
         * requesting a lease. DHCP servers and proxies do use it to prioritize
         * clients.
         *
         * Note: Some DHCP relays reject a `secs` value of 0 (which might look
         *       like it is uninitialized). Hence, we always clamp the value to
         *       the range `[1, 65535]`.
         */

        secs = message->userdata.base_time - message->userdata.start_time;
        secs /= 1000ULL * 1000ULL * 1000ULL; /* nsecs to secs */
        secs = C_CLAMP(secs, 1, UINT16_MAX);

        n_dhcp4_outgoing_set_secs(message, secs);
}

int n_dhcp4_c_connection_listen(NDhcp4CConnection *connection) {
        _c_cleanup_(c_closep) int fd_packet = -1;
        int r;

        if (connection->state == N_DHCP4_C_CONNECTION_STATE_PACKET)
                return 0;

        c_assert(connection->state == N_DHCP4_C_CONNECTION_STATE_INIT ||
                 connection->state == N_DHCP4_C_CONNECTION_STATE_DRAINING ||
                 connection->state == N_DHCP4_C_CONNECTION_STATE_UDP);

        if (connection->fd_packet >= 0) {
                epoll_ctl(connection->fd_epoll, EPOLL_CTL_DEL, connection->fd_packet, NULL);
                connection->fd_packet = c_close(connection->fd_packet);
        }

        if (connection->fd_udp >= 0) {
                epoll_ctl(connection->fd_epoll, EPOLL_CTL_DEL, connection->fd_udp, NULL);
                connection->fd_udp = c_close(connection->fd_udp);
        }

        r = n_dhcp4_c_socket_packet_new(&fd_packet, connection->client_config->ifindex);
        if (r)
                return r;

        r = epoll_ctl(connection->fd_epoll,
                      EPOLL_CTL_ADD,
                      fd_packet,
                      &(struct epoll_event){
                              .events = EPOLLIN,
                              .data = { .u32 = N_DHCP4_CLIENT_EPOLL_IO },
                      });
        if (r < 0)
                return -errno;

        connection->state = N_DHCP4_C_CONNECTION_STATE_PACKET;
        connection->fd_packet = fd_packet;
        fd_packet = -1;
        return 0;
}

int n_dhcp4_c_connection_connect(NDhcp4CConnection *connection,
                                 const struct in_addr *client,
                                 const struct in_addr *server) {
        int r, fd_udp;

        c_assert(connection->state == N_DHCP4_C_CONNECTION_STATE_PACKET);

        r = n_dhcp4_c_socket_udp_new(&fd_udp,
                                     connection->client_config->ifindex,
                                     client,
                                     server);
        if (r)
                return r;

        r = epoll_ctl(connection->fd_epoll,
                      EPOLL_CTL_ADD,
                      fd_udp,
                      &(struct epoll_event){
                              .events = EPOLLIN,
                              .data = { .u32 = N_DHCP4_CLIENT_EPOLL_IO },
                      });
        if (r < 0) {
                r = -errno;
                goto exit_fd;
        }

        r = packet_shutdown(connection->fd_packet);
        if (r < 0)
                goto exit_epoll;

        connection->state = N_DHCP4_C_CONNECTION_STATE_DRAINING;
        connection->fd_udp = fd_udp;
        connection->client_ip = client->s_addr;
        connection->server_ip = server->s_addr;
        fd_udp = -1;
        return 0;

exit_epoll:
        epoll_ctl(connection->fd_epoll, EPOLL_CTL_DEL, fd_udp, NULL);
exit_fd:
        close(fd_udp);
        return r;
}

void n_dhcp4_c_connection_close(NDhcp4CConnection *connection) {
        if (connection->fd_udp >= 0) {
                epoll_ctl(connection->fd_epoll, EPOLL_CTL_DEL, connection->fd_udp, NULL);
                connection->fd_udp = c_close(connection->fd_udp);
        }

        if (connection->fd_packet >= 0) {
                epoll_ctl(connection->fd_epoll, EPOLL_CTL_DEL, connection->fd_packet, NULL);
                connection->fd_packet = c_close(connection->fd_packet);
        }

        connection->fd_epoll = -1;
        connection->state = N_DHCP4_C_CONNECTION_STATE_CLOSED;
}

static int n_dhcp4_c_connection_verify_incoming(NDhcp4CConnection *connection,
                                                NDhcp4Incoming *message,
                                                uint8_t *typep) {
        NDhcp4Header *header = n_dhcp4_incoming_get_header(message);
        uint8_t type;
        uint32_t request_xid;
        uint8_t *id;
        size_t n_id;
        int r;

        r = n_dhcp4_incoming_query_message_type(message, &type);
        if (r) {
                if (r == N_DHCP4_E_UNSET)
                        return N_DHCP4_E_MALFORMED;
                else
                        return r;
        }

        switch (type) {
        case N_DHCP4_MESSAGE_OFFER:
        case N_DHCP4_MESSAGE_ACK:
        case N_DHCP4_MESSAGE_NAK:
                /*
                 * Only accept replies if there is a pending request, and it
                 * has a matching transaction id.
                 */
                if (!connection->request)
                        return N_DHCP4_E_UNEXPECTED;

                n_dhcp4_outgoing_get_xid(connection->request, &request_xid);
                if (header->xid != request_xid)
                        return N_DHCP4_E_UNEXPECTED;

                break;
        case N_DHCP4_MESSAGE_FORCERENEW:
                /*
                 * Force renew messages are triggered by a server, and do not
                 * match a pending request.
                 */
                break;
        default:
                return N_DHCP4_E_UNEXPECTED;
        }

        /*
         * In case our transport makes use of the 'chaddr' field, make sure it
         * matches exactly our address.
         */
        switch (connection->client_config->transport) {
        case N_DHCP4_TRANSPORT_ETHERNET:
                c_assert(connection->client_config->n_mac == ETH_ALEN);

                if (header->hlen != ETH_ALEN)
                        return N_DHCP4_E_UNEXPECTED;
                if (memcmp(header->chaddr, connection->client_config->mac, ETH_ALEN) != 0)
                        return N_DHCP4_E_UNEXPECTED;

                break;
        case N_DHCP4_TRANSPORT_INFINIBAND:
                if (header->hlen != 0)
                        return N_DHCP4_E_UNEXPECTED;

                break;
        }

        /*
         * If a server passes us back a client ID, it must be the one we
         * provided. We ignore any packets that have mismatching client-ids.
         */
        id = NULL;
        n_id = 0;
        r = n_dhcp4_incoming_query(message, N_DHCP4_OPTION_CLIENT_IDENTIFIER, &id, &n_id);
        if (r) {
                if (r != N_DHCP4_E_UNSET)
                        return r;
        } else {
                if (n_id != connection->client_config->n_client_id)
                        return N_DHCP4_E_UNEXPECTED;
                if (memcmp(id, connection->client_config->client_id, n_id) != 0)
                        return N_DHCP4_E_UNEXPECTED;
        }

        *typep = type;
        return 0;
}

void n_dhcp4_c_connection_get_timeout(NDhcp4CConnection *connection,
                                      uint64_t *timeoutp) {
        uint64_t timeout;
        size_t n_send;

        if (!connection->request) {
                *timeoutp = 0;
                return;
        }

        switch (connection->request->userdata.type) {
        case N_DHCP4_C_MESSAGE_DISCOVER:
        case N_DHCP4_C_MESSAGE_SELECT:
        case N_DHCP4_C_MESSAGE_INFORM:
                /*
                 * Resend with an exponential backoff and a one second random
                 * slack, from a minimum of two seconds to a maximum of sixty
                 * four.
                 *
                 * Note that the RFC says to start at four rather than two
                 * seconds, and use [-1,1] slack, rather than [0,1].
                 */
                n_send = connection->request->userdata.n_send;
                if (n_send >= 6)
                        n_send = 6;

                timeout = connection->request->userdata.send_time + ((1ULL << n_send) * 1000000000ULL) + connection->request->userdata.send_jitter;

                break;
        case N_DHCP4_C_MESSAGE_REBIND:
        case N_DHCP4_C_MESSAGE_RENEW:
        case N_DHCP4_C_MESSAGE_REBOOT:
                /*
                 * Resend every sixty seconds with a one second random slack.
                 *
                 * Note that the RFC says to do this at most once, but we do
                 * it until we are cancelled.
                 */
                timeout = connection->request->userdata.send_time + (60ULL * 1000000000ULL) + connection->request->userdata.send_jitter;

                break;
        case N_DHCP4_C_MESSAGE_DECLINE:
        case N_DHCP4_C_MESSAGE_RELEASE:
                /* XXX make sure these message types are never pinned? */
                timeout = 0;
                break;
        default:
                c_assert(0);
        }

        *timeoutp = timeout;
}

static int n_dhcp4_c_connection_packet_broadcast(NDhcp4CConnection *connection,
                                                 NDhcp4Outgoing *message) {
        int r;

        c_assert(connection->state == N_DHCP4_C_CONNECTION_STATE_PACKET);

        r = n_dhcp4_c_socket_packet_send(connection->fd_packet,
                                         connection->client_config->ifindex,
                                         connection->client_config->broadcast_mac,
                                         connection->client_config->n_broadcast_mac,
                                         message);
        if (r)
                return r;

        return 0;
}

static int n_dhcp4_c_connection_udp_broadcast(NDhcp4CConnection *connection,
                                              NDhcp4Outgoing *message) {
        int r;

        c_assert(connection->state == N_DHCP4_C_CONNECTION_STATE_DRAINING ||
               connection->state == N_DHCP4_C_CONNECTION_STATE_UDP);

        r = n_dhcp4_c_socket_udp_broadcast(connection->fd_udp, message);
        if (r)
                return r;

        return 0;
}

static int n_dhcp4_c_connection_udp_send(NDhcp4CConnection *connection,
                                         NDhcp4Outgoing *message) {
        int r;

        c_assert(connection->state == N_DHCP4_C_CONNECTION_STATE_DRAINING ||
               connection->state == N_DHCP4_C_CONNECTION_STATE_UDP);

        r = n_dhcp4_c_socket_udp_send(connection->fd_udp, message);
        if (r)
                return r;

        return 0;
}

static void n_dhcp4_c_connection_init_header(NDhcp4CConnection *connection,
                                             NDhcp4Header *header) {
        bool broadcast = connection->client_config->request_broadcast;

        header->op = N_DHCP4_OP_BOOTREQUEST;

        switch (connection->client_config->transport) {
        case N_DHCP4_TRANSPORT_ETHERNET:
                c_assert(connection->client_config->n_mac == ETH_ALEN);

                header->htype = ARPHRD_ETHER;
                header->hlen = ETH_ALEN;
                memcpy(header->chaddr, connection->client_config->mac, ETH_ALEN);
                break;
        case N_DHCP4_TRANSPORT_INFINIBAND:
                header->htype = ARPHRD_INFINIBAND;
                header->hlen = 0;

                /* infiniband mandates to request broadcasts */
                broadcast = true;
                break;
        default:
                abort();
                break;
        }

        if (connection->client_ip != INADDR_ANY) {
                header->ciaddr = connection->client_ip;
        } else {
                /*
                 * When the IP stack has not been configured, we may
                 * not be able to receive unicast packets, depending
                 * on the hardware. If that is the case we must request
                 * replies from the server to be broadcast.
                 *
                 * Once the IP stack has been configured, receiving
                 * unicast packets is never a problem, so the broadcast
                 * flag should not be set.
                 */
                if (broadcast)
                        header->flags |= N_DHCP4_MESSAGE_FLAG_BROADCAST;
        }
}

static int n_dhcp4_c_connection_new_message(NDhcp4CConnection *connection,
                                            NDhcp4Outgoing **messagep,
                                            uint8_t type) {
        _c_cleanup_(n_dhcp4_outgoing_freep) NDhcp4Outgoing *message = NULL;
        NDhcp4Header *header;
        uint8_t message_type;
        bool via_packet_socket = false;
        int r;

        switch (type) {
        case N_DHCP4_C_MESSAGE_DISCOVER:
                message_type = N_DHCP4_MESSAGE_DISCOVER;
                via_packet_socket = true;
                break;
        case N_DHCP4_C_MESSAGE_INFORM:
                message_type = N_DHCP4_MESSAGE_INFORM;
                break;
        case N_DHCP4_C_MESSAGE_SELECT:
                message_type = N_DHCP4_MESSAGE_REQUEST;
                via_packet_socket = true;
                break;
        case N_DHCP4_C_MESSAGE_RENEW:
                message_type = N_DHCP4_MESSAGE_REQUEST;
                break;
        case N_DHCP4_C_MESSAGE_REBIND:
                message_type = N_DHCP4_MESSAGE_REQUEST;
                break;
        case N_DHCP4_C_MESSAGE_REBOOT:
                message_type = N_DHCP4_MESSAGE_REQUEST;
                via_packet_socket = true;
                break;
        case N_DHCP4_C_MESSAGE_RELEASE:
                message_type = N_DHCP4_MESSAGE_RELEASE;
                break;
        case N_DHCP4_C_MESSAGE_DECLINE:
                message_type = N_DHCP4_MESSAGE_DECLINE;
                via_packet_socket = true;
                break;
        default:
                abort();
                return -ENOTRECOVERABLE;
        }

        /*
         * We explicitly pass 0 as maximum message size, which makes
         * NDhcp4Outgoing use the mandated default value from the spec (see its
         * implementation). While the transport and like layers might support
         * bigger MTUs (and we very likely know about them through
         * n_dhcp4_client_update_mtu()), we cannot assume the target DHCP
         * server supports parsing packets bigger than the minimum (and it is
         * allowed to refuse bigger IP packets, even if the network supports
         * transmission of them).
         *
         * We could theoretically increase this for packets other than the
         * initial discovery. However, clients are unlikely to ever send large
         * packets, so we just keep the same default for all outgoing packets.
         */
        r = n_dhcp4_outgoing_new(&message, 0, N_DHCP4_OVERLOAD_FILE | N_DHCP4_OVERLOAD_SNAME);
        if (r)
                return r;

        header = n_dhcp4_outgoing_get_header(message);
        n_dhcp4_c_connection_init_header(connection, header);

        message->userdata.type = type;
        message->userdata.message_type = message_type;

        /*
         * Note that some implementations expect the MESSAGE_TYPE option to be
         * the first option, and possibly even hard-code access to it. Hence,
         * we really should make sure to pass it first as well.
         */
        r = n_dhcp4_outgoing_append(message, N_DHCP4_OPTION_MESSAGE_TYPE, &message_type, sizeof(message_type));
        if (r)
                return r;

        r = n_dhcp4_outgoing_append(message,
                                    N_DHCP4_OPTION_CLIENT_IDENTIFIER,
                                    connection->client_config->client_id,
                                    connection->client_config->n_client_id);
        if (r)
                return r;

        switch (message_type) {
        case N_DHCP4_MESSAGE_DISCOVER:
        case N_DHCP4_MESSAGE_REQUEST:
        case N_DHCP4_MESSAGE_INFORM: {
                uint16_t mtu;

                if (connection->probe_config->n_request_parameters > 0) {
                        r = n_dhcp4_outgoing_append(message,
                                                    N_DHCP4_OPTION_PARAMETER_REQUEST_LIST,
                                                    connection->probe_config->request_parameters,
                                                    connection->probe_config->n_request_parameters);
                        if (r)
                                return r;
                }

                if (via_packet_socket) {
                        /*
                         * In case of packet sockets, we do not support
                         * fragmentation. Hence, our maximum message size
                         * equals the transport MTU. In case no mtu is given,
                         * we use the minimum size mandated by the IP spec. If
                         * we omit the field, some implementations will
                         * interpret this to mean any packet size is supported,
                         * which we rather not want as default behavior (we can
                         * always support suppressing this field, if that is
                         * what the caller wants).
                         */
                        mtu = htons(connection->mtu ?: N_DHCP4_NETWORK_IP_MINIMUM_MAX_SIZE);
                        r = n_dhcp4_outgoing_append(message, N_DHCP4_OPTION_MAXIMUM_MESSAGE_SIZE, &mtu, sizeof(mtu));
                        if (r)
                                return r;
                } else {
                        /*
                         * Once we use UDP sockets, we support fragmentation
                         * through the kernel IP stack. This means, the biggest
                         * message we can receive is the maximum UDP size plus
                         * the possible IP header. This would sum up to
                         * 2^16-1 + 20 (or even 2^16-1 + 60 if pedantic) and
                         * thus exceed the option field. Hence, we simply set
                         * the option to the maximum possible value.
                         */
                        mtu = htons(UINT16_MAX);
                        r = n_dhcp4_outgoing_append(message, N_DHCP4_OPTION_MAXIMUM_MESSAGE_SIZE, &mtu, sizeof(mtu));
                        if (r)
                                return r;
                }

                break;
        }
        default:
                break;
        }

        *messagep = message;
        message = NULL;
        return 0;
}

/*
 *      RFC2131 3.1
 *
 *      The client broadcasts a DHCPDISCOVER message on its local physical
 *      subnet.  The DHCPDISCOVER message MAY include options that suggest
 *      values for the network address and lease duration.  BOOTP relay
 *      agents may pass the message on to DHCP servers not on the same
 *      physical subnet.
 *
 *      RFC2131 3.5
 *
 *      [...] in its initial DHCPDISCOVER or DHCPREQUEST message, a client
 *      may provide the server with a list of specific parameters the
 *      client is interested in.  If the client includes a list of
 *      parameters in a DHCPDISCOVER message, it MUST include that list in
 *      any subsequent DHCPREQUEST messages.
 *
 *      [...]
 *
 *      In addition, the client may suggest values for the network address
 *      and lease time in the DHCPDISCOVER message.  The client may include
 *      the 'requested IP address' option to suggest that a particular IP
 *      address be assigned, and may include the 'IP address lease time'
 *      option to suggest the lease time it would like.  Other options
 *      representing "hints" at configuration parameters are allowed in a
 *      DHCPDISCOVER or DHCPREQUEST message.
 *
 *      RFC2131 4.4.1
 *
 *      The client generates and records a random transaction identifier and
 *      inserts that identifier into the 'xid' field.  The client records its
 *      own local time for later use in computing the lease expiration.  The
 *      client then broadcasts the DHCPDISCOVER on the local hardware
 *      broadcast address to the 0xffffffff IP broadcast address and 'DHCP
 *      server' UDP port.
 *
 *      If the 'xid' of an arriving DHCPOFFER message does not match the
 *      'xid' of the most recent DHCPDISCOVER message, the DHCPOFFER message
 *      must be silently discarded.  Any arriving DHCPACK messages must be
 *      silently discarded.
 */
int n_dhcp4_c_connection_discover_new(NDhcp4CConnection *connection,
                                      NDhcp4Outgoing **requestp) {
        _c_cleanup_(n_dhcp4_outgoing_freep) NDhcp4Outgoing *message = NULL;
        int r;

        r = n_dhcp4_c_connection_new_message(connection, &message, N_DHCP4_C_MESSAGE_DISCOVER);
        if (r)
                return r;

        *requestp = message;
        message = NULL;
        return 0;
}

/*
 *
 *      RFC2131 4.1.1
 *
 *      The DHCPREQUEST message contains the same 'xid' as the DHCPOFFER
 *      message.
 *
 *      RFC2131 4.3.2
 *
 *      Client inserts the address of the selected server in 'server
 *      identifier', 'ciaddr' MUST be zero, 'requested IP address' MUST be
 *      filled in with the yiaddr value from the chosen DHCPOFFER.
 */
int n_dhcp4_c_connection_select_new(NDhcp4CConnection *connection,
                                    NDhcp4Outgoing **requestp,
                                    NDhcp4Incoming *offer) {
        _c_cleanup_(n_dhcp4_outgoing_freep) NDhcp4Outgoing *message = NULL;
        struct in_addr client;
        struct in_addr server;
        uint32_t xid;
        int r;

        n_dhcp4_incoming_get_yiaddr(offer, &client);

        r = n_dhcp4_incoming_query_server_identifier(offer, &server);
        if (r)
                return r;

        r = n_dhcp4_c_connection_new_message(connection, &message, N_DHCP4_C_MESSAGE_SELECT);
        if (r)
                return r;

        r = n_dhcp4_outgoing_append(message, N_DHCP4_OPTION_REQUESTED_IP_ADDRESS, &client, sizeof(client));
        if (r)
                return r;

        r = n_dhcp4_outgoing_append(message, N_DHCP4_OPTION_SERVER_IDENTIFIER, &server, sizeof(server));
        if (r)
                return r;

        /*
         * SELECT continues the transaction started by DISCOVER, and as such
         * keeps the same start time. We also have to preserve the base time
         * of the selected lease as well as the transaction ID.
         */
        message->userdata.start_time = offer->userdata.start_time;
        message->userdata.base_time = offer->userdata.base_time;
        message->userdata.client_addr = client.s_addr;
        n_dhcp4_incoming_get_xid(offer, &xid);
        n_dhcp4_outgoing_set_xid(message, xid);

        *requestp = message;
        message = NULL;
        return 0;
}

/*
 *      RFC2131 4.3.2
 *
 *      'server identifier' MUST NOT be filled in, 'requested IP address'
 *      option MUST be filled in with client's notion of its previously
 *      assigned address. 'ciaddr' MUST be zero. The client is seeking to
 *      verify a previously allocated, cached configuration. Server SHOULD
 *      send a DHCPNAK message to the client if the 'requested IP address'
 *      is incorrect, or is on the wrong network.
 */
int n_dhcp4_c_connection_reboot_new(NDhcp4CConnection *connection,
                                    NDhcp4Outgoing **requestp,
                                    const struct in_addr *client) {
        _c_cleanup_(n_dhcp4_outgoing_freep) NDhcp4Outgoing *message = NULL;
        int r;

        r = n_dhcp4_c_connection_new_message(connection, &message, N_DHCP4_C_MESSAGE_REBOOT);
        if (r)
                return r;

        r = n_dhcp4_outgoing_append(message, N_DHCP4_OPTION_REQUESTED_IP_ADDRESS, client, sizeof(*client));
        if (r)
                return r;

        *requestp = message;
        message = NULL;
        return 0;
}

/*
 *      RFC2131 4.3.2
 *
 *      'server identifier' MUST NOT be filled in, 'requested IP address'
 *      option MUST NOT be filled in, 'ciaddr' MUST be filled in with
 *      client's IP address. In this situation, the client is completely
 *      configured, and is trying to extend its lease. This message will
 *      be unicast, so no relay agents will be involved in its
 *      transmission.  Because 'giaddr' is therefore not filled in, the
 *      DHCP server will trust the value in 'ciaddr', and use it when
 *      replying to the client.
 *
 *      A client MAY choose to renew or extend its lease prior to T1.  The
 *      server may choose not to extend the lease (as a policy decision by
 *      the network administrator), but should return a DHCPACK message
 *      regardless.
 *
 *      RFC2131 4.4.5
 *
 *      At time T1 the client moves to RENEWING state and sends (via unicast)
 *      a DHCPREQUEST message to the server to extend its lease.  The client
 *      sets the 'ciaddr' field in the DHCPREQUEST to its current network
 *      address. The client records the local time at which the DHCPREQUEST
 *      message is sent for computation of the lease expiration time.  The
 *      client MUST NOT include a 'server identifier' in the DHCPREQUEST
 *      message.
 */
int n_dhcp4_c_connection_renew_new(NDhcp4CConnection *connection,
                                   NDhcp4Outgoing **requestp) {
        _c_cleanup_(n_dhcp4_outgoing_freep) NDhcp4Outgoing *message = NULL;
        int r;

        r = n_dhcp4_c_connection_new_message(connection, &message, N_DHCP4_C_MESSAGE_RENEW);
        if (r)
                return r;

        message->userdata.client_addr = connection->client_ip;
        *requestp = message;
        message = NULL;
        return 0;
}

/*
 *      RFC2131 4.3.2
 *
 *      'server identifier' MUST NOT be filled in, 'requested IP address'
 *      option MUST NOT be filled in, 'ciaddr' MUST be filled in with
 *      client's IP address. In this situation, the client is completely
 *      configured, and is trying to extend its lease. This message MUST
 *      be broadcast to the 0xffffffff IP broadcast address.  The DHCP
 *      server SHOULD check 'ciaddr' for correctness before replying to
 *      the DHCPREQUEST.
 *
 *      RFC2131 4.4.5
 *
 *      If no DHCPACK arrives before time T2, the client moves to REBINDING
 *      state and sends (via broadcast) a DHCPREQUEST message to extend its
 *      lease.  The client sets the 'ciaddr' field in the DHCPREQUEST to its
 *      current network address.  The client MUST NOT include a 'server
 *      identifier' in the DHCPREQUEST message.
 */
int n_dhcp4_c_connection_rebind_new(NDhcp4CConnection *connection,
                                    NDhcp4Outgoing **requestp) {
        _c_cleanup_(n_dhcp4_outgoing_freep) NDhcp4Outgoing *message = NULL;
        int r;

        r = n_dhcp4_c_connection_new_message(connection, &message, N_DHCP4_C_MESSAGE_REBIND);
        if (r)
                return r;

        message->userdata.client_addr = connection->client_ip;
        *requestp = message;
        message = NULL;
        return 0;
}

/*
 *      RFC2131 3.2
 *
 *      If the client detects that the IP address in the DHCPACK message
 *      is already in use, the client MUST send a DHCPDECLINE message to the
 *      server and restarts the configuration process by requesting a
 *      new network address.
 *
 *      RFC2131 4.4.4
 *
 *      Because the client is declining the use of the IP address supplied by
 *      the server, the client broadcasts DHCPDECLINE messages.
 */
int n_dhcp4_c_connection_decline_new(NDhcp4CConnection *connection,
                                     NDhcp4Outgoing **requestp,
                                     NDhcp4Incoming *ack,
                                     const char *error) {
        _c_cleanup_(n_dhcp4_outgoing_freep) NDhcp4Outgoing *message = NULL;
        struct in_addr client;
        struct in_addr server;
        int r;

        n_dhcp4_incoming_get_yiaddr(ack, &client);

        r = n_dhcp4_incoming_query_server_identifier(ack, &server);
        if (r)
                return r;

        r = n_dhcp4_c_connection_new_message(connection, &message, N_DHCP4_C_MESSAGE_DECLINE);
        if (r)
                return r;

        r = n_dhcp4_outgoing_append(message, N_DHCP4_OPTION_REQUESTED_IP_ADDRESS, &client, sizeof(client));
        if (r)
                return r;

        r = n_dhcp4_outgoing_append(message, N_DHCP4_OPTION_SERVER_IDENTIFIER, &server, sizeof(server));
        if (r)
                return r;

        if (error) {
                r = n_dhcp4_outgoing_append(message, N_DHCP4_OPTION_ERROR_MESSAGE, error, strlen(error) + 1);
                if (r)
                        return r;
        }

        message->userdata.client_addr = client.s_addr;
        *requestp = message;
        message = NULL;
        return 0;
}

/*
 *      RFC2131 3.4
 *
 *      If a client has obtained a network address through some other means
 *      (e.g., manual configuration), it may use a DHCPINFORM request message
 *      to obtain other local configuration parameters.
 *
 *      RFC2131 4.4
 *
 *      The DHCPINFORM message is not shown in figure 5.  A client simply
 *      sends the DHCPINFORM and waits for DHCPACK messages.  Once the client
 *      has selected its parameters, it has completed the configuration
 *      process.
 *
 *      RFC2131 4.4.3
 *
 *      The client sends a DHCPINFORM message. The client may request
 *      specific configuration parameters by including the 'parameter request
 *      list' option. The client generates and records a random transaction
 *      identifier and inserts that identifier into the 'xid' field. The
 *      client places its own network address in the 'ciaddr' field. The
 *      client SHOULD NOT request lease time parameters.
 *
 *      The client then unicasts the DHCPINFORM to the DHCP server if it
 *      knows the server's address, otherwise it broadcasts the message to
 *      the limited (all 1s) broadcast address.  DHCPINFORM messages MUST be
 *      directed to the 'DHCP server' UDP port.
 */
int n_dhcp4_c_connection_inform_new(NDhcp4CConnection *connection,
                                    NDhcp4Outgoing **requestp) {
        _c_cleanup_(n_dhcp4_outgoing_freep) NDhcp4Outgoing *message = NULL;
        int r;

        r = n_dhcp4_c_connection_new_message(connection, &message, N_DHCP4_C_MESSAGE_INFORM);
        if (r)
                return r;

        message->userdata.client_addr = connection->client_ip;
        *requestp = message;
        message = NULL;
        return 0;
}

/*
 *      RFC2131 3.1
 *
 *      The client may choose to relinquish its lease on a network address
 *      by sending a DHCPRELEASE message to the server.  The client
 *      identifies the lease to be released with its 'client identifier',
 *      or 'chaddr' and network address in the DHCPRELEASE message. If the
 *      client used a 'client identifier' when it obtained the lease, it
 *      MUST use the same 'client identifier' in the DHCPRELEASE message.
 *
 *      RFC2131 3.2
 *
 *      The client may choose to relinquish its lease on a network
 *      address by sending a DHCPRELEASE message to the server.  The
 *      client identifies the lease to be released with its
 *      'client identifier', or 'chaddr' and network address in the
 *      DHCPRELEASE message.
 *
 *      Note that in this case, where the client retains its network
 *      address locally, the client will not normally relinquish its
 *      lease during a graceful shutdown.  Only in the case where the
 *      client explicitly needs to relinquish its lease, e.g., the client
 *      is about to be moved to a different subnet, will the client send
 *      a DHCPRELEASE message.
 *
 *      RFC2131 4.4.4
 *
 *      The client unicasts DHCPRELEASE messages to the server.
 *
 *      RFC2131 4.4.6
 *
 *      If the client no longer requires use of its assigned network address
 *      (e.g., the client is gracefully shut down), the client sends a
 *      DHCPRELEASE message to the server.  Note that the correct operation
 *      of DHCP does not depend on the transmission of DHCPRELEASE messages.
 */
int n_dhcp4_c_connection_release_new(NDhcp4CConnection *connection,
                                     NDhcp4Outgoing **requestp,
                                     const char *error) {
        _c_cleanup_(n_dhcp4_outgoing_freep) NDhcp4Outgoing *message = NULL;
        int r;

        r = n_dhcp4_c_connection_new_message(connection, &message, N_DHCP4_C_MESSAGE_RELEASE);
        if (r)
                return r;

        r = n_dhcp4_outgoing_append(message, N_DHCP4_OPTION_SERVER_IDENTIFIER, &connection->server_ip, sizeof(connection->server_ip));
        if (r)
                return r;

        if (error) {
                r = n_dhcp4_outgoing_append(message, N_DHCP4_OPTION_ERROR_MESSAGE, error, strlen(error) + 1);
                if (r)
                        return r;
        }

        *requestp = message;
        message = NULL;
        return 0;
}

static const char *message_type_to_str(uint8_t type) {
        switch (type) {
        case N_DHCP4_MESSAGE_DISCOVER:
                return "DISCOVER";
        case N_DHCP4_MESSAGE_OFFER:
                return "OFFER";
        case N_DHCP4_MESSAGE_REQUEST:
                return "REQUEST";
        case N_DHCP4_MESSAGE_DECLINE:
                return "DECLINE";
        case N_DHCP4_MESSAGE_ACK:
                return "ACK";
        case N_DHCP4_MESSAGE_NAK:
                return "NACK";
        case N_DHCP4_MESSAGE_RELEASE:
                return "RELEASE";
        case N_DHCP4_MESSAGE_INFORM:
                return "INFORM";
        case N_DHCP4_MESSAGE_FORCERENEW:
                return "FORCERENEW";
        default:
                return "UNKNOWN";
        }
}

static int n_dhcp4_c_connection_send_request(NDhcp4CConnection *connection,
                                             NDhcp4Outgoing *request,
                                             uint64_t timestamp) {
        char server_addr[INET_ADDRSTRLEN];
        char client_addr[INET_ADDRSTRLEN];
        int r;
        bool broadcast = false;

        /*
         * Increment the base time and reset the xid field,
         * where applicable. We never alter the header on
         * resends of SELECT, as it must always match the
         * OFFER message they are in reply to.
         */
        switch (request->userdata.type) {
        case N_DHCP4_C_MESSAGE_DISCOVER:
        case N_DHCP4_C_MESSAGE_INFORM:
        case N_DHCP4_C_MESSAGE_REBOOT:
        case N_DHCP4_C_MESSAGE_REBIND:
        case N_DHCP4_C_MESSAGE_RENEW:
                request->userdata.base_time = timestamp;
                n_dhcp4_outgoing_set_xid(request, n_dhcp4_client_probe_config_get_random(connection->probe_config));

                break;
        case N_DHCP4_C_MESSAGE_SELECT:
        case N_DHCP4_C_MESSAGE_DECLINE:
        case N_DHCP4_C_MESSAGE_RELEASE:
                break;
        default:
                c_assert(0);
        }

        request->userdata.send_time = timestamp;
        request->userdata.send_jitter = (n_dhcp4_client_probe_config_get_random(connection->probe_config) % 1000000000ULL);
        n_dhcp4_c_connection_outgoing_set_secs(request);

        switch (request->userdata.type) {
        case N_DHCP4_C_MESSAGE_DISCOVER:
        case N_DHCP4_C_MESSAGE_SELECT:
        case N_DHCP4_C_MESSAGE_REBOOT:
        case N_DHCP4_C_MESSAGE_DECLINE:
        case N_DHCP4_C_MESSAGE_REBIND:
                broadcast = true;
                r = n_dhcp4_c_connection_packet_broadcast(connection, request);
                if (r)
                        return r;
                break;
        case N_DHCP4_C_MESSAGE_INFORM:
                broadcast = true;
                r = n_dhcp4_c_connection_udp_broadcast(connection, request);
                if (r)
                        return r;

                break;
        case N_DHCP4_C_MESSAGE_RENEW:
        case N_DHCP4_C_MESSAGE_RELEASE:
                r = n_dhcp4_c_connection_udp_send(connection, request);
                if (r)
                        return r;

                break;
        default:
                c_assert(0);
        }

        if (request->userdata.client_addr == INADDR_ANY) {
                n_dhcp4_c_log(connection->client_config, LOG_INFO,
                              "sent %s to %s",
                              message_type_to_str(request->userdata.message_type),
                              broadcast ?
                              "255.255.255.255" :
                              inet_ntop(AF_INET, &connection->server_ip,
                                        server_addr, sizeof(server_addr)));
        } else {
                n_dhcp4_c_log(connection->client_config, LOG_INFO,
                              "sent %s of %s to %s",
                              message_type_to_str(request->userdata.message_type),
                              inet_ntop(AF_INET, &request->userdata.client_addr,
                                        client_addr, sizeof(client_addr)),
                              broadcast ?
                              "255.255.255.255" :
                              inet_ntop(AF_INET, &connection->server_ip,
                                        server_addr, sizeof(server_addr)));
        }

        ++request->userdata.n_send;
        return 0;
}

int n_dhcp4_c_connection_start_request(NDhcp4CConnection *connection,
                                       NDhcp4Outgoing *request,
                                       uint64_t timestamp) {
        int r;

        /*
         * This function starts a request, but in the case of SELECT it
         * continues a previous transaction, so we do not want to reset
         * the start time. Only set the start time if it was not already
         * set.
         */
        if (request->userdata.start_time == 0)
                request->userdata.start_time = timestamp;

        n_dhcp4_outgoing_free(connection->request);
        connection->request = request;

        r = n_dhcp4_c_connection_send_request(connection, request, timestamp);
        if (r)
                return r;

        return 0;
}

int n_dhcp4_c_connection_dispatch_timer(NDhcp4CConnection *connection,
                                        uint64_t timestamp) {
        uint64_t timeout;
        int r;

        if (!connection->request)
                return 0;

        n_dhcp4_c_connection_get_timeout(connection, &timeout);

        if (timeout > timestamp)
                return 0;

        r = n_dhcp4_c_connection_send_request(connection, connection->request, timestamp);
        if (r)
                return r;

        return 0;
}

int n_dhcp4_c_connection_dispatch_io(NDhcp4CConnection *connection,
                                     NDhcp4Incoming **messagep) {
        _c_cleanup_(n_dhcp4_incoming_freep) NDhcp4Incoming *message = NULL;
        char serv_addr[INET_ADDRSTRLEN];
        char client_addr[INET_ADDRSTRLEN];
        uint8_t type;
        int r;

        switch (connection->state) {
        case N_DHCP4_C_CONNECTION_STATE_PACKET:
                r = n_dhcp4_c_socket_packet_recv(connection->fd_packet,
                                                 connection->scratch_buffer,
                                                 sizeof(connection->scratch_buffer),
                                                 &message);
                if (r)
                        return r;

                break;
        case N_DHCP4_C_CONNECTION_STATE_DRAINING:
                r = n_dhcp4_c_socket_packet_recv(connection->fd_packet,
                                                 connection->scratch_buffer,
                                                 sizeof(connection->scratch_buffer),
                                                 &message);
                if (!r)
                        break;
                else if (r != N_DHCP4_E_AGAIN)
                        return r;

                /*
                 * The UDP socket is open and the packet socket has been shut down
                 * and drained, clean up the packet socket and fall through to
                 * dispatching the UDP socket.
                 */
                r = epoll_ctl(connection->fd_epoll, EPOLL_CTL_DEL, connection->fd_packet, NULL);
                c_assert(!r);
                connection->fd_packet = c_close(connection->fd_packet);
                connection->state = N_DHCP4_C_CONNECTION_STATE_UDP;

                /* fall-through */
        case N_DHCP4_C_CONNECTION_STATE_UDP:
                r = n_dhcp4_c_socket_udp_recv(connection->fd_udp,
                                              connection->scratch_buffer,
                                              sizeof(connection->scratch_buffer),
                                              &message);
                if (r)
                        return r;

                break;
        default:
                abort();
                return -ENOTRECOVERABLE;
        }

        r = n_dhcp4_c_connection_verify_incoming(connection, message, &type);
        if (r)
                return r;

        if (type == N_DHCP4_MESSAGE_OFFER || type == N_DHCP4_MESSAGE_ACK) {
                n_dhcp4_c_log(connection->client_config, LOG_INFO,
                              "received %s of %s from %s",
                              message_type_to_str(type),
                              inet_ntop(AF_INET, &message->message.header.yiaddr,
                                        client_addr, sizeof(client_addr)),
                              inet_ntop(AF_INET, &message->message.header.siaddr,
                                        serv_addr, sizeof(serv_addr)));
        } else {
                n_dhcp4_c_log(connection->client_config, LOG_INFO,
                              "received %s from %s",
                              message_type_to_str(type),
                              inet_ntop(AF_INET, &message->message.header.siaddr,
                                        serv_addr, sizeof(serv_addr)));
        }

        switch (type) {
        case N_DHCP4_MESSAGE_OFFER:
        case N_DHCP4_MESSAGE_ACK:
        case N_DHCP4_MESSAGE_NAK:
                /*
                 * Remember the start time of the transaction, and the base
                 * time of any relative timestamps from the pending request.
                 * Thes same times applies to the response, and sholud be
                 * copied over.
                 */
                message->userdata.start_time = connection->request->userdata.start_time;
                message->userdata.base_time = connection->request->userdata.base_time;

                if (type != N_DHCP4_MESSAGE_OFFER) {
                        /*
                         * We only allow one reply to ACK or NAK, but for OFFER we must
                         * accept several, so we do not free the pinned request.
                         */
                        connection->request = n_dhcp4_outgoing_free(connection->request);
                }

                break;
        default:
                break;
        }

        *messagep = message;
        message = NULL;
        return 0;
}