|
| | |
| /*
* Copyright (C) Vlad Vostrykh
*
* HOWTO:
* 1.put this file into nginx-xx/src/http/modules
* 2. edit file nginx-xx/auto/modules, add 2 lines at the top:
* HTTP_MODULES="$HTTP_MODULES ngx_http_voc_module"
* HTTP_SRCS="$HTTP_SRCS src/http/modules/ngx_http_voc_module.c"
*
* 3. ./configure, make, make install -- in usual way
* 4. change config, i.e.
* Location = /vocd {
* voc /tmp/vochat;
* }
* 5. start nginx
*
*/
#include
#include
#include
//unix sockets
#include
#include
//to avoid error with timeval structure
#include
#include
#include
typedef struct {
ngx_str_t serv_socket;
} ngx_http_voc_conf_t;
static void *ngx_http_voc_create_conf(ngx_conf_t *cf);
static char *ngx_http_voc(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static ngx_command_t ngx_http_voc_commands[] = {
{ ngx_string("voc"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_http_voc,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_voc_conf_t, serv_socket),
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_voc_module_ctx = {
NULL, /* preconfiguration */
NULL, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_voc_create_conf, /* create location configuration */
NULL /* merge location configuration */
};
ngx_module_t ngx_http_voc_module = {
NGX_MODULE_V1,
&ngx_http_voc_module_ctx, /* module context */
ngx_http_voc_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static ngx_int_t send_error(ngx_http_request_t *r, char *errtext) {
ngx_int_t rc;
ngx_buf_t *b;
ngx_chain_t out;
rc = ngx_http_discard_body(r);
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
if (b == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
out.buf = b;
out.next = NULL;
r->headers_out.content_type.len = sizeof("text/html") - 1;
r->headers_out.content_type.data = (u_char *) "text/html";
char tmp[4098];
//!TODO have to check ngx_sprintf and other functions, probably I don't need additional buffer 'tmp' here
//unfortunately there are not so many comments in the nginx-source code
sprintf(tmp,
"Voodoo chat ngx_voc module.
\n"\
" Cannot connect to Voodoo Chat daemon, error is: %s
"\
"Please, contact server administrator\n\n\n", errtext);
b->pos = (u_char *)tmp;
b->last = b->pos + strlen(tmp);
b->memory = 1;
b->last_buf = 1;
r->headers_out.status = NGX_HTTP_SERVICE_UNAVAILABLE;
r->headers_out.content_length_n = -1;
r->headers_out.last_modified_time = -1;
rc = ngx_http_send_header(r);
return ngx_http_output_filter(r, &out);
}
static struct cmsghdr *cmptr = NULL; /* buffer is malloc'ed first time */
#define CONTROLLEN (sizeof(struct cmsghdr) + sizeof(int))
static ngx_int_t
ngx_http_voc_handler(ngx_http_request_t *r)
{
ngx_int_t rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
ngx_http_voc_conf_t *conf;
conf = ngx_http_get_module_loc_conf(r, ngx_http_voc_module);
int error = 0, mysent = 0;
//well, for the daemon i need: 32 for sessID, and 4 for daemon type (&p=1)
//so set max limit to 256, probably will use something later.
char myargs[256];
if ( r->args.len == 0 )
rc = send_error(r, "no request");
else {
int sockfd, servlen;
struct sockaddr_un serv_uaddr;
bzero (&(serv_uaddr), sizeof (serv_uaddr));
serv_uaddr.sun_family = AF_UNIX;
strcpy (serv_uaddr.sun_path, (char *) conf->serv_socket.data);
if ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) == -1)
rc = send_error(r, strerror(errno));
else {
#if defined(__FreeBSD__)
serv_uaddr.sun_len = sizeof(serv_uaddr.sun_len)+ sizeof(serv_uaddr.sun_family)+strlen(serv_uaddr.sun_path);
if (connect (sockfd, (struct sockaddr *) &serv_uaddr, serv_uaddr.sun_len) == -1)
#else
//linux, but probably some else
servlen = strlen (serv_uaddr.sun_path) + sizeof (serv_uaddr.sun_family);
if (connect (sockfd, (struct sockaddr *) &serv_uaddr, servlen) == -1)
#endif
rc = send_error(r, strerror(errno));
else {
//sending socket to Voodoo Chat daemon
struct iovec iov[2];
struct msghdr msg;
char buf[2];
iov[0].iov_base = buf;
iov[0].iov_len = 2;
strncpy(myargs, (char *) r->args.data, 255);
char *end = strchr(myargs, ' ');
if (end != NULL) end = 0;
myargs[255] = 0;
iov[1].iov_base = myargs;
iov[1].iov_len = strlen(myargs);
msg.msg_iov = iov;
msg.msg_iovlen = 2;
msg.msg_name = NULL;
msg.msg_namelen = 0;
if (r->connection->fd < 0) {
msg.msg_control = NULL;
msg.msg_controllen = 0;
buf[1] = -r->connection->fd; /* nonzero status means error */
if (buf[1] == 0)
buf[1] = 1; /* -256, etc. would screw up protocol */
} else {
if (cmptr == NULL && (cmptr = malloc (CONTROLLEN)) == NULL) {
rc = send_error(r, "cannot malloc memory");
error = 1;
} else {
//(cmptr = cmsghdr *)malloc(CONTROLLEN)) == NULL)
cmptr->cmsg_level = SOL_SOCKET;
cmptr->cmsg_type = SCM_RIGHTS;
cmptr->cmsg_len = CONTROLLEN;
msg.msg_control = (caddr_t) cmptr;
msg.msg_controllen = CONTROLLEN;
*(int *) CMSG_DATA (cmptr) = r->connection->fd; /* the fd to pass */
buf[1] = 0; /* zero status means OK */
}
}
if (!error) {
buf[0] = 0; /* null byte flag to recv_fd() */
mysent = sendmsg (sockfd, &msg, 0);
if (mysent == -1)
rc = send_error(r, strerror(errno));
else
//if ok, and now the socket in the voc-daemon, let's tell nginx to close it:
rc = NGX_HTTP_CLOSE;
}
}//end of if connect
close(sockfd);
}//end of if socket()
}
return rc;
}
static void *
ngx_http_voc_create_conf(ngx_conf_t *cf)
{
ngx_http_voc_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_voc_conf_t));
if (conf == NULL) {
return NGX_CONF_ERROR;
}
return conf;
}
static char *
ngx_http_voc(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_voc_conf_t *vconf = conf;
ngx_str_t *value;
value = cf->args->elts;
vconf->serv_socket = value[1];
if (vconf == NULL) {
return NGX_CONF_ERROR;
}
ngx_http_core_loc_conf_t *clcf;
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
clcf->handler = ngx_http_voc_handler;
return NGX_CONF_OK;
}
| |
| | |
|
|
TƏQVİM |
|
« APREL 2024 » | B | BE | ÇA | Ç | CA | C | Ş | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 |
|
|
|
|
*** |
|
|
|
|
|
F O R U M |
|
ABŞ-IN ŞTATLARI (14)
A.DÜMA, O.BALZAK, V.HÜQO VƏ S. (11)
HİKMƏTLİ SÖZLƏR (10)
SƏDİ, Ə.FİRDOVSİ, Ö.XƏYYAM VƏ S. (8)
AZƏRBAYCANIN SU ANBARLARI (7)
PUŞKİN, TOLSTOY, DOSTOYEVSKİ, ŞOLOXOV VƏ S. (7)
HÖTE, ŞİLLER, BÖLL VƏ S. (7)
R.N.GÜNTƏKİN, N.HİKMƏT, O.PAMUK VƏ S. (7)
ÇÖRÇİL, ŞEKSPİR, KLARK VƏ S (6)
N.TUSİ, BƏHMƏNYAR, A.HACIYEV ... (5)
DEKART, PASKAL,PUANKARE,LANQRANJ VƏ AMPER (4)
DASTAN (3)
Z.BÜNYADOV, Y.MAHMUDOV,ORUC BƏY BAYAT .... (3)
MOBİL TELEFONLAR ÜÇÜN LAZIMILI KODLAR. (2)
DON KİXOT, BENAVANTE VƏ KAMİLO XOSE SELA (2)
|
|
|
|
SON ŞƏRHLƏR |
|
:menim e-mailime hec gondermeyin bunlarin hamisini bilirem
|
|
|
|