<p><a class="user-mention" data-hovercard-type="user" data-hovercard-url="/hovercards?user_id=184809" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/miconda">@miconda</a> I would mainly love some pointers as to how/where to implement this.</p>
<p>My initial thought was to modify <code>handle_new_connect()</code> to change the connection state to <code>S_CONN_OK</code> instead of <code>S_CONN_ACCEPT</code>, and then peek the connection to see if we are getting a PROXY v1/v2 header, and the override the connection information structs. However, this obviously runs amuck of clean code, and obviously might wait a long time if the header isn't sent immediately.</p>
<p>Would it, instead, make more sense to attempt to parse it in a fashion not unlike the <code>tcp_read_hep3</code>/<code>tcp_header_headers</code> combination in <code>tcp_read.c</code> around line 1490:</p>
<div class="highlight highlight-source-c"><pre><span class="pl-k">if</span>(unlikely(ksr_tcp_accept_hep3!=<span class="pl-c1">0</span>)) {
  bytes=<span class="pl-c1">tcp_read_hep3</span>(con, read_flags);
  <span class="pl-k">if</span> (bytes>=<span class="pl-c1">0</span>) {
    <span class="pl-k">if</span>(!(con-><span class="pl-smi">req</span>.<span class="pl-smi">flags</span> & F_TCP_REQ_HEP3)) {
      <span class="pl-c"><span class="pl-c">/*</span> not hep3, try to read headers <span class="pl-c">*/</span></span>
      bytes=<span class="pl-c1">tcp_read_headers</span>(con, read_flags);
    }
  }
} <span class="pl-k">else</span> {
  bytes=<span class="pl-c1">tcp_read_headers</span>(con, read_flags);
}</pre></div>
<p>So for example:</p>
<div class="highlight highlight-source-c"><pre><span class="pl-k">if</span>(unlikely(ksr_tcp_accept_hep3!=<span class="pl-c1">0</span>)) {
  bytes=<span class="pl-c1">tcp_read_hep3</span>(con, read_flags);
  <span class="pl-k">if</span> (bytes>=<span class="pl-c1">0</span>) {
    <span class="pl-k">if</span>(!(con-><span class="pl-smi">req</span>.<span class="pl-smi">flags</span> & F_TCP_REQ_HEP3)) {
      <span class="pl-c"><span class="pl-c">/*</span> not hep3, try to read headers <span class="pl-c">*/</span></span>
      <span class="pl-k">goto</span> read_headers
    }
  }
} <span class="pl-k">else</span> {
read_headers:
  <span class="pl-k">if</span> (<span class="pl-c1">unlikely</span>(read_proxy_protocol!=<span class="pl-c1">0</span>)) {
    <span class="pl-c1">tcp_read_proxy_protocol</span>(con, read_flags);
  }
  bytes=<span class="pl-c1">tcp_read_headers</span>(con, read_flags);
}</pre></div>
<p>Obviously, the above is just imaginary pseudocode. Thoughts?</p>

