Sunday, July 4, 2010

Bluetooth RFCOMM example in C

1) Packages needed:- bluetooth,bluez,libbluetooth, libbluetooth-dev and pthread support

*Below is a text chat client-server example which uses bluetooth as a medium for communication.
*RFCOMM protocol is used here because it's more reliable(similar to TCP).
*It uses the pthread library for multithreading.
* You can use the command hcitool to list the bluetooth devices in your computer.
$ hcitool dev
Devices:
hci0 00:1F:81:00:01:00

SERVER (server.c)
[code]
/*
compilation step

# gcc -o server server.c -lbluetooth -lpthread

*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>

int s,client ;
void ctrl_c_handler(int signal);
void close_sockets();
void *readMsg();
void *sendMsg();

int main(int argc,char **argv){
(void) signal(SIGINT,ctrl_c_handler);

pthread_t readT, writeT;
char *message1 = "Read thread\n";
char *message2 = "Write thread\n";
int iret1, iret2;

struct sockaddr_rc loc_addr={ 0 },client_addr={ 0 };
char buf[18] = { 0 };

unsigned int opt = sizeof(client_addr) ;


//allocate socket
s = socket(AF_BLUETOOTH,SOCK_STREAM,BTPROTO_RFCOMM) ;


//bind socket to port 1 of the first available
loc_addr.rc_family = AF_BLUETOOTH ;
str2ba("00:1F:81:00:00:01",&loc_addr.rc_bdaddr) ;//hci0; server device address is given
loc_addr.rc_channel = 1 ; //port (maximum should be 30 for RFCOMM)

bind(s,(struct sockaddr *)&loc_addr,sizeof(loc_addr));
printf("Binding success\n");

//put socket into listen mode
listen(s,1) ;
printf("socket in listen mode\n");
//accept one connection

client = accept(s,(struct sockaddr *)&client_addr,&opt);
ba2str(&client_addr.rc_bdaddr,buf);
fprintf(stdout,"Connection accepted from %s\n",buf);

/* Create independent threads each of which will execute function */

iret1 = pthread_create(&readT,NULL,readMsg,(void*) message1);
iret2 = pthread_create(&writeT,NULL,sendMsg,(void*) message2);

pthread_join(readT,NULL);
pthread_join(writeT,NULL);

close_sockets() ;
return 0 ;
}

void *sendMsg(){
char msg[25] ;
int status ;

do{
memset(msg,0,sizeof(msg));
fgets(msg,24,stdin);
if(strncmp("EXIT",msg,4)==0 || strncmp("exit",msg,4)==0)break;
status = send(client,msg,strlen(msg),0);
fprintf(stdout,"Status = %d\n",status);
}while(status > 0);
}

void *readMsg(){
int bytes_read;
char buf[1024] = { 0 };
do{
memset(buf,0,sizeof(buf));
//read data from the client
bytes_read = recv(client,buf,sizeof(buf),0) ;
fprintf(stdout,"Bytes read = %d\n",bytes_read);
if(bytes_read <= 0)break;
fprintf(stdout,"<<>> %s",buf);
}while(1);
}

void close_sockets(){
//close connection
close(client);
close(s) ;
printf("sockets closed\n");
}

void ctrl_c_handler(int signal) {
printf("Catched signal: %d ... !!\n", signal);
close_sockets();
exit(0);
//(void) signal(SIGINT, SIG_DFL);
}



CLIENT(client.c)

/*
compilation step

# gcc -o client client.c -lbluetooth -lpthread

*/

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>

int s ;
void ctrl_c_handler(int signal);
void close_sockets();
void *readMsg();
void *sendMsg();

int main(int argc,char **argv){
(void) signal(SIGINT,ctrl_c_handler);

pthread_t readT, writeT;
char *message1 = "Read thread\n";
char *message2 = "Write thread\n";
int iret1, iret2;

struct sockaddr_rc addr= { 0 };
int status ;
char dest[18] = "00:1F:81:00:00:01";
char msg[25];

//allocate a socket
s = socket(AF_BLUETOOTH,SOCK_STREAM,BTPROTO_RFCOMM);
addr.rc_family = AF_BLUETOOTH ;
addr.rc_channel = 1 ;
str2ba(dest,&addr.rc_bdaddr);

//connect to server
printf("going 2 connect\n");
status = connect(s,(struct sockaddr *)&addr,sizeof(addr)) ;

//send a message
if(0 == status){
printf("connect success\n");

/* Create independent threads each of which will execute function */

iret1 = pthread_create(&readT,NULL,readMsg,(void*) message1);
iret2 = pthread_create(&writeT,NULL,sendMsg,(void*) message2);

pthread_join(readT,NULL);
pthread_join(writeT,NULL);

}


close_sockets();
return 0;
}

