- FD_ZERO(&fds);
- FD_SET(s, &fds);
- tv.tv_sec = 0; /* wait, but only for 10us */
- tv.tv_usec = 10;
-
- generic[0] = 0x00;
-
- readgood = 0;
- i = 0;
- j = 0;
- /* read first 6 bytes (the FLAP header only) off the socket */
- while ( (select(s+1, &fds, NULL, NULL, &tv) == 1) && (i < 6))
- {
- if ((err = Read(s, &(generic[i]), 1)) < 0)
- {
- /* error is probably not recoverable...(must be a pessimistic day) */
- aim_conn_close(conn);
- return err;
- }
-
- if (readgood == 0)
- {
- if (generic[i] == 0x2a)
- {
- readgood = 1;
-#if debug > 1
- printf("%x ", generic[i]);
- fflush(stdout);
-#endif
- i++;
- }
- else
- {
-#if debug > 1
- printf("skipping 0x%d ", generic[i]);
- fflush(stdout);
-#endif
- j++;
- }
- }
- else
- {
-#if debug > 1
- printf("%x ", generic[i]);
-#endif
- i++;
- }
- FD_ZERO(&fds);
- FD_SET(s, &fds);
- tv.tv_sec= 2;
- tv.tv_usec= 2;
- }
+ if (conn->status & AIM_CONN_STATUS_INPROGRESS)
+ return aim_conn_completeconnect(sess, conn);
+
+ /*
+ * Rendezvous (client-client) connections do not speak
+ * FLAP, so this function will break on them.
+ */
+ if (conn->type == AIM_CONN_TYPE_RENDEZVOUS)
+ return aim_get_command_rendezvous(sess, conn);
+ if (conn->type == AIM_CONN_TYPE_RENDEZVOUS_OUT) {
+ printf("out on fd %d\n", conn->fd);
+ return 0;
+ }
+
+ /*
+ * Read FLAP header. Six bytes:
+ *
+ * 0 char -- Always 0x2a
+ * 1 char -- Channel ID. Usually 2 -- 1 and 4 are used during login.
+ * 2 short -- Sequence number
+ * 4 short -- Number of data bytes that follow.
+ */
+ faim_mutex_lock(&conn->active);
+ if (aim_recv(conn->fd, generic, 6) < 6){
+ aim_conn_close(conn);
+ faim_mutex_unlock(&conn->active);
+ return -1;
+ }
+
+ /*
+ * This shouldn't happen unless the socket breaks, the server breaks,
+ * or we break. We must handle it just in case.
+ */
+ if (generic[0] != 0x2a) {
+ faimdprintf(1, "Bad incoming data!");
+ aim_conn_close(conn);
+ faim_mutex_unlock(&conn->active);
+ return -1;
+ }