1 /* posix */ 2 #include <sys/types.h> 3 #include <unistd.h> 4 #include <stdlib.h> 5 #include <stdio.h> 6 #include <string.h> 7 #include <time.h> 8 9 /* socket extensions */ 10 #include <sys/socket.h> 11 #include <netinet/in.h> 12 #include <sys/un.h> 13 14 int rresvport(int * p)15rresvport(int *p) 16 { 17 int fd; 18 short i; 19 struct sockaddr_in in; 20 static int next; 21 22 fd = socket(PF_INET, SOCK_STREAM, 0); 23 if(fd < 0) 24 return -1; 25 i = 600 + ((getpid()+next++)%(1024-600)); 26 memset(&in, 0, sizeof(in)); 27 in.sin_family = AF_INET; 28 in.sin_port = htons(i); 29 printf("in.sin_port = %d\n", in.sin_port); 30 if(bind(fd, &in, sizeof(in)) < 0){ 31 close(fd); 32 return -1; 33 } 34 if(p) 35 *p = i; 36 return fd; 37 } 38