<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto">Hello, <div><br></div><div>I suggest you to read nginx development guide which is available on nginx.org. </div><div><br></div><div>About getting x forward for headers, if you take a look at some built in module there’s already exist implementation which takes the headers and returns an array.<br><br><div dir="ltr">--<div>Hưng</div></div><div dir="ltr"><br><blockquote type="cite">On Dec 29, 2019, at 03:49, Robert Paprocki <rpaprocki@fearnothingproductions.net> wrote:<br><br></blockquote></div><blockquote type="cite"><div dir="ltr"><meta http-equiv="content-type" content="text/html; charset=utf-8">The array has 0 nelts because you haven’t added anything to do. nelts is the number of elements in the array, not the size of the array. <div><br></div><div>Add an element to the array with ngx_array_push(). </div><div><div dir="ltr"><br><blockquote type="cite">On Dec 28, 2019, at 11:35, Marcin Kozlowski <marcinguy@gmail.com> wrote:<br><br></blockquote></div><blockquote type="cite"><div dir="ltr"><div dir="ltr"><div>Still have few questions. Help would be great</div><div><br></div><div>Beginner in writing NGINX modules<br><br>Have this question<br><br>1) How do I create array, add element to it and than create a hashtable from it.<br><br>Below I try to achieve it:<br><br>NX_LOG_DEBUG(_debug_whitelist_heavy,<br>NGX_LOG_EMERG, cf, 0,<br>               "finalizing hashtables array %i",  dlc->pass_rules->nelts);<br><br>headers_ar = ngx_array_create(cf->pool, dlc->pass_rules->nelts, sizeof(ngx_hash_key_t));<br><br><br><br>if (headers_ar) {<br>    NX_LOG_DEBUG(_debug_readconf,  NGX_LOG_EMERG, cf, 0,<br>               "headers array %i",headers_ar->nelts);<br><br>2) Why headers_ar has 0 elemets<br><br>nginx: [emerg] finalizing hashtables array 6 in /etc/nginx/nginx.conf:124<br>nginx: [emerg] headers array 0 in /etc/nginx/nginx.conf:124<br><br><br><br>3) I later want to build hashtable based on this array:<br><br>    dlc->passr_headers_hash =  (ngx_hash_t*) ngx_pcalloc(cf->pool, sizeof(ngx_hash_t));<br>    hash_init.hash = dlc->passr_headers_hash;<br>    <a href="http://hash_init.name">hash_init.name</a> = "passr_headers_hash";<br><br><br><br>    if (ngx_hash_init(&hash_init, (ngx_hash_key_t*) headers_ar->elts,<br>                      headers_ar->nelts) != NGX_OK) {<br>      ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "$HEADERS hashtable init failed"); /* LCOV_EXCL_LINE */<br>      return (NGX_ERROR); /* LCOV_EXCL_LINE */<br>    }<br><br>    else {<br>      NX_LOG_DEBUG(_debug_whitelist, NGX_LOG_EMERG, cf, 0, "$HEADERS hashtable init successed %d !",<br>                         dlc->passr_headers_hash->size);<br>    }<br><br><br><br>4) Can somebody post simple code that<br><br>a) creates array<br>b) pushes one IP<br>c) create hashtable from it<br><br>Thanks,</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Dec 28, 2019 at 3:36 PM Marcin Kozlowski <<a href="mailto:marcinguy@gmail.com">marcinguy@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Ignore my last post.</div><div><br></div><div>My error.</div><div><br></div><div>Thanks,<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Dec 27, 2019 at 11:42 PM Marcin Kozlowski <<a href="mailto:marcinguy@gmail.com" target="_blank">marcinguy@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div>Thanks. Almost got my modifications to NAXSI ready.</div><div><br></div><div>But currently have a blocker with getting just the X-Forwarded-for IP</div><div><br></div><div>The code below:</div><div><br></div><div><span>ngx_uint_t n;</span></div><div><span>ngx_table_elt_t **h;<br>
ngx_array_t a;<br>
a = req->headers_in.x_forwarded_for;<br>
n = a.nelts;<br>
h = a.elts;<br> <br> 
<br>for (i = 0; i<n; i++) {<br>  
ngx_log_error(NGX_LOG_ERR, req->connection->log,<br>
              0, "x_forwarded_for: %s", h[i]->value.data);<br>}</span></div><div><span><br></span></div><div><span>gets a String with several IP (i.e client, server, request etc)<br></span></div><div><span><br></span></div><div><span>Tried to parse the string using strtok(), interating through it .... but it segfaults. I guess I am missing some NGINX module knowledge.<br></span></div><div><span><br></span></div><div><span>How to properly get first string up to first "," from the  h[i]->value.data using NGINX functions/types or other correct way to do it.<br></span></div><div><span><br></span></div><div><span>Thanks,</span></div><div><span><br></span></div><div><span><br></span></div><div><span><br></span></div><div><span><br></span></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Dec 25, 2019 at 12:17 PM Ruslan Ermilov <<a href="mailto:ru@nginx.com" target="_blank">ru@nginx.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Tue, Dec 24, 2019 at 08:00:26PM +0100, Marcin Kozlowski wrote:<br>
> Thanks.<br>
> <br>
> Works. For the reference, this is the code I used:<br>
> <br>
>   ngx_uint_t n;<br>
>   ngx_table_elt_t **h;<br>
>   ngx_array_t a;<br>
>   a = req->headers_in.x_forwarded_for;<br>
>   n = a.nelts;<br>
>   h = a.elts;<br>
> <br>
> <br>
>   for (i = 0; i<n; i++) {<br>
>     ngx_log_error(NGX_LOG_ERR, req->connection->log,<br>
>               0, "x_forwarded_for: %s", h[i]->value.data);<br>
>   }<br>
> <br>
> BTW What would be the best practice in NGINX NASIX module or any other<br>
> module to load a file with hundreds entries of IPs (hashmap, or what<br>
> structure would be best?) which should be whitelisted later for comparison<br>
> in NASIX module logic. Those IP should never be blocked by NAXSI.<br>
> <br>
> When should I load this file in memory, in which component<br>
> /module/function/step?<br>
> <br>
> Links to some guides/sample code would be also appreciated.<br>
> <br>
> Thanks,<br>
<br>
<a href="http://nginx.org/en/docs/http/ngx_http_geo_module.html" rel="noreferrer" target="_blank">http://nginx.org/en/docs/http/ngx_http_geo_module.html</a><br>
_______________________________________________<br>
nginx-devel mailing list<br>
<a href="mailto:nginx-devel@nginx.org" target="_blank">nginx-devel@nginx.org</a><br>
<a href="http://mailman.nginx.org/mailman/listinfo/nginx-devel" rel="noreferrer" target="_blank">http://mailman.nginx.org/mailman/listinfo/nginx-devel</a><br>
</blockquote></div></div>
</blockquote></div>
</blockquote></div>
<span>_______________________________________________</span><br><span>nginx-devel mailing list</span><br><span>nginx-devel@nginx.org</span><br><span>http://mailman.nginx.org/mailman/listinfo/nginx-devel</span></div></blockquote></div><span>_______________________________________________</span><br><span>nginx-devel mailing list</span><br><span>nginx-devel@nginx.org</span><br><span>http://mailman.nginx.org/mailman/listinfo/nginx-devel</span></div></blockquote></div></body></html>