void adbd_auth_confirm_key(const char* key, size_t len, atransport* t) { if (!usb_transport) { usb_transport = t; t->AddDisconnect(&usb_disconnect); } if (framework_fd < 0) { LOG(ERROR) << "Client not connected"; needs_retry = true; return; } if (key[len - 1] != '\0') { LOG(ERROR) << "Key must be a null-terminated string"; return; } char msg[MAX_PAYLOAD_V1]; int msg_len = snprintf(msg, sizeof(msg), "PK%s", key); if (msg_len >= static_cast<int>(sizeof(msg))) { LOG(ERROR) << "Key too long (" << msg_len << ")"; return; } LOG(DEBUG) << "Sending '" << msg << "'"; if (unix_write(framework_fd, msg, msg_len) == -1) { PLOG(ERROR) << "Failed to write PK"; return; } }
static void file_buff_done( FileBuff buff ) { if (buff->count > 0) { unix_write( buff->fd, buff->data, buff->count ); buff->count = 0; } }
static int unix_send(struct socket *sock, void *buff, int len, int nonblock, unsigned int flags) { if (flags != 0) return -EINVAL; return unix_write(sock, (char *) buff, len, nonblock); }
int file_write(void * fd, char * buffer, int size) { #ifdef WIN32 return win32_rw(fd, TRUE, buffer, size); #else return unix_write(fd, buffer, size, NULL); #endif }
static void file_buff_write( FileBuff buff, const void* src, int len ) { while (len > 0) { int avail = sizeof(buff->data) - buff->count; if (avail > len) avail = len; memcpy( buff->data + buff->count, src, avail ); len -= avail; src = (char*)src + avail; buff->count += avail; if (buff->count == FILE_BUFF_SIZE) { unix_write( buff->fd, buff->data, buff->count ); buff->count = 0; } } }
static void read_and_dump(int fd) { char buf[4096]; int len; while(fd >= 0) { len = adb_read(fd, buf, 4096); if(len == 0) { break; } if(len < 0) { if(errno == EINTR) continue; break; } /* we want to output to stdout, so no adb_write here !! */ unix_write(1, buf, len); } }
int main(int argc, char *argv[]) { if (argc != 2) return 1; int fdfifo = open(argv[1], O_WRONLY); assert(fdfifo >= 0); struct flock fl; fl.l_start = 0; fl.l_len = 0; fl.l_pid = getpid(); fl.l_type = F_WRLCK; fl.l_whence = SEEK_SET; int ret = fcntl(fdfifo, F_SETLKW, &fl); assert(ret >= 0); for (;;) { unsigned char buf[BUFSIZE]; ret = read(0, buf, sizeof(buf)); if (ret < 0) { if (errno != EINTR) break; else continue; } if (ret == 0) break; ret = unix_write(fdfifo, buf, ret); if (!ret) break; } close(fdfifo); return 0; }
static int unix_to_unix(MainParam_t *mp) { return unix_write(0, mp, 0); }
static int dos_to_unix(direntry_t *entry, MainParam_t *mp) { return unix_write(entry, mp, 1); }
/*M \emph{Simple HTTP streaming server main loop.} The mainloop opens the MPEG Audio file \verb|filename|, reads each frame into an rtp packet and sends it out using HTTP. After sending a packet, the mainloop sleeps for the duration of the packet, synchronizing itself when the sleep is not accurate enough. If the sleep desynchronizes itself from the stream more than \verb|MAX_WAIT_TIME|, the synchronization is reset. **/ int poc_mainloop(http_server_t *server, char *filename, int quiet) { /*M Open file for reading. **/ file_t mp3_file; if (!file_open_read(&mp3_file, filename)) { fprintf(stderr, "Could not open mp3 file: %s\n", filename); return 0; } if (!quiet) fprintf(stderr, "\rStreaming %s...\n", filename); static long wait_time = 0; unsigned long frame_time = 0; mp3_frame_t frame; /*M Cycle through the frames and send them using HTTP. **/ while ((mp3_next_frame(&mp3_file, &frame) >= 0) && !finished) { /*M Get start time for this frame iteration. **/ struct timeval tv; gettimeofday(&tv, NULL); unsigned long start_sec, start_usec; start_sec = tv.tv_sec; start_usec = tv.tv_usec; /*M Go through HTTP main routine and check for timeouts, received data, etc... **/ if (!http_server_main(server, NULL)) { fprintf(stderr, "Http main error\n"); return 0; } /*M Write frame to HTTP clients. **/ int i; for (i = 0; i < server->num_clients; i++) { if ((server->clients[i].fd != -1) && (server->clients[i].found >= 2)) { int ret; ret = unix_write(server->clients[i].fd, frame.raw, frame.frame_size); if (ret != frame.frame_size) { fprintf(stderr, "Error writing to client %d: %d\n", i, ret); http_client_close(server, server->clients + i); } } } frame_time += frame.usec; wait_time += frame.usec; /*M Sleep for duration of frame. **/ if (wait_time > 1000) usleep(wait_time); /*M Print information. **/ if (!quiet) { static int count = 0; if ((count++) % 10 == 0) { if (mp3_file.size > 0) { fprintf(stderr, "\r%02ld:%02ld/%02ld:%02ld %7ld/%7ld (%3ld%%) %3ldkbit/s %4ldb ", (frame_time/1000000) / 60, (frame_time/1000000) % 60, (long)((float)(frame_time/1000) / ((float)mp3_file.offset+1) * (float)mp3_file.size) / 60000, (long)((float)(frame_time/1000) / ((float)mp3_file.offset+1) * (float)mp3_file.size) / 1000 % 60, mp3_file.offset, mp3_file.size, (long)(100*(float)mp3_file.offset/(float)mp3_file.size), frame.bitrate, frame.frame_size); } else { fprintf(stderr, "\r%02ld:%02ld %ld %3ldkbit/s %4ldb ", (frame_time/1000000) / 60, (frame_time/1000000) % 60, mp3_file.offset, frame.bitrate, frame.frame_size); } } fflush(stderr); } /*M Get length of iteration. **/ gettimeofday(&tv, NULL); unsigned long len = (tv.tv_sec - start_sec) * 1000000 + (tv.tv_usec - start_usec); wait_time -= len; if (abs(wait_time) > MAX_WAIT_TIME) wait_time = 0; } if (!file_close(&mp3_file)) { fprintf(stderr, "Could not close mp3 file %s\n", filename); return 0; } return 1; }