+
+static void reconnect_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data)
+{
+ assert(rpc->magic == RPC_CONTEXT_MAGIC);
+
+ if (status != RPC_STATUS_SUCCESS) {
+ rpc_error_all_pdus(rpc, "RPC ERROR: Failed to reconnect async");
+ return;
+ }
+
+ rpc->is_connected = 1;
+ rpc->connect_cb = NULL;
+}
+
+/* disconnect but do not error all PDUs, just move pdus in-flight back to the outqueue and reconnect */
+static int rpc_reconnect_requeue(struct rpc_context *rpc)
+{
+ struct rpc_pdu *pdu;
+
+ assert(rpc->magic == RPC_CONTEXT_MAGIC);
+
+ if (rpc->fd != -1) {
+ close(rpc->fd);
+ }
+ rpc->fd = -1;
+
+ rpc->is_connected = 0;
+
+ /* socket is closed so we will not get any replies to any commands
+ * in flight. Move them all over from the waitpdu queue back to the out queue
+ */
+ for (pdu=rpc->waitpdu; pdu; pdu=pdu->next) {
+ SLIST_REMOVE(&rpc->waitpdu, pdu);
+ SLIST_ADD(&rpc->outqueue, pdu);
+ /* we have to re-send the whole pdu again */
+ pdu->written = 0;
+ }
+
+ if (rpc->auto_reconnect != 0) {
+ rpc->connect_cb = reconnect_cb;
+
+ if (rpc_connect_sockaddr_async(rpc, &rpc->s) != 0) {
+ rpc_error_all_pdus(rpc, "RPC ERROR: Failed to reconnect async");
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
+
+int rpc_bind_udp(struct rpc_context *rpc, char *addr, int port)
+{
+ struct addrinfo *ai = NULL;
+ char service[6];
+
+ assert(rpc->magic == RPC_CONTEXT_MAGIC);
+
+ if (rpc->is_udp == 0) {
+ rpc_set_error(rpc, "Cant not bind UDP. Not UDP context");
+ return -1;
+ }
+
+ sprintf(service, "%d", port);
+ if (getaddrinfo(addr, service, NULL, &ai) != 0) {
+ rpc_set_error(rpc, "Invalid address:%s. "
+ "Can not resolv into IPv4/v6 structure.");
+ return -1;
+ }
+
+ switch(ai->ai_family) {
+ case AF_INET:
+ rpc->fd = socket(ai->ai_family, SOCK_DGRAM, 0);
+ if (rpc->fd == -1) {
+ rpc_set_error(rpc, "Failed to create UDP socket: %s", strerror(errno));
+ freeaddrinfo(ai);
+ return -1;
+ }
+
+ if (bind(rpc->fd, (struct sockaddr *)ai->ai_addr, sizeof(struct sockaddr_in)) != 0) {
+ rpc_set_error(rpc, "Failed to bind to UDP socket: %s",strerror(errno));
+ freeaddrinfo(ai);
+ return -1;
+ }
+ break;
+ default:
+ rpc_set_error(rpc, "Can not handle UPD sockets of family %d yet", ai->ai_family);
+ freeaddrinfo(ai);
+ return -1;
+ }
+
+ freeaddrinfo(ai);
+
+ return 0;
+}
+
+int rpc_set_udp_destination(struct rpc_context *rpc, char *addr, int port, int is_broadcast)
+{
+ struct addrinfo *ai = NULL;
+ char service[6];
+
+ assert(rpc->magic == RPC_CONTEXT_MAGIC);
+
+ if (rpc->is_udp == 0) {
+ rpc_set_error(rpc, "Can not set destination sockaddr. Not UDP context");
+ return -1;
+ }
+
+ sprintf(service, "%d", port);
+ if (getaddrinfo(addr, service, NULL, &ai) != 0) {
+ rpc_set_error(rpc, "Invalid address:%s. "
+ "Can not resolv into IPv4/v6 structure.");
+ return -1;
+ }
+
+ if (rpc->udp_dest) {
+ free(rpc->udp_dest);
+ rpc->udp_dest = NULL;
+ }
+ rpc->udp_dest = malloc(ai->ai_addrlen);
+ if (rpc->udp_dest == NULL) {
+ rpc_set_error(rpc, "Out of memory. Failed to allocate sockaddr structure");
+ freeaddrinfo(ai);
+ return -1;
+ }
+ memcpy(rpc->udp_dest, ai->ai_addr, ai->ai_addrlen);
+ freeaddrinfo(ai);
+
+ rpc->is_broadcast = is_broadcast;
+ setsockopt(rpc->fd, SOL_SOCKET, SO_BROADCAST, (char *)&is_broadcast, sizeof(is_broadcast));
+
+ return 0;
+}
+
+struct sockaddr *rpc_get_recv_sockaddr(struct rpc_context *rpc)
+{
+ assert(rpc->magic == RPC_CONTEXT_MAGIC);
+
+ return (struct sockaddr *)&rpc->udp_src;
+}
+
+int rpc_queue_length(struct rpc_context *rpc)
+{
+ int i=0;
+ struct rpc_pdu *pdu;
+
+ assert(rpc->magic == RPC_CONTEXT_MAGIC);
+
+ for(pdu = rpc->outqueue; pdu; pdu = pdu->next) {
+ i++;
+ }
+ for(pdu = rpc->waitpdu; pdu; pdu = pdu->next) {
+ i++;
+ }
+ return i;
+}
+
+void rpc_set_fd(struct rpc_context *rpc, int fd)
+{
+ assert(rpc->magic == RPC_CONTEXT_MAGIC);
+
+ rpc->fd = fd;
+}