LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 02-04-2012, 04:59 PM   #1
marko745
LQ Newbie
 
Registered: Dec 2011
Posts: 12

Rep: Reputation: Disabled
IP address c++


Hi

How do I change ip address on packet ?
I have somethik like ...

#include <iostream>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <stdio.h>


using namespace std;

unsigned short checksum(unsigned short *addr, int len)
{
register int sum = 0;
u_short answer = 0;
register u_short *w = addr;
register int nleft = len;

while (nleft > 1)
{
sum += *w++;
nleft -= 2;
}

if (nleft == 1)
{
*(u_char *) (&answer) = *(u_char *) w;
sum += answer;
}

sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
answer = ~sum;
return (answer);
}

int
main(int agrc, char * agrv[])
{

socklen_t size;
hostent *host;
icmphdr *icmp, *icmpRecv;
iphdr *ip;
in_addr a;
int sock, lenght;
int ttl=0;
sockaddr_in sendSockAddr;
char *addrString;
const char *adres="192.168.8.9";
int i;
char *packet;
unsigned short int pid=getpid();

cout<<agrv[1]<<endl;
if ((host = gethostbyname(agrv[1])) == NULL)
{
cerr<<"hostname"<<endl;
return -1;
}

if (( sock = socket(AF_INET,SOCK_RAW,IPPROTO_ICMP)) == -1)
{
cerr<<"socket"<<endl;
return -2;
}

ttl=255;

//setsockopt(sock, SOL_IP, IP_TTL, &ttl,sizeof(int));

sendSockAddr.sin_family = AF_INET;
sendSockAddr.sin_port = 0;
sendSockAddr.sin_addr.s_addr=inet_addr("10.0.0.2"); //this is //destination ??
//inet_aton(AF_INET,"10.0.0.1",&sendSockAddr.sin_addr.s_addr);
a.s_addr=inet_addr("10.0.0.77");
memset(&(sendSockAddr.sin_zero), '\0',8);
//a.sin_addr.s_addr=inet_addr(adres);
//memcpy(&(sendSockAddr.sin_addr), host->h_addr, host->h_length);

icmp = (icmphdr *) malloc (sizeof(icmphdr));
icmp->type = ICMP_ECHO;
icmp->code = 0;
icmp->un.echo.id = pid;
icmp->un.echo.sequence = 1;
icmp->checksum = checksum((unsigned short *)icmp, sizeof(icmphdr));

ip = (iphdr *) malloc (sizeof(iphdr));
ip->ihl = 5;
ip->version = 4;
ip->tos = 0;
ip->tot_len = sizeof(struct iphdr) + sizeof(struct icmphdr);
ip->id = htons(0);
ip->frag_off = 0;
ip->ttl = 255;
ip->protocol = IPPROTO_ICMP;
ip->saddr=inet_addr(adres);
ip->check = checksum((unsigned short *)ip, sizeof( iphdr));

//packet=(char *) malloc(sizeof( iphdr) + sizeof( icmphdr));

sendto(sock, (char *)icmp, sizeof(icmphdr), 0, (sockaddr *)&sendSockAddr, sizeof(sockaddr));

close(sock);

return 0;
}
 
Old 02-05-2012, 01:34 PM   #2
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,702
Blog Entries: 23

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
How do I change ip address on packet ?
The question is : why? If you change the IP address of a packet, you can potentially destroy the stream it belongs to...

Just a thought, but...looking on to see where THIS goes to...

Thor
 
Old 02-05-2012, 02:02 PM   #3
marko745
LQ Newbie
 
Registered: Dec 2011
Posts: 12

Original Poster
Rep: Reputation: Disabled
why?... no why, but how
Imagine situation:
I want send icmp (request) packet with ip address of xy(for example my chief ) to router and my chief get icmp (replay) from router. How do i do

