Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C by j ( 16 years ago )
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<features.h>
#include<linux/if_packet.h>
#include<linux/if_ether.h>
#include<errno.h>
#include<sys/ioctl.h>
#include<net/if.h>
#include<linux/ip.h>
#include<arpa/inet.h>
#include<linux/tcp.h>
#include<netinet/in.h>
#include <string.h>



int recieve(struct Adresses *Adress, int k,struct Sender *Sender){

    struct sockaddr packetS; //the final packet struct
    unsigned char packet[2048]; //content of packet
    int len; //length of packet
    char str[INET_ADDRSTRLEN];

   
    //creating raw socket

    int rawsock;
    if ((rawsock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP))) == -1) {
        perror("Error creating raw socket: ");
        exit(-1);
    }

    len = recvfrom(rawsock, packet, sizeof (packet) - 1, 0, (struct sockaddr*) &packetS;, sizeof(packetS));
    close(rawsock);

    struct ethhdr *ethernet_header;
    struct iphdr *ip_header;

    //Checking the size of packet to verify whether it COULD be a TCP packet
    if (len >= (sizeof (struct ethhdr))) {
        ethernet_header = (struct ethhdr *) packet;
        if (len >= (sizeof (struct ethhdr) + sizeof (struct iphdr)) && ntohs(ethernet_header->
                h_proto) == ETH_P_IP) {
            ip_header = (struct iphdr *) (packet + sizeof (struct ethhdr));
        }
    }
    Send.Sender = inet_ntop(AF_INET, &ip;_header->saddr, str, sizeof (str));
    
    for(int l = 0; l < k; l++){

        if(Adress[l].Ipadress == Send.Sender)
            recieve(Adress, k);

    }
    return 1;
};

struct Adresses{
    short Number;
    char* Ipadress;
};

struct Sender{
    char* Sender;
};

int main(int argc, char * argv []) {

    const int addr = 20;
    Adresses Adress[addr];
    Sender Send;
    char buf[4096];

    FILE* adresses;
    FILE* newadress;

    adresses = fopen&#40;"adress.txt", "r+"&#41;;
    newadress = fopen&#40;"adress.txt", "w+"&#41;;

    int k = 0;

    while(!feof(adresses)){

        fscanf(adresses, "%i %s", &Adress;[k].Number, &Adress;[k].Ipadress);
        k= k+1;

    }
    fclose(adresses);

    recieve(Adress, k);

    int sd;
    struct sockaddr_in sa;

    if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        printf(" socket () failed \n ");
        exit(EXIT_FAILURE);
    }

    sa . sin_family = AF_INET; /* IPv4 */
    sa . sin_port = htons(33333);
    sa . sin_addr.s_addr = inet_addr("127.0.0.1");

    /* Datagramm als an JavaProgramm schicken */

    if (sendto(sd, Send.Sender, strlen(Send.Sender), 0, (struct sockaddr *) & sa, sizeof ( sa)) < 0) {
        printf(" sendto () failed \n ");
        close(sd);
        exit(EXIT_FAILURE);
    }

    printf(" Anfrage an UDPProgramm verschickt .\n ");
    close(sd);

    int fd;
    if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        printf(" socket () failed \n ");
        exit(EXIT_FAILURE);
    }

    int bytes;
    struct sockaddr_in packetUDP;
    memset(&packetUDP;, 0, sizeof(packetUDP));
    packetUDP . sin_family = AF_INET;
    packetUDP . sin_port = htons(33333);
    packetUDP . sin_addr.s_addr = INADDR_ANY;

    printf("das\n");
   
    bytes = recvfrom(fd, buf, sizeof( buf)-1, 0, (struct sockaddr*) &packetUDP;,sizeof(packetUDP));

    if (bytes == -1) {
        
        printf(" recvfrom () failed \n ");
        close(fd);
        exit(EXIT_FAILURE);

    }    
    buf[bytes] = '\0';
    
    
    printf(" Antwort von User erhalten: %s .\n ", buf);

    return 0;
}

 

Revise this Paste

Parent: 20513
Children: 20523
Your Name: Code Language: