int gdbr_attach(libgdbr_t *g, int pid) { int ret; char *cmd; size_t buffer_size; if (!g || !g->sock) { return -1; } g->stop_reason.is_valid = false; reg_cache.valid = false; if (g->stub_features.extended_mode == -1) { gdbr_check_extended_mode (g); } if (!g->stub_features.extended_mode) { // vAttach needs extended mode to do anything. return -2; } buffer_size = strlen (CMD_ATTACH) + (sizeof (int) * 2) + 1; cmd = calloc (buffer_size, sizeof (char)); if (!cmd) { return -1; } ret = snprintf (cmd, buffer_size, "%s%x", CMD_ATTACH, pid); if (ret < 0) { free (cmd); return ret; } ret = send_msg (g, cmd); free(cmd); if (ret < 0) { return ret; } if (read_packet (g, false) >= 0) { return handle_attach (g); } return -1; }
void usbip_run (const USB_DEVICE_DESCRIPTOR *dev_dsc) /* simple TCP server */ { struct sockaddr_in serv, cli; int listenfd, sockfd, nb; #ifdef LINUX unsigned int clilen; #else int clilen; #endif unsigned char attached; #ifndef LINUX WSAStartup (wVersionRequested, &wsaData); if (wsaData.wVersion != wVersionRequested) { fprintf (stderr, "\n Wrong version\n"); exit (-1); } #endif if ((listenfd = socket (PF_INET, SOCK_STREAM, 0)) < 0) { printf ("socket error : %s \n", strerror (errno)); exit (1); }; int reuse = 1; if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) < 0) perror("setsockopt(SO_REUSEADDR) failed"); memset (&serv, 0, sizeof (serv)); serv.sin_family = AF_INET; serv.sin_addr.s_addr = htonl (INADDR_ANY); serv.sin_port = htons (TCP_SERV_PORT); if (bind (listenfd, (sockaddr *) & serv, sizeof (serv)) < 0) { printf ("bind error : %s \n", strerror (errno)); exit (1); }; if (listen (listenfd, SOMAXCONN) < 0) { printf ("listen error : %s \n", strerror (errno)); exit (1); }; for (;;) { clilen = sizeof (cli); if ( (sockfd = accept (listenfd, (sockaddr *) & cli, & clilen)) < 0) { printf ("accept error : %s \n", strerror (errno)); exit (1); }; printf("Connection address:%s\n",inet_ntoa(cli.sin_addr)); attached=0; while(1) { if(! attached) { OP_REQ_DEVLIST req; if ((nb = recv (sockfd, (char *)&req, sizeof(OP_REQ_DEVLIST), 0)) != sizeof(OP_REQ_DEVLIST)) { //printf ("receive error : %s \n", strerror (errno)); break; }; #ifdef _DEBUG print_recv((char *)&req, sizeof(OP_REQ_DEVLIST),"OP_REQ_DEVLIST"); #endif req.command=ntohs(req.command); printf("Header Packet\n"); printf("command: 0x%02X\n",req.command); if(req.command == 0x8005) { OP_REP_DEVLIST list; printf("list of devices\n"); handle_device_list(dev_dsc,&list); if (send (sockfd, (char *)&list.header, sizeof(OP_REP_DEVLIST_HEADER), 0) != sizeof(OP_REP_DEVLIST_HEADER)) { printf ("send error : %s \n", strerror (errno)); break; }; if (send (sockfd, (char *)&list.device, sizeof(OP_REP_DEVLIST_DEVICE), 0) != sizeof(OP_REP_DEVLIST_DEVICE)) { printf ("send error : %s \n", strerror (errno)); break; }; if (send (sockfd, (char *)list.interfaces, sizeof(OP_REP_DEVLIST_INTERFACE)*list.device.bNumInterfaces, 0) != sizeof(OP_REP_DEVLIST_INTERFACE)*list.device.bNumInterfaces) { printf ("send error : %s \n", strerror (errno)); break; }; free(list.interfaces); } else if(req.command == 0x8003) { char busid[32]; OP_REP_IMPORT rep; printf("attach device\n"); if ((nb = recv (sockfd, busid, 32, 0)) != 32) { printf ("receive error : %s \n", strerror (errno)); break; }; #ifdef _DEBUG print_recv(busid, 32,"Busid"); #endif handle_attach(dev_dsc,&rep); if (send (sockfd, (char *)&rep, sizeof(OP_REP_IMPORT), 0) != sizeof(OP_REP_IMPORT)) { printf ("send error : %s \n", strerror (errno)); break; }; attached = 1; } } else { printf("------------------------------------------------\n"); printf("handles requests\n"); USBIP_CMD_SUBMIT cmd; USBIP_RET_SUBMIT usb_req; if ((nb = recv (sockfd, (char *)&cmd, sizeof(USBIP_CMD_SUBMIT), 0)) != sizeof(USBIP_CMD_SUBMIT)) { printf ("receive error : %s \n", strerror (errno)); break; }; #ifdef _DEBUG print_recv((char *)&cmd, sizeof(USBIP_CMD_SUBMIT),"USBIP_CMD_SUBMIT"); #endif unpack((int *)&cmd,sizeof(USBIP_CMD_SUBMIT)); printf("usbip cmd %u\n",cmd.command); printf("usbip seqnum %u\n",cmd.seqnum); printf("usbip devid %u\n",cmd.devid); printf("usbip direction %u\n",cmd.direction); printf("usbip ep %u\n",cmd.ep); printf("usbip flags %u\n",cmd.transfer_flags); printf("usbip number of packets %u\n",cmd.number_of_packets); printf("usbip interval %u\n",cmd.interval); #ifdef LINUX printf("usbip setup %llu\n",cmd.setup); #else printf("usbip setup %I64u\n",cmd.setup); #endif printf("usbip buffer lenght %u\n",cmd.transfer_buffer_length); usb_req.command=0; usb_req.seqnum=cmd.seqnum; usb_req.devid=cmd.devid; usb_req.direction=cmd.direction; usb_req.ep=cmd.ep; usb_req.status=0; usb_req.actual_length=0; usb_req.start_frame=0; usb_req.number_of_packets=0; usb_req.error_count=0; usb_req.setup=cmd.setup; if(cmd.command == 1) handle_usb_request(sockfd, &usb_req, cmd.transfer_buffer_length); if(cmd.command == 2) //unlink urb { printf("####################### Unlink URB %u (not working!!!)\n",cmd.transfer_flags); //FIXME /* USBIP_RET_UNLINK ret; printf("####################### Unlink URB %u\n",cmd.transfer_flags); ret.command=htonl(0x04); ret.devid=htonl(cmd.devid); ret.direction=htonl(cmd.direction); ret.ep=htonl(cmd.ep); ret.seqnum=htonl(cmd.seqnum); ret.status=htonl(1); if (send (sockfd, (char *)&ret, sizeof(USBIP_RET_UNLINK), 0) != sizeof(USBIP_RET_UNLINK)) { printf ("send error : %s \n", strerror (errno)); exit(-1); }; */ } if(cmd.command > 2) { printf("Unknown USBIP cmd!\n"); close (sockfd); #ifndef LINUX WSACleanup (); #endif return; }; } } close (sockfd); }; #ifndef LINUX WSACleanup (); #endif };