Retourner au contenu associé (entrée de forum : Raw socket (BUG))
Posté par DeadMaXfr le 20 janvier 2009 à 08:47. En réponse au message Raw socket (BUG). Évalué à 3.
#include pthread.h #include sys/socket.h #include netpacket/packet.h #include net/ethernet.h #include arpa/inet.h #include sys/ioctl.h #include linux/if.h #include string.h #include stdio.h #include errno.h int sock; struct msghdr packet = {0}; struct sockaddr_ll addr = {0}; struct iovec iov = {0}; struct ifreq ifr = { 0 }; char *name="lo"; char buffer[1500] = {0}; pthread_t tid; pthread_mutex_t mutex; pthread_cond_t cond; int recivelen; fd_set readset; struct timeval tv; static void *reciver(void *arg) { int datapresent; //synchronisation pthread_mutex_lock(&mutex); pthread_cond_signal(&cond); pthread_mutex_unlock(&mutex); //construction du select tv.tv_sec = 1; tv.tv_usec = 0; FD_ZERO(&readset); FD_SET(sock,&readset); // données présentes ? datapresent=select(sock+1, &readset,NULL,NULL,&tv); if(datapresent<=0) { // non printf("timeout\n"); return NULL; } //oui recivelen=recvmsg(sock,&packet,MSG_DONTWAIT); printf("recvmsg return : %d\n",recivelen); return NULL; } int main() { int errorCondition = 0; // overture de la socket sock = socket ( PF_PACKET, SOCK_DGRAM, htons ( ETH_P_ALL ) ); if ( !sock ) return -1; // attachement sur le loopback errorCondition = setsockopt ( sock,SOL_SOCKET,SO_BINDTODEVICE,name,2); if ( errorCondition ) return errorCondition; // recupère l'index de l'interface. Pas utile ici mais on ne sait jamais strcpy ( ifr.ifr_name, name); errorCondition = ioctl ( sock, SIOCGIFINDEX, &ifr ); if ( errorCondition ) return errorCondition; //construction du packet addr.sll_family = AF_PACKET; addr.sll_ifindex = ifr.ifr_ifindex; addr.sll_halen = ETH_ALEN; addr.sll_protocol = htons ( ETH_P_ALL ); packet.msg_name=&addr; packet.msg_namelen = sizeof ( struct sockaddr_ll ); packet.msg_iov = &iov; packet.msg_iovlen = 1; iov.iov_base=&buffer; iov.iov_len=1500; packet.msg_control = NULL; packet.msg_controllen = 0; packet.msg_flags = 0; // initialisation du mutex pour une sychronisation pthread_mutex_init(&mutex,NULL); pthread_cond_init(&cond,NULL); // démarrage du thread d'écoute pthread_mutex_lock(&mutex); pthread_create(&tid,NULL,&reciver,NULL); // attente du bon démarrage, sinon ça fausse le test pthread_cond_wait(&cond,&mutex); pthread_mutex_unlock(&mutex); // attente de la fin du thread pthread_join(tid,NULL); // fermeture avec close. errorCondition = close(sock); if(errorCondition) printf("close fail. errno : %d\n",errno); }
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
# [ RESOLU ]
Posté par DeadMaXfr . En réponse au message Raw socket (BUG). Évalué à 3.