TSVConnFdCreate

    TSVConn (int fd)

    On success, the returned object owns the socket and the caller must not close it. If TSVConnFdCreate() fails, NULL is returned, the socket is unchanged and the caller must close it.

    1. // the TSHttpTxn pointer.
    2. VDEBUG("allocated server intercept state istate=%p for txn=%p", istate, cdata.txn);
    3. // Set up a connection to our real origin, which will be
    4. // 127.0.0.1:$PORT.
    5. memset(&addr, 0, sizeof(addr));
    6. addr.sin.sin_family = AF_INET;
    7. addr.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); // XXX config option
    8. addr.sin.sin_port = htons(PORT); // XXX config option
    9. // Normally, we would use TSNetConnect to connect to a secondary service, but to demonstrate
    10. fd = ::socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    11. TSReleaseAssert(fd != -1);
    12. if (::connect(fd, &addr.sa, sizeof(addr.sin)) == -1) {
    13. // We failed to connect to the intercepted origin. Abort the
    14. // server intercept since we cannot handle it.
    15. VDEBUG("connect failed with %s (%d)", strerror(errno), errno);
    16. TSVConnAbort(arg.vc, TS_VC_CLOSE_ABORT);
    17. delete istate;
    18. TSContDestroy(contp);
    19. ::close(fd);
    20. return TS_EVENT_NONE;
    21. }
    22. if ((istate->server.vc = TSVConnFdCreate(fd)) == nullptr) {
    23. TSVConnAbort(arg.vc, TS_VC_CLOSE_ABORT);
    24. delete istate;
    25. TSContDestroy(contp);
    26. ::close(fd);
    27. return TS_EVENT_NONE;
    28. }
    29. VDEBUG("binding client vc=%p to %s:%u", istate->client.vc, inet_ntop(AF_INET, &addr.sin.sin_addr, buf, sizeof(buf)),
    30. (unsigned)ntohs(addr.sin.sin_port));
    31. istate->txn = cdata.txn;
    32. istate->client.vc = arg.vc;
    33. // Reset the continuation data to be our intercept state
    34. // block. We will need this so that we can access both of the

    TSAPI(3ts)