int main() { char request[1000], rfc[50], host[50], port[50], title[100], response[1000]; int err = OK; int method = LISTALL; int i; int recvmethod; generate_request(request, method, 1234, "www.google.com", "5675", "ABCD"); // client printf("sending request:\n%s\n", request); err = parse_request(request, rfc, host, port, title, &recvmethod); // server printf("method is: %d\n", recvmethod); if(err == OK) { generate_response(response, err, recvmethod); append_response(response, atoi(rfc), host, port, title); append_response(response, atoi(rfc), "www.facebook.com", port, title); printf("\nsending\n %s\n", response); printf("\n------Parsing-------\n"); for(i=0; i <100; i++) { if(parse_response(response, i, rfc, title, host, port) > 0) { printf("parsed rfc: %s\n", rfc); printf("parsed host: %s\n", host); printf("parsed title: %s\n", title); printf("parsed port: %s\n", port); } else { printf("\nbreaking at i: %d\n", i); break; } } } else { generate_response(request, err, method); printf("server replied: %s\n", request); } }
int handle_connection(socket_buf sb) { int size; http_request rst; char response_head[] = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"; char response_fail[] = "HTTP/1.1 404 not found\r\nContent-Type: text/html\r\n\r\n<html><body>request not found</body></html>"; if(http_request_parse(sb->read_buf, &rst) == L_HTTP_FAIL) return L_HTTP_FAIL; sb->rb_size = 0; printf("file:%s\n", rst.file); if((size = get_file_size(rst.file)) == L_HTTP_FAIL) { strcpy(sb->write_buf, response_fail); sb->wb_size = strlen(response_fail); return L_HTTP_SUCCESS; } size += strlen(response_head); if(size < strlen(response_head)) return L_HTTP_FAIL; if(socket_write_buf_alloc(sb, size) == L_HTTP_FAIL) return L_HTTP_FAIL; strncpy(sb->write_buf, response_head, strlen(response_head)); generate_response(sb->write_buf + strlen(sb->write_buf), rst.file); sb->wb_size = size; return L_HTTP_SUCCESS; }
err_t http_recv_callback(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) { http_arg *a = (http_arg*)arg; if (g_webserver_debug) xil_printf("%d (%d): R%d %d..\r\n", a?a->count:0, tpcb->state, p->len, p->tot_len); /* do not read the packet if we are not in ESTABLISHED state */ if (tpcb->state >= 5 && tpcb->state <= 8) { pbuf_free(p); return -1; } else if (tpcb->state > 8) { pbuf_free(p); return -1; } /* acknowledge that we've read the payload */ tcp_recved(tpcb, p->len); /* read and decipher the request */ /* this function takes care of generating a request, sending it, * and closing the connection if all data can been sent. If * not, then it sets up the appropriate arguments to the sent * callback handler. */ generate_response(tpcb, p->payload, p->len); /* free received packet */ pbuf_free(p); return ERR_OK; }
/* thread spawned for each connection */ void process_http_request(int sd) { int read_len,j; int RECV_BUF_SIZE = 1400; /* http request size can be a max of RECV_BUF_SIZE */ char recv_buf[1400]; /* since these buffers are allocated on the stack .. */ /* .. care should be taken to ensure there are no overflows */ /* read in the request */ if ((read_len = read(sd, recv_buf, RECV_BUF_SIZE)) < 0) { close(sd); #ifdef OS_IS_FREERTOS vTaskDelete(NULL); #endif return; } /* respond to request */ generate_response(sd, recv_buf, read_len); j = 0; while(j < 5000) j++; /* close connection */ close(sd); #ifdef OS_IS_FREERTOS vTaskDelete(NULL); #endif return; }
static void create_filter(struct eq_data *eq, unsigned size_log2, struct eq_gain *gains, unsigned num_gains, double beta) { int i; int half_block_size = eq->block_size >> 1; double window_mod = 1.0 / kaiser_window(0.0, beta); fft_t *fft = fft_new(size_log2); float *time_filter = (float*)calloc(eq->block_size * 2, sizeof(*time_filter)); if (!fft || !time_filter) goto end; // Make sure bands are in correct order. qsort(gains, num_gains, sizeof(*gains), gains_cmp); // Compute desired filter response. generate_response(eq->filter, gains, num_gains, half_block_size); // Get equivalent time-domain filter. fft_process_inverse(fft, time_filter, eq->filter, 1); // ifftshift() to create the correct linear phase filter. // The filter response was designed with zero phase, which won't work unless we compensate // for the repeating property of the FFT here by flipping left and right blocks. for (i = 0; i < half_block_size; i++) { float tmp = time_filter[i + half_block_size]; time_filter[i + half_block_size] = time_filter[i]; time_filter[i] = tmp; } // Apply a window to smooth out the frequency repsonse. for (i = 0; i < (int)eq->block_size; i++) { // Kaiser window. double phase = (double)i / (eq->block_size - 1); phase = 2.0 * (phase - 0.5); time_filter[i] *= window_mod * kaiser_window(phase, beta); } // Debugging. #if 0 FILE *file = fopen("/tmp/test.txt", "w"); if (file) { for (i = 0; i < (int)eq->block_size; i++) fprintf(file, "%.6f\n", time_filter[i]); fclose(file); } #endif // Padded FFT to create our FFT filter. fft_process_forward(eq->fft, eq->filter, time_filter, 1); end: fft_free(fft); free(time_filter); }
static void cache_ready_notify(void *user_data) { struct pbap_session *pbap = user_data; DBG(""); phonebook_req_finalize(pbap->obj->request); pbap->obj->request = NULL; pbap->cache.valid = TRUE; generate_response(pbap); obex_object_set_io_flags(pbap->obj, G_IO_IN, 0); }
/** * Callback function to handle the sipc reg response. */ static gboolean sipc_reg_cb(gint sk, gpointer user_data) { gchar buf[BUF_LENGTH]; gchar *digest; gchar *nonce, *key, *aeskey; gchar *response; gint n; fetion_account *ac = (fetion_account*)user_data; if ((n = recv(sk, buf, sizeof(buf), 0)) == -1) { hybrid_account_error_reason(ac->account, _("sipc reg error.")); return FALSE; } buf[n] = '\0'; hybrid_debug_info("fetion", "recv:\n%s", buf); /* parse response, we need the key and nouce */ digest = sip_header_get_attr(buf, "W"); if (parse_sipc_reg_response(digest, &nonce, &key) != HYBRID_OK) { g_free(digest); return FALSE; } aeskey = generate_aes_key(); response = generate_response(nonce, ac->userid, ac->password, key, aeskey); /* fill verify_data for pic confirm */ strncpy(verify_data.response, response, sizeof(verify_data.response)); /* now we start to handle the pushed messages */ ac->source = hybrid_event_add(sk, HYBRID_EVENT_READ, hybrid_push_cb, ac); /* start sipc authencation action. */ sipc_aut_action(sk, ac, response); g_free(digest); g_free(nonce); g_free(key); g_free(aeskey); g_free(response); return FALSE; }
static void *vobject_list_open(const char *name, int oflag, mode_t mode, void *context, size_t *size, int *err) { struct pbap_session *pbap = context; struct pbap_object *obj = NULL; int ret; void *request; DBG("name %s context %p valid %d", name, context, pbap->cache.valid); if (oflag != O_RDONLY) { ret = -EPERM; goto fail; } if (name == NULL) { ret = -EBADR; goto fail; } /* PullvCardListing always get the contacts from the cache */ if (pbap->cache.valid) { obj = vobject_create(pbap, NULL); ret = generate_response(pbap); } else { request = phonebook_create_cache(name, cache_entry_notify, cache_ready_notify, pbap, &ret); if (ret == 0) obj = vobject_create(pbap, request); } if (ret < 0) goto fail; if (err) *err = 0; return obj; fail: if (obj) vobject_close(obj); if (err) *err = ret; return NULL; }
/* thread spawned for each connection */ void process_http_request(int sd) { int read_len; /* since these buffers are allocated on the stack .. */ /* .. care should be taken to ensure there are no overflows */ char recv_buf[RECV_BUF_SIZE]; /* read in the request */ if ((read_len = read(sd, recv_buf, RECV_BUF_SIZE)) < 0) return; /* respond to request */ generate_response(sd, recv_buf, read_len); /* close connection */ close(sd); }
void run_client_manager(unsigned int num, int socket_slave, srv::parameters params) { //printf("Run client manager id: %d\n", (int)std::this_thread::get_id()); const unsigned short buffer_size(2048); char buffer[buffer_size]; memset(buffer, 0, buffer_size); while(true) { size_t recv_bytes = recv(socket_slave, (void*)buffer, buffer_size, MSG_NOSIGNAL); if(recv_bytes > 0) { std::string nnn = std::to_string(num) + "_log.txt"; FILE* logfile = fopen(nnn.c_str(), "w"); if(logfile) { for(int i = 0; i < recv_bytes; i++) { fprintf(logfile, "%c", buffer[i]); } fclose(logfile); } std::vector<std::string> string_list; parce_request(buffer, buffer_size, string_list); std::string response; if(generate_response(string_list, params, response)) { send(socket_slave, (void*)response.c_str(), response.size(), MSG_NOSIGNAL); } } else { //printf("Close client manager id: %d\n", (unsigned int)std::this_thread::get_id()); shutdown(socket_slave, SHUT_RDWR); close(socket_slave); break; } } }
static void create_filter(struct eq_data *eq, unsigned size_log2, struct eq_gain *gains, unsigned num_gains, double beta, const char *filter_path) { int i; int half_block_size = eq->block_size >> 1; double window_mod = 1.0 / kaiser_window_function(0.0, beta); fft_t *fft = fft_new(size_log2); float *time_filter = (float*)calloc(eq->block_size * 2 + 1, sizeof(*time_filter)); if (!fft || !time_filter) goto end; /* Make sure bands are in correct order. */ qsort(gains, num_gains, sizeof(*gains), gains_cmp); /* Compute desired filter response. */ generate_response(eq->filter, gains, num_gains, half_block_size); /* Get equivalent time-domain filter. */ fft_process_inverse(fft, time_filter, eq->filter, 1); // ifftshift() to create the correct linear phase filter. // The filter response was designed with zero phase, which won't work unless we compensate // for the repeating property of the FFT here by flipping left and right blocks. for (i = 0; i < half_block_size; i++) { float tmp = time_filter[i + half_block_size]; time_filter[i + half_block_size] = time_filter[i]; time_filter[i] = tmp; } /* Apply a window to smooth out the frequency repsonse. */ for (i = 0; i < (int)eq->block_size; i++) { /* Kaiser window. */ double phase = (double)i / eq->block_size; phase = 2.0 * (phase - 0.5); time_filter[i] *= window_mod * kaiser_window_function(phase, beta); } /* Debugging. */ if (filter_path) { FILE *file = fopen(filter_path, "w"); if (file) { for (i = 0; i < (int)eq->block_size - 1; i++) fprintf(file, "%.8f\n", time_filter[i + 1]); fclose(file); } } /* Padded FFT to create our FFT filter. * Make our even-length filter odd by discarding the first coefficient. * For some interesting reason, this allows us to design an odd-length linear phase filter. */ fft_process_forward(eq->fft, eq->filter, time_filter + 1, 1); end: fft_free(fft); free(time_filter); }