int main(int argc, const char *argv[]) { int listenfd, connfd; pid_t childpid; socklen_t clilen; struct sockaddr_in cliaddr, servaddr; listenfd = socket_r(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(argv[1]); servaddr.sin_port = htons(80); //void bind_r(int listening_socket, const struct sockaddr *addr, socklen_t addrlen, void *arg); bind_r(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr), (char *)argv[1]); //void listen_r(int sockfd, int backlog, void *arg); listen_r(listenfd, 10, (char *)argv[1]); for(;;){ clilen = sizeof(cliaddr); //int accept_r(int sockfd, struct sockaddr *addr, socklen_t *addrlen); connfd = accept_r(listenfd, (struct sockaddr *)&cliaddr, &clilen); if((childpid = fork_r()) == 0){ close_r(listenfd); str_echo(connfd); exit(0); } } close_r(connfd); return 0; }
void start_test(int role) { int sk; int i = 0; DEBUG_PRINT(DEBUG_NONE, "\nStarting tests...\n"); repeat_count = repeat; if (MIXED == role) { repeat_count = repeat_count * msg_cnt; /* Repeat per assoc. */ mixed_mode_test(); return; } sk = socket_r(); bind_r(sk, &s_loc); /* Do we need to do bindx() to add any additional addresses? */ if (bindx_add_addrs) bindx_r(sk, bindx_add_addrs, bindx_add_count, SCTP_BINDX_ADD_ADDR); if (role == SERVER) { listen_r(sk, 100); } else { if (socket_type == SOCK_STREAM && connectx_count == 0) connect_r(sk, (struct sockaddr *)&s_rem, r_len); if (connectx_count != 0) connectx_r(sk, connectx_addrs, connectx_count); } if (!debug_level) { printf(" "); } for(i = 0; i < repeat_count; i++) { if (role == SERVER) { DEBUG_PRINT(DEBUG_NONE, "Server: Receiving packets.\n"); server(sk); } else { DEBUG_PRINT(DEBUG_NONE, "Client: Sending packets.(%d/%d)\n", i+1, repeat_count); client(sk); } fflush(stdout); } close_r(sk); } /* start_test() */
void start_test(int role) { int sk, pid; int i = 0; DEBUG_PRINT(DEBUG_NONE, "\nStarting tests...\n"); repeat_count = repeat; DEBUG_PRINT(DEBUG_MIN, "\tsocket(SOCK_STREAM, IPPROTO_SCTP)"); if ((sk = socket(s_loc.ss_family, SOCK_STREAM, IPPROTO_SCTP)) < 0 ) { fprintf(stderr, "\n\n\t\t*** socket: failed to create" " socket: %s ***\n", strerror(errno)); exit(1); } DEBUG_PRINT(DEBUG_MIN, " -> sk=%d\n", sk); bind_r(sk, &s_loc); if (role == SERVER) { listen_r(sk, 1); accept_r(sk); } else { connect_r(sk, (struct sockaddr *)&s_rem, r_len); } if ((pid = fork()) == 0) { settimerhandle(); printstatus(gsk); while(1); } else { if (!debug_level) { printf(" "); } for(i = 0; i < repeat_count; i++) { if (role == SERVER) { DEBUG_PRINT(DEBUG_NONE, "Server: Receiving packets.(%d/%d)\n", i+1, repeat_count); server(gsk); } else { DEBUG_PRINT(DEBUG_NONE, "Client: Sending packets.(%d/%d)\n", i+1, repeat_count); client(sk); } fflush(stdout); } if (role == SERVER) close_r(gsk); close_r(sk); } } /* start_test() */
void start_test(int role) { int sk; int i = 0; DEBUG_PRINT(DEBUG_NONE, "\nStarting tests...\n"); repeat_count = repeat; if (MIXED == role) { repeat_count = repeat_count * msg_cnt; /* Repeat per assoc. */ mixed_mode_test(); return; } sk = socket_r(); bind_r(sk, &s_loc); if (role == SERVER) { listen_r(sk, 100); } if (!debug_level) { printf(" "); } for(i = 0; i < repeat_count; i++) { if (role == SERVER) { DEBUG_PRINT(DEBUG_NONE, "Server: Receiving packets.\n"); server(sk); } else { DEBUG_PRINT(DEBUG_NONE, "Client: Sending packets.(%d/%d)\n", i+1, repeat_count); client(sk); } fflush(stdout); } close_r(sk); } /* start_test() */
int main(int argc, const char *argv[]) { struct sockaddr_in server_addr, client_addr; socklen_t client_addr_len; int connection_sock, listen_sock; char buf[MAXLINE]; char str[INET_ADDRSTRLEN]; int i,n; char bindmsg[1024]; int optval = 1; //struct sigaction { //void (*sa_handler)(int); //void (*sa_sigaction)(int, siginfo_t *, void *); //sigset_t sa_mask; //int sa_flags; //void (*sa_restorer)(void); //}; struct sigaction act, oact; //void *memcpy(void *dest, const void *src, size_t n); strcpy(bindmsg, argv[1]); strcat(bindmsg, " prot 8000"); if(argc != 2) err_sys("Usage ./server server_addr"); //int socket_r(int domain, int type, int protocol); listen_sock = socket(AF_INET, SOCK_STREAM, 0); //int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen); setsockopt_r(listen_sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)); memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(SERVER_PORT); //int inet_pton(int af, const char *src, void *dst); inet_pton(AF_INET, argv[1], &server_addr.sin_addr); //void bind_r(int listening_socket, const struct sockaddr *addr, socklen_t addrlen, void *arg); bind_r(listen_sock, (struct sockaddr *)&server_addr, sizeof(server_addr), (void *)bindmsg); //void listen_r(int sockfd, int backlog, void *arg); listen_r(listen_sock, SERVER_BACKLOG, (void *)bindmsg); //int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact) //siganl process act.sa_flags = 0; act.sa_handler = signal_handler; sigemptyset(&act.sa_mask); sigaction(SIGCHLD, &act, &oact); fprintf(stdout, "accepting connections ...\n"); pid_t pid; while(1){ client_addr_len = sizeof(client_addr); //int accept_r(int sockfd, struct sockaddr *addr, socklen_t *addrlen); connection_sock = accept_r(listen_sock, (struct sockaddr *)&client_addr, &client_addr_len); if(connection_sock < 0){ if(errno == EINTR) continue; err_ret("accept() failed"); } pid = fork(); if(pid < 0) err_sys("call to fork"); else if(pid == 0){ close_r(listen_sock); //ssize_t read_r(int fd, void *ptr, size_t nbytes); while(1){ n = read_r(connection_sock, buf, MAXLINE); if(n == 0) { fprintf(stdout, "peer side(%s port %d) has been closed.\n", inet_ntop(AF_INET, &client_addr.sin_addr, str, sizeof(str)), ntohs(client_addr.sin_port)); break; } fprintf(stdout, "reciveed from %s at port %d\n", inet_ntop(AF_INET, &client_addr.sin_addr, str, sizeof(str)), ntohs(client_addr.sin_port)); for (i = 0; i < n; i++) { buf[i] = toupper(buf[i]); } //ssize_t write_r(int fd, const void *ptr, size_t nbytes); write_r(connection_sock, buf, n); } close_r(connection_sock); exit(0); } else { close_r(connection_sock); } //void close_r(int fd); //close_r(connection_sock); } return 0; }
void mixed_mode_test(void) { int error, i, j, max_fd, sks, size; int assoc_i, n_msg_size, n_order, n_stream; int done = 0; fd_set *ibitsp = NULL, *obitsp = NULL, *xbitsp = NULL; char incmsg[CMSG_SPACE(sizeof(_sctp_cmsg_data_t))]; struct iovec iov; struct msghdr inmessage; _assoc_state *as; /* Set up the listeners. If listeners is 0, set up one socket for * transmitting only. */ iov.iov_base = NULL; max_fd = -1; sks = (0 == listeners) ? 1 : listeners; memset(poll_sks, 0, sizeof(sks * sizeof(_poll_sks))); for (i = 0; i < sks; i++) { poll_sks[i].sk = socket_r(); if (s_loc.ss_family == AF_INET6) ( (struct sockaddr_in6 *)&s_loc)->sin6_port = htons(local_port + i); else ( (struct sockaddr_in *)&s_loc)->sin_port = htons(local_port + i); bind_r(poll_sks[i].sk, &s_loc); if (listeners) { listen_r(poll_sks[i].sk, 100); } if (max_msgsize > DEFAULT_MAX_WINDOW) { if (setsockopt(poll_sks[i].sk, SOL_SOCKET, SO_RCVBUF, &max_msgsize, sizeof(max_msgsize)) < 0) { perror("setsockopt(SO_RCVBUF)"); exit(1); } } if (tosend) { if ((poll_sks[i].assoc_state = (_assoc_state *)malloc( sizeof(_assoc_state) * tosend)) == NULL) { printf("Can't allocate memory.\n"); goto clean_up; } memset(poll_sks[i].assoc_state, 0, sizeof(_assoc_state) * tosend); } if (poll_sks[i].sk > max_fd) { max_fd = poll_sks[i].sk; } } size = howmany(max_fd + 1, NFDBITS) * sizeof(fd_mask); if ((ibitsp = (fd_set *)malloc(size)) == NULL) { printf("Can't allocate memory.\n"); goto clean_up; } if ((obitsp = (fd_set *)malloc(size)) == NULL) { printf("Can't allocate memory.\n"); goto clean_up; } if ((xbitsp = (fd_set *)malloc(size)) == NULL) { printf("Can't allocate memory.\n"); goto clean_up; } memset(ibitsp, 0, size); memset(obitsp, 0, size); memset(xbitsp, 0, size); /* Initialize inmessage with enough space for DATA... */ memset(&inmessage, 0, sizeof(inmessage)); if ((iov.iov_base = malloc(REALLY_BIG)) == NULL) { fprintf(stderr, "\n\t\t*** malloc not enough memory!!! ***\n"); goto clean_up; } iov.iov_len = REALLY_BIG; inmessage.msg_iov = &iov; inmessage.msg_iovlen = 1; /* or a control message. */ inmessage.msg_control = incmsg; inmessage.msg_controllen = sizeof(incmsg); /* Set up the remote port number per association for output. */ for (i = 0; i < sks; i++) { as = poll_sks[i].assoc_state; for (j = 0; j < tosend; j++) { as[j].rem_port = remote_port + j; } } while (!done) { for (i = 0; i < sks; i++) { FD_SET(poll_sks[i].sk, ibitsp); FD_SET(poll_sks[i].sk, obitsp); FD_SET(poll_sks[i].sk, xbitsp); } if ((error = select(max_fd + 1, ibitsp, obitsp, xbitsp, (struct timeval *)0)) < 0) { fprintf(stderr, "\n\t\t*** select() failed "); fprintf(stderr, "with error: %s\n\n", strerror(errno)); fflush(stdout); goto clean_up; } for (i = 0; i < sks; i++) { /* Is there anything to read from the socket? */ if (listeners && FD_ISSET(poll_sks[i].sk, ibitsp)) { FD_CLR(poll_sks[i].sk, ibitsp); error = recvmsg(poll_sks[i].sk, &inmessage, MSG_WAITALL); if (error < 0) { fprintf(stderr, "\n\t\t*** recvmsg: %s ***\n\n", strerror(errno)); fflush(stdout); goto clean_up; } else if (error == 0) { printf("\n\t\trecvmsg() returned "); printf("0 !!!!\n"); fflush(stdout); } print_message(poll_sks[i].sk, &inmessage, error); inmessage.msg_control = incmsg; inmessage.msg_controllen = sizeof(incmsg); iov.iov_len = REALLY_BIG; } /* Is this socket writeable? */ if (tosend && FD_ISSET(poll_sks[i].sk, obitsp)) { FD_CLR(poll_sks[i].sk, obitsp); /* Pick an association. */ assoc_i = next_assoc(i, poll_sks[i].assoc_i, assoc_pattern); if (assoc_i < 0) { /* No work to do on any associations. * We are probably done. */ if (!listeners) { done = 1; } continue; } poll_sks[i].assoc_i = assoc_i; as = poll_sks[i].assoc_state; n_msg_size = next_msg_size(as[assoc_i].msg_cnt); n_order = as[assoc_i].order_state = next_order(as[assoc_i].order_state, order_pattern); n_stream = as[assoc_i].stream_state = next_stream(as[assoc_i].stream_state, stream_pattern); /* Set the destination port. */ if (s_rem.ss_family == AF_INET6) ( (struct sockaddr_in6 *)&s_rem)-> sin6_port = htons(as[assoc_i].rem_port); else ( (struct sockaddr_in *)&s_rem)-> sin_port = htons(as[assoc_i].rem_port); /* Send a message thru the association. */ if (send_r(poll_sks[i].sk, n_stream, n_order, n_msg_size, assoc_i) < 0) { /* Don't increment counter if there * is a problem of sending. */ continue; } /* Increment counters. */ if (++as[assoc_i].msg_cnt >= MSG_CNT) { as[assoc_i].msg_cnt = 0; } if (++as[assoc_i].msg_sent >= repeat_count) { fprintf(stderr, "Association #%d in ", assoc_i); fprintf(stderr, "sk=%d has ", poll_sks[i].sk); fprintf(stderr, "completed %d msg as ", as[assoc_i].msg_sent); fprintf(stderr, "cycle %d.\n", ++as[assoc_i].cycle); /* In the mixed mode, -x not only * specify the longer repeat cycle, * but it also mean to run the test * forever. */ if (xflag) { as[assoc_i].msg_sent = 0; } } } } } clean_up: for (i = 0; i < sks; i++) { close(poll_sks[i].sk); if (poll_sks[i].assoc_state) { free(poll_sks[i].assoc_state); } } if (ibitsp) free(ibitsp); if (obitsp) free(obitsp); if (xbitsp) free(xbitsp); if (iov.iov_base) free(iov.iov_base); } /* mixed_mode_test() */