sábado, 22 de febrero de 2014

TCP Checksum


unsigned short  tcp_checksum (int count, unsigned short * addr,unsigned long ip_src,unsigned int ip_dst)
{
    unsigned long sum = 0;
    unsigned long sum2 = 0;
    int k=0;
    int count2;

count2=count;
    count=count>>8;
   // printf("\nCount =%i \n",count);
    while (count > 1) {
     //   printf("\n%04x ",htons(*addr   ));
sum += htons(*addr++);    
        count -= 2;
    }

    // Add left-over byte, if any
    if (count > 0)
        sum += * (unsigned char *) htons(addr);

//printf("Pseudoheader\n");
sum += htons(ip_dst) & 0x00ffff;//printf("\n%04x ",htons(ip_dst) );
         sum += htons(ip_dst>>16) ; //printf("\n%04x ",htons(ip_dst>>16) );
sum += htons(ip_src) & 0x00ffff; //printf("\n%04x ",htons(ip_src) );
         sum += htons(ip_src>>16) ; //printf("\n%04x ",htons(ip_src>>16) );
         sum += 0x6; //printf("\n%04x ",0x6 );
         sum += htons(pacsize+count2);
//printf("\n%04x ",htons(pacsize+count2));

    while (sum >> 16)
        sum = (sum & 0xffff) + (sum >> 16);

    return (unsigned short )(htons(~sum));
}

unsigned short  checksum (int count, unsigned short * addr) {
    unsigned long sum = 0;

    count=count>>8;
   // printf("Count =%x \n",count);
    while (count > 1) {
     //   printf("%x ",(*addr<<8 &0xff00  ));
sum += *addr++;    
        count -= 2;
    }

    // Add left-over byte, if any
    if (count > 0)
        sum += * (unsigned char *) addr;

    if (sum >> 16)
        sum = (sum & 0xffff) + (sum >> 16);

/*while (sum >> 16)
        sum = (sum & 0xffff) + (sum >> 16);*/

    return (unsigned short )(~sum);
}

viernes, 7 de febrero de 2014

Read and Write IDE ATA functions in C

The following functions in C  allows you to read and write a single sector in a hard disk:

int IDE1=0x1F0;
unsigned int drive=0x00;

int Read_Disk(unsigned long addr, unsigned char *targetbuffer)
{
unsigned int i;
int idx;

unsigned int tmpword;
unsigned char buffer[512];
outb(0x00,IDE1+0x01);//null data
outb(0x1,IDE1+0x02);//sector count
outb((unsigned char) addr,IDE1+0x03 );
outb((unsigned char)(addr >> 8),IDE1+0x04 );
outb((unsigned char)(addr >> 16),IDE1+0x05 );
outb(0xE0 | (drive << 4) | ((addr >> 24) & 0x0F),IDE1+0x06 );
outb( 0x20,IDE1+0x07);
while (!(inb(IDE1+0x07) &0x08)) {
//printf("waiting for HD 0x%x\n",addr);
inb(IDE1+0x07);
}
for (idx = 0; idx < 512; idx++)

{
tmpword = inw(IDE1+0x0);
buffer[idx] = (unsigned char)tmpword;
buffer[idx  + 1] = (unsigned char)(tmpword >> 8);
   //printf("%hhx %hhx ",(unsigned char)buffer[idx],(unsigned char)buffer[idx+1]);
  idx++;
}

memcpy(targetbuffer,&buffer,512);

return idx;
}

int Write_Disk(unsigned long addr, unsigned char *targetbuffer)
{

unsigned int i,drive;

int idx;
int m=0;
drive=0;
unsigned int tmpword;
unsigned char buffer[512];
memcpy(&buffer,targetbuffer,512);
outb(0x00,IDE1+0x1);//null data
outb(0x1,IDE1+0x2);//sector count
outb((unsigned char) addr,IDE1+0x3 );
outb((unsigned char)(addr >> 8),IDE1+0x4);
outb((unsigned char)(addr >> 16),IDE1+0x5 );
outb( 0xE0 | (drive << 4) | ((addr >> 24) & 0x0F),IDE1+0x6);
outb( 0x31,IDE1+0x7);//write command
while ((inb(IDE1+0x7) != 0x58)) {
//printf("Write waiting for HD\n");
}
for (idx = 0; idx < 2*512; idx++)

{
tmpword = buffer[ idx ] | (buffer[ idx  + 1] << 8);
outw(tmpword,IDE1+0x0);
idx++;
}

return idx;
}


miércoles, 1 de enero de 2014

Simple terminal emulator for Linux part 1

This is a simple version of a minicom program.

It is based on: http://www.lafn.org/~dave/linux/Serial-Programming-HOWTO-B.txt

it sends characters to a minicom terminal.

#include <signal.h>
#include <stdio.h>   /* Standard input/output definitions */
#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h>

#define BAUDRATE B9600
#define MODEMDEVICE "/dev/ttyUSB0"
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#define FALSE 0
#define TRUE 1
// Based on http://www.lafn.org/~dave/linux/Serial-Programming-HOWTO-B.txt
volatile int STOP=FALSE;

struct termios orig_termios;


int main()
{
int fd,c, res;
struct termios oldtio,newtio;
struct termios oldtio2,newtio2;
char buf[255];
char ca;
int r;
unsigned char ch;
fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
if (fd <0) {perror(MODEMDEVICE); exit(-1); }
tcgetattr(fd,&oldtio); /* save current port settings */
tcgetattr(0,&oldtio2); /* save current port settings */
bzero(&newtio, sizeof(newtio));
bzero(&newtio2, sizeof(newtio2));
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;

/* set input mode (non-canonical, no echo,...) */
newtio.c_lflag = 0;

newtio.c_cc[VTIME]    = 0;   /* inter-character timer unused */
newtio.c_cc[VMIN]     = 0;   /* blocking read until 5 chars received */
newtio2.c_lflag = 0;
newtio2.c_cc[VTIME]    = 0;   /* inter-character timer unused */
newtio2.c_cc[VMIN]     = 0;   /* blocking read until 5 chars received */


tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
tcflush(0, TCIFLUSH);
tcsetattr(0,TCSANOW,&newtio2);


while (STOP==FALSE)
{       /* loop for input */

res = read(fd,buf,255);   /* returns after 5 chars have been input */
          buf[res]=0;            
          if(res>0)
          printf("%s:%d\n", buf, res);
          if (buf[0]=='z') STOP=TRUE;
         r = read(0, &ch, 1);
if(r>0){
// printf("%c\n", ch, r);
          write(fd,&ch,1);        
}
}
tcsetattr(fd,TCSANOW,&oldtio);
tcsetattr(0, TCSANOW,&oldtio2);
}

At the moment it needs a "\n" to get the printf working.

code com