int irda_socket_read(irda_t s, void * data, size_t * size, int * timeoutp) { int timeout; #if defined(_WIN32) || defined(WIN32) int outlen = 0; #else ssize_t outlen = 0; #endif CHECK_IRDA_HANDLE(s) if (data == NULL || size == NULL) { #if defined(_WIN32) || defined(WIN32) SetLastError(ERROR_INVALID_HANDLE); return -1; #else errno = EINVAL; return -1; #endif } if (! IS_SELECTABLE(s)) { #if defined(_WIN32) || defined(WIN32) SetLastError(ERROR_NOT_SUPPORTED); return -1; #else errno = EINVAL; return -1; #endif } BEGIN_SELECT_LOOP(s) timeout = internal_select(s, 0, interval); if (! timeout) outlen = recv(s->fd, (char *)data, (* size), 0); if (timeout == 1) { if (timeoutp != NULL) * timeoutp = 1; break; } END_SELECT_LOOP(s) if (outlen < 0) return -1; * size = (size_t)outlen; return 0; }
TEE_Result iso7816_select_next(struct tee_se_channel *c) { return internal_select(c, NULL, NEXT_OCCURRENCE); }
TEE_Result iso7816_select(struct tee_se_channel *c, struct tee_se_aid *aid) { return internal_select(c, aid, FIRST_OR_ONLY_OCCURRENCE); }
int irda_socket_write(irda_t s, const void * data, size_t * size, int * timeoutp) { int timeout = 0; #if defined(_WIN32) || defined(WIN32) unsigned int nbytes = 0; #else size_t nbytes = 0; #endif CHECK_IRDA_HANDLE(s) if (data == NULL || size == NULL) { #if defined(_WIN32) || defined(WIN32) SetLastError(ERROR_INVALID_HANDLE); return -1; #else errno = EINVAL; return -1; #endif } if (! IS_SELECTABLE(s)) { #if defined(_WIN32) || defined(WIN32) SetLastError(ERROR_NOT_SUPPORTED); return -1; #else errno = EINVAL; return -1; #endif } while (nbytes < (* size)) { int n = 0; BEGIN_SELECT_LOOP(s) timeout = internal_select(s, 1, interval); if (! timeout) n = send(s->fd, (char*)data + nbytes, (* size) - nbytes, 0); if (timeout == 1) { if (timeoutp != NULL) * timeoutp = 1; break; } END_SELECT_LOOP(s) if (n < 0) { // Error in send() if (size != NULL) * size = nbytes; return -1; } if (n == 0) break; nbytes += n; } * size = nbytes; return 0; }
int internal_connect(irda_t s, const struct sockaddr * addr, int len, int * timeoutp) { int rc, timeout; timeout = 0; rc = connect(s->fd, addr, len); #if defined(_WIN32) || defined(WIN32) if (s->timeout > 0) { if (rc < 0 && WSAGetLastError() == WSAEWOULDBLOCK && IS_SELECTABLE(s)) { fd_set fds; fd_set fds_exc; struct timeval tv; tv.tv_sec = (s->timeout / 1000); tv.tv_usec = (s->timeout % 1000) * 1000; FD_ZERO(& fds); FD_SET(s->fd, & fds); FD_ZERO(& fds_exc); FD_SET(s->fd, & fds_exc); rc = select(s->fd+1, NULL, & fds, & fds_exc, & tv); if (rc == 0) { rc = WSAEWOULDBLOCK; timeout = 1; } else if (rc > 0) { if (FD_ISSET(s->fd, & fds)) rc = 0; else { int rc_size = sizeof rc; assert(FD_ISSET(s->fd, & fds_exc)); if (getsockopt(s->fd, SOL_SOCKET, SO_ERROR, (char *)(& rc), & rc_size) == 0) WSASetLastError(rc); else rc = WSAGetLastError(); } } } } if (rc < 0) rc = WSAGetLastError(); #else if (s->timeout > 0) { if (rc < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) { timeout = internal_select(s, 1, s->timeout); if (timeout == 0) { socklen_t rc_size = sizeof rc; (void)getsockopt(s->fd, SOL_SOCKET, SO_ERROR, & rc, & rc_size); if (rc == EISCONN) rc = 0; errno = rc; } else if (timeout == -1) rc = errno; else rc = EWOULDBLOCK; } } if (rc < 0) rc = errno; #endif * timeoutp = timeout; return rc; }