<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">—<br />You are receiving this because you are subscribed to this thread.<br />Reply to this email directly, <a href="https://github.com/kamailio/kamailio/issues/1757#issuecomment-444910587">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/AF36ZXCLsMB-UNyvWgnarXYmAHCIhg8Oks5u2Te1gaJpZM4ZGo-t">mute the thread</a>.<img src="https://github.com/notifications/beacon/AF36ZWNkD_zCkhr-ZGH41__CsainzztPks5u2Te1gaJpZM4ZGo-t.gif" height="1" width="1" alt="" /></p>
<script type="application/json" data-scope="inboxmarkup">{"api_version":"1.0","publisher":{"api_key":"05dde50f1d1a384dd78767c55493e4bb","name":"GitHub"},"entity":{"external_key":"github/kamailio/kamailio","title":"kamailio/kamailio","subtitle":"GitHub repository","main_image_url":"https://assets-cdn.github.com/images/email/message_cards/header.png","avatar_image_url":"https://assets-cdn.github.com/images/email/message_cards/avatar.png","action":{"name":"Open in GitHub","url":"https://github.com/kamailio/kamailio"}},"updates":{"snippets":[{"icon":"PERSON","message":"@teotwaki in #1757: @miconda I would mainly love some pointers as to how/where to implement this.\r\n\r\nMy initial thought was to modify `handle_new_connect()` to change the connection state to `S_CONN_OK` instead of `S_CONN_ACCEPT`, and then peek the connection to see if we are getting a PROXY v1/v2 header, and the override the connection information structs. However, this obviously runs amuck of clean code, and obviously might wait a long time if the header isn't sent immediately.\r\n\r\nWould it, instead, make more sense to attempt to parse it in a fashion not unlike the `tcp_read_hep3`/`tcp_header_headers` combination in `tcp_read.c` around line 1490:\r\n\r\n```c\r\nif(unlikely(ksr_tcp_accept_hep3!=0)) {\r\n  bytes=tcp_read_hep3(con, read_flags);\r\n  if (bytes\u003e=0) {\r\n    if(!(con-\u003ereq.flags \u0026 F_TCP_REQ_HEP3)) {\r\n      /* not hep3, try to read headers */\r\n      bytes=tcp_read_headers(con, read_flags);\r\n    }\r\n  }\r\n} else {\r\n  bytes=tcp_read_headers(con, read_flags);\r\n}\r\n```\r\n\r\nSo for example:\r\n\r\n```c\r\nif(unlikely(ksr_tcp_accept_hep3!=0)) {\r\n  bytes=tcp_read_hep3(con, read_flags);\r\n  if (bytes\u003e=0) {\r\n    if(!(con-\u003ereq.flags \u0026 F_TCP_REQ_HEP3)) {\r\n      /* not hep3, try to read headers */\r\n      goto read_headers\r\n    }\r\n  }\r\n} else {\r\nread_headers:\r\n  if (unlikely(read_proxy_protocol!=0)) {\r\n    tcp_read_proxy_protocol(con, read_flags);\r\n  }\r\n  bytes=tcp_read_headers(con, read_flags);\r\n}\r\n```\r\n\r\nObviously, the above is just imaginary pseudocode. Thoughts?"}],"action":{"name":"View Issue","url":"https://github.com/kamailio/kamailio/issues/1757#issuecomment-444910587"}}}</script>
<script type="application/ld+json">[
{
"@context": "http://schema.org",
"@type": "EmailMessage",
"potentialAction": {
"@type": "ViewAction",
"target": "https://github.com/kamailio/kamailio/issues/1757#issuecomment-444910587",
"url": "https://github.com/kamailio/kamailio/issues/1757#issuecomment-444910587",
"name": "View Issue"
},
"description": "View this Issue on GitHub",
"publisher": {
"@type": "Organization",
"name": "GitHub",
"url": "https://github.com"
}
},
{
"@type": "MessageCard",
"@context": "http://schema.org/extensions",
"hideOriginalBody": "false",
"originator": "AF6C5A86-E920-430C-9C59-A73278B5EFEB",
"title": "Re: [kamailio/kamailio] Proxy Protocol support (#1757)",
"sections": [
{
"text": "",
"activityTitle": "**Sebastian Lauwers**",
"activityImage": "https://assets-cdn.github.com/images/email/message_cards/avatar.png",
"activitySubtitle": "@teotwaki",
"facts": [

]
}
],
"potentialAction": [
{
"name": "Add a comment",
"@type": "ActionCard",
"inputs": [
{
"isMultiLine": true,
"@type": "TextInput",
"id": "IssueComment",
"isRequired": false
}
],
"actions": [
{
"name": "Comment",
"@type": "HttpPOST",
"target": "https://api.github.com",
"body": "{\n\"commandName\": \"IssueComment\",\n\"repositoryFullName\": \"kamailio/kamailio\",\n\"issueId\": 1757,\n\"IssueComment\": \"{{IssueComment.value}}\"\n}"
}
]
},
{
"name": "Close issue",
"@type": "HttpPOST",
"target": "https://api.github.com",
"body": "{\n\"commandName\": \"IssueClose\",\n\"repositoryFullName\": \"kamailio/kamailio\",\n\"issueId\": 1757\n}"
},
{
"targets": [
{
"os": "default",
"uri": "https://github.com/kamailio/kamailio/issues/1757#issuecomment-444910587"
}
],
"@type": "OpenUri",
"name": "View on GitHub"
},
{
"name": "Unsubscribe",
"@type": "HttpPOST",
"target": "https://api.github.com",
"body": "{\n\"commandName\": \"MuteNotification\",\n\"threadId\": 421171117\n}"
}
],
"themeColor": "26292E"
}
]</script>