1 /*
2  * Copyright (c) 2016-2021, OARC, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *    the documentation and/or other materials provided with the
15  *    distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include "config.h"
36 
37 #include "endpoint.h"
38 #include "args.h"
39 #include "iaddr.h"
40 
endpoint_arg(endpoint_list * list,const char * arg)41 void endpoint_arg(endpoint_list* list, const char* arg)
42 {
43     struct addrinfo* ai;
44     iaddr            ia;
45     void*            p;
46 
47     if (inet_pton(AF_INET6, arg, &ia.u.a6) > 0) {
48         ia.af = AF_INET6;
49         endpoint_add(list, ia);
50     } else if (inet_pton(AF_INET, arg, &ia.u.a4) > 0) {
51         ia.af = AF_INET;
52         endpoint_add(list, ia);
53     } else if (getaddrinfo(arg, NULL, NULL, &ai) == 0) {
54         struct addrinfo* a;
55 
56         for (a = ai; a != NULL; a = a->ai_next) {
57             if (a->ai_socktype != SOCK_DGRAM)
58                 continue;
59             switch (a->ai_family) {
60             case PF_INET:
61                 ia.af = AF_INET;
62                 p     = &((struct sockaddr_in*)a->ai_addr)
63                          ->sin_addr;
64                 memcpy(&ia.u.a4, p, sizeof ia.u.a4);
65                 break;
66             case PF_INET6:
67                 ia.af = AF_INET6;
68                 p     = &((struct sockaddr_in6*)a->ai_addr)
69                          ->sin6_addr;
70                 memcpy(&ia.u.a6, p, sizeof ia.u.a6);
71                 break;
72             default:
73                 continue;
74             }
75             endpoint_add(list, ia);
76         }
77         freeaddrinfo(ai);
78     } else
79         usage("invalid host address");
80 }
81 
endpoint_add(endpoint_list * list,iaddr ia)82 void endpoint_add(endpoint_list* list, iaddr ia)
83 {
84     endpoint_ptr ep;
85 
86     ep = calloc(1, sizeof *ep);
87     assert(ep != NULL);
88     INIT_LINK(ep, link);
89     ep->ia = ia;
90     APPEND(*list, ep, link);
91 }
92 
ep_present(const endpoint_list * list,iaddr ia)93 int ep_present(const endpoint_list* list, iaddr ia)
94 {
95     endpoint_ptr ep;
96 
97     for (ep = HEAD(*list);
98          ep != NULL;
99          ep = NEXT(ep, link))
100         if (ia_equal(ia, ep->ia))
101             return TRUE;
102     return (FALSE);
103 }
104