* \brief Open network port using STATE(rig) data * * Open network port using STATE(rig) data. * NB: The signal PIPE will be ignored for the whole application. * * \param rp Port data structure (must spec port id eg hostname:port) * \param default_port Default network socket port * \return RIG_OK or < 0 if error */
| 229 | * \return RIG_OK or < 0 if error |
| 230 | */ |
| 231 | int network_open(hamlib_port_t *rp, int default_port) |
| 232 | { |
| 233 | int fd; /* File descriptor for the port */ |
| 234 | int status; |
| 235 | struct addrinfo hints, *res, *saved_res; |
| 236 | struct in6_addr serveraddr; |
| 237 | struct sockaddr_in client; |
| 238 | char hoststr[256], portstr[6] = ""; |
| 239 | |
| 240 | #ifdef __MINGW32__ |
| 241 | status = network_init(); |
| 242 | |
| 243 | if (status != RIG_OK) { return (status); } |
| 244 | |
| 245 | #endif |
| 246 | |
| 247 | if (!rp) |
| 248 | { |
| 249 | return (-RIG_EINVAL); |
| 250 | } |
| 251 | |
| 252 | memset(&hints, 0, sizeof(hints)); |
| 253 | hints.ai_flags = NI_NUMERICSERV; |
| 254 | hints.ai_family = AF_UNSPEC; |
| 255 | |
| 256 | if (rp->type.rig == RIG_PORT_UDP_NETWORK) |
| 257 | { |
| 258 | hints.ai_socktype = SOCK_DGRAM; |
| 259 | rig_debug(RIG_DEBUG_VERBOSE, "%s: UDP connect\n", __func__); |
| 260 | } |
| 261 | else |
| 262 | { |
| 263 | hints.ai_socktype = SOCK_STREAM; |
| 264 | rig_debug(RIG_DEBUG_VERBOSE, "%s: TCP connect\n", __func__); |
| 265 | } |
| 266 | |
| 267 | if (rp->pathname[0] == ':' && rp->pathname[1] != ':') |
| 268 | { |
| 269 | SNPRINTF(portstr, sizeof(portstr) - 1, "%s", rp->pathname + 1); |
| 270 | } |
| 271 | else |
| 272 | { |
| 273 | if (strlen(rp->pathname)) |
| 274 | { |
| 275 | status = parse_hoststr(rp->pathname, sizeof(rp->pathname), hoststr, portstr); |
| 276 | |
| 277 | if (status != RIG_OK) { return (status); } |
| 278 | |
| 279 | rig_debug(RIG_DEBUG_TRACE, "%s: hoststr=%s, portstr=%s\n", __func__, hoststr, |
| 280 | portstr); |
| 281 | |
| 282 | } |
| 283 | |
| 284 | if (strlen(portstr) == 0) |
| 285 | { |
| 286 | SNPRINTF(portstr, sizeof(portstr), "%d", default_port); |
| 287 | } |
| 288 | } |
no test coverage detected