stumpless-sys 0.3.0

Bindings for the Stumpless logging library.
Documentation
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
// SPDX-License-Identifier: Apache-2.0

/*
 * Copyright 2020-2024 Joel E. Anderson
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <stumpless/target.h>
#include <stumpless/target/network.h>
#include "private/config/wrapper/locale.h"
#include "private/config/wrapper/network_supported.h"
#include "private/config/wrapper/thread_safety.h"
#include "private/error.h"
#include "private/memory.h"
#include "private/strhelper.h"
#include "private/target.h"
#include "private/target/network.h"
#include "private/validate.h"

static CONFIG_THREAD_LOCAL_STORAGE char *tcp_send_buffer = NULL;
static CONFIG_THREAD_LOCAL_STORAGE size_t tcp_send_buffer_length = 0;

static
void
destroy_ipv4_target( const struct network_target *target ) {

  if( target->transport == STUMPLESS_TCP_TRANSPORT_PROTOCOL ) {
    config_close_tcp4_target( target );

  } else { // STUMPLESS_UDP_TRANSPORT_PROTOCOL
    config_close_udp4_target( target );

  }
}

static
void
destroy_ipv6_target( const struct network_target *target ) {

  if( target->transport == STUMPLESS_TCP_TRANSPORT_PROTOCOL ) {
    config_close_tcp6_target( target );

  } else { // STUMPLESS_UDP_TRANSPORT_PROTOCOL
    config_close_udp6_target( target );

  }
}

static
struct network_target *
init_ipv4_target( struct network_target *target ) {

  switch( target->transport ) {

    case STUMPLESS_TCP_TRANSPORT_PROTOCOL:
      config_init_tcp4( target );
      return target;

    case STUMPLESS_UDP_TRANSPORT_PROTOCOL:
      config_init_udp4( target );
      return target;

    default:
      raise_transport_protocol_unsupported(  );
      return NULL;

  }
}

static
struct network_target *
init_ipv6_target( struct network_target *target ) {

  switch( target->transport ) {

    case STUMPLESS_TCP_TRANSPORT_PROTOCOL:
      config_init_tcp6( target );
      return target;

    case STUMPLESS_UDP_TRANSPORT_PROTOCOL:
      config_init_udp6( target );
      return target;

    default:
      raise_transport_protocol_unsupported(  );
      return NULL;

  }
}

static
struct network_target *
init_network_target( struct network_target *target ) {

  switch( target->network ) {

    case STUMPLESS_IPV4_NETWORK_PROTOCOL:
      return init_ipv4_target( target );

    case STUMPLESS_IPV6_NETWORK_PROTOCOL:
      return init_ipv6_target( target );

    default:
      raise_network_protocol_unsupported(  );
      return NULL;

  }
}

static
int
ipv4_target_is_open( const struct network_target *target ) {

  if( target->transport == STUMPLESS_TCP_TRANSPORT_PROTOCOL ) {
      return config_tcp4_is_open( target );

  } else { // STUMPLESS_UDP_TRANSPORT_PROTOCOL
      return config_udp4_is_open( target );

  }
}

static
int
ipv6_target_is_open( const struct network_target *target ) {

  if( target->transport == STUMPLESS_TCP_TRANSPORT_PROTOCOL ) {
      return config_tcp6_is_open( target );

  } else { // STUMPLESS_UDP_TRANSPORT_PROTOCOL
      return config_udp6_is_open( target );

  }
}

static
struct network_target *
open_ipv4_target( struct network_target *target ) {

  switch( target->transport ) {

    case STUMPLESS_TCP_TRANSPORT_PROTOCOL:
      return config_open_tcp4_target( target );

    case STUMPLESS_UDP_TRANSPORT_PROTOCOL:
      return config_open_udp4_target( target );

    default:
      raise_transport_protocol_unsupported(  );
      return NULL;

  }
}

static
struct network_target *
open_ipv6_target( struct network_target *target ) {

  switch( target->transport ) {

    case STUMPLESS_TCP_TRANSPORT_PROTOCOL:
      return config_open_tcp6_target( target );

    case STUMPLESS_UDP_TRANSPORT_PROTOCOL:
      return config_open_udp6_target( target );

    default:
      raise_transport_protocol_unsupported(  );
      return NULL;

  }
}

static
struct network_target *
open_private_network_target( struct network_target *target ) {

  switch( target->network ) {

    case STUMPLESS_IPV4_NETWORK_PROTOCOL:
      return open_ipv4_target( target );

    case STUMPLESS_IPV6_NETWORK_PROTOCOL:
      return open_ipv6_target( target );

    default:
      raise_network_protocol_unsupported(  );
      return NULL;

  }
}

static
struct network_target *
reopen_ipv4_target( struct network_target *target ) {

  if( target->transport == STUMPLESS_TCP_TRANSPORT_PROTOCOL ) {
      return config_reopen_tcp4_target( target );

  } else { // STUMPLESS_UDP_TRANSPORT_PROTOCOL
      return config_reopen_udp4_target( target );

  }
}

static
struct network_target *
reopen_ipv6_target( struct network_target *target ) {

  if( target->transport == STUMPLESS_TCP_TRANSPORT_PROTOCOL ) {
      return config_reopen_tcp6_target( target );

  } else { // STUMPLESS_UDP_TRANSPORT_PROTOCOL
      return config_reopen_udp6_target( target );

  }
}

static
struct network_target *
reopen_network_target( struct network_target *target ) {

  if( target->network == STUMPLESS_IPV4_NETWORK_PROTOCOL ) {
      return reopen_ipv4_target( target );

  } else { // STUMPLESS_IPV6_NETWORK_PROTOCOL
      return reopen_ipv6_target( target );

  }
}

static
int
sendto_tcp_target( struct network_target *target,
                   const char *msg,
                   size_t msg_length ) {
  size_t int_length;
  size_t required_length;
  char *new_buffer;

  required_length = msg_length + 50;
  if( tcp_send_buffer_length < required_length ) {
    new_buffer = realloc_mem( tcp_send_buffer, required_length );

    if( !new_buffer ) {
      return -1;

    } else {
      tcp_send_buffer = new_buffer;
      tcp_send_buffer_length = required_length;

    }
  }

  snprintf( tcp_send_buffer, 50, "%zd ", msg_length );
  int_length = strlen( tcp_send_buffer );
  memcpy( tcp_send_buffer + int_length, msg, msg_length );

  if( target->network == STUMPLESS_IPV4_NETWORK_PROTOCOL ) {
    return config_sendto_tcp4_target( target,
                                      tcp_send_buffer,
                                      int_length + msg_length );

  } else { // STUMPLESS_IPV6_NETWORK_PROTOCOL
    return config_sendto_tcp6_target( target,
                                      tcp_send_buffer,
                                      int_length + msg_length );

  }
}

static
int
sendto_udp_target( const struct network_target *target,
                   const char *msg,
                   size_t msg_length ) {
  size_t effective_length;

  if( msg_length > target->max_msg_size ) {
    effective_length = target->max_msg_size;
    raise_argument_too_big( L10N_MESSAGE_TOO_BIG_FOR_DATAGRAM_ERROR_MESSAGE,
                            msg_length,
                            L10N_MESSAGE_SIZE_ERROR_CODE_TYPE );
  } else {
    effective_length = msg_length;
  }

  if( target->network == STUMPLESS_IPV4_NETWORK_PROTOCOL ) {
    return config_sendto_udp4_target( target, msg, effective_length );

  } else { // STUMPLESS_IPV6_NETWORK_PROTOCOL
    return config_sendto_udp6_target( target, msg, effective_length );

  }
}

/* public definitions */

