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; } |
Quote:
Just a thought, but...looking on to see where THIS goes to... :) Thor |
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 ? |
Quote:
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... |
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; |
Quote:
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:
|
Ok I understand but how do I change ip address of packet in c++ program ?
|
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? |
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 |
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. |