Re: TLS server-client in Pike

Henrik Grubbström <[email protected]>
Newsgroups gmane.comp.lang.pike.user
Organization Roxen Internet Software AB
Message-ID <[email protected]>
On Wed, 1 Jun 2016, Siddhant Gupta wrote:

> I am trying to implement a server using tcp which has tls wrapped around it.
> This is the code for my server

> constant port = 3000;
> Stdio.Port mainsock;
> SSL.context ctx = SSL.context();
> int main(){
>   mainsock = Stdio.Port();
>   if(!mainsock->bind(port)){
>     write("Error");
>     return 1;
>   }
>   Stdio.File sock = mainsock->accept();
>   SSL.sslfile ssock = SSL.sslfile(sock,ctx);
>   write(ssock->query_address());
>   write("\n"+ssock->is_open());

You're missing a call to SSL.File()->accept(), otherwise SSL.File won't 
know if it is supposed to be the client or the server. Considering that 
your code seems to be using blocking I/O, you probably want to call 
set_blocking() (SSL.File defaults to nonblocking mode) as well.

     ssock->set_blocking();
     ssock->accept();

>   ssock->write("test");
>   while(1){
>     string str = Stdio.stdin->gets();
>     ssock->write(str+"\n");
>   }
>   return -1;
> }

> and this is my client

> int main()
> {
>    Stdio.File sock = Stdio.File();
>    sock->connect("127.0.0.1",3000);
>    SSL.sslfile ssl = SSL.sslfile(sock, SSL.context());

And here you need the corresponding call to SSL.File()->connect().

      ssl->set_blocking();
      ssl->connect();

>     write("Response:\n%s\n", ssl->read(1024,1));
> }

You may want to take a look at the code in SSL.https and SSL.Port.

> However I am not getting any message from the server to the client.This
> works without tls coming in.

 	/grubba

-- 
Henrik Grubbström					[email protected]
Roxen Internet Software AB
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.