Hello, I am doing a plugin that needs to listen at a particular port to send the events (connections and desconnections) to the connected clients. This is the main reason because I have used the fork() call but seems not to work very well:
OPENVPN_EXPORT openvpn_plugin_handle_t openvpn_plugin_open_v1 (unsigned int *type_mask, const char *argv[], const char *envp[]) { int procid; if (pipe(data_pipe) == -1) { perror("pipe"); exit(1); } procid=fork(); if ((procid) == 0) { //I am the child (I should execute the listen and create new forks for the accept() clients) int nsocket; /* This part is for the socket (must be before registering anything to prevent double events) */ struct sockaddr_in serversocket; nsocket = socket(AF_INET, SOCK_STREAM, 0); serversocket.sin_family = AF_INET; serversocket.sin_port = htons(PORT); serversocket.sin_addr.s_addr = INADDR_ANY; serversocket.sin_port = htons(PORT); serversocket.sin_addr.s_addr = INADDR_ANY; memset(&(serversocket.sin_zero), 0, 8); bind(nsocket, (struct sockaddr *)&serversocket, sizeof(struct sockaddr)); listen(nsocket, MAX_WAIT_CLIENTS); for(;;){ /* Once connection is accepted, we send this information to the pipe for the next event * on the next event of the plugin, parent thread will add those file descriptors connected and * will remove sockets giving errors from the notification list. */ struct sockaddr_un remoteconnection; //remoteconnection.sin_family=AF_INET; int sin_size = sizeof(struct sockaddr_un); //int sin_size=sizeof(remoteconnection); int newfd=accept(nsocket, (struct sockaddr *)&remoteconnection, &sin_size); if (newfd != -1) { char temp[10]; sprintf(temp,"Connected"); send(newfd, temp, strlen(temp), 0); write(data_pipe[1], &newfd, 1); } } exit(0); } else { ... here I return the context. } questions are: Can I use fork? Is any example of a plugin using fork()? Where should I use the fork/must execute listen() call?. What happens is that the client can never finish to connect to the server and in the middle of the negociation, if I connect to the socket, the server breaks down (segmentation fault). Thanks in advance for your help or ideas. -- Jose Sánchez <jose.informat...@centrolamilagrosa.org> CGED La Milagrosa http://www.centrolamilagrosa.org Grupo de investigación en Gerontología http://gerontologia.udc.es Esta información es privada y confidencial y está dirigida únicamente a su destinatario. Si usted no es el destinatario original de este mensaje y por este medio pudo acceder a dicha información por favor elimine el mensaje y notifique el envío erróneo al remitente. La distribución o copia de este mensaje está estrictamente prohibida. Esta comunicación es sólo para propósitos de información y no debería ser considerada como una declaración oficial del CGED La Milagrosa. La transmisión de e-mails no garantiza que el correo electrónico sea seguro. Por consiguiente, no manifestamos que esta información sea completa o precisa. Toda información está sujeta a alterarse sin previo aviso. Este Correo Electrónico ha sido procesado por nuestro sistema de Antivirus corporativo. This information is private and confidential and intended for the recipient only. If you are not the intended recipient of this message you are hereby notified that any review it and notify the sender immediately, dissemination, distribution or copying of this message is strictly prohibited. This communication is for information purposes only and should not be regarded as an official statement from CGED La Milagrosa. Email transmission cannot be guaranteed to be secure. Therefore, we do not represent that this information is complete or accurate and it should not be relied upon as such.All information is subject to change without notice. This email has been scanned by our Antivirus system