Annotation of embedaddon/bird2/proto/rpki/tcp_transport.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  *     BIRD -- An implementation of the TCP protocol for the RPKI protocol transport
                      3:  *
                      4:  *     (c) 2015 CZ.NIC
                      5:  *     (c) 2015 Pavel Tvrdik <pawel.tvrdik@gmail.com>
                      6:  *
                      7:  *     This file was a part of RTRlib: http://rpki.realmv6.org/
                      8:  *
                      9:  *     Can be freely distributed and used under the terms of the GNU GPL.
                     10:  */
                     11: 
                     12: #include <errno.h>
                     13: #include <netdb.h>
                     14: #include <stdio.h>
                     15: #include <stdlib.h>
                     16: #include <string.h>
                     17: #include <sys/socket.h>
                     18: #include <sys/types.h>
                     19: #include <unistd.h>
                     20: 
                     21: #include "rpki.h"
                     22: #include "sysdep/unix/unix.h"
                     23: 
                     24: static int
                     25: rpki_tr_tcp_open(struct rpki_tr_sock *tr)
                     26: {
                     27:   sock *sk = tr->sk;
                     28: 
                     29:   sk->type = SK_TCP_ACTIVE;
                     30: 
                     31:   if (sk_open(sk) != 0)
                     32:     return RPKI_TR_ERROR;
                     33: 
                     34:   return RPKI_TR_SUCCESS;
                     35: }
                     36: 
                     37: static const char *
                     38: rpki_tr_tcp_ident(struct rpki_tr_sock *tr)
                     39: {
                     40:   struct rpki_cache *cache = tr->cache;
                     41:   struct rpki_config *cf = (void *) cache->p->p.cf;
                     42: 
                     43:   if (tr->ident != NULL)
                     44:     return tr->ident;
                     45: 
                     46:   const char *host = cf->hostname;
                     47:   ip_addr ip = cf->ip;
                     48:   u16 port = cf->port;
                     49: 
                     50:   size_t colon_and_port_len = 6; /* max ":65535" */
                     51:   size_t ident_len;
                     52:   if (host)
                     53:     ident_len = strlen(host) + colon_and_port_len + 1;
                     54:   else
                     55:     ident_len = IPA_MAX_TEXT_LENGTH + colon_and_port_len + 1;
                     56: 
                     57:   char *ident = mb_alloc(cache->pool, ident_len);
                     58:   if (host)
                     59:     bsnprintf(ident, ident_len, "%s:%u", host, port);
                     60:   else
                     61:     bsnprintf(ident, ident_len, "%I:%u", ip, port);
                     62: 
                     63:   tr->ident = ident;
                     64:   return tr->ident;
                     65: }
                     66: 
                     67: /**
                     68:  * rpki_tr_tcp_init - initializes the RPKI transport structure for a TCP connection
                     69:  * @tr: allocated RPKI transport structure
                     70:  */
                     71: void
                     72: rpki_tr_tcp_init(struct rpki_tr_sock *tr)
                     73: {
                     74:   tr->open_fp = &rpki_tr_tcp_open;
                     75:   tr->ident_fp = &rpki_tr_tcp_ident;
                     76: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>