I am trying to read from a packet payload just its User agent.
I am searching for "User-Agent" in packet payload and and from then on copying as many chars as there are between the occurrence of "User-Agent" and the first occurrence of '\n' right after that. Here's my code:
sprintf(
stat,"%.*s\0",
strstr(strstr(p->data, "User-Agent:"),"\n") - strstr(p开发者_如何学运维->data, "User-Agent:"),
strstr(p->data, "User-Agent: ")
);
The trouble is that for a LOT of packets, this is causing seg fault. I am sorry for this silly way to write it; if you know a better way to write (or format), please tell me.
Why are you writing code that is so complex? The pointer arthritic could end end pointing to something that will give a seg fault. Consider refactoring the code.
精彩评论