void
stumpless_close_network_target( const struct stumpless_target *target ) {
  clear_error(  );

  if( !target ) {
    raise_argument_empty( L10N_NULL_ARG_ERROR_MESSAGE( "target" ) );
    return;
  }

  if( target->type != STUMPLESS_NETWORK_TARGET ) {
    raise_target_incompatible( L10N_INVALID_TARGET_TYPE_ERROR_MESSAGE );
    return;
  }

  destroy_network_target( target->id );
  destroy_target( target );
}

const char *
stumpless_get_destination( const struct stumpless_target *target ) {
  const struct network_target *net_target;
  const char *destination_copy = NULL;

  VALIDATE_ARG_NOT_NULL( target );

  lock_target( target );
  if( target->type != STUMPLESS_NETWORK_TARGET ) {
    raise_target_incompatible( L10N_DESTINATION_NETWORK_ONLY_ERROR_MESSAGE );
    goto cleanup_and_return;
  }

  net_target = target->id;

  if( !net_target->destination ) {
    goto cleanup_and_return;
  }

  destination_copy = copy_cstring( net_target->destination );
  if( !destination_copy ) {
    goto cleanup_and_return;
  }

  clear_error(  );

cleanup_and_return:
  unlock_target( target );
  return destination_copy;
}

const char *
stumpless_get_transport_port( const struct stumpless_target *target ) {
  const struct network_target *net_target;
  const char *port_copy = NULL;

  VALIDATE_ARG_NOT_NULL( target );

  lock_target( target );
  if( target->type != STUMPLESS_NETWORK_TARGET ) {
    raise_target_incompatible( L10N_TRANSPORT_PORT_NETWORK_ONLY_ERROR_MESSAGE );
    goto cleanup_and_return;
  }

  net_target = target->id;

  if( !net_target->port ) {
    goto cleanup_and_return;
  }

  port_copy = copy_cstring( net_target->port );
  if( !port_copy ) {
    goto cleanup_and_return;
  }

  clear_error(  );

cleanup_and_return:
  unlock_target( target );
  return port_copy;
}

