static int write_block(unsigned char tr, unsigned char se, const unsigned char *blk, int size, int read_status) { int i = 0; unsigned char status[2]; SETSTATEDEBUG((void)0); status[0] = tr; status[1] = se; write_n(status, 2); SETSTATEDEBUG((void)0); /* send first byte twice if length is odd */ if(size % 2) { write_n(blk, 2); i = 1; } SETSTATEDEBUG(debugLibImgByteCount=0); write_n(blk+i, size-i); SETSTATEDEBUG(debugLibImgByteCount=-1); #ifndef USE_CBM_IEC_WAIT if(size == BLOCKSIZE) { arch_usleep(20000); } #endif SETSTATEDEBUG((void)0); read_n(status, 2); SETSTATEDEBUG((void)0); return status[1]; }
// Write one cv::Mat to file bool write_one(FILE * file, const cv::Mat & data) { bool okay = true; okay &= write_one(file, int32_t(data.rows)); okay &= write_one(file, int32_t(data.cols)); okay &= write_one(file, uint32_t(data.type())); // If matrix memory is continuous, we can reshape the matrix int rows = data.rows, cols = data.cols; if (data.isContinuous()) { cols = rows*cols; rows = 1; } // Currently only supports float/double matrices! assert(data.depth() == CV_32F || data.depth() == CV_64F); if (data.depth() == CV_32F) for (int r = 0; r < rows; ++r) okay &= write_n(file, data.ptr<float>(r), cols); else if (data.depth() == CV_64F) for (int r = 0; r < rows; ++r) okay &= write_n(file, data.ptr<double>(r), cols); else return false; return okay; }
/* Process the data from socket and pseudo tty */ static int process_data(int fd) { struct pollfd p[2]; char buf[1024]; int err, r; p[0].fd = 0; p[0].events = POLLIN | POLLERR | POLLHUP | POLLNVAL; p[1].fd = fd; p[1].events = POLLIN | POLLERR | POLLHUP | POLLNVAL; err = 0; while (!__io_canceled) { p[0].revents = 0; p[1].revents = 0; err = poll(p, 2, -1); if (err < 0) break; err = 0; if (p[0].revents) { if (p[0].revents & (POLLERR | POLLHUP | POLLNVAL)) break; r = read(0, buf, sizeof(buf)); if (r < 0) { if (errno != EINTR && errno != EAGAIN) { err = r; break; } } err = write_n(fd, buf, r); if (err < 0) break; } if (p[1].revents) { if (p[1].revents & (POLLERR | POLLHUP | POLLNVAL)) break; r = read(fd, buf, sizeof(buf)); if (r < 0) { if (errno != EINTR && errno != EAGAIN) { err = r; break; } } err = write_n(1, buf, r); if (err < 0) break; } } return err; }
static int write_string (int fd, const char *str) { int len = strlen (str); int rc; if (write_n (fd, &len, sizeof (int)) < 0) { fprintf (stderr, "systemsafe: write: %s\n", strerror (errno)); return (-1); } rc = write_n (fd, str, len); return (rc); }
static void process_msg(msg_t* msg, int localfd) { void* buffer = NULL; unsigned short len; int sys; size_t room_id; if (parse_msg(msg, &sys, &buffer, &len, &room_id)) { if (sys) client_process_sys(msg, buffer, len); else { ssize_t written = write_n(localfd, buffer, len); SYSLOG(LOG_INFO, "write local length: %ld", written); } } else { SYSLOG(LOG_WARNING, "Parse message error"); return; } if (buffer) pool_room_free(&this.pool, room_id); this.client.status = (this.client.status & ~CLIENT_STATUS_WAITING_BODY) | CLIENT_STATUS_WAITING_HEADER; this.client.want = sizeof(msg_t); this.client.read = this.client.buffer; }
int main(int argc, char* argv[]) { process_arg(argc,argv); tcpudp_fd=open_socket(); assert(tcpudp_fd>0); if(server) server_accept(NULL); tun_fd=tun_alloc(tun_name,IFF_TAP); assert(tun_fd>0); get_macaddr(); write_n(tcpudp_fd,tun_mac,6); getack(); if(pthread_create(&pt_read_from_if,NULL,read_from_if,NULL)!=0){ perror("pthread_create"); exit(-1); } if(pthread_create(&pt_read_from_sock,NULL,read_from_sock,NULL)!=0){ perror("pthread_create"); exit(-1); } printf("engin started, main() going to sleep\n"); pthread_join(pt_read_from_if,NULL); pthread_join(pt_read_from_sock,NULL); return 0; }
int send_frame(pstream_head_t psh, pcodec_head_t pch, const void *buffer, int size) { unsigned char *ptr = send_buffer; memcpy(ptr, psh, sizeof (*psh)); ptr += sizeof (*psh); memcpy(ptr, pch, sizeof (*pch)); ptr += sizeof (*pch); memcpy(ptr, buffer, size); ptr += size; memcpy(ptr, &sync_end_code, sizeof (sync_end_code)); ptr += sizeof (sync_end_code); // if (psh == &video_sh) // { // LOGD("video frame %lu before sending: %lu", video_ch.serial_num, clock()); // } // else // { // LOGD("audio frame %lu before sending: %lu", audio_ch.serial_num, clock()); // } return write_n(sockfd, send_buffer, ptr - send_buffer); }
int main(int argc, char* argv[]) { if (argc > 1) { int i, n = atoi(argv[1]); if (argc == 2) { for (i = 0; i < n; ++i) { write(i); } } else if (argv[2][1] == 'e') { for (i = 0; i < n; ++i) { write_e(i); } } else if (argv[2][1] == 'c') { for (i = 0; i < n; ++i) { write_c(i); } } else if (argv[2][1] == 'n') { for (i = 0; i < n; ++i) { write_n(i); } } } return 0; }
static void hci_acl_data(uint8_t *data) { hci_acl_hdr *ah = (void *) data; struct vhci_conn *conn; uint16_t handle; int fd; handle = acl_handle(btohs(ah->handle)); if (handle > VHCI_MAX_CONN || !(conn = vconn[handle - 1])) { syslog(LOG_ERR, "Bad connection handle %d", handle); return; } fd = g_io_channel_unix_get_fd(conn->chan); if (write_n(fd, data, btohs(ah->dlen) + HCI_ACL_HDR_SIZE) < 0) { close_connection(conn); return; } if (++vdev.acl_cnt > VHCI_ACL_MAX_PKT - 1) { /* Send num of complete packets event */ num_completed_pkts(conn); vdev.acl_cnt = 0; } }
void client_loop(int remotefd, int localfd) { fd_set set; int max; this.client.status = CLIENT_STATUS_NORMAL | CLIENT_STATUS_WAITING_HEADER; this.client.want = sizeof(msg_t); this.client.buffer = this.client.read = pool_room_alloc(&this.pool, RECV_ROOM_IDX, this.client.want); int keepalive_send = 0; int rc; if (this.client.buffer == NULL) { fprintf(stderr, "Not enough memory\n"); return; } this.keepalive_replyed = 1; while (1) { struct timeval tv = {1, 0}; FD_ZERO(&set); FD_SET(remotefd, &set); FD_SET(localfd, &set); max = remotefd > localfd ? remotefd : localfd; if (this.keepalive_replyed && (time(NULL) - this.keepalive) > KEEPALIVE_INTERVAL) { msg_t* msg = new_keepalive_msg(1); write_n(remotefd, msg, sizeof(msg_t)); printf("send keepalive message\n"); this.keepalive = time(NULL); this.keepalive_replyed = 0; pool_room_free(&this.pool, MSG_ROOM_IDX); keepalive_send = 1; } max = select(max + 1, &set, NULL, NULL, &tv); if (max > 0) { rc = client_process(max, &set, remotefd, localfd); switch (rc) { case RETURN_CONNECTION_CLOSED: case RETURN_READ_ERROR: pool_room_free(&this.pool, RECV_ROOM_IDX); return; } } if (keepalive_send && !this.keepalive_replyed && (time(NULL) - this.keepalive) > KEEPALIVE_TIMEOUT) { fprintf(stderr, "keepalive reply timeouted, connection closed\n"); pool_room_free(&this.pool, RECV_ROOM_IDX); return; } } }
int tcp_write(int fd, char *buf, int len) { register char *ptr; ptr = buf - sizeof(short); *((unsigned short *)ptr) = htons(len); len = (len & VTUN_FSIZE_MASK) + sizeof(short); return write_n(fd, ptr, len); }
void send_coord(void *buf, int len) { if (current_proc == PROC_COORD) { pinfo(PINFO_WARN, FALSE, "coordinator sending to itself?"); return; } if (write_n(coord_fd, buf, len) == -1) { pinfo(PINFO_FATAL, TRUE, "write() to COORD failed"); srvr_term(1); } }
static void client_process(int max, fd_set* set, int remotefd, int localfd) { unsigned char buffer[1024] = {0}; ssize_t readen; if (FD_ISSET(localfd, set)) { readen = read(localfd, buffer, sizeof(buffer)); if (readen > 0) write_n(remotefd, buffer, readen); } if (FD_ISSET(remotefd, set)) { readen = read(remotefd, buffer, sizeof(buffer)); if (readen > 0) write_n(localfd, buffer, readen); else { fprintf(stderr, "read error\n"); close(remotefd); exit(1); } } }
/* * Print padded messages. * Used by 'auth' function to force all messages * to be the same len. */ int print_p(int fd,const char *fmt, ...) { char buf[VTUN_MESG_SIZE]; va_list ap; memset(buf,0,sizeof(buf)); /* print the argument string */ va_start(ap, fmt); vsnprintf(buf,sizeof(buf)-1, fmt, ap); va_end(ap); return write_n(fd, buf, sizeof(buf)); }
static void accept_and_check(int bindfd) { int fd = accept(bindfd, NULL, NULL); char buffer[sizeof(CLIENT_AUTH_MSG) - 1]; ssize_t readen; if (fd == -1) return; write_n(fd, SERVER_AUTH_MSG, sizeof(SERVER_AUTH_MSG) - 1); readen = read_t(fd, buffer, sizeof(buffer), 5); if (readen <= 0) { struct sockaddr_in addr; socklen_t len = sizeof(addr); char* str; if (getpeername(fd, (struct sockaddr*)&addr, &len) == -1) { perror("getpeername"); close(fd); return; } str = inet_ntoa(addr.sin_addr); fprintf(stderr, "authcheck failed: %s\n", str); close(fd); return; } if (strncmp(buffer, CLIENT_AUTH_MSG, sizeof(CLIENT_AUTH_MSG) - 5) == 0) { memcpy(&client_id, &buffer[sizeof(CLIENT_AUTH_MSG) - sizeof(client_id) - 2], sizeof(client_id)); client_id = ntohl(client_id); connfd = fd; } else { struct sockaddr_in addr; socklen_t len = sizeof(addr); char* str; if (getpeername(fd, (struct sockaddr*)&addr, &len) == -1) { perror("getpeername"); close(fd); return; } str = inet_ntoa(addr.sin_addr); fprintf(stderr, "authcheck failed: %s\n", str); close(fd); } }
int connect_server(char* ip, unsigned short port) { int fd, rc; struct sockaddr_in addr = {0}; unsigned char buffer[1024] = {0}; ssize_t readen; fd = socket(AF_INET, SOCK_STREAM, 0); if (fd == -1) { perror("socket"); return -1; } addr.sin_family = AF_INET; addr.sin_port = htons(port); if (inet_aton(ip, &addr.sin_addr) == 0) { fprintf(stderr, "Convert ip address error!\n"); close(fd); return -1; } rc = connect(fd, (struct sockaddr*)&addr, sizeof(addr)); if (rc == -1) { perror("connect"); close(fd); return -1; } readen = read_t(fd, buffer, sizeof(buffer), 5); if (readen > 0 && strcmp(SERVER_AUTH_MSG, buffer) == 0) { pid_t pid = getpid(); strcpy(buffer, CLIENT_AUTH_MSG); client_id = pid; client_id = htonl(client_id); memcpy(&buffer[sizeof(CLIENT_AUTH_MSG) - sizeof(client_id) - 1], &client_id, sizeof(client_id)); write_n(fd, buffer, sizeof(CLIENT_AUTH_MSG) - 1); } else { fprintf(stderr, "is not allowed server\n"); close(fd); return -1; } return fd; }
static void server_process(int max, fd_set* set, int remotefd, int localfd) { unsigned char buffer[1024] = {0}; ssize_t readen; if (FD_ISSET(remotefd, set)) { if (connfd == -1) accept_and_check(remotefd); else { int fd = accept(remotefd, NULL, NULL); if (fd != -1) { fprintf(stderr, "I can only process 1 client!\n"); close(fd); } } } if (connfd != -1 && FD_ISSET(connfd, set)) { readen = read(connfd, buffer, sizeof(buffer)); if (readen > 0) { write_n(localfd, buffer, readen); } else { close(connfd); connfd = -1; } } if (FD_ISSET(localfd, set)) { readen = read(localfd, buffer, sizeof(buffer)); if (connfd != -1 && readen > 0) write_n(connfd, buffer, readen); } }
void* read_from_sock(void* none){ struct pktdata_t recvpktdata; unsigned int size; while(1){ read_n(tcpudp_fd,(char*)&recvpktdata.len,4); size=ntohl(recvpktdata.len); if(size>MAX_PKT){ printf("size received is %d,greater then MAX_PKT\n",size); pthread_cancel(pt_read_from_if); pthread_exit(none); } printf("read %d bytes from tcp sock\n",size); read_n(tcpudp_fd,recvpktdata.data,size); write_n(tun_fd,recvpktdata.data,size); } return none; }
void reply_echo(int fd, struct iphdr* ipHdr) { unsigned char ipHdrLen = ipHdr->ihl << 2; struct icmphdr* icmpHdr = (struct icmphdr*)((char*)ipHdr + ipHdrLen); // 跳过IP头和可选头 unsigned short ipLen = ntohs(ipHdr->tot_len) - ipHdrLen; __be32 tmp = ipHdr->saddr; ipHdr->saddr = ipHdr->daddr; ipHdr->daddr = tmp; ipHdr->check = 0; ipHdr->check = checksum(ipHdr, ipHdrLen); icmpHdr->type = ICMP_ECHOREPLY; icmpHdr->checksum = 0; icmpHdr->checksum = checksum(icmpHdr, ipLen); write_n(fd, ipHdr, ipHdrLen + ipLen); }
static int send_track_map(imgcopy_settings *settings, unsigned char tr, const char *trackmap, unsigned char count) { int i, size; unsigned char *data; size = imgcopy_sector_count(settings, tr); data = malloc(2+2*size); data[0] = tr; data[1] = count; /* build track map */ for(i = 0; i < size; i++) data[2+2*i] = data[2+2*i+1] = !NEED_SECTOR(trackmap[i]); write_n(data, 2*size+2); free(data); SETSTATEDEBUG((void)0); return 0; }
static void handle_system_request (int fd) { char *cmd, *path, **env, **oldenv; int rc; if ((rc = read_string (fd, &cmd)) < 0) { fprintf (stderr, "systemsafe: read cmd: %s\n", strerror (errno)); exit (0); } if (rc == 0) /* EOF, time to exit */ exit (0); if (read_string (fd, &path) < 0) { fprintf (stderr, "systemsafe: read path: %s\n", strerror (errno)); exit (0); } if (read_env (fd, &env) < 0) { fprintf (stderr, "systemsafe: read env: %s\n", strerror (errno)); exit (0); } if (chdir (path) < 0) fprintf (stderr, "systemsafe: Failed to chdir to %s: %s\n", path, strerror (errno)); oldenv = environ; environ = env; rc = (*real_system) (cmd); write_n (fd, &rc, sizeof (int)); environ = oldenv; free_env (env); free (cmd); free (path); return; }
static void save_link_key(struct link_key *key) { struct link_key *exist; char sa[18], da[18]; int f, err; f = open(hcid.key_file, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); if (f < 0) { syslog(LOG_ERR, "Link key database open failed: %s (%d)", strerror(errno), errno); return; } /* Check if key already exist */ exist = __get_link_key(f, &key->sba, &key->dba); err = 0; if (exist) { off_t o = lseek(f, 0, SEEK_CUR); err = lseek(f, o - sizeof(*key), SEEK_SET); } else err = fcntl(f, F_SETFL, O_APPEND); if (err < 0) { syslog(LOG_ERR, "Link key database seek failed: %s (%d)", strerror(errno), errno); goto failed; } if (write_n(f, key, sizeof(*key)) < 0) { syslog(LOG_ERR, "Link key database write failed: %s (%d)", strerror(errno), errno); } ba2str(&key->sba, sa); ba2str(&key->dba, da); syslog(LOG_INFO, "%s link key %s %s", exist ? "Replacing" : "Saving", sa, da); failed: close(f); }
static int read_block(unsigned char tr, unsigned char se, unsigned char *block) { unsigned char status[2]; SETSTATEDEBUG((void)0); status[0] = tr; status[1] = se; write_n(status, 2); #ifndef USE_CBM_IEC_WAIT arch_usleep(20000); #endif SETSTATEDEBUG((void)0); read_n(status, 2); SETSTATEDEBUG(debugLibImgByteCount=0); read_n(block, BLOCKSIZE); SETSTATEDEBUG(debugLibImgByteCount=-1); SETSTATEDEBUG((void)0); return status[1]; }
int main(int argc, char ** argv) { (void)argc; long n = atol(argv[1]); int low,high; long i; double t; if (!pow2check(n)) { fprintf(stderr, "error : n (%ld) not a power of two\n", n); exit(1); } sample_t * buf = calloc(sizeof(sample_t), n); complex double * X = calloc(sizeof(complex double), n); complex double * Y = calloc(sizeof(complex double), n); low = atoi(argv[2]); high = atoi(argv[3]); while (1) { /* 標準入力からn個標本を読む */ ssize_t m = read_n(0, n * sizeof(sample_t), buf); if (m == 0) break; /* 複素数の配列に変換 */ sample_to_complex(buf, X, n); /* FFT -> Y */ fft(X, Y, n); // Yのバンド外を0にする // t = (double) n / SAMPLING_FREQEUENCY; // for (i = 0; i < n; ++i){ // if(i/t<low || i/t>high){ // Y[i] = 0; // } // } /* IFFT -> Z */ ifft(Y, X, n); /* 標本の配列に変換 */ complex_to_sample(Y, buf, n); /* 標準出力へ出力 */ write_n(1, m, buf); } return 0; }
static int check_too_many_consoles(req_t *req) { /* Checks to see if the request matches too many consoles * for the given command. * A MONITOR command can only affect a single console, as can a * CONNECT command unless the broadcast option is enabled. * Returns 0 if the request is valid, or -1 on error. */ ListIterator i; obj_t *obj; char buf[MAX_SOCK_LINE]; assert(!list_is_empty(req->consoles)); if (req->command == CONMAN_CMD_QUERY) return(0); if (list_count(req->consoles) == 1) return(0); if ((req->command == CONMAN_CMD_CONNECT) && (req->enableBroadcast)) return(0); snprintf(buf, sizeof(buf), "Found %d matching consoles", list_count(req->consoles)); send_rsp(req, CONMAN_ERR_TOO_MANY_CONSOLES, buf); /* FIXME? Replace with single write_n()? */ i = list_iterator_create(req->consoles); while ((obj = list_next(i))) { strlcpy(buf, obj->name, sizeof(buf)); strlcat(buf, "\n", sizeof(buf)); if (write_n(req->sd, buf, strlen(buf)) < 0) { log_msg(LOG_NOTICE, "Unable to write to <%s:%d>: %s", req->fqdn, req->port, strerror(errno)); break; } } list_iterator_destroy(i); return(-1); }
gint main (gint argc, gchar **argv) { struct _CamelLockHelperMsg msg; gint len; gint res; gchar *path; fd_set rset; struct timeval tv; struct _lock_info *info; setup_process (); do { /* do a poll/etc, so we can refresh the .locks as required ... */ FD_ZERO (&rset); FD_SET (STDIN_FILENO, &rset); /* check the minimum timeout we need to refresh the next oldest lock */ if (lock_info_list) { time_t now = time (NULL); time_t left; time_t delay = CAMEL_DOT_LOCK_REFRESH; info = lock_info_list; while (info) { left = CAMEL_DOT_LOCK_REFRESH - (now - info->stamp); left = MAX (left, 0); delay = MIN (left, delay); info = info->next; } tv.tv_sec = delay; tv.tv_usec = 0; } d (fprintf (stderr, "lock helper waiting for input\n")); if (select (STDIN_FILENO + 1, &rset, NULL, NULL, lock_info_list ? &tv : NULL) == -1) { if (errno == EINTR) break; continue; } /* did we get a timeout? scan for any locks that need updating */ if (!FD_ISSET (STDIN_FILENO, &rset)) { time_t now = time (NULL); time_t left; d (fprintf (stderr, "Got a timeout, checking locks\n")); info = lock_info_list; while (info) { left = (now - info->stamp); if (left >= CAMEL_DOT_LOCK_REFRESH) { lock_touch (info->path); info->stamp = now; } info = info->next; } continue; } len = read_n (STDIN_FILENO, &msg, sizeof (msg)); if (len == 0) break; res = CAMEL_LOCK_HELPER_STATUS_PROTOCOL; if (len == sizeof (msg) && msg.magic == CAMEL_LOCK_HELPER_MAGIC) { switch (msg.id) { case CAMEL_LOCK_HELPER_LOCK: res = CAMEL_LOCK_HELPER_STATUS_NOMEM; if (msg.data > 0xffff) { res = CAMEL_LOCK_HELPER_STATUS_PROTOCOL; } else if ((path = malloc (msg.data + 1)) != NULL) { res = CAMEL_LOCK_HELPER_STATUS_PROTOCOL; len = read_n (STDIN_FILENO, path, msg.data); if (len == msg.data) { path[len] = 0; res = lock_path (path, &msg.data); } free (path); } break; case CAMEL_LOCK_HELPER_UNLOCK: res = unlock_id (msg.data); break; } } d (fprintf (stderr, "returning result %d\n", res)); msg.id = res; msg.magic = CAMEL_LOCK_HELPER_RETURN_MAGIC; write_n (STDOUT_FILENO, &msg, sizeof (msg)); } while (1); d (fprintf (stderr, "parent exited, clsoing down remaining id's\n")); while (lock_info_list) unlock_id (lock_info_list->id); return 0; }
static int process_frames(int dev, int sock, int fd, unsigned long flags) { struct cmsghdr *cmsg; struct msghdr msg; struct iovec iv; struct hcidump_hdr *dh; struct btsnoop_pkt *dp; struct frame frm; struct pollfd fds[2]; int nfds = 0; char *buf, *ctrl; int len, hdr_size = HCIDUMP_HDR_SIZE; if (sock < 0) return -1; if (snap_len < SNAP_LEN) snap_len = SNAP_LEN; if (flags & DUMP_BTSNOOP) hdr_size = BTSNOOP_PKT_SIZE; buf = malloc(snap_len + hdr_size); if (!buf) { perror("Can't allocate data buffer"); return -1; } dh = (void *) buf; dp = (void *) buf; frm.data = buf + hdr_size; ctrl = malloc(100); if (!ctrl) { free(buf); perror("Can't allocate control buffer"); return -1; } if (dev == HCI_DEV_NONE) printf("system: "); else printf("device: hci%d ", dev); printf("snap_len: %d filter: 0x%lx\n", snap_len, parser.filter); memset(&msg, 0, sizeof(msg)); fds[nfds].fd = sock; fds[nfds].events = POLLIN; fds[nfds].revents = 0; nfds++; while (1) { int i, n = poll(fds, nfds, -1); if (n <= 0) continue; for (i = 0; i < nfds; i++) { if (fds[i].revents & (POLLHUP | POLLERR | POLLNVAL)) { if (fds[i].fd == sock) printf("device: disconnected\n"); else printf("client: disconnect\n"); return 0; } } iv.iov_base = frm.data; iv.iov_len = snap_len; msg.msg_iov = &iv; msg.msg_iovlen = 1; msg.msg_control = ctrl; msg.msg_controllen = 100; len = recvmsg(sock, &msg, MSG_DONTWAIT); if (len < 0) { if (errno == EAGAIN || errno == EINTR) continue; perror("Receive failed"); return -1; } /* Process control message */ frm.data_len = len; frm.dev_id = dev; frm.in = 0; frm.pppdump_fd = parser.pppdump_fd; frm.audio_fd = parser.audio_fd; cmsg = CMSG_FIRSTHDR(&msg); while (cmsg) { int dir; switch (cmsg->cmsg_type) { case HCI_CMSG_DIR: memcpy(&dir, CMSG_DATA(cmsg), sizeof(int)); frm.in = (uint8_t) dir; break; case HCI_CMSG_TSTAMP: memcpy(&frm.ts, CMSG_DATA(cmsg), sizeof(struct timeval)); break; } cmsg = CMSG_NXTHDR(&msg, cmsg); } frm.ptr = frm.data; frm.len = frm.data_len; switch (mode) { case WRITE: /* Save or send dump */ if (flags & DUMP_BTSNOOP) { uint64_t ts; uint8_t pkt_type = ((uint8_t *) frm.data)[0]; dp->size = htobe32(frm.data_len); dp->len = dp->size; dp->flags = be32toh(frm.in & 0x01); dp->drops = 0; ts = (frm.ts.tv_sec - 946684800ll) * 1000000ll + frm.ts.tv_usec; dp->ts = htobe64(ts + 0x00E03AB44A676000ll); if (pkt_type == HCI_COMMAND_PKT || pkt_type == HCI_EVENT_PKT) dp->flags |= be32toh(0x02); } else { dh->len = htobs(frm.data_len); dh->in = frm.in; dh->ts_sec = htobl(frm.ts.tv_sec); dh->ts_usec = htobl(frm.ts.tv_usec); } if (write_n(fd, buf, frm.data_len + hdr_size) < 0) { perror("Write error"); return -1; } break; default: /* Parse and print */ parse(&frm); break; } } return 0; }
static int ldap_deactivate_utmp(char *devnam) { int rc; int fd; off_t offset; struct ppp_utmp entry; char *device; char *p; if ((device = malloc(MAXPATHLEN)) == NULL) { error("Not enough memory\n"); return -1; } memset(device, 0, MAXPATHLEN); memset(&entry, 0, sizeof(struct ppp_utmp)); p = device; strncpy(device, devnam, MAXLINELEN-1); if(strncmp(device,"/dev/",5) == 0) p += 5; #ifdef DEBUG info("LDAP: deactivating %s\n",devnam); #endif if ((fd = open(UTMP, O_RDWR, 0600)) == -1){ error("LDAP: can't open utmp file: %s\n", strerror(errno)); return -1; } if ((rc = lockf(fd, F_LOCK, 0)) == -1){ error("LDAP: can't lock utmp file: %s\n", strerror(errno)); return -1; } while(read_n(fd, &entry, sizeof(struct ppp_utmp))) { if (strncmp(entry.line, p, MAXLINELEN-1) == 0) { entry.state = IDLE; lseek(fd, -sizeof(struct ppp_utmp), SEEK_CUR); if ((rc = write_n(fd, &entry, sizeof(struct ppp_utmp))) == -1) { error("LDAP: can't change utmp record status: %s\n", strerror(errno)); return -1; } } } free(device); lseek(fd, 0, SEEK_SET); if ((rc = lockf(fd, F_ULOCK, 0)) == -1){ error("LDAP: can't unlock utmp file: %s\n", strerror(errno)); return -1; } close(fd); return 1; }
static int ldap_activate_utmp(struct ldap_data *ldap_data, char *devnam, char *ppp_devname, char* user) { int rc; int fd; off_t offset; struct ppp_utmp entry; char *device; char *p; memset(&entry, 0, sizeof(struct ppp_utmp)); if ((device = malloc(MAXPATHLEN)) == NULL) { error("Not enough memory\n"); return -1; } memset(device, '\0', MAXPATHLEN); if ((fd = open(UTMP , O_RDWR | O_CREAT, 0644)) == -1) { error("LDAP: can't open utmp file\n"); return -1; } strncpy(device, devnam, MAXLINELEN-1); p = device; if(strncmp(device,"/dev/",5) == 0) p += 5; if ((rc = lockf(fd, F_LOCK, 0)) == -1) { error("LDAP: can't lock utmp file: %s\n", strerror(errno)); return -1; } switch ((offset = utmp_seek(fd, devnam))) { case -1: strncpy(entry.line, p, MAXLINELEN-1); strncpy(entry.login, user, MAXNAMELEN-1); strncpy(entry.ifname, ppp_devname, MAXIFLEN-1); if (!ldap_data->address_set) entry.ip_address = ipcp_wantoptions[0].hisaddr; else entry.ip_address = ldap_data->addr; entry.time = time(NULL); entry.state = ACTIVE; lseek(fd, 0, SEEK_END); if ((write_n(fd, &entry, sizeof(struct ppp_utmp))) == -1){ error("LDAP: failed to write utmp entry\n"); return -1; } break; default: lseek(fd, offset, SEEK_SET); read_n(fd,&entry,sizeof(struct ppp_utmp)); strncpy(entry.line, p, MAXLINELEN-1); strncpy(entry.login, user, MAXNAMELEN-1); strncpy(entry.ifname, ppp_devname, MAXIFLEN-1); if (!ldap_data->address_set) entry.ip_address = ipcp_wantoptions[0].hisaddr; else entry.ip_address = ldap_data->addr; entry.time = time(NULL); entry.state = ACTIVE; lseek(fd, offset, SEEK_SET); if ((write_n(fd, &entry, sizeof(struct ppp_utmp))) == -1){ error("LDAP: failed to write utmp entry\n"); return -1; } break; } free(device); lseek(fd, 0, SEEK_SET); if ((rc = lockf(fd, F_ULOCK, 0)) == -1) { error("LDAP: can't unlock utmp file: %s\n", strerror(errno)); return -1; } if ((rc = close(fd)) == -1) { error("LDAP: can't close utmp file: %s\n", strerror(errno)); return -1; } return 1; }
bool RoboClaw::SetPWMMode(uint8_t address, uint8_t mode){ return write_n(3,address,SETPWMMODE,mode); }