| 1 |
#ifndef PROACTOR_NETADDR_INTERNAL_H |
| 2 |
#define PROACTOR_NETADDR_INTERNAL_H |
| 3 |
|
| 4 |
/* |
| 5 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 6 |
* or more contributor license agreements. See the NOTICE file |
| 7 |
* distributed with this work for additional information |
| 8 |
* regarding copyright ownership. The ASF licenses this file |
| 9 |
* to you under the Apache License, Version 2.0 (the |
| 10 |
* "License"); you may not use this file except in compliance |
| 11 |
* with the License. You may obtain a copy of the License at |
| 12 |
* |
| 13 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 14 |
* |
| 15 |
* Unless required by applicable law or agreed to in writing, |
| 16 |
* software distributed under the License is distributed on an |
| 17 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 18 |
* KIND, either express or implied. See the License for the |
| 19 |
* specific language governing permissions and limitations |
| 20 |
* under the License. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include <stdint.h> |
| 24 |
|
| 25 |
struct pn_netaddr_t { |
| 26 |
union { |
| 27 |
struct sockaddr sa; |
| 28 |
struct sockaddr_in sin; |
| 29 |
struct sockaddr_in6 sin6; |
| 30 |
} ss; |
| 31 |
struct pn_netaddr_t *next; |
| 32 |
}; |
| 33 |
|
| 34 |
/* Return port or -1 if sa is not a known address type */ |
| 35 |
static inline int get_port(const struct sockaddr *sa) { |
| 36 |
switch (sa->sa_family) { |
| 37 |
case AF_INET: return ((struct sockaddr_in*)sa)->sin_port; |
| 38 |
case AF_INET6: return ((struct sockaddr_in6*)sa)->sin6_port; |
| 39 |
default: return -1; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
/* Set the port in sa or do nothing if it is not a known address type */ |
| 44 |
static inline void set_port(struct sockaddr *sa, uint16_t port) { |
| 45 |
switch (sa->sa_family) { |
| 46 |
case AF_INET: ((struct sockaddr_in*)sa)->sin_port = port; break; |
| 47 |
case AF_INET6: ((struct sockaddr_in6*)sa)->sin6_port = port; break; |
| 48 |
default: break; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
/* If want has port=0 and got has port > 0 then return port of got, else return 0 */ |
| 53 |
static inline uint16_t check_dynamic_port(const struct sockaddr *want, const struct sockaddr *got) { |
| 54 |
if (get_port(want) == 0) { |
| 55 |
int port = get_port(got); |
| 56 |
if (port > 0) return (uint16_t)port; |
| 57 |
} |
| 58 |
return 0; |
| 59 |
} |
| 60 |
|
| 61 |
#endif /*!PROACTOR_NETADDR_INTERNAL_H*/ |