size_t
stumpless_get_udp_max_message_size( const struct stumpless_target *target ) {
  const struct network_target *net_target;
  size_t result;

  if( !target ) {
    raise_argument_empty( L10N_NULL_ARG_ERROR_MESSAGE( "target" ) );
    goto fail;
  }

  lock_target( target );
  if( target->type != STUMPLESS_NETWORK_TARGET ) {
    goto incompatible;
  }

  net_target = target->id;
  if( net_target->transport != STUMPLESS_UDP_TRANSPORT_PROTOCOL ) {
    goto incompatible;
  }

  result = net_target->max_msg_size;
  unlock_target( target );

  clear_error(  );
  return result;

incompatible:
  unlock_target( target );
  raise_target_incompatible( L10N_MAX_MESSAGE_SIZE_UDP_ONLY_ERROR_MESSAGE );
fail:
  return 0;
}

struct stumpless_target *
stumpless_new_network_target( const char *name,
                              enum stumpless_network_protocol network,
                              enum stumpless_transport_protocol transport ) {
  struct stumpless_target *target;

  VALIDATE_ARG_NOT_NULL( name );

  target = new_target( STUMPLESS_NETWORK_TARGET, name );
  if( !target ) {
    goto fail;
  }

  target->id = new_network_target( network, transport );
  if( !target->id ) {
    goto fail_id;
  }

  clear_error(  );
  return target;

fail_id:
  destroy_target( target );
fail:
  return NULL;
}

struct stumpless_target *
stumpless_new_tcp4_target( const char *name ) {
  return stumpless_new_network_target( name,
                                       STUMPLESS_IPV4_NETWORK_PROTOCOL,
                                       STUMPLESS_TCP_TRANSPORT_PROTOCOL );
}

struct stumpless_target *
stumpless_new_tcp6_target( const char *name ) {
  return stumpless_new_network_target( name,
                                       STUMPLESS_IPV6_NETWORK_PROTOCOL,
                                       STUMPLESS_TCP_TRANSPORT_PROTOCOL );
}

struct stumpless_target *
stumpless_new_udp4_target( const char *name ) {
  return stumpless_new_network_target( name,
                                       STUMPLESS_IPV4_NETWORK_PROTOCOL,
                                       STUMPLESS_UDP_TRANSPORT_PROTOCOL );
}

struct stumpless_target *
stumpless_new_udp6_target( const char *name ) {
  return stumpless_new_network_target( name,
                                       STUMPLESS_IPV6_NETWORK_PROTOCOL,
                                       STUMPLESS_UDP_TRANSPORT_PROTOCOL );
}

struct stumpless_target *
stumpless_open_network_target( const char *name,
                               const char *destination,
                               enum stumpless_network_protocol network,
                               enum stumpless_transport_protocol transport ) {
  struct stumpless_target *target;

  VALIDATE_ARG_NOT_NULL( name );
  VALIDATE_ARG_NOT_NULL( destination );

  target = new_target( STUMPLESS_NETWORK_TARGET, name );

  if( !target ) {
    goto fail;
  }

  target->id = open_new_network_target( destination, network, transport );
  if( !target->id ) {
    goto fail_id;
  }

  stumpless_set_current_target( target );
  return target;

fail_id:
  destroy_target( target );
fail:
  return NULL;
}

