- err = bufferevent_write (pkt->conn->bev, pkt->rpkt->data,
- pkt->rpkt->data_len);
- if (err < 0)
- rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
- "bufferevent_write: %s", evutil_gai_strerror(err));
-}
-
-static void
-_event_cb (struct bufferevent *bev, short events, void *ctx)
-{
- struct rs_packet *pkt = (struct rs_packet *) ctx;
- struct rs_connection *conn;
- struct rs_peer *p;
-
- assert (pkt);
- assert (pkt->conn);
- assert (pkt->conn->active_peer);
- conn = pkt->conn;
- p = conn->active_peer;
-
- p->is_connecting = 0;
- if (events & BEV_EVENT_CONNECTED)
- {
- p->is_connected = 1;
-#if defined (DEBUG)
- fprintf (stderr, "%s: connected\n", __func__);
-#endif
- _do_send (pkt);
- /* Packet will be freed in write callback. */
- }
- else if (events & BEV_EVENT_ERROR)
- rs_err_conn_push_fl (pkt->conn, RSE_CONNERR, __FILE__, __LINE__, NULL);
-}
-
-static void
-_write_cb (struct bufferevent *bev, void *ctx)
-{
- struct rs_packet *pkt = (struct rs_packet *) ctx;
-
- assert (pkt);
- assert (pkt->conn);
-#if defined (DEBUG)
- fprintf (stderr, "%s: packet written, breaking event loop\n", __func__);
-#endif
- if (event_base_loopbreak (pkt->conn->evb) < 0)
- abort (); /* FIXME */
- rs_packet_destroy (pkt);
-}
-
-static void
-_read_cb (struct bufferevent *bev, void *ctx)
-{
- struct rs_packet *pkt = (struct rs_packet *) ctx;
- size_t n;
-
- assert (pkt);
- assert (pkt->conn);
- if (!pkt->hdr_read_flag)
- {
- n = bufferevent_read (pkt->conn->bev, pkt->hdr, RS_HEADER_LEN);
- if (n == RS_HEADER_LEN)
- {
- pkt->hdr_read_flag = 1;
- pkt->rpkt->data_len = (pkt->hdr[2] << 8) + pkt->hdr[3];
- if (pkt->rpkt->data_len < 20 /* || len > 4096 */)
- abort (); /* FIXME: Read and discard packet. */
- pkt->rpkt->data = rs_malloc (pkt->conn->ctx, pkt->rpkt->data_len);
- if (!pkt->rpkt->data)
- {
- rs_err_conn_push_fl (pkt->conn, RSE_NOMEM, __FILE__, __LINE__,
- NULL);
- abort (); /* FIXME: Read and discard packet. */
- }
- memcpy (pkt->rpkt->data, pkt->hdr, RS_HEADER_LEN);
- bufferevent_setwatermark (pkt->conn->bev, EV_READ,
- pkt->rpkt->data_len - RS_HEADER_LEN, 0);
-#if defined (DEBUG)
- fprintf (stderr, "%s: packet header read, total pkt len=%d\n",
- __func__, pkt->rpkt->data_len);
-#endif
- }
- else if (n < 0)
- return; /* Buffer frozen. */
- else
- assert (!"short header");
- }
-
-#if defined (DEBUG)
- printf ("%s: trying to read %d octets of packet data\n", __func__, pkt->rpkt->data_len - RS_HEADER_LEN);
-#endif
- n = bufferevent_read (pkt->conn->bev, pkt->rpkt->data + RS_HEADER_LEN, pkt->rpkt->data_len - RS_HEADER_LEN);
-#if defined (DEBUG)
- printf ("%s: read %d octets of packet data\n", __func__, n);
-#endif
- if (n == pkt->rpkt->data_len - RS_HEADER_LEN)
- {
- bufferevent_disable (pkt->conn->bev, EV_READ);
- pkt->hdr_read_flag = 0;
- memset (pkt->hdr, 0, sizeof(*pkt->hdr));
-#if defined (DEBUG)
- fprintf (stderr, "%s: complete packet read\n", __func__);
-#endif
- rad_decode (pkt->rpkt, NULL, pkt->conn->active_peer->secret);
- if (event_base_loopbreak (pkt->conn->evb) < 0)
- abort (); /* FIXME */
- }
- else if (n < 0)
- return; /* Buffer frozen. */
- else
- assert (!"short packet");
-}