0

Here is a code I'm trying to use Objective C method in C function callaback. As result it crashes:

typedef void (^BlockCallback)(u_char *args, const struct pcap_pkthdr *header, const u_char *packet);

- (void)startProcessing:(BlockCallback)progressCallback
{
    pcap_loop(handle, packetsNumber, (__bridge void *)progressCallback, NULL);// it crashes here
}
....

[self startProcessing:^(u_char *args, const struct pcap_pkthdr *header, const u_char *packet) {
/// some processing code here
}];
Serge
  • 2,031
  • 3
  • 33
  • 56

1 Answers1

1

I don't see how C can call the block like that. This should work though, provided that first pointer (user) is a user-defined context that you can use to pass self to the callback function:

static void _pcapCallback(u_char *user, const struct pcap_pkthdr *header, const u_char *packet)
{
    MyClass *myClass = (MyClass *)user;
    [myClass _pcapHeader:header packet:packet];
}

@implementation MyClass

- (void)_pcapHeader:(const struct pcap_pkthdr *)header
             packet:(const u_char *)packet
{
    // do thing
}

- (void)startProcessing
{
    pcap_loop(handle, packetsNumber, _pcapCallback, self);
}

...

@end
trojanfoe
  • 120,358
  • 21
  • 212
  • 242