Posts

Avoid using comparison operators on time_t

As specific in  http://www.cplusplus.com/reference/ctime/time_t/ , the time_t may be implemented using alternative time representations by libraries. Just because time_t is arithmetic, that doesn't mean it stores time as monotone increasing values for advancing time. It can be different in different systems. Although most of linux distros do store it as integer but it is no harm to be careful. The time.h header file provides us difftime function just for calculating the different by seconds between two  time_t variables. Some may worry about the overhead of the function over a simple subtract expression. Well, in some architectures, it's implemented as a macro. For example, POSIX,  http://man7.org/linux/man-pages/man3/difftime.3.html So, the right way to compare 2 time_t values, a, b should be: if (difftime(b, a) > 0) //... If you check for a time duration is passed or not, do something like, time_t now = time(NULL); if (difftime(now, a) >= duration) //...

One sunset among many

Image
Memories fade. The feeling that this picture brings to me is losing day by day. Post it here with a hope that at some moment in the future, it will suddenly comeback and remind me the good old days when I feel calm and peaceful.

SSL/TLS hoạt động ra sao?

Image
Bài viết này hệ thống lại một số kiến thức tôi tìm hiểu được sau một thời gian làm việc với SSL. Hy vọng phần nào giúp cho những ai chưa rõ và có nhu cầu tìm hiểu. Bài viết bao gồm: - Sơ lược về lịch sử để giúp các bạn phân biệt được SSL và TLS - Các khái niệm: review kiến thức về một số khái niệm bạn cần nắm trước khi đọc phần nội dung chính - TLS handshake protocol. Lịch sử SSL : Secure Sockets Layer, là một giao thức bảo mật kênh truyền được phát triển bởi Netscape năm 1994. Phiên bản mới nhất của SSL là 3.0 (1996). TLS : Transport Layer Security, là giao thức bảo mật kênh truyền được phát triển bởi IETF dựa trên SSL 3.0. Phiên bản ổn định: TLS 1.2 (2008), phiên bản mới nhất: TLS 1.3 (2018). Mặc dù TLS được phát triển dựa trên SSL nhưng 2 giao thức này không thay thế cho nhau. Một số tính năng trên TLS không được hỗ trợ bởi SSL. Năm 2011, IETF khuyên không dùng SSL 2.0. Năm 2015, IETF khuyên không dùng SSL 3.0. TLS là giao thức tiêu chuẩn được IETF công bố và được sử dụng rộng

Stop using the mongoose's default connection

Image
Look at the below typical example of mongoose use. const mongoose = require( 'mongoose' ); mongoose.connect( 'mongodb://localhost:27017/myapp' , {useNewUrlParser: true }); var MyModel = mongoose.model( 'Test' , new Schema({ name: String })); // Works MyModel.findOne( function (error, result) { /* ... */ }); What if we want to make another connections? Or if we want to connect to another database? We can't use mongoose.connect() again, mongoose won't know which one we want to interact with. And don't ever think of creating different modules where separated mongoose objects are created and used because require() doesn't work that way, mongoose object is cached for the first time it is imported. The connection object is used to create and retrieve models. Models are always scoped to a single connection. Please be aware that mongoose creates a default connection when we call mongoose.connect() . We can access the default connection

Force Internet connection to desired network adapter

Image
PROBLEM: Working in an IT company, you may be familiar with the situation where you have 2 network interfaces connecting to 2 different networks (or even a same network). Sometimes you want traffic going through one specific interface but not through the other. You are not sure and you switch off one interface to avoid the problem. That's okay but it's kind of annoying. SOLUTION: I faced the same problem. I did some research and found  this help. Let's summarize it. Windows decides which interface to handle which traffic based on a routing table. To see it issue command route print on cmd terminal. You will get something like below. Currently, I don't connect my wire cable so there is only one record for destination 0.0.0.0. If there are more than one with 0.0.0.0, then the one with the lowest metric will be used. (0.0.0.0 matches every IP address). Usually, if you want to route internet traffics, you should change the 0.0.0.0 record. For other networks,

Measure time execution in a C program

There are many ways to measure time in a C program. I use  clock_gettime () since it supports high resolution clock which we can count on. Header #include <time.h> Time storage structure struct timespec { time_t tv_sec; /* seconds */ long tv_nsec; /* nanoseconds */ }; Function int clock_gettime ( clockid_t clk_id, struct timespec *tp); is used to retrieve the time of the specified clock clk_id. There are several kind of clock ID but the two of them may be used the most. CLOCK_REALTIME : System-wide clock that measures real (i.e., wall-clock) time. CLOCK_MONOTONIC : Clock that cannot be set and represents monotonic time since some unspecified starting point. On Linux, that point corresponds to the number of seconds that the system has been running since it was booted. Below is an example of using the function. #include <stdio.h> #include <time.h> struct timespec time_subtract ( struct timespec t1, struct

Python application crashes without throwing any exception

PROBLEM: Recently I have written a flask application and encountered a problem that the application randomly dies. My application is to provide a simple REST API to query some personal information. It serves both HTTP and HTTPS. I run the app by python command, just like below python my_app.py (Yeah, I know that command is for development only and should not be used in production) Everything went well for a couple of tests, til the rainy time, literally, there was a heavy rain outside. I sent a HTTPS request using postman and the above command just stopped. I tried several times, the issue did not happen permanently. Sometimes it crashed, sometimes it went well. There was not any information dumping out to screen. The command was just completed with a lame terminal's prompt. HOW I DEAL WITH IT: I was just a novice in python and was expecting for an exception that would tell me the exact reason why my application had been crashed. Well, there was nothing. I goog