mirror of
https://github.com/lxsang/ant-http
synced 2025-07-13 13:04:23 +02:00
cleanup code
This commit is contained in:
11
lib/handle.c
11
lib/handle.c
@ -280,7 +280,11 @@ int antd_send(void *src, const void* data_in, int len_in)
|
||||
uint8_t* data = (uint8_t*)data_in;
|
||||
int len = len_in;
|
||||
antd_client_t * source = (antd_client_t *) src;
|
||||
|
||||
if(!src || !data)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifdef USE_ZLIB
|
||||
if(source->zstream && source->z_level != ANTD_CNONE)
|
||||
{
|
||||
@ -314,11 +318,6 @@ int antd_send(void *src, const void* data_in, int len_in)
|
||||
}
|
||||
#endif
|
||||
|
||||
if(!src || !data)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
int written;
|
||||
char* ptr;
|
||||
int writelen = 0;
|
||||
|
198
lib/mypty.c
198
lib/mypty.c
@ -1,198 +0,0 @@
|
||||
//#define _XOPEN_SOURCE 600
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
//#define __USE_BSD #define _BSD_SOURCE
|
||||
#include <termios.h>
|
||||
#include <sys/select.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <string.h>
|
||||
//#include <strings.h>
|
||||
int read_buf(int fd, char*buf,int size)
|
||||
{
|
||||
int i = 0;
|
||||
char c = '\0';
|
||||
int n;
|
||||
while ((i < size - 1) && (c != '\n'))
|
||||
{
|
||||
n = read(fd, &c, 1);
|
||||
if (n > 0)
|
||||
{
|
||||
buf[i] = c;
|
||||
i++;
|
||||
}
|
||||
else if(n == -1) return n;
|
||||
else
|
||||
c = '\n';
|
||||
}
|
||||
buf[i] = '\0';
|
||||
return i;
|
||||
}
|
||||
|
||||
int main(int ac, char *av[])
|
||||
{
|
||||
int fdm, fds;
|
||||
int rc;
|
||||
char input[150];
|
||||
// Check arguments
|
||||
fdm = posix_openpt(O_RDWR);
|
||||
if (fdm < 0)
|
||||
{
|
||||
fprintf(stderr, "Error %d on posix_openpt()\n", errno);
|
||||
return 1;
|
||||
}
|
||||
|
||||
rc = grantpt(fdm);
|
||||
if (rc != 0)
|
||||
{
|
||||
fprintf(stderr, "Error %d on grantpt()\n", errno);
|
||||
return 1;
|
||||
}
|
||||
|
||||
rc = unlockpt(fdm);
|
||||
if (rc != 0)
|
||||
{
|
||||
fprintf(stderr, "Error %d on unlockpt()\n", errno);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Open the slave side ot the PTY
|
||||
fds = open(ptsname(fdm), O_RDWR);
|
||||
|
||||
// Create the child process
|
||||
if (fork())
|
||||
{
|
||||
fd_set fd_in;
|
||||
|
||||
// FATHER
|
||||
|
||||
// Close the slave side of the PTY
|
||||
close(fds);
|
||||
|
||||
while (1)
|
||||
{
|
||||
// Wait for data from standard input and master side of PTY
|
||||
FD_ZERO(&fd_in);
|
||||
FD_SET(0, &fd_in);
|
||||
FD_SET(fdm, &fd_in);
|
||||
|
||||
rc = select(fdm + 1, &fd_in, NULL, NULL, NULL);
|
||||
switch(rc)
|
||||
{
|
||||
case -1 : fprintf(stderr, "Error %d on select()\n", errno);
|
||||
exit(1);
|
||||
|
||||
default :
|
||||
{
|
||||
// If data on standard input
|
||||
if (FD_ISSET(0, &fd_in))
|
||||
{
|
||||
rc = read(0, input, sizeof(input));
|
||||
if (rc > 0)
|
||||
{
|
||||
// Send data on the master side of PTY
|
||||
//printf("DATA sent\n");
|
||||
write(fdm, input, rc);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (rc < 0)
|
||||
{
|
||||
fprintf(stderr, "Error %d on read standard input\n", errno);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If data on master side of PTY
|
||||
if (FD_ISSET(fdm, &fd_in))
|
||||
{
|
||||
rc = read(fdm, input, sizeof(input));
|
||||
if (rc > 0)
|
||||
{
|
||||
// Send data on standard output
|
||||
write(1, input, rc);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (rc <= 0)
|
||||
{
|
||||
fprintf(stderr, "Error %d on read master PTY\n", errno);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} // End switch
|
||||
} // End while
|
||||
}
|
||||
else
|
||||
{
|
||||
struct termios slave_orig_term_settings; // Saved terminal settings
|
||||
struct termios new_term_settings; // Current terminal settings
|
||||
|
||||
// CHILD
|
||||
|
||||
// Close the master side of the PTY
|
||||
close(fdm);
|
||||
|
||||
// Save the defaults parameters of the slave side of the PTY
|
||||
rc = tcgetattr(fds, &slave_orig_term_settings);
|
||||
|
||||
// Set RAW mode on slave side of PTY
|
||||
new_term_settings = slave_orig_term_settings;
|
||||
cfmakeraw (&new_term_settings);
|
||||
tcsetattr (fds, TCSANOW, &new_term_settings);
|
||||
|
||||
// The slave side of the PTY becomes the standard input and outputs of the child process
|
||||
close(0); // Close standard input (current terminal)
|
||||
close(1); // Close standard output (current terminal)
|
||||
close(2); // Close standard error (current terminal)
|
||||
|
||||
dup(fds); // PTY becomes standard input (0)
|
||||
dup(fds); // PTY becomes standard output (1)
|
||||
dup(fds); // PTY becomes standard error (2)
|
||||
|
||||
// Now the original file descriptor is useless
|
||||
close(fds);
|
||||
|
||||
// Make the current process a new session leader
|
||||
setsid();
|
||||
|
||||
// As the child is a session leader, set the controlling terminal to be the slave side of the PTY
|
||||
// (Mandatory for programs like the shell to make them manage correctly their outputs)
|
||||
ioctl(0, TIOCSCTTY, 1);
|
||||
|
||||
system("/bin/bash");
|
||||
/*fd_set child_fds;
|
||||
char cinput[150];
|
||||
while(1){
|
||||
//printf("Wait for data\n");
|
||||
FD_ZERO(&child_fds);
|
||||
FD_SET(0, &child_fds);
|
||||
rc = select(1, &child_fds,NULL, NULL, NULL);
|
||||
if(rc == -1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (FD_ISSET(0, &child_fds))
|
||||
{
|
||||
rc = read_buf(0, cinput, sizeof(cinput));
|
||||
if (rc > 0)
|
||||
{
|
||||
system(cinput);
|
||||
} else if(rc < 0) break;
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
// if Error...
|
||||
_exit(1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
@ -1,108 +0,0 @@
|
||||
// network support
|
||||
#include <netinet/in.h>
|
||||
#include <ifaddrs.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <pthread.h>
|
||||
#include <errno.h>
|
||||
#include <sys/socket.h>
|
||||
#include <time.h>
|
||||
#include <resolv.h>
|
||||
|
||||
#include "../plugin.h"
|
||||
#define RQ
|
||||
#define REQUEST_PATTERN "POST /node_register HTTP/1.0\r\nHost: antd\r\nUser-Agent: antd\r\nContent-Type: application/json\r\nContent-Length: %d\r\n\r\n%s"
|
||||
#define JSON_MSG "{\"ip\":\"%s\",\"port\":\"%d\"}"
|
||||
void init();
|
||||
struct master_conf_t{
|
||||
int port;
|
||||
char* ip;
|
||||
} ;
|
||||
|
||||
struct master_conf_t mconfig;
|
||||
|
||||
call __init__ = init;
|
||||
|
||||
/*char* get_ip_address()
|
||||
{
|
||||
struct ifaddrs* addrs;
|
||||
getifaddrs(&addrs);
|
||||
struct ifaddrs* tmp = addrs;
|
||||
char* ip;
|
||||
while (tmp)
|
||||
{
|
||||
if (tmp->ifa_addr && tmp->ifa_addr->sa_family == AF_INET)
|
||||
{
|
||||
struct sockaddr_in *pAddr = (struct sockaddr_in *)tmp->ifa_addr;
|
||||
ip = inet_ntoa(pAddr->sin_addr);
|
||||
if(strcmp(ip,"127.0.0.1") != 0)
|
||||
return ip;
|
||||
}
|
||||
tmp = tmp->ifa_next;
|
||||
}
|
||||
freeifaddrs(addrs);
|
||||
return "127.0.0.1";
|
||||
}*/
|
||||
int inform_master()
|
||||
{
|
||||
int sockfd;
|
||||
//rpc_response_t* rdata = NULL;
|
||||
char* request;
|
||||
char* data = __s(JSON_MSG,get_ip_address(),__plugin__.sport);
|
||||
while((sockfd = request_socket(mconfig.ip, mconfig.port)) == -1)
|
||||
{
|
||||
// wait for 3s and then request to server
|
||||
usleep(3000000);
|
||||
}
|
||||
request = __s(REQUEST_PATTERN, strlen(data), data);
|
||||
send(sockfd,request, strlen(request),0);
|
||||
//rdata = parse_response(sockfd);
|
||||
close(sockfd);
|
||||
LOG("%s","OK, master registered \n");
|
||||
free(request);
|
||||
return 0;
|
||||
}
|
||||
static int config_handler(void* conf, const char* section, const char* name,
|
||||
const char* value)
|
||||
{
|
||||
struct master_conf_t* pconfig = (struct master_conf_t*)conf;
|
||||
char * ppath = NULL;
|
||||
if (strcmp(name, "port") == 0) {
|
||||
pconfig->port = atoi(value);
|
||||
} else if (strcmp(name, "ip") == 0) {
|
||||
pconfig->ip = strdup(value);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
void read_config()
|
||||
{
|
||||
mconfig.ip = "127.0.0.1";
|
||||
mconfig.port = 8080;
|
||||
char* file = __s("%s%s%s.ini",config_dir(), DIR_SEP, __plugin__.name);
|
||||
if (ini_parse(file, config_handler, &mconfig) < 0) {
|
||||
LOG("Can't load '%s'\n. Used defaut configuration", file);
|
||||
}
|
||||
printf("%s %d\n",mconfig.ip, mconfig.port );
|
||||
}
|
||||
void init()
|
||||
{
|
||||
read_config();
|
||||
pthread_t newthread;
|
||||
if (pthread_create(&newthread , NULL,(void(*)()) inform_master, NULL) != 0)
|
||||
perror("pthread_create: cannot create daemon for finding master");
|
||||
else
|
||||
{
|
||||
//reclaim the stack data when thread finish
|
||||
pthread_detach(newthread) ;
|
||||
}
|
||||
}
|
||||
void pexit()
|
||||
{
|
||||
LOG("%s","EXIT daemon");
|
||||
}
|
||||
void handler(void* c, const char* m, const char* rqp, dictionary d)
|
||||
{
|
||||
text(c);
|
||||
__t(c,"This is a system plugin. It cant be acessed from the web");
|
||||
}
|
@ -118,7 +118,7 @@ static void destroy_queue(antd_task_queue_t q)
|
||||
free(curr);
|
||||
}
|
||||
}
|
||||
static void work(antd_worker_t* worker)
|
||||
static void* work(antd_worker_t* worker)
|
||||
{
|
||||
antd_scheduler_t* scheduler = (antd_scheduler_t*) worker->manager;
|
||||
while(scheduler->status)
|
||||
@ -142,6 +142,7 @@ static void work(antd_worker_t* worker)
|
||||
}
|
||||
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -1,10 +0,0 @@
|
||||
#include "utils.h"
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
char* v = url_decode("code=3%2B4");
|
||||
if(match_float(argv[1]))
|
||||
printf("It is a float\n");
|
||||
printf("Result is %s\n",v);
|
||||
return 0;
|
||||
}
|
@ -485,8 +485,7 @@ char* __s(const char* fstring,...)
|
||||
int dlen;
|
||||
va_start( arguments, fstring);
|
||||
dlen = vsnprintf(0,0,fstring,arguments) + 1;
|
||||
va_end(arguments);
|
||||
va_end(arguments);
|
||||
va_end(arguments);
|
||||
if ((data = (char*)malloc(dlen*sizeof(char))) != 0)
|
||||
{
|
||||
va_start(arguments, fstring);
|
||||
|
Reference in New Issue
Block a user