LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   IP address c++ (https://www.linuxquestions.org/questions/programming-9/ip-address-c-927631/)

marko745 02-04-2012 03:59 PM

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;
}

ButterflyMelissa 02-05-2012 12:34 PM

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

marko745 02-05-2012 01:02 PM

why?... no why, but how :D
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 :D

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 ?

ButterflyMelissa 02-05-2012 01:34 PM

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...

marko745 02-06-2012 11:37 AM

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;

ButterflyMelissa 02-06-2012 12:46 PM

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.

marko745 02-06-2012 02:02 PM

Ok I understand but how do I change ip address of packet in c++ program ?

Ramurd 02-08-2012 05:15 AM

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?

ButterflyMelissa 02-11-2012 11:13 AM

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

marko745 03-02-2012 02:04 PM

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 ?


All times are GMT -5. The time now is 10:09 PM.