- Notifications
You must be signed in to change notification settings - Fork3
bianc8/rdc22
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Here there are some useful functions
Click to expand!
structsockaddr_inremote;printf("Client ip: %d.%d.%d.%d \nPort: %d",*((unsignedchar*)&remote.sin_addr.s_addr),*((unsignedchar*)&remote.sin_addr.s_addr+1),*((unsignedchar*)&remote.sin_addr.s_addr+2),*((unsignedchar*)&remote.sin_addr.s_addr+3),ntohs(remote.sin_port) );
Click to expand!
/** struct hostent { char *h_name; // official name of host char **h_aliases; // alias list int h_addrtype; // host address type int h_length; // length of address char **h_addr_list; // list of addresses } #define h_addr h_addr_list[0] // for backward compatibility */charhostname[1000];sprintf(hostname,"www.example.com");structhostent*remoteIp;remoteIp=gethostbyname(hostname);printf("Indirizzo di %s : %d.%d.%d.%d\n",hostname, (unsignedchar)(remoteIp->h_addr[0]), (unsignedchar)(remoteIp->h_addr[1]), (unsignedchar)(remoteIp->h_addr[2]), (unsignedchar)(remoteIp->h_addr[3]));
An example of HTTP Date
- Thu, 17 Oct 2019 07:18:26 GMT
So HTTP date format is
- %a, %d %b %Y %H:%M:%S %Z
Here are some functions for handling HTTP Dates:
Click to expand!
chardate_buf[1000];char*getNowHttpDate(){char*format="%a, %d %b %Y %H:%M:%S %Z";time_tnow=time(0);structtmtm=*gmtime(&now);strftime(date_buf,sizeof(date_buf),format,&tm);printf("Time is: [%s]\n",date_buf);returndate_buf; }
Click to expand!
voidsaveEpoch() {FILE*cached=fopen(cacheName,"w+");if (cached!=NULL) {time_tepochNow=time(NULL);fprintf(cached,"%lu\n", (unsigned long)epochNow); }fclose(cached); }
Click to expand!
time_thttpDateToEpoch(char*lastModified) {char*format="%a, %d %b %Y %H:%M:%S %Z";structtm*httpTime=malloc(sizeof(structtm));strptime(lastModified,format,httpTime);// man 3 strptimetime_tepochRemote=mktime(httpTime);// convert tm to epochreturnepochRemote; }
Click to expand!
/** * Replace char '/' with '_' in the given string */voidcharReplace(char*s) {for (inti=0;s[i];i++)if (s[i]=='/')s[i]='_'; }unsignedcharexpired(char*uri,char*last_modified){charresourceName[]="/";// Replace '/' with '_' in resourceNamechartmp[100];strcpy(tmp,resourceName);charReplace(tmp);// Save cacheNamecharcacheName[1000];strcpy(cacheName,"./cache/");strcat(cacheName,tmp);// Open cacheName fileFILE*cached=fopen(cacheName,"r");if (cached==NULL)return1;// read first line of cached filechar*line=0;size_tlen=0;getline(&line,&len,cached);if (httpDateToEpoch(last_modified)<httpDateToEpoch(line))return0;return1; }
Click to expand!
FILE*fin;fin=fopen(uri+1,"rt"));if (fin==NULL) {sprintf(response,"HTTP/1.1 404 File not found\r\n\r\n<html>File non trovato</html>");t=write(s2,response,strlen(response));if (t==-1) {perror("write fallita");return-1; } }else {content_length=0;while ((c=fgetc(fin))!=EOF)content_length++;// get file lengthsprintf(response,"HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nContent-Length: %d\r\n\r\n",content_length);printf("Response: %s\n",response);//send headerst=write(s2,response,strlen(response));if (t==-1) {perror("write fallita");return-1; }rewind(fin);// move pointer to the begin of the file//re-read and send the file, char per charwhile ((c=fgetc(fin))!=EOF) {if (write(s2, (unsignedchar*)&c,1)!=1)perror("Write fallita"); }fclose(fin); }
Useful info
/etc/services: To know all the TCP ports available at the application level.
/etc/protocols. Know assigned Internet Protocol Numbers. In the IPv4 there is an 8 bit field "Protocol" to identify the next level protocol.In IPv6 this field is called the "Next Header" field.
nslookup: finds the ip address of the specified URL (example:www.google.com)
netstat -rn shows routing table
traceroute routes an ip packet in which path it travels by printing the IP of every gateway that decides to drop the packet that was forged with low TTL (time to live, decremented on every hop) count.
curl is a command-line tool to transfer data from or to a server, using one of the supported protocols (DICT, FILE, FTP, FTPS, GOPHER,HTTP,HTTPS, IMAP,IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP)
$ curl http://example.com/
$ curl -v http://example.com/
$ curl -v http://example.com/ --headers [OR -H] "HeaderName: HeaderValue"
Send Curl request using Basic Authentication, --basic is the default authentication mechanism so no need to specify it
$ curl -v http://example.com/ -u "username:password"
$ curl -v http://example.com/ --proxy [OR -x] http://SERVER:PORT
Copy the .vimrc file into your home directory on the server using Unix's scp command.
Remember to change MATRICOLA with your Unipd Student ID.
Login using your SSH credentials.
scp -O ./.vimrc MATRICOLA@SERVER_IP:/home/MATRICOLA/.vimrc
This .vimrc config file allows you to:
- Use your mouse: scrolling wheel to move up or down, click and point
- Move line Up/Down using Ctrl+Shift+Up/Down
- Press F8 toCompile theC programwithout exiting Vim
- Press F9 toExecute theC programwithout exiting Vim
- Auto close brackets
- Replace tabs with 3 spaces
- Highlight matching parentheses
- Auto indent on brackets
- Show line number
- Highlight current line
- Search characters as they are entered
- Search is case insesitive if no case letters are entered, but case sensitive if case letters are entered
- Highlight search results
Click to expand!
Search isUNIDIRECTIONAL but when the search reach one end of the file, pressingn continues the search, starting from the other end of the file.
To search forward use /
To search bacward use ?
x es:
ESC (go into Command mode)/query (forward)?query (backward)ENTER (to stop writing in the search query)(now all search results of the query are highlighted)n (to move to the NEXT occurence in the search results)N (to move to the PREVIOUS occurence in the search results)ESC (to exit Search mode)
Click to expand!
To Compile press F8
To Execute press F9
ESC (go into Command mode)F8 (compile shortcut)F9 (execute shortcut)CTRL+C (to exit compilation/executable) Enter (to re-enter in vim)
Click to expand!
ESC (go into Command mode)CTRL+SHIFT+PAGE UP (to move line up)CTRL+SHIFT+PAGE DOWN (to move line down)i (go into Insert mode)
Click to expand!
Select with the mouse the text you want to copy[ALTERNATIVE ESC (go into Command mode) V100G (to select from current line to line 100, included, using Visual mode)]y (to Copy/yank)d (to Cut/delete)p (to Paste after the cursor)
Click to expand!
Open the file from which you want to copy in Vim using:
vi ogFile.c (ogFile is the destination file)ESC (go into Command mode):ePATH/file (open 'source' file at Path)(select the lines that you want to copy)y (copy/yank):q (close the 'source' file)vi ogFile.c (open the 'destination' file)p (paste the copied lines into the 'destination' file)
Click to expand!
ESC (go into Command mode)u (to Undo)
Click to expand!
CTRL + s (now screen is frozen)(every command that you type when the screen is frozen will be executed, it just won't be displayed in the terminal)CTRL + q (to unfreeze the screen)