void * process_vxlan_control (void * param) { char * c; int socket, accept_socket; char buf[CONTROL_MSG_BUF_LEN]; fd_set fds; socket = create_unix_server_socket (VXLAN_UNIX_DOMAIN); vxlan.control_sock = socket; listen (socket, 1); while (1) { memset (buf, 0, sizeof (buf)); FD_ZERO (&fds); FD_SET (socket, &fds); pselect (socket + 1, &fds, NULL, NULL, NULL, NULL); if (!FD_ISSET (socket, &fds)) break; accept_socket = accept (socket, NULL, 0); if (read (accept_socket, buf, sizeof (buf)) < 0) { warn ("read(2) faild : control socket"); shutdown (accept_socket, 1); close (accept_socket); continue; } for (c = buf; *c == ' '; c++); exec_command_func[strtocmdtype (c)] (c, accept_socket); if (shutdown (accept_socket, SHUT_RDWR) != 0) { error_warn ("%s : shutdown : %s", __func__, strerror (errno)); } if (close (accept_socket) != 0) { error_warn ("%s : close : %s", __func__, strerror (errno)); } } shutdown (vxlan.control_sock, SHUT_RDWR); if (close (vxlan.control_sock) < 0) error_warn ("%s : close control socket failed : %s", strerror (errno)); /* not reached */ return NULL; }
int main(void) { int sfd, cfd, bytes, ret; char buf[16]; buf[15] = 0; ret = sfd = create_unix_server_socket("/tmp/echosock",STREAM,0); if ( ret < 0 ) { perror(0); exit(1); } for ( ;; ) { ret = cfd = accept_unix_stream_socket(sfd,0); if ( ret < 0 ) { perror(0); exit(1); } while ( 0 < ( bytes = read(cfd,buf,15) ) ) { write(cfd,buf,bytes); write(1,buf,bytes); } ret = destroy_unix_socket(cfd); if ( ret < 0 ) { perror(0); exit(1); } } ret = destroy_unix_socket(sfd); if ( ret < 0 ) { perror(0); exit(1); } return 0; }
int main(void) { int sfd, bytes, ret; char buf[128]; char from[128]; memset(buf,0,128); memset(from,0,128); ret = sfd = create_unix_server_socket("/tmp/echosock",LIBSOCKET_DGRAM,0); if ( ret < 0 ) { perror(0); exit(1); } while ( 0 < ( ret = bytes = recvfrom_unix_dgram_socket(sfd,buf,127,from,127,0) ) ) // read() is equivalent to recv_ussocket() { if ( ret < 0 ) { perror(0); exit(1); } write(1,buf,bytes); ret = sendto_unix_dgram_socket(sfd,buf,bytes,from,0); if ( ret < 0 ) { perror(0); exit(1); } } ret = destroy_unix_socket(sfd); if ( ret < 0 ) { perror(0); exit(1); } return 0; }
/* main auth program. Listen on unix socket and send back response */ int authproxy (void) { char buf[BUFSIZE]; int fd,cl; PacketType pkt_type = UNKNOWN; uint32_t len, readlen; AuthFun authfun; fd = create_unix_server_socket(socket_path,LIBSOCKET_STREAM, 0); if (fd == -1) { perror("socket error"); exit(-1); } while (1) { cl = accept_unix_stream_socket(fd, 0); if(cl == -1) { perror("accept error"); break; } readn(cl, &pkt_type, 1); readn(cl, &len, 4); //len = ntohl(len); //fprintf(stdout, "%d\n",len); readlen = (uint32_t)readn(cl, buf, (size_t)len); if(readlen < len) { // incomplete packet fprintf(stderr, "incomplete packet, skip"); close(cl); continue; } authfun = GetAuthFunbyType(pkt_type); if(authfun) authfun(buf, cl); // make fd close_on_exec? else { // fail fprintf(stderr, "unsupported auth tyep"); } close(cl); } close(fd); return 0; }