struct stumpless_target *
stumpless_open_tcp4_target( const char *name, const char *destination ) {
  return stumpless_open_network_target( name,
                                        destination,
                                        STUMPLESS_IPV4_NETWORK_PROTOCOL,
                                        STUMPLESS_TCP_TRANSPORT_PROTOCOL );
}

struct stumpless_target *
stumpless_open_tcp6_target( const char *name, const char *destination ) {
  return stumpless_open_network_target( name,
                                        destination,
                                        STUMPLESS_IPV6_NETWORK_PROTOCOL,
                                        STUMPLESS_TCP_TRANSPORT_PROTOCOL );
}

struct stumpless_target *
stumpless_open_udp4_target( const char *name, const char *destination ) {
  return stumpless_open_network_target( name,
                                        destination,
                                        STUMPLESS_IPV4_NETWORK_PROTOCOL,
                                        STUMPLESS_UDP_TRANSPORT_PROTOCOL );
}

struct stumpless_target *
stumpless_open_udp6_target( const char *name, const char *destination ) {
  return stumpless_open_network_target( name,
                                        destination,
                                        STUMPLESS_IPV6_NETWORK_PROTOCOL,
                                        STUMPLESS_UDP_TRANSPORT_PROTOCOL );
}

struct stumpless_target *
stumpless_set_destination( struct stumpless_target *target,
                           const char *destination ) {
  const char *destination_copy;
  struct network_target *net_target;
  const char *old_destination;

  VALIDATE_ARG_NOT_NULL( target );
  VALIDATE_ARG_NOT_NULL( destination );

  destination_copy = copy_cstring( destination );
  if( !destination_copy ) {
    return NULL;
  }

  lock_target( target );
  if( target->type != STUMPLESS_NETWORK_TARGET ) {
    raise_target_incompatible( L10N_DESTINATION_NETWORK_ONLY_ERROR_MESSAGE );
    goto cleanup_and_fail;
  }

  net_target = target->id;
  old_destination = net_target->destination;
  net_target->destination = destination_copy;
  clear_error(  );

  reopen_network_target( net_target );

  unlock_target( target );
  free_mem( old_destination );
  return target;

cleanup_and_fail:
  unlock_target( target );
  free_mem( destination_copy );
  return NULL;
}

struct stumpless_target *
stumpless_set_transport_port( struct stumpless_target *target,
                              const char *port ) {
  const char *port_copy;
  struct network_target *net_target;
  const char *old_port;

  VALIDATE_ARG_NOT_NULL( target );
  VALIDATE_ARG_NOT_NULL( port );

  port_copy = copy_cstring( port );
  if( !port_copy ) {
    return NULL;
  }

  lock_target( target );
  if( target->type != STUMPLESS_NETWORK_TARGET ) {
    raise_target_incompatible( L10N_TRANSPORT_PORT_NETWORK_ONLY_ERROR_MESSAGE );
    goto cleanup_and_fail;
  }

  net_target = target->id;
  old_port = net_target->port;
  net_target->port = port_copy;
  clear_error(  );

  reopen_network_target( net_target );

  unlock_target( target );
  free_mem( old_port );
  return target;

cleanup_and_fail:
  unlock_target( target );
  free_mem( port_copy );
  return NULL;
}

struct stumpless_target *
stumpless_set_udp_max_message_size( struct stumpless_target *target,
                                    size_t max_msg_size ) {
  struct network_target *net_target;

  VALIDATE_ARG_NOT_NULL( target );

  lock_target( target );
  if( target->type != STUMPLESS_NETWORK_TARGET ) {
    goto incompatible;
  }

  net_target = target->id;
  if( net_target->transport != STUMPLESS_UDP_TRANSPORT_PROTOCOL ) {
    goto incompatible;
  }

  net_target = target->id;
  net_target->max_msg_size = max_msg_size;
  unlock_target( target );

  clear_error(  );
  return target;

incompatible:
  unlock_target( target );
  raise_target_incompatible( L10N_MAX_MESSAGE_SIZE_UDP_ONLY_ERROR_MESSAGE );
  return NULL;
}

enum stumpless_network_protocol
stumpless_get_network_protocol( const struct stumpless_target *target ) {
  const struct network_target *net_target;
  enum stumpless_network_protocol network_protocol;

  VALIDATE_ARG_NOT_NULL_INT_RETURN( target );

  lock_target( target );
  if( target->type != STUMPLESS_NETWORK_TARGET ) {
    raise_target_incompatible( L10N_INVALID_TARGET_TYPE_ERROR_MESSAGE );
    goto cleanup_and_fail;
  }

  net_target = target->id;
  network_protocol = net_target->network;
  unlock_target( target );

  return network_protocol;

cleanup_and_fail:
  unlock_target( target );
  return ( enum stumpless_network_protocol ) -1;
}

