memcpy

multithreading test

Nov 29th, 2011
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.39 KB | None | 0 0
  1. void mod_pcap_loop(struct syn_t *syn) {
  2.     struct pcap_pkthdr *phead = calloc(1, sizeof(*phead));
  3.     const u_char *packet = NULL;
  4.     while(1) {
  5.         packet = pcap_next(syn->pd, phead);
  6.         if (packet) break;
  7.     }
  8.     const struct sniff_ip *ip = (struct sniff_ip*)(packet + SIZE_ETHERNET);
  9.     u_int size_ip = IP_HL(ip)*4;
  10.     const struct sniff_tcp *tcp = (struct sniff_tcp*)(packet + SIZE_ETHERNET + size_ip);
  11.     printf("reply! (%d bytes): (seq %d) (ack %d) (ports %d -> %d) (flags ", (int)phead->caplen,
  12.            ntohl(tcp->th_seq),ntohl(tcp->th_ack),ntohs(tcp->th_sport),
  13.            ntohs(tcp->th_dport));
  14.     if (tcp->th_flags & TH_URG) printf("URG ");
  15.     if (tcp->th_flags & TH_SYN) printf("SYN ");
  16.     if (tcp->th_flags & TH_ACK) printf("ACK ");
  17.     if (tcp->th_flags & TH_PUSH)printf("PSH ");
  18.     if (tcp->th_flags & TH_RST) printf("RST ");
  19.     if (tcp->th_flags & TH_FIN) printf("FIN ");
  20.     printf(")\n");
  21.     if (tcp->th_flags & TH_ACK && tcp->th_flags & TH_SYN) {
  22.         syn->syn_data.port = ntohs(tcp->th_dport);
  23.         syn->syn_data.seq = ntohl(tcp->th_seq);
  24.         syn->syn_data.ack = ntohl(tcp->th_ack);
  25.     }
  26. }
  27.  
  28. void init_pcap(struct syn_t *syn) {
  29.     char errbuf[PCAP_ERRBUF_SIZE];
  30.     char filter_exp[40];
  31.     char *dev = (char *)calloc(1, 4);
  32. #ifdef __APPLE__
  33.     memcpy(dev, "en1", 3);
  34. #elif __linux__
  35.     memcpy(dev, "eth1", 4);
  36. #else
  37.     memcpy(dev, "lo0", 3);
  38. #endif
  39.     struct bpf_program fp;
  40.     bpf_u_int32 mask, net; 
  41.    
  42.     sprintf(filter_exp, "src host %s", syn->dst_ip);
  43.     pcap_lookupnet(dev, &net, &mask, errbuf);
  44.     syn->pd = pcap_open_live(dev, BUFSIZ, 1, 1000, errbuf);
  45.     pcap_compile(syn->pd, &fp, filter_exp, 0, net);
  46.     pcap_setfilter(syn->pd, &fp);
  47. }
  48.  
  49. void *func_probe(void *vd) {
  50.     struct syn_t *syn = (struct syn_t *)vd;
  51.    
  52.     pthread_mutex_lock(&syn->_b_mutex[1]);
  53.     while(syn->tcm != 2) pthread_cond_wait(&syn->_b_cond[1], &syn->_b_mutex[1]);
  54.     pthread_mutex_unlock(&syn->_b_mutex[1]);
  55.     printf("Thread %#x reached (%s)\n",(unsigned)pthread_self(),__func__);
  56.     struct pkt_summary seed;
  57.    
  58.     seed.seq = (int)(arc4random() % MAX_SEQ);
  59.     seed.port = (int)abs((arc4random() % (MAX_P-MIN_P)) + MIN_P-1);
  60.     seed.ack = 0;
  61.    
  62.     pthread_mutex_lock(&syn->_b_mutex[0]);
  63.    
  64.     printf("Sending SYN probe on port %d with seq %d and ack %d\n",seed.port,seed.seq,seed.ack);
  65.     send_probe(TH_SYN,htonl(seed.seq),htonl(seed.ack),INADDR_LOCAL,syn->dst_ip,seed.port,80);
  66.  
  67.     syn->state = S_SYN_SENT;
  68.    
  69.     pthread_cond_signal(&syn->_b_cond[0]);
  70.     while (syn->state != S_SYN_RCVD) pthread_cond_wait(&syn->_b_cond[0],&syn->_b_mutex[0]);
  71.     pthread_cond_signal(&syn->_b_cond[0]);
  72.     pthread_mutex_unlock(&syn->_b_mutex[0]);   
  73.  
  74.     pthread_mutex_lock(&syn->_b_mutex[0]);
  75.    
  76.     seed.seq = seed.seq + 1;
  77.     seed.ack = syn->syn_data.seq + 1;
  78.    
  79.     printf("Sending ACK probe on port %d with seq %d and ack %d\n",seed.port,seed.seq,seed.ack);
  80.     send_probe(TH_ACK,htonl(seed.seq),htonl(seed.ack),INADDR_LOCAL,syn->dst_ip,seed.port,80);
  81.     syn->state = S_ACK_SENT;
  82.    
  83.     pthread_cond_signal(&syn->_b_cond[0]);
  84.     while (syn->state != S_ACK_RCVD) pthread_cond_wait(&syn->_b_cond[0],&syn->_b_mutex[0]);
  85.     pthread_cond_signal(&syn->_b_cond[0]);
  86.     pthread_mutex_unlock(&syn->_b_mutex[0]);   
  87.    
  88.     return vd;
  89. }
  90.  
  91. void *func_check(void *vd) {
  92.     struct syn_t *syn = (struct syn_t *)vd;
  93.    
  94.     pthread_mutex_lock(&syn->_b_mutex[1]);
  95.     while(syn->tcm != 2) pthread_cond_wait(&syn->_b_cond[1], &syn->_b_mutex[1]);
  96.     pthread_mutex_unlock(&syn->_b_mutex[1]);
  97.     printf("Thread %#x reached (%s)\n",(unsigned)pthread_self(),__func__);
  98.    
  99.     init_pcap(syn);
  100.    
  101.     pthread_mutex_lock(&syn->_b_mutex[0]);
  102.    
  103.     while(syn->state != S_SYN_SENT)
  104.         pthread_cond_wait(&syn->_b_cond[0],&syn->_b_mutex[0]);
  105.     mod_pcap_loop(syn);
  106.     syn->state = S_SYN_RCVD;
  107.    
  108.     pthread_cond_signal(&syn->_b_cond[0]);
  109.     pthread_cond_wait(&syn->_b_cond[0],&syn->_b_mutex[0]);
  110.     pthread_cond_signal(&syn->_b_cond[0]);
  111.    
  112.     pthread_mutex_unlock(&syn->_b_mutex[0]);
  113.    
  114.     pthread_mutex_lock(&syn->_b_mutex[0]);
  115.    
  116.     while(syn->state != S_ACK_SENT)
  117.         pthread_cond_wait(&syn->_b_cond[0],&syn->_b_mutex[0]);
  118.     mod_pcap_loop(syn);
  119.     syn->state = S_ACK_RCVD;
  120.    
  121.     pthread_cond_signal(&syn->_b_cond[0]);
  122.     pthread_cond_wait(&syn->_b_cond[0],&syn->_b_mutex[0]);
  123.     pthread_cond_signal(&syn->_b_cond[0]);
  124.    
  125.     pthread_mutex_unlock(&syn->_b_mutex[0]);
  126.  
  127.     return vd;
  128. }
  129.  
  130. int main(int argc, char **argv) {
  131.     struct syn_t *syn;
  132.    
  133.     signal(SIGINT, &h_mac_quit);
  134.     signal(SIGUSR1,&h_mac_quit);
  135.     signal(SIGSEGV,&h_mac_quit);
  136.    
  137.     if (getuid() && geteuid()) { die_with_signal("not root"); }
  138.     syn = calloc(1, sizeof(struct syn_t));
  139.    
  140.     syn->dst_ip = fillstruct("www.google.com");
  141.    
  142.     pthread_mutex_init(&syn->_b_mutex[0], NULL);
  143.     pthread_mutex_init(&syn->_b_mutex[1], NULL);
  144.     pthread_cond_init(&syn->_b_cond[0], NULL);
  145.     pthread_cond_init(&syn->_b_cond[1], NULL);
  146.    
  147.     pthread_mutex_lock(&syn->_b_mutex[1]);
  148.    
  149.     pthread_create(&syn->thrs[0],NULL,func_probe, (void*)syn);
  150.     printf("Spawning thread probe (%#x) (%d)\n",(unsigned)syn->thrs[0],syn->tcm++);
  151.     pthread_create(&syn->thrs[1],NULL,func_check, (void*)syn);
  152.     printf("Spawning check probe (%#x) (%d)\n",(unsigned)syn->thrs[1],syn->tcm++);
  153.    
  154.     pthread_cond_broadcast(&syn->_b_cond[1]);
  155.     pthread_mutex_unlock(&syn->_b_mutex[1]);
  156.    
  157.     pthread_join(syn->thrs[1],NULL);
  158.     pthread_join(syn->thrs[0],NULL);
  159.  
  160.     printf("connection established!\n");
  161.    
  162.     pthread_mutex_destroy(&syn->_b_mutex[0]);
  163.     pthread_mutex_destroy(&syn->_b_mutex[1]);
  164.     pthread_cond_destroy(&syn->_b_cond[0]);
  165.     pthread_cond_destroy(&syn->_b_cond[1]);
  166.     return 1;
  167. }
  168.  
Advertisement
Add Comment
Please, Sign In to add comment