void *sendMsg(){
char msg[25] ;
int status ;

do{
memset(msg,0,sizeof(msg));
fgets(msg,24,stdin);
if(strncmp("EXIT",msg,4)==0 || strncmp("exit",msg,4)==0)break;
status = send(s,msg,strlen(msg),0);
fprintf(stdout,"Status = %d\n",status);
}while(status > 0);
}

void *readMsg(){
int bytes_read;
char buf[1024] = { 0 };
do{
memset(buf,0,sizeof(buf));
//read data from the client
bytes_read = recv(s,buf,sizeof(buf),0) ;
fprintf(stdout,"Bytes read = %d\n",bytes_read);
if(bytes_read <= 0)break;
fprintf(stdout,"<<>> %s",buf);
}while(1);
}

void close_sockets(){
close(s);
fprintf(stdout,"Close sockets\n");
}

void ctrl_c_handler(int signal){
fprintf(stdout,"Interrupt caught[NO: %d ]\n",signal);
close_sockets();
exit(0);
}

[/code]

Wednesday, June 30, 2010

EXIM

* dnsbl.info --> lists blacklisted mail servers.
* spamcop.net --> listed mail servers for spam.

* Commands
1) Count total mails in queue
$ exim -bpc
2) Give mail header
$ exim -Mvh
3) List messages
$ exim -bp
4) Message from a particular user.
5) Lists mail received by a particular user
$ exiqgrep -f
6) List receipients
$ exiqgrep -r

* Frozen Mail --> due to spam
1) Froze a mail
$ exim -f
2) Display message body
$ exim -Mvb
3) Remove a message
$ exim -Mrm
4) Forceful delivery --> If its frzen then exim would try to deliver but fails.
$ exim -M
5) Freeze a message
$ exim -Mf

