/** * Close the existing connection to PEERINFO and reconnect. * * @param h handle to the service */ static void reconnect (struct GNUNET_PEERINFO_Handle *h) { if (GNUNET_SCHEDULER_NO_TASK != h->r_task) { GNUNET_SCHEDULER_cancel (h->r_task); h->r_task = GNUNET_SCHEDULER_NO_TASK; } if (NULL != h->th) { GNUNET_CLIENT_notify_transmit_ready_cancel (h->th); h->th = NULL; } if (NULL != h->client) { GNUNET_CLIENT_disconnect (h->client); h->client = NULL; } h->in_receive = GNUNET_NO; h->client = GNUNET_CLIENT_connect ("peerinfo", h->cfg); if (NULL == h->client) { h->r_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect_task, h); return; } trigger_transmit (h); }
static void retry_transmit(struct r3964_info *pInfo) { if(pInfo->nRetry<R3964_MAX_RETRIES) { TRACE_PE("transmission failed. Retry #%d", pInfo->nRetry); pInfo->bcc = 0; put_char(pInfo, STX); flush(pInfo); pInfo->state = R3964_TX_REQUEST; pInfo->count_down = R3964_TO_QVZ; pInfo->nRetry++; } else { TRACE_PE("transmission failed after %d retries", R3964_MAX_RETRIES); remove_from_tx_queue(pInfo, R3964_TX_FAIL); put_char(pInfo, NAK); flush(pInfo); pInfo->state = R3964_IDLE; trigger_transmit(pInfo); } }
/** * Call a method for each known matching host and change its trust * value. The callback method will be invoked once for each matching * host and then finally once with a NULL pointer. After that final * invocation, the iterator context must no longer be used. * * Instead of calling this function with 'peer == NULL' it is often * better to use 'GNUNET_PEERINFO_notify'. * * @param h handle to the peerinfo service * @param peer restrict iteration to this peer only (can be NULL) * @param timeout how long to wait until timing out * @param callback the method to call for each peer * @param callback_cls closure for callback * @return iterator context */ struct GNUNET_PEERINFO_IteratorContext * GNUNET_PEERINFO_iterate (struct GNUNET_PEERINFO_Handle *h, const struct GNUNET_PeerIdentity *peer, struct GNUNET_TIME_Relative timeout, GNUNET_PEERINFO_Processor callback, void *callback_cls) { struct GNUNET_MessageHeader *lapm; struct ListPeerMessage *lpm; struct GNUNET_PEERINFO_IteratorContext *ic; struct GNUNET_PEERINFO_AddContext *ac; ic = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_IteratorContext)); if (NULL == peer) { LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting list of peers from PEERINFO service\n"); ac = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_AddContext) + sizeof (struct GNUNET_MessageHeader)); ac->size = sizeof (struct GNUNET_MessageHeader); lapm = (struct GNUNET_MessageHeader *) &ac[1]; lapm->size = htons (sizeof (struct GNUNET_MessageHeader)); lapm->type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL); } else { LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting information on peer `%4s' from PEERINFO service\n", GNUNET_i2s (peer)); ac = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_AddContext) + sizeof (struct ListPeerMessage)); ac->size = sizeof (struct ListPeerMessage); lpm = (struct ListPeerMessage *) &ac[1]; lpm->header.size = htons (sizeof (struct ListPeerMessage)); lpm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET); memcpy (&lpm->peer, peer, sizeof (struct GNUNET_PeerIdentity)); ic->have_peer = GNUNET_YES; ic->peer = *peer; } ic->h = h; ic->ac = ac; ic->callback = callback; ic->callback_cls = callback_cls; ic->timeout = GNUNET_TIME_relative_to_absolute (timeout); ic->timeout_task = GNUNET_SCHEDULER_add_delayed (timeout, &signal_timeout, ic); ac->cont = &iterator_start_receive; ac->cont_cls = ic; GNUNET_CONTAINER_DLL_insert_tail (h->ac_head, h->ac_tail, ac); GNUNET_CONTAINER_DLL_insert_tail (h->ic_head, h->ic_tail, ic); trigger_transmit (h); return ic; }
static ssize_t r3964_write(struct tty_struct *tty, struct file *file, const unsigned char *data, size_t count) { struct r3964_info *pInfo = tty->disc_data; struct r3964_block_header *pHeader; struct r3964_client_info *pClient; unsigned char *new_data; TRACE_L("write request, %d characters", count); if (!pInfo) return -EIO; if (count > R3964_MTU) { if (pInfo->flags & R3964_DEBUG) { TRACE_L(KERN_WARNING "r3964_write: truncating user " "packet from %u to mtu %d", count, R3964_MTU); } count = R3964_MTU; } new_data = kmalloc(count + sizeof(struct r3964_block_header), GFP_KERNEL); TRACE_M("r3964_write - kmalloc %p", new_data); if (new_data == NULL) { if (pInfo->flags & R3964_DEBUG) { printk(KERN_ERR "r3964_write: no memory\n"); } return -ENOSPC; } pHeader = (struct r3964_block_header *)new_data; pHeader->data = new_data + sizeof(struct r3964_block_header); pHeader->length = count; pHeader->locks = 0; pHeader->owner = NULL; tty_lock(); pClient = findClient(pInfo, task_pid(current)); if (pClient) { pHeader->owner = pClient; } memcpy(pHeader->data, data, count); if (pInfo->flags & R3964_DEBUG) { dump_block(pHeader->data, count); } add_tx_queue(pInfo, pHeader); trigger_transmit(pInfo); tty_unlock(); return 0; }
/** * Transmit the request at the head of the transmission queue * and trigger continuation (if any). * * @param cls the 'struct GNUNET_PEERINFO_Handle' (with the queue) * @param size size of the buffer (0 on error) * @param buf where to copy the message * @return number of bytes copied to buf */ static size_t do_transmit (void *cls, size_t size, void *buf) { struct GNUNET_PEERINFO_Handle *h = cls; struct GNUNET_PEERINFO_AddContext *ac = h->ac_head; size_t ret; h->th = NULL; if (NULL == ac) return 0; /* request was cancelled in the meantime */ if (NULL == buf) { /* peerinfo service died */ LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, "Failed to transmit message to `%s' service.\n", "PEERINFO"); GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ac); reconnect (h); if (NULL != ac->cont) ac->cont (ac->cont_cls, _("failed to transmit request (service down?)")); GNUNET_free (ac); return 0; } ret = ac->size; if (size < ret) { /* change in head of queue (i.e. cancel + add), try again */ trigger_transmit (h); return 0; } LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting request of size %u to `%s' service.\n", ret, "PEERINFO"); memcpy (buf, &ac[1], ret); GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ac); trigger_transmit (h); if (NULL != ac->cont) ac->cont (ac->cont_cls, NULL); GNUNET_free (ac); return ret; }
/** * Add a host to the persistent list. This method operates in * semi-reliable mode: if the transmission is not completed by * the time 'GNUNET_PEERINFO_disconnect' is called, it will be * aborted. Furthermore, if a second HELLO is added for the * same peer before the first one was transmitted, PEERINFO may * merge the two HELLOs prior to transmission to the service. * * @param h handle to the peerinfo service * @param hello the verified (!) HELLO message * @param cont continuation to call when done, NULL is allowed * @param cont_cls closure for 'cont' * @return handle to cancel add operation; all pending * 'add' operations will be cancelled automatically * on disconnect, so it is not necessary to keep this * handle (unless 'cont' is NULL and at some point * calling 'cont' must be prevented) */ struct GNUNET_PEERINFO_AddContext * GNUNET_PEERINFO_add_peer (struct GNUNET_PEERINFO_Handle *h, const struct GNUNET_HELLO_Message *hello, GNUNET_PEERINFO_Continuation cont, void *cont_cls) { uint16_t hs = GNUNET_HELLO_size (hello); struct GNUNET_PEERINFO_AddContext *ac; struct GNUNET_PeerIdentity peer; GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_id (hello, &peer)); LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding peer `%s' to PEERINFO database (%u bytes of `%s')\n", GNUNET_i2s (&peer), hs, "HELLO"); ac = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_AddContext) + hs); ac->h = h; ac->size = hs; ac->cont = cont; ac->cont_cls = cont_cls; memcpy (&ac[1], hello, hs); GNUNET_CONTAINER_DLL_insert_tail (h->ac_head, h->ac_tail, ac); trigger_transmit (h); return ac; }
static void receive_char(struct r3964_info *pInfo, const unsigned char c) { switch (pInfo->state) { case R3964_TX_REQUEST: if (c == DLE) { TRACE_PS("TX_REQUEST - got DLE"); pInfo->state = R3964_TRANSMITTING; pInfo->tx_position = 0; transmit_block(pInfo); } else if (c == STX) { if (pInfo->nRetry == 0) { TRACE_PE("TX_REQUEST - init conflict"); if (pInfo->priority == R3964_SLAVE) { goto start_receiving; } } else { TRACE_PE("TX_REQUEST - secondary init " "conflict!? Switching to SLAVE mode " "for next rx."); goto start_receiving; } } else { TRACE_PE("TX_REQUEST - char != DLE: %x", c); retry_transmit(pInfo); } break; case R3964_TRANSMITTING: if (c == NAK) { TRACE_PE("TRANSMITTING - got NAK"); retry_transmit(pInfo); } else { TRACE_PE("TRANSMITTING - got invalid char"); pInfo->state = R3964_WAIT_ZVZ_BEFORE_TX_RETRY; mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ); } break; case R3964_WAIT_FOR_TX_ACK: if (c == DLE) { TRACE_PS("WAIT_FOR_TX_ACK - got DLE"); remove_from_tx_queue(pInfo, R3964_OK); pInfo->state = R3964_IDLE; trigger_transmit(pInfo); } else { retry_transmit(pInfo); } break; case R3964_WAIT_FOR_RX_REPEAT: /* FALLTROUGH */ case R3964_IDLE: if (c == STX) { /* Prevent rx_queue from overflow: */ if (pInfo->blocks_in_rx_queue >= R3964_MAX_BLOCKS_IN_RX_QUEUE) { TRACE_PE("IDLE - got STX but no space in " "rx_queue!"); pInfo->state = R3964_WAIT_FOR_RX_BUF; mod_timer(&pInfo->tmr, jiffies + R3964_TO_NO_BUF); break; } start_receiving: /* Ok, start receiving: */ TRACE_PS("IDLE - got STX"); pInfo->rx_position = 0; pInfo->last_rx = 0; pInfo->flags &= ~R3964_ERROR; pInfo->state = R3964_RECEIVING; mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ); pInfo->nRetry = 0; put_char(pInfo, DLE); flush(pInfo); pInfo->bcc = 0; } break; case R3964_RECEIVING: if (pInfo->rx_position < RX_BUF_SIZE) { pInfo->bcc ^= c; if (c == DLE) { if (pInfo->last_rx == DLE) { pInfo->last_rx = 0; goto char_to_buf; } pInfo->last_rx = DLE; break; } else if ((c == ETX) && (pInfo->last_rx == DLE)) { if (pInfo->flags & R3964_BCC) { pInfo->state = R3964_WAIT_FOR_BCC; mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ); } else { on_receive_block(pInfo); } } else { pInfo->last_rx = c; char_to_buf: pInfo->rx_buf[pInfo->rx_position++] = c; mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ); } } /* else: overflow-msg? BUF_SIZE>MTU; should not happen? */ break; case R3964_WAIT_FOR_BCC: pInfo->last_rx = c; on_receive_block(pInfo); break; } }
static void on_receive_block(struct r3964_info *pInfo) { unsigned int length; struct r3964_client_info *pClient; struct r3964_block_header *pBlock; length = pInfo->rx_position; /* compare byte checksum characters: */ if (pInfo->flags & R3964_BCC) { if (pInfo->bcc != pInfo->last_rx) { TRACE_PE("checksum error - got %x but expected %x", pInfo->last_rx, pInfo->bcc); pInfo->flags |= R3964_CHECKSUM; } } /* check for errors (parity, overrun,...): */ if (pInfo->flags & R3964_ERROR) { TRACE_PE("on_receive_block - transmission failed error %x", pInfo->flags & R3964_ERROR); put_char(pInfo, NAK); flush(pInfo); if (pInfo->nRetry < R3964_MAX_RETRIES) { pInfo->state = R3964_WAIT_FOR_RX_REPEAT; pInfo->nRetry++; mod_timer(&pInfo->tmr, jiffies + R3964_TO_RX_PANIC); } else { TRACE_PE("on_receive_block - failed after max retries"); pInfo->state = R3964_IDLE; } return; } /* received block; submit DLE: */ put_char(pInfo, DLE); flush(pInfo); del_timer_sync(&pInfo->tmr); TRACE_PS(" rx success: got %d chars", length); /* prepare struct r3964_block_header: */ pBlock = kmalloc(length + sizeof(struct r3964_block_header), GFP_KERNEL); TRACE_M("on_receive_block - kmalloc %p", pBlock); if (pBlock == NULL) return; pBlock->length = length; pBlock->data = ((unsigned char *)pBlock) + sizeof(struct r3964_block_header); pBlock->locks = 0; pBlock->next = NULL; pBlock->owner = NULL; memcpy(pBlock->data, pInfo->rx_buf, length); /* queue block into rx_queue: */ add_rx_queue(pInfo, pBlock); /* notify attached client processes: */ for (pClient = pInfo->firstClient; pClient; pClient = pClient->next) { if (pClient->sig_flags & R3964_SIG_DATA) { add_msg(pClient, R3964_MSG_DATA, length, R3964_OK, pBlock); } } wake_up_interruptible(&pInfo->read_wait); pInfo->state = R3964_IDLE; trigger_transmit(pInfo); }
static ssize_t r3964_write(struct tty_struct *tty, struct file *file, const unsigned char *data, size_t count) { struct r3964_info *pInfo = (struct r3964_info *)tty->disc_data; struct r3964_block_header *pHeader; struct r3964_client_info *pClient; unsigned char *new_data; TRACE_L("write request, %d characters", count); /* * Verify the pointers */ if (!pInfo) return -EIO; /* * Ensure that the caller does not wish to send too much. */ if (count > R3964_MTU) { if (pInfo->flags & R3964_DEBUG) { TRACE_L(KERN_WARNING "r3964_write: truncating user " "packet from %u to mtu %d", count, R3964_MTU); } count = R3964_MTU; } /* * Allocate a buffer for the data and copy it from the buffer with header prepended */ new_data = kmalloc(count + sizeof(struct r3964_block_header), GFP_KERNEL); TRACE_M("r3964_write - kmalloc %p", new_data); if (new_data == NULL) { if (pInfo->flags & R3964_DEBUG) { printk(KERN_ERR "r3964_write: no memory\n"); } return -ENOSPC; } pHeader = (struct r3964_block_header *)new_data; pHeader->data = new_data + sizeof(struct r3964_block_header); pHeader->length = count; pHeader->locks = 0; pHeader->owner = NULL; pClient = findClient(pInfo, task_pid(current)); if (pClient) { pHeader->owner = pClient; } memcpy(pHeader->data, data, count); /* We already verified this */ if (pInfo->flags & R3964_DEBUG) { dump_block(pHeader->data, count); } /* * Add buffer to transmit-queue: */ add_tx_queue(pInfo, pHeader); trigger_transmit(pInfo); return 0; }
/** * Type of a function to call when we receive a message from the * service. Call the iterator with the result and (if applicable) * continue to receive more messages or trigger processing the next * event (if applicable). * * @param cls closure * @param msg message received, NULL on timeout or fatal error */ static void peerinfo_handler (void *cls, const struct GNUNET_MessageHeader *msg) { struct GNUNET_PEERINFO_Handle *h = cls; struct GNUNET_PEERINFO_IteratorContext *ic = h->ic_head; const struct InfoMessage *im; const struct GNUNET_HELLO_Message *hello; GNUNET_PEERINFO_Processor cb; struct GNUNET_PeerIdentity id; void *cb_cls; uint16_t ms; GNUNET_assert (NULL != ic); h->in_receive = GNUNET_NO; ic->in_receive = GNUNET_NO; cb = ic->callback; cb_cls = ic->callback_cls; if (NULL == msg) { /* peerinfo service died, signal error */ GNUNET_PEERINFO_iterate_cancel (ic); reconnect (h); if (NULL != cb) cb (cb_cls, NULL, NULL, _("Failed to receive response from `PEERINFO' service.")); return; } if (GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END == ntohs (msg->type)) { /* normal end of list of peers, signal end, process next pending request */ LOG (GNUNET_ERROR_TYPE_DEBUG, "Received end of list of peers from `%s' service\n", "PEERINFO"); GNUNET_PEERINFO_iterate_cancel (ic); trigger_transmit (h); if (NULL != cb) cb (cb_cls, NULL, NULL, NULL); return; } ms = ntohs (msg->size); if ((ms < sizeof (struct InfoMessage)) || (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO)) { /* malformed message */ GNUNET_break (0); GNUNET_PEERINFO_iterate_cancel (ic); reconnect (h); if (NULL != cb) cb (cb_cls, NULL, NULL, _("Received invalid message from `PEERINFO' service.")); return; } im = (const struct InfoMessage *) msg; GNUNET_break (0 == ntohl (im->reserved)); if ( (GNUNET_YES == ic->have_peer) && (0 != memcmp (&ic->peer, &im->peer, sizeof (struct GNUNET_PeerIdentity))) ) { /* bogus message (from a different iteration call?); out of sequence! */ LOG (GNUNET_ERROR_TYPE_ERROR, "Received HELLO for peer `%s', expected peer `%s'\n", GNUNET_h2s (&im->peer.hashPubKey), GNUNET_i2s (&ic->peer)); GNUNET_break (0); GNUNET_PEERINFO_iterate_cancel (ic); reconnect (h); if (NULL != cb) cb (cb_cls, NULL, NULL, _("Received invalid message from `PEERINFO' service.")); return; } hello = NULL; if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader)) { hello = (const struct GNUNET_HELLO_Message *) &im[1]; if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello)) { /* malformed message */ GNUNET_break (0); GNUNET_PEERINFO_iterate_cancel (ic); reconnect (h); if (NULL != cb) cb (cb_cls, NULL, NULL, _("Received invalid message from `PEERINFO' service.")); return; } if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &id)) { /* malformed message */ GNUNET_break (0); GNUNET_PEERINFO_iterate_cancel (ic); reconnect (h); if (NULL != cb) cb (cb_cls, NULL, NULL, _("Received invalid message from `PEERINFO' service.")); return; } if (0 != memcmp (&im->peer, &id, sizeof (struct GNUNET_PeerIdentity))) { /* malformed message */ GNUNET_break (0); GNUNET_PEERINFO_iterate_cancel (ic); reconnect (h); if (NULL != cb) cb (cb_cls, NULL, NULL, _("Received invalid message from `PEERINFO' service.")); return; } } /* normal data message */ LOG (GNUNET_ERROR_TYPE_DEBUG, "Received %u bytes of `%s' information about peer `%s' from `%s' service\n", (hello == NULL) ? 0 : (unsigned int) GNUNET_HELLO_size (hello), "HELLO", GNUNET_i2s (&im->peer), "PEERINFO"); h->in_receive = GNUNET_YES; ic->in_receive = GNUNET_YES; GNUNET_CLIENT_receive (h->client, &peerinfo_handler, h, GNUNET_TIME_absolute_get_remaining (ic->timeout)); if (NULL != cb) cb (cb_cls, &im->peer, hello, NULL); }