如何通过CFNetwork获取到CFSocket信息

最近在看关于CFNetwork层网络机制的Open Source . 想拿到每个数据包的内容及本地、远程两端的Socket信息

通过对CoreFoundation下面的CFStream类两个方法(CFReadStreamRead、CFWriteStreamWrite)进行MSHookFunction Hook 能获取到读写流内容

CFStream 建立在 CFSocket 之上,按理是可以获得CFSocket 对象 分析了许久 未果 不知道是否有方法可以通过(CFReadStreamRead、CFWriteStreamWrite)获取到CFSocket 对象 ,进一步就可以获得当前网络包本地IP、端口 及 远程IP、端口 并与之相对应

CFIndex CFWriteStreamWrite(CFWriteStreamRef stream, const UInt8 *buffer, CFIndex bufferLength);

CFIndex CFReadStreamRead(CFReadStreamRef stream, UInt8 *buffer, CFIndex bufferLength);

请教各位大佬了 小弟新手 望知道的大佬 指点一二思路 感谢

首先最外层是

CFIndex CFReadStreamRead(CFReadStreamRef readStream, UInt8 *buffer, CFIndex bufferLength); //<0>

对于各种不同的Stream,实际上CFStream里面存了一个callback结构体,(类似于子类化,但其实没有"类")通过不同的函数指针决定了不同Stream的功能.
对于"直接"接在Socket上面的Stream,这个东西在CFSocketStream.c:499(CFNetwork-129.20).对于从流中读取,函数长这样:

/* static / CFIndex _SocketStreamRead(CFReadStreamRef stream, UInt8 buffer, CFIndex bufferLength, CFStreamError* error, Boolean* atEOF, _CFSocketStreamContext* ctxt); //<1>

其中实际的数据通过_CFSocketRecv 进行读取,这里的参数就是你想要的CFSocket了,位置来自<1>函数的ctxt Context对象中,ctxt对象往上找,就是stream->info(记得判断流的类型,info在不同类型的流之间是不同的)

CF_EXPORT void* _CFStreamGetInfoPointer(struct _CFStream* stream) {
return stream == NULL? NULL : stream->info;
}

以上内容多半是猜的,我不对其负责.以下内容是从开源项目头文件复制的,没人对其负责.


struct _CFStream {

    CFRuntimeBase _cfBase;

    CFOptionFlags flags;

    CFErrorRef error; // if callBacks->version < 2, this is actually a pointer to a CFStreamError

    struct _CFStreamClient *client;

    /* NOTE: CFNetwork is still using _CFStreamGetInfoPointer, and so this slot needs to stay in this position (as the fifth field in the structure) */

    /* NOTE: This can be taken out once CFNetwork rebuilds */

    /* NOTE: <rdar://problem/13678879> Remove comment once CFNetwork has been rebuilt */

    void *info;

    const struct _CFStreamCallBacks *callBacks;  // This will not exist (will not be allocated) if the callbacks are from our known, "blessed" set.

    CFLock_t streamLock;

    CFArrayRef previousRunloopsAndModes;

    dispatch_queue_t queue;

};

typedef struct {

    CFSpinLock_t                _lock;              /* Protection for read-half versus write-half */

    UInt32                      _flags;

    CFStreamError               _error;

    CFReadStreamRef             _clientReadStream;

    CFWriteStreamRef            _clientWriteStream;

    CFSocketRef                 _socket;            /* Actual underlying CFSocket */

    CFMutableArrayRef           _readloops;

    CFMutableArrayRef           _writeloops;

    CFMutableArrayRef           _sharedloops;

    CFMutableArrayRef           _schedulables;      /* Items to be scheduled (i.e. socket, reachability, host, etc.) */

    CFMutableDictionaryRef      _properties;        /* Host and port and reachability should be here too. */

} _CFSocketStreamContext;

感谢回复 对的 你说的这块我知道 分析到此处来了的 之前没贴上

可能是我哪儿不太清楚 stream->info 一直编译不过

error: member access into incomplete type ‘struct CFStream’
void * info = streams ->info;

应该是引入的方式不对

64位的手机可以直接stream偏移40拿info (