/*! \brief Initialize a socket and fill \ref sockaddr * \param[out] ss socket address (will be filled in) * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT * * This function creates (and optionall binds/connects) a socket using * \ref osmo_sock_init, but also fills the \a ss structure. */ int osmo_sock_init_sa(struct sockaddr *ss, uint16_t type, uint8_t proto, unsigned int flags) { char host[NI_MAXHOST]; uint16_t port; struct sockaddr_in *sin; struct sockaddr_in6 *sin6; int s, sa_len; /* determine port and host from ss */ switch (ss->sa_family) { case AF_INET: sin = (struct sockaddr_in *) ss; sa_len = sizeof(struct sockaddr_in); port = ntohs(sin->sin_port); break; case AF_INET6: sin6 = (struct sockaddr_in6 *) ss; sa_len = sizeof(struct sockaddr_in6); port = ntohs(sin6->sin6_port); break; default: return -EINVAL; } s = getnameinfo(ss, sa_len, host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); if (s != 0) { perror("getnameinfo failed"); return s; } return osmo_sock_init(ss->sa_family, type, proto, host, port, flags); }
/*! \brief Create a new (sending) GSMTAP source socket * \param[in] host host name or IP address in string format * \param[in] port UDP port number in host byte order * * Opens a GSMTAP source (sending) socket, conncet it to host/port and * return resulting fd. If \a host is NULL, the destination address * will be localhost. If \a port is 0, the default \ref * GSMTAP_UDP_PORT will be used. * */ int gsmtap_source_init_fd(const char *host, uint16_t port) { if (port == 0) port = GSMTAP_UDP_PORT; if (host == NULL) host = "localhost"; return osmo_sock_init(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, host, port, OSMO_SOCK_F_CONNECT); }
int ipa_server_link_open(struct ipa_server_link *link) { int ret; ret = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, link->addr, link->port, OSMO_SOCK_F_BIND); if (ret < 0) return ret; link->ofd.fd = ret; if (osmo_fd_register(&link->ofd) < 0) { close(ret); return -EIO; } return 0; }
/*! \brief Initialize a socket and fill \ref osmo_fd * \param[out] osmocom file descriptor (will be filled in) * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP * \param[in] host remote host name or IP address in string form * \param[in] port remote port number in host byte order * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT * * This function creates (and optionall binds/connects) a socket using * \ref osmo_sock_init, but also fills the \a ofd structure. */ int osmo_sock_init_ofd(struct osmo_fd *ofd, int family, int type, int proto, const char *host, uint16_t port, unsigned int flags) { int sfd, rc; sfd = osmo_sock_init(family, type, proto, host, port, flags); if (sfd < 0) return sfd; ofd->fd = sfd; ofd->when = BSC_FD_READ; rc = osmo_fd_register(ofd); if (rc < 0) { close(sfd); return rc; } return sfd; }
int ipa_client_conn_open(struct ipa_client_conn *link) { int ret; link->state = IPA_CLIENT_LINK_STATE_CONNECTING; ret = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, link->addr, link->port, OSMO_SOCK_F_CONNECT|OSMO_SOCK_F_NONBLOCK); if (ret < 0) return ret; link->ofd->fd = ret; link->ofd->when |= BSC_FD_WRITE; if (osmo_fd_register(link->ofd) < 0) { close(ret); link->ofd->fd = -1; return -EIO; } return 0; }
int main(int argc, char **argv) { struct l1fwd_hdl *l1fh; struct femtol1_hdl *fl1h; int rc, i; printf("sizeof(GsmL1_Prim_t) = %zu\n", sizeof(GsmL1_Prim_t)); printf("sizeof(SuperFemto_Prim_t) = %zu\n", sizeof(SuperFemto_Prim_t)); bts_log_init(NULL); /* * hack and prevent that two l1fwd-proxy/sysmobts run at the same * time. This is done by binding to the same VTY port. */ rc = osmo_sock_init(AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "127.0.0.1", 4241, OSMO_SOCK_F_BIND); if (rc < 0) { fprintf(stderr, "Failed to bind to the BTS VTY port.\n"); return EXIT_FAILURE; } /* allocate new femtol1_handle */ fl1h = talloc_zero(NULL, struct femtol1_hdl); INIT_LLIST_HEAD(&fl1h->wlc_list); /* open the actual hardware transport */ for (i = 0; i < ARRAY_SIZE(fl1h->write_q); i++) { rc = l1if_transport_open(i, fl1h); if (rc < 0) exit(1); } /* create our fwd handle */ l1fh = talloc_zero(NULL, struct l1fwd_hdl); l1fh->fl1h = fl1h; fl1h->priv = l1fh; /* Open UDP */ for (i = 0; i < ARRAY_SIZE(l1fh->udp_wq); i++) { struct osmo_wqueue *wq = &l1fh->udp_wq[i]; osmo_wqueue_init(wq, 10); wq->write_cb = udp_write_cb; wq->read_cb = udp_read_cb; wq->bfd.when |= BSC_FD_READ; wq->bfd.data = l1fh; wq->bfd.priv_nr = i; rc = osmo_sock_init_ofd(&wq->bfd, AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, NULL, fwd_udp_ports[i], OSMO_SOCK_F_BIND); if (rc < 0) { perror("sock_init"); exit(1); } } while (1) { rc = osmo_select_main(0); if (rc < 0) { perror("select"); exit(1); } } exit(0); }