Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Wednesday, August 19, 2015

Ref: Interview with Brian Kernighan

An interview done by Computerphile. Early days of C by Brian Kernighan.



Friday, April 10, 2015

Creating a muli-processes DTLS server.

I need to create an UDP server working with multiple clients over DTLS.  I found one note about net-snmp's DTLS Implementation Notes which almost has the same requirement as mime.  But it seems it falls back to use memory BIO and cache the data, and Campgnol VPN's solution is making use of NAT traversal technique that open each new connection with a new UDP port, which does not fit my requirement to only use one UDP port for server.

From the notes, I notice the author is almost succeeded with peeking the incoming traffic except the "Packets Pile Up" problem.  I think that is the right direction for me.

I tried on a single process server first. With recvmsg() with MSG_PEEK, I could get the client's address and port number, from which I could select the right context for the DTLS link.  I realized that I need a custom BIO module to avoid the"Packets Pile Up" problem.

I made a copy of crypto/bio/bss_dgram.c from OpenSSL's source and renamed it to my_udp.c, then stripped away anything unrelated to my usage, basically removed all platform specific and SCTP code except for Linux.  Then further removed all code related to connected filed flag.  Then changed the reading function to peek the packet first and compare the source IP and port number to the current one.  If it is not the current one, then fake an EAGAIN error.

Since server is listening on multiple interfaces, when sending data out, it needs to set its source address correspondingly.  But UDP does not directly give the local address used for the data.  With socket option IP_PKTINFO turn on for UDP server socket, local server address could be found with recvmsg() together with MSG_PEEK.  It is then set in my custom UDP BIO to be used when sending out data.  This seems to work fine with single server process.

But then I was facing the challenge to serve client on multiple processes on the same UDP port. After some tried and errors, I settled on the following method:

The parent creates socketpair() with each child for communication.  For the start, only the first child would listen on the UDP server port.  After peeking from the incoming data, calculate a child index from the client address and port. If the child index is its own, it proceeds to process the data, if not, it stops monitoring the server port for incoming data and send a command to parent and inform parent that the other child should handle the incoming data.  When parent receives the command, it will send the information to the other child, the other child gets the message and start monitoring server port and process incoming data and so on.

It seems working, but have not test out the performance, whether it is worth the trouble to have multiple processes.

Friday, November 30, 2012

Ref: How to convert from UTC to local time in C?

Reference: How to convert from UTC to local time in C?

I think the following from stackoverflow is the best portable solution if timegm() is not in the system.

#include <stdio.h>
#include <time.h>
#include <assert.h>

time_t my_timegm(struct tm *tm) {
    time_t epoch = 0;
    time_t offset = mktime(gmtime(&epoch));
    time_t utc = mktime(tm);
    return difftime(utc, offset);
}


int main(void) {
    time_t now = time(0);
    struct tm local = *localtime(&now);
    struct tm utc = *gmtime(&now);
    time_t t1 = mktime(&local);
    time_t t2 = my_timegm(&utc);
    assert(t1 == t2);
    printf("t =%lu\nt1=%lu\nt2=%lu\n",now,t1,t2);
    return 0;
}

Saturday, June 9, 2012

Ada Unbounded String and Vector Container

Just to get myself to familiar with the Vector container and Unbounded String, I wrote the follow small test program:
-- test unbounded string with vector

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Containers.Vectors;
With Ada.Text_IO.Unbounded_IO;
with Ada.Command_Line; use Ada.Command_Line;

procedure ubt is
   package My_Vector is new Ada.Containers.Vectors (Natural,
     Unbounded_String);
   use My_Vector;
   package SUIO renames Ada.Text_IO.Unbounded_IO;

   -- read input file and add each line into str_pool till file end
   procedure read_input (str_pool : in out My_Vector.Vector;
                         File     : File_Type) is
   begin
      loop
         str_pool.Append (SUIO.Get_Line (File));
      end loop;
   exception
      when End_Error =>
         null;
   end read_input;

   File : File_Type;
   str_pool : My_Vector.Vector;
begin
   Put_Line ("Argument 1 is : " & Argument (1));
   Open (File, In_File, Argument (1));
   read_input (str_pool, File);
   Close (File);

   Put_Line ("Total line: " & str_pool.Length'Img);
   Put_Line ("File content:");
   for i in  0 .. Integer(str_pool.Length) - 1 loop
      SUIO.Put_Line (str_pool.Element(Integer(i)));
   end loop;

   str_pool.Clear;
end ubt;

Compile it with
gnatmake ubt
It reads the input file line by line and stores them into the Vector container, then prints out the lines.

Reference: Ada.Containers.Vectors

Friday, June 8, 2012

TeX and TeXiA

I have been quite fond of TeX and LaTeX since my first encounter of it.  The type set document looks the best and professional compare with other word processors.  But it is hard to use when you trying to change the default way of text layout.  The TeX macro language is quite hard to understand even I myself am professional programmer.

Over the years, I would like to see if I could come out a better one, but due to various reasons and my own laziness,  there is no progress.  I finally set myself to start a little bit by re-implementing the TeX engine in Ada - TeXiA, for the purpose to understand how TeX work internally and learning the Ada programming language at the same time.  The progress is slow, currently the project just started and not yet reach section 100 of TeX: The Program (Massachusetts: Addison-Wesley, 1986). ISBN 0-201-13437-3.

I found the programming style of TeX, the literate programming introduced by Donald Knuth, hard to follow for me.  I'm not a smart guy as Donald.  I have to refer to a few C implementations to understand the whole picture: the texlive web2c version, yytex and Common TeX.

There are several other projects that related to computer typesetting are quite interesting: