2

In the Lua Nginx module docs about ngx.socket.tcp() (Link), it says:

Creates and returns a TCP or stream-oriented Unix domain socket object (also known as one type of the "cosocket" objects)

This TCP socket can be used to connect to remote host, but in the wiki about unix domain socket (Link):

A Unix domain socket or IPC socket (inter-process communication socket) is a data communications endpoint for exchanging data between processes executing on the same host operating system

This leads me to some questions:

  1. Is this TCP socket another type of Unix domain socket? What is this TCP socket really be?
  2. What is cosocket referred in the docs? I can't find it using google
  3. TCP protocol is also used in the transport layer of the TCP/IP model, is this API allow to program directly in the transport layer, bypassing the application layer?
lazzy_ms
  • 1,169
  • 2
  • 18
  • 40
Dauto98
  • 177
  • 3
  • 12

1 Answers1

2
  1. Is this TCP socket another type of Unix domain socket? What is this TCP socket really be?

Both TCP and unix domain socket are stream sockets. You may create both type of sockets using ngx.socket.tcp() API. The type of OS socket would be defined by connect method, there are two different syntaxes:

If you connect using tcpsock:connect(host, port, options_table?) syntax the socket will be TCP socket.

If you connect by tcpsock:connect("unix:/path/to/unix-domain.socket", options_table?) syntax the socket will be unix domain socket. Obviously you cannot use unix domain socket for network communication.

  1. What is cosocket referred in the docs? I can't find it using google

Cosocket is the term of OpenResty ecosystem. If you create an object using ngx.socket.tcp() API - you create a cosocket object.

  1. TCP protocol is also used in the transport layer of the TCP/IP model, is this API allow to program directly in the transport layer, bypassing the application layer?

Exactly.

Alexander Altshuler
  • 2,930
  • 1
  • 17
  • 27
  • Thank you. So according to the 3rd question, HTTP is just TCP message with some predefined format? Can we mimic HTTP connection using TCP socket above and send text in HTTP format? E.g: "GET /test HTTP/1.1" ? – Dauto98 Aug 14 '18 at 04:32
  • Yes, for example https://github.com/pintsized/lua-resty-http library implement HTTP protocol using cosocket nginx API. – Alexander Altshuler Aug 14 '18 at 11:43