Set IP_HDRINCL to setsockopt function in win32

折月煮酒 提交于 2019-12-11 15:18:29

问题


I'm fighting with raw sockets in Win32 and now I'm stuck, the soetsockopt function give me the 10022 error (invalid argument), but I think I pass the correct arguments... of course I'm wrong u_u'

sock = socket(AF_INET,SOCK_RAW,IPPROTO_UDP);
if (sock == SOCKET_ERROR)
{
  printf("Error socket(): %d", WSAGetLastError());
  return;
}
char on = 1;
error =  setsockopt(sock,IPPROTO_IP,IP_HDRINCL,&on,sizeof(on)); 
if (sock == SOCKET_ERROR)
{
  printf("Error setsockopt(): %d", WSAGetLastError());
  return;
}

Anybody knows what happen to my code?


回答1:


As far as I remember you need to use int on = 1 instead of char...




回答2:


You should use DWORD 1 or bool true, there are lots of documents that show code with char, int, DWORD, bool, but the right one is book or DWORD, maybe int in some systems... but currently it worked for me as bool.

By the way, I've found error 10014 WSAEFAULT when executing the same function setsockopt with IP_HDRINCL in Win7, it works in XP but in Win7 it always fails and don't know why... maybe it is disabled in recent windows versions?



来源:https://stackoverflow.com/questions/1115511/set-ip-hdrincl-to-setsockopt-function-in-win32

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!