void peisk_bluetoothConnectOutgoing() { int i; PeisBluetoothAdaptor *adaptor; int status=0; for(i=0;i<peisk_nBluetoothAdaptors;i++) { adaptor = &peisk_bluetoothAdaptors[i]; if(adaptor->outgoing.mode == 0) continue; if(adaptor->outgoing.mode == 1) { /* See if connect() have finished */ struct pollfd fds; fds.fd = adaptor->outgoing.socket; fds.events = -1; fds.revents = 0; if(poll(&fds,1,0)) { socklen_t slen; slen=sizeof(status); if(getsockopt(adaptor->outgoing.socket,SOL_SOCKET,SO_ERROR,(void*) &status,&slen)) { printf("Get sockopt failed...\n"); } printf("Connection finished: status=0x%x (time elapsed: %.3fs)\n",status, peisk_gettimef()-adaptor->outgoing.timeout+PEISK_CONNECT_TIMEOUT); if(status) { printf("Outgoing connection failed: "); perror(""); printf("\n"); peisk_abortConnect(adaptor->outgoing.connection); close(adaptor->outgoing.socket); adaptor->outgoing.mode = 0; } else { adaptor->outgoing.connection->type=eBluetoothConnection; adaptor->outgoing.connection->connection.bluetooth.adaptor=adaptor; adaptor->outgoing.connection->connection.bluetooth.socket=adaptor->outgoing.socket; printf("Outgoing connection %x has id %d\n", (unsigned int) adaptor->outgoing.connection,adaptor->outgoing.connection->id); PeisConnectMessage message; peisk_initConnectMessage(&message,adaptor->outgoing.flags); printf("Sending connection message:\n"); peisk_hexDump(&message,sizeof(message)); printf("\n"); send(adaptor->outgoing.socket,&message,sizeof(message),MSG_NOSIGNAL); peisk_outgoingConnectFinished(adaptor->outgoing.connection,adaptor->outgoing.flags); if(1 || peisk_printLevel & PEISK_PRINT_CONNECTIONS) fprintf(stdout,"peisk: new outbound BLUETOOTH connection established\n"); adaptor->outgoing.mode = 0; } } } if(adaptor->outgoing.timeout < peisk_timeNow && adaptor->outgoing.mode != 0) { printf("Giving up on outgoing CONNECT\n"); peisk_abortConnect(adaptor->outgoing.connection); close(adaptor->outgoing.socket); adaptor->outgoing.mode = 0; } } }
PeisConnection *peisk_tcpConnect(char *name,int port,int flags) { struct hostent *hostent; struct sockaddr_in peerAddr; int sock; int retval; socklen_t retlen; fd_set writeSet, readSet, exepSet; struct timeval timeout; PeisConnection *connection; /** \todo peisk_tcpConnect - refuse to connect to hosts we already are connected to. */ /* Refuse connections to ourselves */ if(port == peiskernel.tcp_serverPort && (strcmp(name,"localhost") == 0 || strcmp(name,"127.0.0.1") == 0 || strcmp(name,peiskernel.hostInfo.hostname) == 0)) return NULL; /* Lookup hostname */ h_errno=0; hostent=gethostbyname(name); if(!hostent) { fprintf(stdout, "peisk: gethostbyname(%s) failed. h_errno=%d hostent=%x\n", name,h_errno,(unsigned int)(unsigned long) hostent); return NULL; } /* Create connection structure to use. This will allocate and place the connection in PENDING mode. */ connection = peisk_newConnection(); /* Prepare OS socket */ sock=socket(AF_INET,SOCK_STREAM,0); if(sock == -1) { perror("Failed to create a new socket\n"); return NULL; } peerAddr.sin_family = AF_INET; peerAddr.sin_port = htons(port); peerAddr.sin_addr.s_addr = INADDR_ANY; bzero(&(peerAddr.sin_zero),8); bcopy((char *)hostent->h_addr,(char *)&peerAddr.sin_addr,hostent->h_length); /* Set socket non blocking before connecting */ if(fcntl(sock,F_SETFL,O_NONBLOCK) == -1) { fprintf(stderr,"peisk: error setting socket nonblocking, not connecting\n"); close(sock); return NULL; } /* Start connecting */ connect(sock,(struct sockaddr*)&peerAddr,sizeof(struct sockaddr)); /** For now, wait and see if connection has succeeded within 100ms and continue afterwards. \todo Handle queued connection attempts better, eg. by placing them on a queue of pending connections */ timeout.tv_sec=0; timeout.tv_usec=100000; /* Use a timeout of 100ms */ FD_ZERO(&readSet); FD_ZERO(&writeSet); FD_ZERO(&exepSet); FD_SET(sock,&writeSet); retval=select(sock+1, &readSet, &writeSet, &exepSet, &timeout); if(retval <= 0) { if(peisk_printLevel & PEISK_PRINT_CONNECTIONS) fprintf(stdout,"peisk: failed to connect to %s:%d.\n Select returned: %d\n",name,port,retval); return NULL; } retlen=sizeof(int); if(getsockopt(sock,SOL_SOCKET,SO_ERROR,&retval,&retlen) == -1) { if(peisk_printLevel & PEISK_PRINT_CONNECTIONS) fprintf(stdout,"peisk: failed to connect to %s:%d.\n Getsockopt failed, errno: %d\n",name,port,errno); return NULL; } if(retval) { if(peisk_printLevel & PEISK_PRINT_CONNECTIONS) fprintf(stdout, "peisk: failed to connect to %s:%d.\n SO_ERROR is %d\n", name,port,retval); return NULL; } connection->type=eTCPConnection; connection->connection.tcp.socket=sock; PeisConnectMessage message; peisk_initConnectMessage(&message,flags); send(sock,&message,sizeof(message),MSG_NOSIGNAL); peisk_outgoingConnectFinished(connection,flags); if(peisk_printLevel & PEISK_PRINT_CONNECTIONS) fprintf(stdout,"peisk: new outbound tcp/ip connection #%d to %s:%d established, flags=%d\n", connection->id,name,port,flags); return connection; }