--- /dev/null
+// vim: smarttab tabstop=8 shiftwidth=2 expandtab
+/********************************************************/
+/* */
+/* Implement all routine to manage SMTP low level */
+/* exchange. */
+/* */
+/********************************************************/
+#include <stdbool.h>
+#include <stdio.h>
+
+#include "unieml.h"
+
+static _Bool modopen; //module open/close status
+
+/*
+^L
+*/
+/********************************************************/
+/* */
+/* Procedure to "open/close" module and do */
+/* homework purpose */
+/* return zero if everything right */
+/* */
+/********************************************************/
+int eml_modeunieml(_Bool mode)
+
+{
+#define OPEP "unidoc.c:soc_modeunisoc"
+
+int status;
+
+status=0;
+if (mode!=modopen) {
+ switch ((int)mode) {
+ case true :
+ break;
+ case false :
+ break;
+ default :
+ (void) fprintf(stderr,"Calling %s with wrong mode='%d' (Bug?!):",
+ OPEP,(int)mode);
+ status=-1;
+ break;
+ }
+ modopen=mode;
+ }
+return status;
+#undef OPEP
+}
#include "subrou.h"
#include "unisoc.h"
-#include "unisoc.h"
//Need to have GNU_SOURCE define within CFLAGS
#ifdef AI_ALL
*/
/********************************************************/
/* */
+/* Procedure fine the hostname related to an */
+/* address, if local is true return the local */
+/* hostname. */
+/* */
+/********************************************************/
+PUBLIC char *soc_getaddrname(int handle,_Bool local,const char *unknown)
+
+{
+#define OPEP "unisoc.c:soc_getaddrname"
+
+char *name;
+struct sockaddr connip;
+socklen_t taille;
+int status;
+char host[NI_MAXHOST];
+
+name=(char *)unknown;
+if (name==(char *)0)
+ name="NO_HOST_NAME";
+taille=sizeof(connip);
+if (local==true) {
+ if ((status=getsockname(handle,&connip,&taille))==0) {
+ if (getnameinfo(&connip,taille,host,sizeof(host),(char *)0,0,NI_NAMEREQD)==0)
+ name=host;
+ }
+ }
+else {
+ if ((status=getpeername(handle,&connip,&taille))==0) {
+ if (getnameinfo(&connip,taille,host,sizeof(host),(char *)0,0,NI_NUMERICHOST)==0)
+ name=host;
+ }
+ }
+if (status<0) {
+ switch (errno) {
+ case ENOTCONN : //other side just vanished
+ break;
+ default :
+ (void) rou_alert(0,"%s, Unable to find reverse (local=%d, error=<%s>)",
+ OPEP,local,strerror(errno));
+ break;
+ }
+ }
+return strdup(name);
+#undef OPEP
+}
+/*
+\f
+*/
+/********************************************************/
+/* */
/* Procedure to free memory used by a bind */
/* definition. */
/* */
*/
/********************************************************/
/* */
-/* Procedure to bind to a set of socket and return */
-/* true if all socket are open ready. */
-/* */
-/********************************************************/
-PUBLIC _Bool soc_mulopen(SOCTYP **bindings)
-
-{
-#define OPEP "unisoc.c:soc_mulopen"
-
-_Bool ready;
-uid_t gid;
-uid_t uid;
-int phase;
-_Bool proceed;
-
-ready=false;
-gid=getegid(); //let be back to root if needed
-uid=geteuid(); //to open device on < 1024 por
-phase=0;
-proceed=true;
-while (proceed==true) {
- switch (phase) {
- case 0 : //checking if the point is ready;
- if (bindings==(SOCTYP **)0) {
- (void) rou_alert(0,"%s bindings list pointer is NULL (Bug!?)",OPEP);
- phase=999; //not going further
- }
- break;
- case 1 : //
- break;
- if ((setegid(getgid())<0)||(seteuid(getuid())<0)) {
- (void) rou_core_dump("%s, Unable to get root privilege! "
- "(program config? error=<%s>)",
- OPEP,strerror(errno));
- //program is crashing HARD
- }
- break;
- case 2 : //binding on all interface
- ready=true;
- while (*bindings!=(SOCTYP *)0) {
- if (soc_openbinding(*bindings)==false)
- ready=false; //Trouble? Unable to open channel
- bindings++;
- }
- break;
- default : //SAFE Guard
- proceed=false;
- break;
- }
- phase++;
- }
-(void) setegid(gid); //set back the user,group ID
-(void) seteuid(uid);
-return ready;
-#undef OPEP
-}
-/*
-^L
-*/
-/********************************************************/
-/* */
-/* Procedure to close all socket previously binded */
-/* */
-/********************************************************/
-PUBLIC void soc_mulclose(SOCTYP **bindings)
-
-{
-#define OPEP "unisoc.c:soc_mulclose"
-
-int phase;
-_Bool proceed;
-
-phase=0;
-proceed=true;
-while (proceed==true) {
- switch (phase) {
- case 0 : //checking if the point is ready;
- if (bindings==(SOCTYP **)0) {
- (void) rou_alert(0,"%s bindings list pointer is NULL (Bug!?)",OPEP);
- phase=999; //not going further
- }
- break;
- case 1 : //
- while (*bindings!=(SOCTYP *)0) {
- (void) soc_closebinding(*bindings);
- bindings++;
- }
- break;
- default : //SAFE Guard
- proceed=false;
- break;
- }
- phase++;
- }
-#undef OPEP
-}
-/*
-^L
-*/
-/********************************************************/
-/* */
/* Procedure to wait an accept a connexion comming */
/* from a remote client. */
/* return the handle */
pid_t waiter; //binding manager
}SOCTYP;
+//procedure to get peer or local name related to a socket
+extern char *soc_getaddrname(int handle,_Bool local,const char *unknown);
//procedure to free all memory used by a TCP socket
//definition (once closed)
//procedure to close ONE socket
extern void soc_closebinding(SOCTYP *binding);
-//all socket are open in binding mode
-extern _Bool soc_mulopen(SOCTYP **bindings);
-
-//procedure to close all sockets
-extern void soc_mulclose(SOCTYP **bindings);
-
//procedure to wait and accept remote connexion
extern int soc_accept(SOCTYP *binding,SOCKADDR *addr);