-/* exactly one of client and server must be non-NULL */
-/* return who we received from in *client or *server */
-/* return from in sa if not NULL */
-unsigned char *radudpget(int s, struct client **client, struct server **server, uint16_t *port) {
- int cnt, len;
- unsigned char buf[4], *rad = NULL;
- struct sockaddr_storage from;
- struct sockaddr *fromcopy;
- socklen_t fromlen = sizeof(from);
- struct clsrvconf *p;
- struct list_node *node;
- fd_set readfds;
- struct client *c = NULL;
- struct timeval now;
-
- for (;;) {
- if (rad) {
- free(rad);
- rad = NULL;
- }
- FD_ZERO(&readfds);
- FD_SET(s, &readfds);
- if (select(s + 1, &readfds, NULL, NULL, NULL) < 1)
- continue;
- cnt = recvfrom(s, buf, 4, MSG_PEEK | MSG_TRUNC, (struct sockaddr *)&from, &fromlen);
- if (cnt == -1) {
- debug(DBG_WARN, "radudpget: recv failed");
- continue;
- }
- if (cnt < 20) {
- debug(DBG_WARN, "radudpget: length too small");
- recv(s, buf, 4, 0);
- continue;
- }
-
- p = client
- ? find_clconf(handle, (struct sockaddr *)&from, NULL)
- : find_srvconf(handle, (struct sockaddr *)&from, NULL);
- if (!p) {
- debug(DBG_WARN, "radudpget: got packet from wrong or unknown UDP peer %s, ignoring", addr2string((struct sockaddr *)&from));
- recv(s, buf, 4, 0);
- continue;
- }
-
- len = RADLEN(buf);
- if (len < 20) {
- debug(DBG_WARN, "radudpget: length too small");
- recv(s, buf, 4, 0);
- continue;
- }
-
- rad = malloc(len);
- if (!rad) {
- debug(DBG_ERR, "radudpget: malloc failed");
- recv(s, buf, 4, 0);
- continue;
- }
-
- cnt = recv(s, rad, len, MSG_TRUNC);
- debug(DBG_DBG, "radudpget: got %d bytes from %s", cnt, addr2string((struct sockaddr *)&from));
-
- if (cnt < len) {
- debug(DBG_WARN, "radudpget: packet smaller than length field in radius header");
- continue;
- }
- if (cnt > len)
- debug(DBG_DBG, "radudpget: packet was padded with %d bytes", cnt - len);
-
- if (client) {
- *client = NULL;
- pthread_mutex_lock(p->lock);
- for (node = list_first(p->clients); node;) {
- c = (struct client *)node->data;
- node = list_next(node);
- if (s != c->sock)
- continue;
- gettimeofday(&now, NULL);
- if (!*client && addr_equal((struct sockaddr *)&from, c->addr)) {
- c->expiry = now.tv_sec + 60;
- *client = c;
- }
- if (c->expiry >= now.tv_sec)
- continue;
-
- debug(DBG_DBG, "radudpget: removing expired client (%s)", addr2string(c->addr));
- removeudpclientfromreplyq(c);
- c->replyq = NULL; /* stop removeclient() from removing common udp replyq */
- removelockedclient(c);
- break;
- }
- if (!*client) {
- fromcopy = addr_copy((struct sockaddr *)&from);
- if (!fromcopy) {
- pthread_mutex_unlock(p->lock);
- continue;
- }
- c = addclient(p, 0);
- if (!c) {
- free(fromcopy);
- pthread_mutex_unlock(p->lock);
- continue;
- }
- c->sock = s;
- c->addr = fromcopy;
- gettimeofday(&now, NULL);
- c->expiry = now.tv_sec + 60;
- *client = c;
- }
- pthread_mutex_unlock(p->lock);
- } else if (server)
- *server = p->servers;
- break;
+ assert (r == pkt->rpkt->length);
+ /* Unlink the packet. */
+ conn->out_queue = pkt->next;
+
+ /* If there are more packets in queue, add the write event again. */
+ if (pkt->conn->out_queue)
+ {
+ r = event_add (pkt->conn->wev, NULL);
+ if (r < 0)
+ return rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
+ "event_add: %s", evutil_gai_strerror (r));
+ rs_debug (("%s: re-adding the write event\n", __func__));