Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

winsock, raw socket i problem

[es] :: C/C++ programiranje :: winsock, raw socket i problem

[ Pregleda: 2925 | Odgovora: 3 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

hype

Član broj: 16025
Poruke: 13
*.sttnwaho.covad.net



Profil

icon winsock, raw socket i problem05.04.2004. u 09:18 - pre 244 meseci
Sada vec lupam glavom o zid, i ne pomaze... ne znam zasto ne radi. Hint please!
Izdvojeno parce koda bi trebalo da radi:
- otvori raw socket
- slusa i belezi protokol dolazecih paketa (ukoliko je protokol u opsegu 1-17)
Medjutim nesto nije u redu, da li je potreban jos neki poziv WSAxxxx() ili ... ????


Code:

// listen.c
//

#include <stdio.h>
#include <winsock.h>

struct iphdr {
      u_int ihl_version;
    u_char tos;
    u_int tot_len;
    u_int id;
    u_int frag_off;
    u_char ttl;
    u_char protocol;
    u_int check;
    u_long saddr;
    u_long daddr;
  };

int main(void) {
    WSADATA wsaData;
    WORD wVersionRequested = MAKEWORD(1,1);
    int nRet, s;
    char packet[64];
    struct iphdr *ptr;
    
    nRet = WSAStartup(wVersionRequested, &wsaData);
    if (nRet || (wsaData.wVersion != wVersionRequested)) {
        perror("winsock_ver");
        return -1;
    }
    if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) {
        perror("socket()");
        return -1;
    }
    ptr = (struct iphdr *)packet;
    for (;;) {
        memset(&packet, 64, 0);
        recv(s, (char *)&packet, 64, 0);    // greska ovde!?
        if (ptr->protocol >= 1 && ptr->protocol <= 17)
            fprintf(stderr,"packet protocol %d\n", ptr->protocol);
    }
    closesocket(s);
    WSACleanup();
    return 0;
}

for(i=&g->i;c++<++q;w~=0x2) /* RTFC */
 
Odgovor na temu

Jovan Marjanovic
HP GmbH
Stuttgart

Član broj: 942
Poruke: 456
*.net.external.hp.com

ICQ: 42
Sajt: www.hp.com


Profil

icon Re: winsock, raw socket i problem05.04.2004. u 09:35 - pre 244 meseci
Cisto onako laicki da pitam: gde je tu u celoj prici bind i listen ?
Za automatsko dobijanje Super Moderatorskog statusa na Elitesecurity forumima pritisnite Alt+F4
 
Odgovor na temu

hype

Član broj: 16025
Poruke: 13
*.sttnwaho.covad.net



Profil

icon Re: winsock, raw socket i problem09.04.2004. u 11:23 - pre 244 meseci
Da, izostavio sam bind() i listen() - problem je sto se sa listen ne moze dobiti zeljeni efekat, jer za sock_raw + ipproto_raw listen nije podrzan na socketu...
Trenutno mi instalacija linux-a nije u blizini... da li bi neko bar mogao da proveri da li ce gornji primer raditi na linux-u, sa sledecim izmenama:
- s = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
- u petlji umesto recv() => read(s, &packet, 64)
- i naravno sve WSAxxxx() sklonjene

for(i=&g->i;c++<++q;w~=0x2) /* RTFC */
 
Odgovor na temu

hype

Član broj: 16025
Poruke: 13
*.sttnwaho.covad.net



Profil

icon Re: winsock, raw socket i problem11.05.2004. u 12:26 - pre 242 meseci
Hmm... obozavam kada odgovaram sam sebi.
Dakle, zasukao sam rukave, instalirao linux, napisao mali primer. Da li neko ima bilo kakvu ideju/savet/komentar kako portovati sledece na win?

Code:

// listen.c
//

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <arpa/inet.h>
#include <netdb.h>
#define __FAVOR_BSD 
#include <netinet/ip.h>
#include <netinet/tcp.h>

#define PACKET_SIZE sizeof(struct tcppacket)

struct tcppacket {
  struct iphdr ip;
  struct tcphdr tcp;
};

int main(int argc, char **argv) {
  int     r;
  struct tcppacket packet;
  struct in_addr inaddr;

  if( (r = socket(PF_INET, SOCK_RAW, IPPROTO_TCP)) < 0) {
    perror("socket not created");
    return 1;
  }

  for(;;) {
      memset(&packet, 0, PACKET_SIZE);
      read(r, &packet, PACKET_SIZE);   // ovde je problem, cime zameniti ovo?

      inaddr.s_addr = packet.ip.saddr;
      fprintf(stdout,"pkt: %s:[%d] -> ", inet_ntoa(inaddr), ntohs(packet.tcp.th_sport));
      inaddr.s_addr = packet.ip.daddr;
      fprintf(stdout,"%s:[%d] ", inet_ntoa(inaddr), ntohs(packet.tcp.th_dport));

      if (packet.ip.protocol == 6) {
        fprintf(stdout," tcp flags: ");
        if (packet.tcp.th_flags & TH_SYN) fprintf(stdout,"[syn]");
        if (packet.tcp.th_flags & TH_ACK) fprintf(stdout,"[ack]");
        if (packet.tcp.th_flags & TH_RST) fprintf(stdout,"[rst]");
      }    
      fprintf(stdout,"\n");
      fflush(stdout);
  }

  return 0;
}

for(i=&g->i;c++<++q;w~=0x2) /* RTFC */
 
Odgovor na temu

[es] :: C/C++ programiranje :: winsock, raw socket i problem

[ Pregleda: 2925 | Odgovora: 3 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.