int cccam_write_timeout_sec(int handle, unsigned char *netbuf, int writelen,int timeout) { int status=-1; int w_total = 0; // total bytes written int w_left = writelen; // bytes to be written int ret; if(SelectTimeOutSecond(handle,0, timeout)==1)/*---For test cccam stuck problem--*/ { w_total=S_WRITE(handle, netbuf, writelen); /* return the bytes read */ if((w_total == 0)||(w_total == -1)) { ;/* 这两种情况都可认为是链路关闭*/ //cccam_disconnectHandle_notify(handle); libc_printf("---cccam_write , handle is %d writelen is %d error is %d ---\r\n",handle ,writelen,w_total); //ctrl_cccam_update_control(1); } else { status=0; } } else { libc_printf("---cccam_write , error,server is not ready\r\n"); } return w_total; }
void close_quit(int sock) { if (sock >= 0) { S_WRITE(sock, "QUIT\r\n", 6); S_CLOSE(sock); } }
/*--成功返回0,失败返回-1---*/ int dm_write(int handle, unsigned char *netbuf, int writelen) { #if 1 int status=-1; int w_total = 0; // total bytes written int w_left = writelen; // bytes to be written int ret; if(SelectTimeOutSecond(handle,0, 30)==1) { w_total=S_WRITE(handle, netbuf, writelen); /* return the bytes read */ if((w_total == 0)||(w_total == -1)) { ; } else { status=0; } } else { libc_printf("---dm_write , error,server is not ready, w_total %d \r\n",w_total); } return w_total; #else int status=-1; int w_total = 0; // total bytes written int w_left = writelen; // bytes to be written int ret; SelectTimeOutSecond(handle,0, 30); w_total=S_WRITE(handle, netbuf, writelen); /* return the bytes read */ if((w_total == 0)||(w_total == -1)) { ; } else { status=0; } return w_total; #endif }
void send_control(int sock, char *string, ...) { va_list args; char *line = NULL; char *newline; char *s = NULL; line = safe_strdup(string); va_start(args, string); s = va_arg(args, char *); while ( s ) { newline = strconcat(line, s, NULL); safe_free(line); line = newline; s = va_arg(args, char *); } va_end(args); S_WRITE(sock, line, strlen(line)); safe_free(line); }
int main(int argc, char** argv) { campo schema_client[] = {{"ID", UNSIGNED | INT, 1}, {"NOMBRE", CHAR, 50}, {"APELLIDO", CHAR, 50}, {"EMAIL", CHAR, 80}, {0,0,0}}; campo primary_key[] = {{"ID", UNSIGNED | INT, 1}, {0,0,0}}; int client_key = 0; //puedo llegar hasta 999999 int record_counter = 0; //argv[1] nombre del archivo directo original //argv[2] nombre del archivo directo de salida if (argc != 3) { return -1; } //pedimos memoria para el registro de intercambio record = malloc(REG_SIZEOF(schema_book)); if (record == NULL) { return -2; } //apertura del directo original fd_dir = int D_OPEN(argv[1], READ); if (fd_dir == RES_NULL) { free(record); return -3; } //creacion de archivo secuencial de intercambio if (S_CREATE("temp.sec") == RES_ERROR) { D_CLOSE(fd_dir); free(record); return -4; } //a partir podria hacerse mejoras con las validaciones pertinentes //similares a las anteriores fd_sec = S_OPEN("temp.sec",WRITE); while (client_key <= 999999) { REG_SET(record,schema_client,"ID",client_key); if (D_READ(fd_dir, record) != RES_NO_EXISTE) { //es posible agregar a la logica codigos de error //se mantiene por simplicidad asi S_WRITE(fd_sec, record, REG_SIZEOF(schema_client)); record_counter++; } client_key++; } D_CLOSE(fd_dir); S_CLOSE(fd_sec); fd_sec = S_OPEN("temp.sec",READ); if (D_CREATE(argv[2], schema_client, primary_key, record_counter + ceil(record_counter * 0.35)) == RES_ERROR) { //mantengo secuencial para posterior procesamiento S_CLOSE(fd_sec); free(record); return -5; } fd_dir = D_OPEN(argv[2], WRITE); while (S_READ(fd_sec, record) != RES_EOF) { D_WRITE(fd_dir, record); } S_CLOSE(fd_sec); S_DESTROY("temp.sec"); D_CLOSE(fd_dir); free(record); return 0; }
int NwebWrite(int handle, unsigned char *netbuf, int writelen) { int ret=-1; ret= S_WRITE(handle, netbuf, writelen); }
int http_transfer(UrlResource *rsrc, libnet_callback notify) { FILE *out = NULL; Url *u = NULL; Url *proxy_url = NULL; Url *redir_u = NULL; char *request = NULL; //char *raw_header = NULL; HttpHeader *header = NULL; //char *len_string = NULL; //char *new_location = NULL; char *tmp_string = NULL; //char buf[BUFSIZE]; int sock = -1; ssize_t bytes_read = 0; int msg_code = 0; int retval = 0; int i; char *buf = MALLOC(BUFSIZE); if (!buf) { LIBNET_DEBUG("No enough memory!\n"); return 0; } /* make sure we haven't recursed too much */ if ( redirect_count > REDIRECT_MAX ) { LIBNET_DEBUG("redirection max count exceeded (looping redirect?)"); redirect_count = 0; msg_code = -NET_ERR_CONNECT_FAILED; goto cleanup; } /* make sure everything's initialized to something useful */ u = rsrc->url; if ( !u->host ) { LIBNET_DEBUG("no host specified"); msg_code = -NET_ERR_CONNECT_FAILED; goto cleanup; } /* fill in proxyness */ if ( !rsrc->proxy ) { rsrc->proxy = get_proxy("HTTP_PROXY"); } if (( NULL == rsrc->outfile ) && (NULL == rsrc->buffer)) { if ( u->file ) rsrc->outfile = strdup(u->file); else rsrc->outfile = strdup("index.html"); } if ( !u->path ) u->path = strdup("/"); if ( !u->file ) u->file = strdup(""); /* funny looking */ if ( !u->port ) u->port = 80; rsrc->options |= default_opts; /* send the request to either the proxy or the remote host */ if ( rsrc->proxy ) { proxy_url = url_new(); url_init(proxy_url, rsrc->proxy); if ( !proxy_url->port ) proxy_url->port = 80; if ( !proxy_url->host ) { LIBNET_DEBUG( "bad proxy `%s'", rsrc->proxy); msg_code = -NET_ERR_CONNECT_FAILED; goto cleanup; } if ( proxy_url->username ) rsrc->proxy_username = strdup(proxy_url->username); if ( proxy_url->password ) rsrc->proxy_password = strdup(proxy_url->password); #ifdef LIB_W5300 sock = w5300_tcp_connect(proxy_url->host, proxy_url->port, rsrc->srcip, rsrc->srcport); #else sock = util_tcp_connect(proxy_url->host, proxy_url->port); #endif if (sock < 0) { msg_code = -NET_ERR_CONNECT_FAILED; goto cleanup; } safe_free(u->path); safe_free(u->file); u->path = strdup(""); u->file = strdup(u->full_url); request = get_request(rsrc); LIBNET_DEBUG("sending request:\n%s", request); S_WRITE(sock, request, STRLEN(request)); FREE(request); } else /* no proxy */ { #ifdef LIB_W5300 sock = w5300_tcp_connect(u->host, u->port, rsrc->srcip, rsrc->srcport); #else sock = util_tcp_connect(u->host, u->port); #endif if (sock < 0) { msg_code = -NET_ERR_CONNECT_FAILED; goto cleanup; } request = get_request(rsrc); LIBNET_DEBUG("sending request:\n%s", request); S_WRITE(sock, request, STRLEN(request)); FREE(request); } if (rsrc->outfile) { if ((get_file_size(rsrc->outfile) > 0) && (rsrc->options & OPT_RESUME)) out = fopen(rsrc->outfile, "rb+"); else out = fopen(rsrc->outfile, "wb"); if ( !out ) { LIBNET_DEBUG( "opening %s: %s", rsrc->outfile, ""); msg_code = -NET_ERR_FILE_SAVE_ERROR; goto cleanup; } } /* check to see if it returned a HTTP 1.x response */ MEMSET(buf, '\0', 5); #ifdef LIB_W5300 bytes_read = S_READ(sock, buf, BUFSIZE); buf[bytes_read] = '\0'; LIBNET_DEBUG("receive respond:(%d)\n%s", bytes_read, buf); #else bytes_read = S_READ(sock, buf, 8); #endif if ( bytes_read <= 0 ) { msg_code = -NET_ERR_HTTP_SERVER_ERROR; goto cleanup; } if ( ! (buf[0] == 'H' && buf[1] == 'T' && buf[2] == 'T' && buf[3] == 'P') ) { if((rsrc->options & OPT_RESUME) && rsrc->outfile_offset) { LIBNET_DEBUG("server does not support resume!"); msg_code = -NET_ERR_OPERATION_NOT_PERMIT; goto cleanup; } //fwrite(buf, bytes_read, 1,out); } else { /* skip the header */ #ifdef LIB_W5300 buf[bytes_read] = '\0'; #else char *raw_header1 = NULL; buf[bytes_read] = '\0'; raw_header1 = get_raw_header(sock); strconcat2(buf, raw_header1, NULL); FREE(raw_header1); LIBNET_DEBUG("receive respond:(%d)\n%s", bytes_read, buf); #endif header = make_http_header(buf); /* check for redirects */ tmp_string = get_header_value("location", header); if (buf[9] == '3' && tmp_string ) { #if 1 //diable redirec function redir_u = url_new(); /* make sure we still send user/password along */ redir_u->username = safe_strdup(u->username); redir_u->password = safe_strdup(u->password); url_init(redir_u, tmp_string); rsrc->url = redir_u; redirect_count++; //retval = transfer(rsrc, notify); transfer((UINT32)rsrc, (UINT32)notify); rsrc->url =u; redirect_count--; if(redirect_count<0) redirect_count=0; if (redir_u) { url_destroy(redir_u); FREE(redir_u); } #endif FREE(tmp_string); tmp_string = NULL; //msg_code = -NET_ERR_OPERATION_NOT_PERMIT;//we can support redirect now, remove this line. goto cleanup; } if (tmp_string) { FREE(tmp_string); tmp_string = NULL; } if (buf[9] == '4' || buf[9] == '5') { for (i = 0; buf[i] && buf[i] != '\n'; i++); buf[i] = '\0'; LIBNET_DEBUG("HTTP error from server: %s\n", buf); if( buf[9] == '4' && buf[10] == '0' && buf[11] == '4') msg_code = -NET_ERR_FILE_NOT_FOUND; else msg_code = -NET_ERR_HTTP_SERVER_ERROR; goto cleanup; } tmp_string = get_header_value("content-length", header); if (tmp_string) { rsrc->outfile_size = (off_t )ATOI(tmp_string); FREE(tmp_string); tmp_string = NULL; if(rsrc->use_pecache ==0) { if ((rsrc->buffer) && (rsrc->buffer_len < rsrc->outfile_size)) { LIBNET_DEBUG("the buffer length less than the file fize (%d < %d)\n", rsrc->buffer, (int)rsrc->outfile_size); msg_code = -NET_ERR_FILE_SAVE_ERROR; goto cleanup; } } } tmp_string = get_header_value("content-range", header); if (tmp_string) { FREE(tmp_string); tmp_string = NULL; rsrc->outfile_size += rsrc->outfile_offset; } if ((!rsrc->outfile_size) && (rsrc->options & OPT_RESUME) && !(rsrc->options & OPT_NORESUME) && rsrc->outfile_offset ) { LIBNET_DEBUG("unable to determine remote file size"); msg_code = -NET_ERR_FILE_SAVE_ERROR; goto cleanup; } } if(rsrc->use_pecache ==0) retval = dump_data(rsrc, sock, out, notify); else retval = dump_data_to_pecache(rsrc, sock, out, notify); cleanup: free_http_header(header); //if (raw_header) // FREE(raw_header); if (proxy_url) { url_destroy(proxy_url); FREE(proxy_url); } if (sock >= 0) { S_CLOSE(sock); } if (out) fclose(out); if (buf) { FREE(buf); } #ifndef WIN32 if (rsrc->outfile) fs_sync(rsrc->outfile); #endif if (msg_code < 0 && rsrc->running) { notify(NET_MSG_DOWNLOAD_FINISH, (UINT32)msg_code); libnet_abort_url_read(TRUE);//to break url_open while loop //retval!=1 means transfer fail } return retval; }