Auto start with systemd


Three standards to start a program upon boot:
1. System V aka sysV classic init: up to Ubuntu 9.04
2. Upstart: for Ubuntu 9.10 to 14.10
3. systemd Ubuntu 15.04 and higher
This is Ubuntu 18.04 LTS running on AWS ec2 m5, systemd, using sshd service as example.
Current runlevel is 5, as seen by $runlevel, and it’s mapped to graphical.target as seen by $ls -l /lib/systemd/system/runlevel5.target and can be confirmed by $systemctl get-default
Not sure why it’s defaulted to graphic as this system doesn’t even have graphics installed.
Both symbol links, /etc/systemd/system/sshd.service and /etc/systemd/system/multi-user.target.wants/ssh.service points to /lib/systemd/system/ssh.service
To enable a service, create such a link, which can be achieved by:
sudo systemctl enable sshd.service
How rc.local works:
/lib/systemd/system/rc.local.service -> /lib/systemd/system/rc-local.service
“ExecStart=/etc/rc.local start” is in rc-local.service
There is no symbol link of rc.local in /etc/systemd/ because it’s pulled by /lib/systemd/system-generators/systemd-rc-local-generator automatically, which is binary.

Advertisements

Setup CDN on DNS


Depending on how much control given to CDN provider, most to least, here are the four config options.
Assumption: we are the video content provider and our server domain name is awsq.MaxxSports.cc
1. Redirect to CDN’s domain name with CNAME
2. Change my DNS so that it uses CDN’s Nameservers to resolve awsq.MaxxSports.cc
3. By A record, let awsq.MaxxSports.cc point to CDN’s IP.
4. Use a separate domain name for CDN access, e.g. d2t4kyunpgtwxc.cloudfront.net

More details for the four options, with the same numbering.
1. we have least control, since all our DNS does is just a redirection. This is the setup of Tencent CDN, like *.cdn.dnsv1.com
2. CDN has much control. For requests of awsq.MaxxSports.cc, CDN decides which IP to resolve to, e.g. depending on each user’s geographic location.
Taking CloudFlare as an example, after login at https://dash.cloudflare.com/login, it asks to update my DNS settng at enom.com with those nameservers: scott.ns.cloudflare.com venus.ns.cloudflare.com
Since multiple domain names could share one CDN’s IP, users can only access http://awsq.MaxxSports.cc by domain name, instead of direct IP.
3. since the A record is maintained at my DNS, namely enom.com, I know the CDN’s IP my domain name is mapped to. This is not good as we loses the ability to dynamically map domain name to different IPs in different country for example.
4. this is the setup we use for our streaming server, e.g. d2t4kyunpgtwxc.cloudfront.net. Requests for awsq.MaxxSports.cc go to our original server, and requests for d2t4kyunpgtwxc.cloudfront.net go to our CDN servers.

ICE STUN and TURN


Assuming host A B both behind NAT on the internet.
STUN: Session Traversal Utilities for NAT
A B talk to STUN server to know each other’s external IP and port state for hole punching.
The goal is to let A B directly talk to each other on UCP and sometimes TCP, if NAT is not symmetric.
This public IP is also needed in RTSP SDP when RTP over UDP.
STUN server itself communicates over UDP by default.
TURN: Traversal Using Relays around NAT
A first asks TURN server a permission, and then A sends all data to TURN, which is relayed to B.
Used for some NAT TCP, when hole punching doesn’t work.
ICE: Interactive Connectivity Establishment
Just a combination of STUN and TURN, and automatically switches between the two.

Special Years


I notice 1900 1970 and 1601 are used in time presentation.
1900: ts.tm_year is number of year since 1900.
ts is a struct, returned by localtime and gmtime, and get converted to string by asctime
1970: time_t is the number of seconds since start of this year UTC. returned by time and mktime(tm)
This is called Epoch AKA Unix timestamps and POSIX time. converted to string by ctime.
1601: FILETIME on Windows, 64bit, is the number of 0.1ms since start of this year in UTC
This year is the start of a 400-year cycle in Gregorian calendar.