sockaddr_in soc;
iphdr *ip;
.
.
.
soc.sin_addr.s_addr = inet_addr("10.0.0.2"); // this just change destination address
.
.
ip->saddr=inet_addr("10.0.0.2); // what do this ???

"destroy the stream" some bad packets can destroy connection ?
 
Old 02-05-2012, 02:34 PM   #4
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,702
Blog Entries: 23

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
"destroy the stream" some bad packets can destroy connection ?
No, in TCP, there is no connection as such, but a stream of packages. Imagine you're downloading a pretty big file (an ISO, say) from somewhere. Of course, that file is not being sent an one lump, but instead is broken up in segments, each with (among others) a sequence numbner, a sender IP and a recipient IP. Imagine one of these being "lost" due to an altered IP address. You never have a connection, but merely send a packet with a request to some server having your IP and the server's IP. The reply is one or more packets depending on the size of the requested data. The packets arrive because they are being routed to their destination.
Upon reception, your system (one of the OSI layers in fact) strips these extra's and reassembles the whole file using the sequence numbers...
But, I agree, it is an intersting excercise...and well worth this read, I guess...
 
Old 02-06-2012, 12:37 PM   #5
marko745
LQ Newbie
 
Registered: Dec 2011
Posts: 12

Original Poster
Rep: Reputation: Disabled
Yes I think my program is possible to do but I do not know how it wrote in c++. "Some server having you IP address" you mean DNS server or server with (ISO) or DHCP or .... ?

btw: Chief and I are in one subnet;
 
Old 02-06-2012, 01:46 PM   #6
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,702
Blog Entries: 23

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Some server having you IP address
Hmm, okay, let's go from the start. You get an IP address from you provider. As soon as the lease is up, your PC requests a new one. Google does"nt need to request a new one; Google should NOT request a new one. It has a fixed IP addres. Somewhere in the tables of the DNS system, there is a name - IP pair for Google - but others are in there as well. Those with a fixed IP address, not people like you and me, we are just "lending" the IP address.
If you type in www.google.com - your browser requests the matching IP address and sends a packet to the IP address (and Google) with a "get" request for the main page. That packet has two IP addresses: yours and Google's.
If you want to change the destination of that packet, you could (in pure theory) extract Google's IP address and replace it with Yahoo's address. Not very productive, but very possible.
You'd have to capture these packets whizzing by first, though

Quote:
Chief and I are in one subnet
Of course, that is a requirement if you want to interact...good assessment.

Last edited by ButterflyMelissa; 02-06-2012 at 01:47 PM.
 
Old 02-06-2012, 03:02 PM   #7
marko745
LQ Newbie
 
Registered: Dec 2011
Posts: 12

Original Poster
Rep: Reputation: Disabled
Ok I understand but how do I change ip address of packet in c++ program ?
 
Old 02-08-2012, 06:15 AM   #8
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 678

Rep: Reputation: 105Reputation: 105
That raises another question: what happens in this situation:

I get my lease from my provider for 1 hour; I do a big download of a complete linux distro but alas, slow link, so the download takes more than 1 hour. I get a new lease (with a different IP) from my provider... what happens to the stream?
 
Old 02-11-2012, 12:13 PM   #9
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,702
Blog Entries: 23

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
The stream gets ... broken. However, in case of a torrent, the software is clever enough to pick it back up...
But...interesting question. Well, TCP/IP is clever enough to notice some bits missing, and will request (with the new IP address) the missing pieces...

Thor
 
Old 03-02-2012, 03:04 PM   #10
marko745
LQ Newbie
 
Registered: Dec 2011
Posts: 12

Original Poster
Rep: Reputation: Disabled
TCI is too clever. It do counts with pockets and it knows all figures of packets. And my question is same how it is in c++ language ?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Program to assign globa lIPv6 address and bind() to the previously assigned address. mwnn Linux - Networking 2 10-07-2010 03:29 AM
Address Space Randomization on 2.6.28-15-generic ubuntu 9.04. Finding base address nullprocess Linux - Security 1 09-15-2009 01:56 AM
Address Space Randomization on 2.6.28-15-generic ubuntu 9.04. Finding base address nullprocess Linux - Kernel 1 09-14-2009 09:31 PM
how to get ip address, broadcast address, mac address of a machine sumeshstar Programming 2 03-12-2005 05:33 AM

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

All times are GMT -5. The time now is 03:43 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration