zoukankan      html  css  js  c++  java
  • libevent将信号封装为socket通知的核心代码

    #include"stdafx.h"
    #include"iostream"
    #include "algorithm"
    #include"string"
    #include"vector"
    #include"set"
    #include"map"
    #include "iostream"
    #include "thread"
    #include <iostream>  
    #include <algorithm>  
    #include <vector>  
    #include <iterator>  
    #include "rapidxml.hpp"
    #include "rapidxml_utils.hpp"
    #include <winsock2.h>
    #include <windows.h>
    #define MAX_PATH  256
    #pragma comment( lib, "ws2_32.lib") 
    
    int
    evutil_socketpair(int family, int type, int protocol, int fd[2])
    {
        /* This code is originally from Tor.  Used with permission. */
    
        /* This socketpair does not work when localhost is down. So
        * it's really not the same thing at all. But it's close enough
        * for now, and really, when localhost is down sometimes, we
        * have other problems too.
        */
        int listener = -1;
        int connector = -1;
        int acceptor = -1;
        struct sockaddr_in listen_addr;
        struct sockaddr_in connect_addr;
        int size;
        int saved_errno = -1;
    
    
        listener = socket(AF_INET, type, 0);
        if (listener < 0)
        {
            return -1;
        }
        memset(&listen_addr, 0, sizeof(listen_addr));
        listen_addr.sin_family = AF_INET;
        listen_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
        listen_addr.sin_port = 0;    /* kernel chooses port.     */
        int err;
        if (SOCKET_ERROR == bind(listener, (struct sockaddr *) &listen_addr, sizeof(listen_addr)))
            return -1;
        if (listen(listener, 1) == -1)
            goto tidy_up_and_fail;
    
        connector = socket(AF_INET, type, 0);
        if (connector < 0)
            goto tidy_up_and_fail;
        /* We want to find out the port number to connect to.  */
        size = sizeof(connect_addr);
        if (getsockname(listener, (struct sockaddr *) &connect_addr, &size) == -1)
            goto tidy_up_and_fail;
        if (size != sizeof(connect_addr))
            goto abort_tidy_up_and_fail;
        if (connect(connector, (struct sockaddr *) &connect_addr,
            sizeof(connect_addr)) == -1)
            goto tidy_up_and_fail;
    
        size = sizeof(listen_addr);
        acceptor = accept(listener, (struct sockaddr *) &listen_addr, &size);
        if (acceptor < 0)
            goto tidy_up_and_fail;
        if (size != sizeof(listen_addr))
            goto abort_tidy_up_and_fail;
        /* Now check we are talking to ourself by matching port and host on the
        two sockets.     */
        if (getsockname(connector, (struct sockaddr *) &connect_addr, &size) == -1)
            goto tidy_up_and_fail;
        if (size != sizeof(connect_addr)
            || listen_addr.sin_family != connect_addr.sin_family
            || listen_addr.sin_addr.s_addr != connect_addr.sin_addr.s_addr
            || listen_addr.sin_port != connect_addr.sin_port)
            goto abort_tidy_up_and_fail;
        fd[0] = connector;
        fd[1] = acceptor;
    
        return 0;
    
    abort_tidy_up_and_fail:
        saved_errno = WSAECONNABORTED;
    tidy_up_and_fail:
        return -1;
    }
    
    void main(){
        WORD    wVersionRequested;
        WSADATA wsaData;
        int     err, iLen;
        wVersionRequested = MAKEWORD(2, 2);//create 16bit data  
        err = WSAStartup(wVersionRequested, &wsaData); //load win socket 
        int ev_signal_pair[2];
        evutil_socketpair(
            AF_UNIX, SOCK_STREAM, 0, ev_signal_pair);
        send(ev_signal_pair[0], "nihao", 6, 0);
        char c[100] = { 0 };
        recv(ev_signal_pair[1], c, 100, 0);
        std::cout << c;
        system("pause");
    }
  • 相关阅读:
    20174311 唐嘉 《网络对抗技术》Exp2 后门原理与实践
    20174311 唐嘉《网络对抗技术》Exp1 PC平台逆向破解
    20174304王天政《网络对抗技术》Exp9 Web安全基础
    20174304王天政《网络对抗技术》Exp8 Web基础
    20174304王天政《网络对抗技术》Exp7 网络欺诈防范
    20174304王天政《网络对抗技术》Exp6 MSF基础应用
    20174304王天政《网络对抗技术》Exp5 信息搜集与漏洞扫描
    20174304王天政《网络对抗技术》Exp4 恶意代码分析
    20174304王天政《网络对抗技术》Exp3免杀原理与实践
    20174304王天政《网络对抗技术》Exp2-后门原理与实践
  • 原文地址:https://www.cnblogs.com/wangshaowei/p/8762540.html
Copyright © 2011-2022 走看看