blob: 329f379bf4643d955368c6b932f6d754fcaa15bb [file] [log] [blame]
Brian Waters13d96012017-12-08 16:53:31 -06001/*********************************************************************************************************
2* Software License Agreement (BSD License) *
3* Author: Sebastien Decugis <sdecugis@freediameter.net> *
4* *
5* Copyright (c) 2013, WIDE Project and NICT *
6* All rights reserved. *
7* *
8* Redistribution and use of this software in source and binary forms, with or without modification, are *
9* permitted provided that the following conditions are met: *
10* *
11* * Redistributions of source code must retain the above *
12* copyright notice, this list of conditions and the *
13* following disclaimer. *
14* *
15* * Redistributions in binary form must reproduce the above *
16* copyright notice, this list of conditions and the *
17* following disclaimer in the documentation and/or other *
18* materials provided with the distribution. *
19* *
20* * Neither the name of the WIDE Project or NICT nor the *
21* names of its contributors may be used to endorse or *
22* promote products derived from this software without *
23* specific prior written permission of WIDE Project and *
24* NICT. *
25* *
26* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
27* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
28* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
29* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
30* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS *
31* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
32* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF *
33* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
34*********************************************************************************************************/
35
36/* Do not include this directly, use dbg_interactive.i instead */
37
38/****** ENDPOINTS *********/
39
40%{
41
42#include <sys/socket.h>
43#include <netdb.h>
44
45%}
46
47%extend fd_endpoint {
48 fd_endpoint() {
49 struct fd_endpoint *np = (struct fd_endpoint *)calloc(1, sizeof(struct fd_endpoint));
50 if (!np) {
51 DI_ERROR_MALLOC;
52 return NULL;
53 }
54 fd_list_init(&np->chain, np);
55 return np;
56 }
57
58 fd_endpoint(const char * endpoint, uint16_t port = 0, uint32_t flags = EP_FL_CONF) {
59 struct addrinfo hints;
60 struct addrinfo *ai = NULL;
61 int ret;
62
63 memset(&hints, 0, sizeof(hints));
64 hints.ai_family= AF_UNSPEC;
65 hints.ai_flags = AI_NUMERICHOST;
66
67 ret = getaddrinfo(endpoint, NULL, &hints, &ai);
68 if (ret) {
69 DI_ERROR(ret, PyExc_ValueError, gai_strerror(ret));
70 return NULL;
71 }
72
73 if (port) {
74 switch (ai->ai_family) {
75 case AF_INET:
76 ((sSA4 *)ai->ai_addr)->sin_port = htons(port);
77 break;
78 case AF_INET6:
79 ((sSA6 *)ai->ai_addr)->sin6_port = htons(port);
80 break;
81 default:
82 DI_ERROR(EINVAL, PyExc_RuntimeError, "Unknown family returned by getaddrinfo");
83 return NULL;
84 }
85 }
86
87 struct fd_endpoint *np = (struct fd_endpoint *)calloc(1, sizeof(struct fd_endpoint));
88 if (!np) {
89 DI_ERROR_MALLOC;
90 return NULL;
91 }
92 fd_list_init(&np->chain, np);
93
94 memcpy(&np->s.sa, ai->ai_addr, ai->ai_addrlen);
95
96 freeaddrinfo(ai);
97
98 np->flags = flags;
99
100 return np;
101 }
102
103 ~fd_endpoint() {
104 fd_list_unlink(&$self->chain);
105 free($self);
106 }
107
108 /* Merge to a list */
109 %delobject add_merge;
110 void add_merge(struct fd_list * eplist) {
111 int ret;
112
113 if (!eplist) {
114 DI_ERROR(EINVAL, NULL, NULL);
115 return;
116 }
117
118 ret = fd_ep_add_merge( eplist, &$self->s.sa, sSAlen(&$self->s.sa), $self->flags );
119 if (ret) {
120 DI_ERROR(ret, NULL, NULL);
121 return;
122 }
123
124 return;
125 }
126
127 void dump() {
128 char * buf = NULL;
129 size_t len;
130 printf("%s", fd_ep_dump_one(&buf, &len, NULL, 1, $self));
131 free(buf);
132 }
133}