1 #include <stdint.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <sys/types.h> 5 #include <sys/socket.h> 6 #include "../../memcheck.h" 7 8 /* user space headers might not be there, define things ourselves. */ 9 typedef struct { 10 uint8_t b[6]; 11 } __attribute__((packed)) vui_bdaddr_t; 12 13 struct vui_sockaddr_rc { 14 sa_family_t rc_family; 15 vui_bdaddr_t rc_bdaddr; 16 uint8_t rc_channel; 17 }; 18 19 #define VUI_AF_BLUETOOTH 31 20 #define VUI_BTPROTO_RFCOMM 3 21 22 #define VUI_BDADDR_ANY (&(vui_bdaddr_t) {{0, 0, 0, 0, 0, 0}}) 23 24 int 25 main (int argc, char **argv) 26 { 27 int nSocket; 28 29 nSocket = socket(VUI_AF_BLUETOOTH, SOCK_STREAM, VUI_BTPROTO_RFCOMM); 30 31 if (nSocket < 0) 32 { 33 // Not supported, nothing to test... 34 return 1; 35 } 36 37 struct vui_sockaddr_rc aAddr; 38 39 // Store correct values in aAddr but marking it undefined 40 // so as to generate errors. We need to have deterministic 41 // undefined values to have a reproducible test. 42 aAddr.rc_family = VUI_AF_BLUETOOTH; 43 aAddr.rc_bdaddr = *VUI_BDADDR_ANY; 44 aAddr.rc_channel = 5; 45 VALGRIND_MAKE_MEM_UNDEFINED(&aAddr, sizeof(aAddr)); 46 // We re-assign below each piece one by one, so as to 47 // have the piece marked initialised. 48 49 50 // Ignore return values. 51 52 // Everything uninit (family, ...) 53 bind(nSocket, (struct sockaddr *) &aAddr, sizeof(aAddr)); 54 55 // Same but with an unknown family (we hope :) 56 aAddr.rc_family = 12345; 57 // (reset everything to uninit) 58 VALGRIND_MAKE_MEM_UNDEFINED(&aAddr, sizeof(aAddr)); 59 bind(nSocket, (struct sockaddr *) &aAddr, sizeof(aAddr)); 60 61 aAddr.rc_family = VUI_AF_BLUETOOTH; 62 // uninit bdaddr and channel. 63 bind(nSocket, (struct sockaddr *) &aAddr, sizeof(aAddr)); 64 65 aAddr.rc_bdaddr = *VUI_BDADDR_ANY; 66 // uninit channel. 67 bind(nSocket, (struct sockaddr *) &aAddr, sizeof(aAddr)); 68 69 aAddr.rc_channel = 5; 70 // Everything correctly init. 71 bind(nSocket, (struct sockaddr *) &aAddr, sizeof(aAddr)); 72 73 return 0; 74 } 75