enum stumpless_transport_protocol
stumpless_get_transport_protocol( const struct stumpless_target *target ) {
  const struct network_target *net_target;
  enum stumpless_transport_protocol transport_protocol;

  VALIDATE_ARG_NOT_NULL_INT_RETURN( target );

  lock_target( target );
  if( target->type != STUMPLESS_NETWORK_TARGET ) {
    raise_target_incompatible( L10N_INVALID_TARGET_TYPE_ERROR_MESSAGE );
    goto cleanup_and_fail;
  }

  net_target = target->id;
  transport_protocol = net_target->transport;
  unlock_target( target );

  return transport_protocol;

cleanup_and_fail:
  unlock_target( target );
  return ( enum stumpless_transport_protocol ) -1;
}

/* private definitions */

void
destroy_network_target( const struct network_target *target ) {

  if( target->network == STUMPLESS_IPV4_NETWORK_PROTOCOL ) {
    destroy_ipv4_target( target );

  } else { // STUMPLESS_IPV6_NETWORK_PROTOCOL
    destroy_ipv6_target( target );

  }

  free_mem( target->destination );
  free_mem( target->port );
  free_mem( target );
}

void
lock_network_target( const struct network_target *target ) {
  config_lock_mutex( &target->mutex );
}

void
network_free_all( void ) {
  config_network_provider_free_all();
  free_mem( tcp_send_buffer );
  tcp_send_buffer = NULL;
  tcp_send_buffer_length = 0;
}

int
network_target_is_open( const struct stumpless_target *target ) {
  const struct network_target *net_target;

  net_target = ( const struct network_target * ) target->id;

  if( net_target->network == STUMPLESS_IPV4_NETWORK_PROTOCOL ) {
    return ipv4_target_is_open( net_target );

  } else { // STUMPLESS_IPV6_NETWORK_PROTOCOL
    return ipv6_target_is_open( net_target );

  }
}

struct network_target *
new_network_target( enum stumpless_network_protocol network,
                    enum stumpless_transport_protocol transport ) {
  struct network_target *target;
  const char *port_copy;
  const struct network_target *init_result;

  target = alloc_mem( sizeof( *target ) );
  if( !target ) {
    goto fail;
  }

  port_copy = copy_cstring( STUMPLESS_DEFAULT_TRANSPORT_PORT );
  if( !port_copy ) {
    goto fail_port;
  }

  target->destination = NULL;
  target->port = port_copy;
  target->max_msg_size = STUMPLESS_DEFAULT_UDP_MAX_MESSAGE_SIZE;
  target->network = network;
  target->transport = transport;

  init_result = init_network_target( target );
  if( !init_result ) {
    goto fail_init;
  }

  return target;

fail_init:
  free_mem( port_copy );
fail_port:
  free_mem( target );
fail:
  return NULL;
}

struct stumpless_target *
open_network_target( struct stumpless_target *target ) {
  const struct network_target *result;

  result = open_private_network_target( target->id );
  if( result ) {
    return target;

  } else {
    return NULL;

  }
}

struct network_target *
open_new_network_target( const char *destination,
                         enum stumpless_network_protocol network,
                         enum stumpless_transport_protocol transport ) {
  struct network_target *target;
  const struct network_target *open_result;

  target = new_network_target( network, transport );
  if( !target ) {
    goto fail;
  }

  target->destination = copy_cstring( destination );
  if( !target->destination ) {
    goto fail_open;
  }

  open_result = open_private_network_target( target );
  if( !open_result ) {
    goto fail_open;
  }

  return target;

fail_open:
  destroy_network_target( target );
fail:
  return NULL;
}

int
sendto_network_target( struct network_target *target,
                       const char *msg,
                       size_t msg_length ) {
  // leave off the newline
  msg_length--;

  if( target->transport == STUMPLESS_UDP_TRANSPORT_PROTOCOL ) {
     return sendto_udp_target( target, msg, msg_length );

  } else {
     return sendto_tcp_target( target, msg, msg_length );

  }
}

void
unlock_network_target( const struct network_target *target ) {
  config_unlock_mutex( &target->mutex );
}