+void pcap(int fd, FILE *out) {
+ sendline(fd, "%d %d", CONTROL, REQ_PCAP);
+ char data[9018];
+
+ struct {
+ uint32_t magic;
+ uint16_t major;
+ uint16_t minor;
+ uint32_t tz_offset;
+ uint32_t tz_accuracy;
+ uint32_t snaplen;
+ uint32_t ll_type;
+ } header = {
+ 0xa1b2c3d4,
+ 2, 4,
+ 0, 0,
+ sizeof data,
+ 1,
+ };
+
+ struct {
+ uint32_t tv_sec;
+ uint32_t tv_usec;
+ uint32_t len;
+ uint32_t origlen;
+ } packet;
+
+ struct timeval tv;
+
+ fwrite(&header, sizeof header, 1, out);
+ fflush(out);
+
+ char line[32];
+ while(recvline(fd, line, sizeof line)) {
+ int code, req, len;
+ int n = sscanf(line, "%d %d %d", &code, &req, &len);
+ gettimeofday(&tv, NULL);
+ if(n != 3 || code != CONTROL || req != REQ_PCAP || len < 0 || len > sizeof data)
+ break;
+ if(!recvdata(fd, data, len))
+ break;
+ packet.tv_sec = tv.tv_sec;
+ packet.tv_usec = tv.tv_usec;
+ packet.len = len;
+ packet.origlen = len;
+ fwrite(&packet, sizeof packet, 1, out);
+ fwrite(data, len, 1, out);
+ fflush(out);
+ }
+}
+
+#ifdef HAVE_MINGW
+static bool remove_service(void) {
+ SC_HANDLE manager = NULL;
+ SC_HANDLE service = NULL;
+ SERVICE_STATUS status = {0};
+
+ manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
+ if(!manager) {
+ fprintf(stderr, "Could not open service manager: %s\n", winerror(GetLastError()));
+ return false;
+ }
+
+ service = OpenService(manager, identname, SERVICE_ALL_ACCESS);
+
+ if(!service) {
+ fprintf(stderr, "Could not open %s service: %s\n", identname, winerror(GetLastError()));
+ return false;
+ }
+
+ if(!ControlService(service, SERVICE_CONTROL_STOP, &status))
+ fprintf(stderr, "Could not stop %s service: %s\n", identname, winerror(GetLastError()));
+ else
+ fprintf(stderr, "%s service stopped\n", identname);
+
+ if(!DeleteService(service)) {
+ fprintf(stderr, "Could not remove %s service: %s\n", identname, winerror(GetLastError()));
+ return false;
+ }
+
+ fprintf(stderr, "%s service removed\n", identname);
+
+ return true;
+}
+#endif
+