* All queues are in /var/spool/exim/input/
* Stop exim before removing /var/spool/exim/input/*

Mail log & Services
* Bounced message is indicated by **
* Bounced message --> if sending fails then the message gets bounced back.
* Retry period can be changed inside /etc/exim.conf

Exim log
* mainlog --> /var/log/exim
* paniclog --> error related to exim log
* rejectlog --> shows bounced message

Retry
* Inside /etc/exim.conf search for retry (comment it if working fails)



Q) Create an email-account then send & receive email?
Note: dovecot -->> POP
1) yum install exim
2)rpm -qa|grep exim
3) netstat -panel|grep 25
4) telnet 192.168.2.48 25 ------>>>>>>tests for connection
5) Stop sendmail (if its already installed)
6) vim /etc/exim/exim.conf
.........
ListenInterface 0.0.0.0
............
7) Add s/m user
$ useradd user1
$ passwd user1
8) cd /home/user1
9) vim /etc/exim/exim.conf ------->>>>>create mailbox
..........
local_delivery
........
file = ..... $INBOX:/home/$local_part/Inbox ------->>>>>>>Inbox is a file
................
localdomains=@:localhost:user1.com ----->>>>> defines a local domain
..............
10) start exim
11) exim -bt user1@user1.com ----->>>>> tracing the domain

SENDING MAIL
$ telnet 25
HELO hello
.........
MAIL FROM: user1@gmail.com
RCPT TO: user1@yahoo.com
.....
SUBJECT: my subject
DATA
sdfsdfsdfsdlf
.


$ grep /var/log/exim/main_log ----->>>>> check status

INSTALL DOVECOT (Zimbra --> advanced version of Dovecot)
-->>> dovecot listens on all ports supported by POP2,POP3,POPS2,POPS2,IMAP,IMAPS
$ yum install dovecot
$ telnet localhost 110
$ vim /etc/dovecot.conf
............
mail_location= ........$INBOX=/home/%u/Inbox -->>>>> Inbox is a file
.........
$ restart dovecot
$ telnet localhost 110
user user1
pass passuser1
list
.....
retr 1


INSTALL SQUIRRELMAIL

1) Download squirrelmail
2) Extract it to home directory of user1 /home/user1/
3) cd /home/user1/squirrelmail
4) mkdir attach
5) chmod 755 attach data
6) ./configure
select 1,2&4
7) Browse http:///squirrelmail/index.php

Note:* set the VirtualHost entry @ start
* set "retry" option in /etc/exim/exim.conf
* Enable php and mbstring for running squirrelmail

Cpanel for beginners

Features of CPanel

* Domain management
* Email management
* Site management
* Security management(can be used to block IP's)
* Statistics management
* Database management
* Reseller management


->>> We can install a set of predefined applications using Cpanel
->>> Mail Server used is Exim
->>> Webmail is a mail interface

3 Users in Cpanel
->>> Cpanel : domain owner(username is "root")
->>> Webhost manager : Server Admin & Reseller panel
->>> Webmail panel : webmail access panel

Webhost Manager (WHM)
->> Accessed via
http://:2086
https://:2087

Cpanel Access
->> Accessed via
http://:2082
https://:2083


Initial Setup

1) Change root password
2) Set disk quota
3) Set time
4) Statistics
5) Tweak settings -->>> Spam assassin

* PHP (max upload size) --> /usr/local/lib/php.ini

* Statistics Program (3 locations)
1) Statistical software configuration
2) Tweak settings -> stats. programs
3) Feature manager

* Update Preferences --> A cron job run @ 1 @ night (everyday)

* Network Setup
1) Change hostname
2) Nameserver IP's --> assign IP address
3) Resolver

* Securtiy Center
1) Apache mod_userdir Tweak --> temporary URL -->http:///~username/ -->
bandwidth usage shown as IP address & not in sitename
2) Fix insecure permissions --> check for files with insecure permissions.

* When we install Cpanel any required S/w's would be installed automatically.

* Service Monitoring
1) Run every 5 minutes
2) daemon name is chkscrvd. It monitors all services and if it finds any service down, it would
start it.

* Sending email failed (Issue)
1) check whether SMTP is working by
$ telnet 25
2) Some country specific issues(disallowing port 25) --> (Workaround) enable exim on
another port.
3) We could run exim on another port, say 26.


* Backup
(1) --> sda1 (home)
sda2 (backup)
(2) --> sda1 (/home/backup)
(3) --> server1 (home) -->> max 15 hours for restoring.
server2 (backup)

* Manager
1) Package
2) Feature
3) Theme

* Restore multiple backups
* System reboot
* Server status
* List suspended accounts
* Search accounts
* Showaccounts over quota
* Bandwidth usage -->> reset to 0 every month.
* Unsuspend bandwidth

* Email
1)incoming mail -->>new (directory)-->>after reading-->cur(directory)

* Counter application(Counts hits)
Fantastico -->> Application manager

* /etc/trueuserowners

* Cpanel Script
1) restartsrv
2) makecpphp --> built in php in cpanel
3) /usr/local/cpanel/bin/checkperlmodules -->(Install missing perl modules automatically)
4) /scripts/fixquota -->>(/etc/quota.conf) --> If quota exceed issue arrives
5) /scripts/updateuserdomains --> if domain entry is missing.
6) /scripts/easyapache --> add modules to apache(s/w used is EasyApache)
7) eximup --> apply exim update
8) buildeximconf --> builds exim from start.

*Cpanel User Home Directory
1) /home//.....

* BoxTrapper

* Authentication of mail account
1) /home/check/etc/check.com/passwd
2) /home//.htaccess

* crontab -l -u

* Theme --> modify account (/usr/local/cpanel/base/frontend/)


* Adding a Cpanel user
1) Home directory is created as /home/
2) Entry added to /etc/passwd
3) Entry added to /etc/localdomains
4) Entry added to /etc/userdomains
5) Entry added to /etc/trueuserdomains
6) Created /etc/valiases/
7) Created /etc/vfilters/
8) Added zone entry to /etc/named.conf
9) Created zone entry to /var/named
10) Added VirtualHost entry to /usr/local/apache/conf/httpd.conf
11) Created log files in /usr/local/apache/domlogs/
12) Created file in /etc/proftpd
13) Created an entry in /etc/proftpd/passwd.vhost
14) Created file in /var/cpanel/users/



Tuesday, May 4, 2010

Rippind VCD

You only need to perform 3 steps :

step 1. $ vcdxrip --nofiles

This produces the video file and saves it to the current directory in MPEG format

step 2. $ vcdimager -t vcd2 -l "MyLabel" -c filename.cue -b filename.bin filename.mpg

This produces a vcd filesystem formatted image file with extension "cue"
-t option tells the video CD type
-l speicfies the ISO volume label
-c specifies the cue file for output
filename.mpg is the MPEG video produced from step 1

step 3. $ cdrdao write --device /dev/dvd filename.cue

This command writes the cue file (vcd filesystem formatted video file) to the CD.

Have fun with GNU/Linux ........

Tuesday, January 12, 2010

Convert youtube videos to mp3 audio

The ffmpeg command can be used for this purpose .

$ ffmpeg -i youtube.flv -acodec copy ouput.mp3

-i specifies the input file
-acodec Force audio codec to codec. Use the "copy" special value to specify
that the raw codec data must be copied as is.