Created data storage struct

Signed-off-by: Tyrolyean <tyrolyean@tyrolyean.net>
This commit is contained in:
Tyrolyean 2020-04-28 19:44:00 +02:00
parent b2cc23cafa
commit 2a69e3d104
No known key found for this signature in database
GPG Key ID: 81EC9BAC5E9667C6
1 changed files with 67 additions and 60 deletions

View File

@ -35,81 +35,91 @@ void error(char *msg) {
exit(1); exit(1);
} }
struct mail_recv_t{
char* input_buffer;
size_t in_len;
bool in_body;
bool after_body;
char* body_p;
size_t body_offs;
struct pollfd fds[2];
int n;
char buf[1000];
};
struct client_info { struct client_info {
int fd; int fd;
}; };
void receive_mail(char** input_buffer, size_t *in_len, bool *in_body, void receive_mail(struct mail_recv_t* rec){
bool *after_body, char** body_p, size_t *body_offs, struct pollfd fds[2],
char *buf, int n){
if(!*in_body){ if(!rec->in_body){
/* As long as we are outside the real mail body /* As long as we are outside the real mail body
* we can basically passthrough the commands * we can basically passthrough the commands
*/ */
*in_len += n; rec->in_len += rec->n;
*input_buffer = realloc(*input_buffer, *in_len); rec->input_buffer = realloc(rec->input_buffer, rec->in_len);
strncat(*input_buffer,buf, n); strncat(rec->input_buffer,rec->buf, rec->n);
*body_p = detect_start_of_body(*input_buffer); rec->body_p = detect_start_of_body(rec->input_buffer);
if(*body_p != NULL){ if(rec->body_p != NULL){
/* We reached the beginning of the body /* We reached the beginning of the body
* now! */ * now! */
*body_offs = body_p - input_buffer; rec->body_offs = rec->body_p - rec->input_buffer;
*in_body = true; rec->in_body = true;
write((fds[1].fd), write((rec->fds[1].fd),
*input_buffer+(*in_len-n), rec->input_buffer+(rec->in_len-rec->n),
*body_offs-(*in_len-n)); rec->body_offs-(rec->in_len-rec->n));
printf("Beginning of message found! " printf("Beginning of message found! Awaiting end...\n");
"Awaiting end...\n");
}else{ }else{
write((fds[1].fd), buf, n); write((rec->fds[1].fd), rec->buf, rec->n);
} }
} else if(!*after_body){ } else if(!rec->after_body){
/* We keep the body until we have it completely /* We keep the body until we have it completely
*/ */
*in_len += n; rec->in_len += rec->n;
*input_buffer = realloc(*input_buffer, *in_len); rec->input_buffer = realloc(rec->input_buffer, rec->in_len);
strncat(*input_buffer, buf, n); strncat(rec->input_buffer, rec->buf, rec->n);
*body_p = detect_end_of_body(*input_buffer+ rec->body_p = detect_end_of_body(rec->input_buffer+
*body_offs); rec->body_offs);
if(*body_p != NULL){ if(rec->body_p != NULL){
printf("Data found, interpreting...\n"); printf("Data found, interpreting...\n");
size_t body_len = *body_p- size_t body_len = rec->body_p-
(*input_buffer+*body_offs); (rec->input_buffer+rec->body_offs);
char* new_body = attach_files( char* new_body = attach_files(
*input_buffer+*body_offs, rec->input_buffer+rec->body_offs,
body_len); body_len);
if(new_body != NULL){ if(new_body != NULL){
/* Write the replacement */ /* Write the replacement */
write((fds[1].fd), new_body, write((rec->fds[1].fd), new_body,
strlen(new_body)); strlen(new_body));
free(new_body); free(new_body);
}else{ }else{
/* Write the original */ /* Write the original */
write((fds[1].fd), write((rec->fds[1].fd),
*input_buffer+*body_offs, rec->input_buffer+rec->body_offs,
body_len); body_len);
} }
/* Rest of conversation after message */ /* Rest of conversation after message */
write((fds[1].fd), write((rec->fds[1].fd),
*input_buffer+*body_offs+body_len, rec->input_buffer+rec->body_offs+body_len,
*in_len-(*body_offs+body_len)); rec->in_len-(rec->body_offs+body_len));
*after_body = true; rec->after_body = true;
} }
}else{ }else{
write((fds[1].fd), buf, n); write((rec->fds[1].fd), rec->buf, rec->n);
} }
return; return;
@ -140,46 +150,43 @@ void* client_handle_async(void* params){
error("ERROR connecting"); error("ERROR connecting");
} }
int n; /* message byte size */ struct mail_recv_t rec;
char buf[1000]; memset(&rec,0,sizeof(rec));
struct pollfd fds[2];
memset(fds, 0 , sizeof(fds)); rec.fds[0].fd = cli->fd;
fds[0].fd = cli->fd; rec.fds[0].events = POLLIN;
fds[0].events = POLLIN; rec.fds[1].fd = remote_fd;
fds[1].fd = remote_fd; rec.fds[1].events = POLLIN;
fds[1].events = POLLIN;
int timeout = (10 * 1000); /* 10 seconds */ int timeout = (10 * 1000); /* 10 seconds */
char * input_buffer = malloc(1); rec.input_buffer = malloc(1);
input_buffer[0]=0; rec.input_buffer[0]=0;
size_t in_len = 1; rec.in_len = 1;
bool in_body = false, after_body = false; rec.in_body = false;
char* body_p = NULL; rec.after_body = false;
size_t body_offs = 0; rec.body_p = NULL;
rec.body_offs = 0;
while(poll(fds, 2, timeout) > 0){ while(poll(rec.fds, 2, timeout) > 0){
for(size_t i = 0; i < 2; i++){ for(size_t i = 0; i < 2; i++){
if(!(fds[i].revents & POLLIN)){ if(!(rec.fds[i].revents & POLLIN)){
continue; continue;
} }
bzero(buf, sizeof(buf)); bzero(rec.buf, sizeof(rec.buf));
n = read(fds[i].fd, buf, sizeof(buf)-1); rec.n = read(rec.fds[i].fd, rec.buf, sizeof(rec.buf)-1);
if (n <= 0) { if (rec.n <= 0) {
goto closeup; goto closeup;
} }
if(i == 0){ if(i == 0){
receive_mail(&input_buffer, receive_mail(&rec);
&in_len, &in_body, &after_body,
&body_p, &body_offs,
fds, buf, n);
}else{ }else{
write((fds[!i].fd), buf, n); write((rec.fds[!i].fd), rec.buf, rec.n);
} }
@ -192,7 +199,7 @@ closeup:
close(cli->fd); close(cli->fd);
close(remote_fd); close(remote_fd);
free(cli); free(cli);
free(input_buffer); free(rec.input_buffer);
return NULL; return NULL;
} }