LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-16-2014, 10:58 PM   #16
Syndacate
Member
 
Registered: Aug 2008
Location: Santa Clara, CA
Distribution: Ubuntu, mainly. Too much stuff works out of the box O.o
Posts: 71

Rep: Reputation: 52

Quote:
Originally Posted by kikilinux View Post
Dear Syndacate
thanks to answer, it is not completely same but ur solution is work for me.
I want the to set a specific bit of specific byte for example "first bit of 21st byte".
i problem solved.

Best
Just shift whatever bit you want to set into the offset of the byte, so to modify my code to set ONLY the 1st bit (and not clear the rest), just do:
Code:
char *ip_hdr_ptr = (char*)&ip_header;
*(ip_hdr_ptr + 20) |= 1;
If you want to set the 5th bit of the 21st byte, then just shift the 1:
Code:
char *ip_hdr_ptr = (char*)&ip_header;
*(ip_hdr_ptr + 20) |= 1 << 5;
If you want to clear the 5th bit of the 21st byte, then invert it and AND remaining bits (you can just take out the shift to clear 1st bit):
Code:
char *ip_hdr_ptr = (char*)&ip_header;
*(ip_hdr_ptr + 20) &= ~(1 << 5);
Hope that works better for ya. No need to worry about memory allocations and temporary values and the like.
 
1 members found this post helpful.
Old 05-17-2014, 12:12 AM   #17
kikilinux
Member
 
Registered: Sep 2012
Posts: 125

Original Poster
Rep: Reputation: Disabled
I believe i can't correct my errors
Can anyone help me to have an OR operation on 22st byte of ip header (ip_header)?


struct iphdr *ip_header;
struct tcphdr *tcp_header;

// Get the ip header
ip_header = (struct iphdr *)skb_network_header(skb);
skb_set_transport_header(skb, ip_header->ihl * 4);
tcp_header = (struct tcphdr *)skb_transport_header(skb);

How can we point to a specific byte of ip_header?
Is it possible to perform OR bitwise operation on 22st or 21st bytes (option field) with no more variable declaration ?
At the end please suggest me a book for networking programming in kernel(if exists).

any help would be greatly appreciated.
Best Regards.
 
Old 05-17-2014, 02:25 AM   #18
Syndacate
Member
 
Registered: Aug 2008
Location: Santa Clara, CA
Distribution: Ubuntu, mainly. Too much stuff works out of the box O.o
Posts: 71

Rep: Reputation: 52
Quote:
Originally Posted by kikilinux View Post
I believe i can't correct my errors
Can anyone help me to have an OR operation on 22st byte of ip header (ip_header)?


struct iphdr *ip_header;
struct tcphdr *tcp_header;

// Get the ip header
ip_header = (struct iphdr *)skb_network_header(skb);
skb_set_transport_header(skb, ip_header->ihl * 4);
tcp_header = (struct tcphdr *)skb_transport_header(skb);

How can we point to a specific byte of ip_header?
Is it possible to perform OR bitwise operation on 22st or 21st bytes (option field) with no more variable declaration ?
At the end please suggest me a book for networking programming in kernel(if exists).

any help would be greatly appreciated.
Best Regards.
I don't know any network programming books that are good, offhand.

As for the operation...w/o declaring any more variables you have to use a few parenthesis..some of them aren't necessary, but I like to be very clear w/ my intentions:
(Note: For the offsets I treated values as 0 based)
Code:
    struct iphdr *ip_header;
    struct tcphdr *tcp_header;

    // Get the ip header
    ip_header = (struct iphdr *)skb_network_header(skb);
    skb_set_transport_header(skb, ip_header->ihl * 4);
    tcp_header = (struct tcphdr *)skb_transport_header(skb);

    // Set 21st byte, bit 1 to 1
    *(((uint8_t*)ip_header) + 21) |= 1;

    // Set 22nd byte, bit 4 to 1
    *(((uint8_t*)ip_header) + 22) |= 1 << 4;

    // Clear 21st byte, bit 3
    *(((uint8_t*)ip_header) + 21) &= ~(1 << 3);
Hope that explains it a little better..it should work .

NOTE: "uint8_t" is guaranteed to be 8 bits, unlike a char.
 
1 members found this post helpful.
Old 05-17-2014, 03:48 AM   #19
kikilinux
Member
 
Registered: Sep 2012
Posts: 125

Original Poster
Rep: Reputation: Disabled
Yes it worked
thanks to answer.
 
  


Reply

Tags
bits, programming



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Can we perform TWO MMAP operation on same /dev file dgashu Programming 3 01-31-2013 09:48 AM
C program to perform an operation continuously until user presses a key ananth86coolguy Linux - Newbie 4 03-13-2009 09:59 AM
how do i use bash to perform an operation on multiple files Brynn Linux - Newbie 6 07-24-2006 06:02 AM
Perform an operation on files x, y...direct output to x.out, y.out respectively johndoe0028 Linux - General 6 04-02-2006 10:39 PM
about bitwise operators? eshwar_ind Programming 17 10-25-2004 02:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration