Logo Search packages:      
Sourcecode: hamlib version File versions  Download package

int network_open ( hamlib_port_t rp,
int  default_port 
)

Open network port using rig.state data.

Open Open network port using rig.state data. NB: the signal PIPE will be ignored for the whole application.

Parameters:
rp port data structure (must spec port id eg hostname:port)
Returns:
RIG_OK or < 0 if error

Definition at line 150 of file network.c.

References hamlib_port_t::fd, hamlib_port_t::pathname, rig_debug(), RIG_DEBUG_ERR, RIG_ECONF, RIG_EINVAL, RIG_EIO, and RIG_OK.

Referenced by port_open(), and rot_open().

{
      int fd;                       /* File descriptor for the port */
      int status;
      struct addrinfo hints, *res;
      char *portstr;
      char hostname[FILPATHLEN] = "127.0.0.1";
      char defaultportstr[8];

#ifdef __MINGW32__
      WSADATA wsadata;
      if (!(wsstarted++) && WSAStartup(MAKEWORD(1,1), &wsadata) == SOCKET_ERROR) {
            rig_debug(RIG_DEBUG_ERR, "Error creating socket\n");
            return -RIG_EIO;
      }
#endif

      if (!rp)
            return -RIG_EINVAL;

      memset(&hints, 0, sizeof(hints));
      hints.ai_family = AF_INET /* PF_UNSPEC */;
      hints.ai_socktype = SOCK_STREAM;

      if (rp->pathname[0] == ':') {
            portstr = rp->pathname+1;
      } else {
            strncpy(hostname, rp->pathname, FILPATHLEN-1);

            /* search last ':', because IPv6 may have some */
            portstr = strrchr(hostname, ':');
            if (portstr) {
                  *portstr++ = '\0';
            } else {
                  sprintf(defaultportstr, "%d", default_port);
                  portstr = defaultportstr;
            }
      }
    
      status=getaddrinfo(hostname, portstr, &hints, &res);
      if (status != 0) {
            rig_debug(RIG_DEBUG_ERR, "Cannot get host \"%s\": %s\n",
                              rp->pathname, gai_strerror(errno));
            return -RIG_ECONF;
      }

      /* we don't want a signal when connection get broken */
#ifdef SIGPIPE
      signal(SIGPIPE, SIG_IGN);
#endif

      fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
      if (fd < 0)
            return -RIG_EIO;

      status = connect(fd, res->ai_addr, res->ai_addrlen);
      freeaddrinfo(res);
      if (status < 0) {
            rig_debug(RIG_DEBUG_ERR, "Cannot open NET device \"%s\": %s\n",
                              rp->pathname, strerror(errno));
            close(fd);
            return -RIG_EIO;
      }

      rp->fd = fd;

      return RIG_OK;
}


Generated by  Doxygen 1.6.0   Back to index