jabberd2  2.2.16
sm/sm.h
Go to the documentation of this file.
00001 /*
00002  * jabberd - Jabber Open Source Server
00003  * Copyright (c) 2002 Jeremie Miller, Thomas Muldowney,
00004  *                    Ryan Eatmon, Robert Norris
00005  *
00006  * This program is free software; you can redistribute it and/or drvify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
00019  */
00020 
00029 #ifdef HAVE_CONFIG_H
00030   #include <config.h>
00031 #endif
00032 
00033 #include "sx/sx.h"
00034 #include "mio/mio.h"
00035 #include "util/util.h"
00036 #include "storage/storage.h"
00037 
00038 #ifdef HAVE_SIGNAL_H
00039   #include <signal.h>
00040 #endif
00041 #ifdef HAVE_SYS_STAT_H
00042   #include <sys/stat.h>
00043 #endif
00044 
00045 #ifdef _WIN32
00046   #ifdef _USRDLL
00047     #define DLLEXPORT  __declspec(dllexport)
00048     #define SM_API     __declspec(dllimport)
00049   #else
00050     #define DLLEXPORT  __declspec(dllimport)
00051     #define SM_API     __declspec(dllexport)
00052   #endif
00053 #else
00054   #define DLLEXPORT
00055   #define SM_API
00056 #endif
00057 
00058 /* forward declarations */
00059 typedef struct sm_st        *sm_t;
00060 typedef struct user_st      *user_t;
00061 typedef struct sess_st      *sess_t;
00062 typedef struct aci_st       *aci_t;
00063 typedef struct mm_st        *mm_t;
00064 
00065 /* namespace uri strings */
00066 #include "util/uri.h"
00067 
00068 /* indexed known namespace values */
00069 #define ns_AUTH         (1)
00070 #define ns_REGISTER     (2)
00071 #define ns_ROSTER       (3)
00072 #define ns_AGENTS       (4)
00073 #define ns_DELAY        (5)
00074 #define ns_BROWSE       (6)
00075 #define ns_EVENT        (7)
00076 #define ns_GATEWAY      (8)
00077 #define ns_EXPIRE       (9)
00078 #define ns_SEARCH       (10)
00079 #define ns_DISCO        (11)
00080 #define ns_DISCO_ITEMS  (12)
00081 #define ns_DISCO_INFO   (13)
00082 
00083 #define ns_AMP                          (14)
00084 #define ns_AMP_ERRORS                   (15)
00085 #define ns_AMP_ACTION_DROP              (16)
00086 #define ns_AMP_ACTION_ERROR             (17)
00087 #define ns_AMP_ACTION_NOTIFY            (18)
00088 #define ns_AMP_CONDITION_DELIVER        (19)
00089 #define ns_AMP_CONDITION_EXPIREAT       (20)
00090 #define ns_AMP_CONDITION_MATCHRESOURCE  (21)
00091 
00093 typedef enum {
00094     pkt_NONE = 0x00,            
00095     pkt_MESSAGE = 0x10,         
00096     pkt_MESSAGE_CHAT = 0x11,    
00097     pkt_MESSAGE_HEADLINE = 0x12,
00098     pkt_MESSAGE_GROUPCHAT = 0x14,
00099     pkt_PRESENCE = 0x20,        
00100     pkt_PRESENCE_UN = 0x21,     
00101     pkt_PRESENCE_PROBE = 0x24,  
00102     pkt_S10N = 0x40,            
00103     pkt_S10N_ED = 0x41,         
00104     pkt_S10N_UN = 0x42,         
00105     pkt_S10N_UNED = 0x44,       
00106     pkt_IQ = 0x80,              
00107     pkt_IQ_SET = 0x81,          
00108     pkt_IQ_RESULT = 0x82,       
00109     pkt_SESS = 0x100,           
00110     pkt_SESS_END = 0x101,       
00111     pkt_SESS_CREATE = 0x102,    
00112     pkt_SESS_DELETE = 0x104,    
00113     pkt_SESS_FAILED = 0x08,     
00114     pkt_SESS_MASK = 0x10f,      
00115     pkt_ERROR = 0x200           
00116 } pkt_type_t;
00117 
00119 typedef enum {
00120     route_NONE = 0x00,          
00121     route_UNICAST = 0x10,       
00122     route_BROADCAST = 0x11,     
00123     route_ADV = 0x20,           
00124     route_ADV_UN = 0x21,        
00125     route_ERROR = 0x40          
00126 } route_type_t;
00127 
00129 typedef struct pkt_st {
00130     sm_t                sm;         
00132     sess_t              source;     
00134     jid_t               rto, rfrom; 
00136     route_type_t        rtype;      
00138     pkt_type_t          type;       
00140     jid_t               to, from;   
00142     int                 ns;         
00144     int                 pri;        
00146     nad_t               nad;        
00147 } *pkt_t;
00148 
00150 typedef struct item_st {
00151     jid_t               jid;        
00153     char                *name;      
00155     char                **groups;   
00157     int                 ngroups;    
00159     int                 to, from;   
00161     int                 ask;        
00163     int                 ver;        
00164 } *item_t;
00165 
00167 struct sm_st {
00168     char                *id;                
00170     char                *router_ip;         
00171     int                 router_port;        
00172     char                *router_user;       
00173     char                *router_pass;       
00174     char                *router_pemfile;    
00177     mio_t               mio;                
00179     sx_env_t            sx_env;             
00180     sx_plugin_t         sx_sasl;            
00181     sx_plugin_t         sx_ssl;             
00183     sx_t                router;             
00184     mio_fd_t            fd;                 
00186     xht                 users;              
00188     xht                 sessions;           
00190     xht                 xmlns;              
00191     xht                 xmlns_refcount;     
00193     xht                 features;           
00195     config_t            config;             
00197     log_t               log;                
00199     log_type_t          log_type;           
00200     char                *log_facility;      
00201     char                *log_ident;         
00203     int                 retry_init;         
00204     int                 retry_lost;         
00205     int                 retry_sleep;        
00206     int                 retry_left;         
00208     storage_t           st;                 
00210     mm_t                mm;                 
00212     xht                 acls;               
00214     char                signature[2048];    
00215     int                 siglen;             
00217     int                 started;            
00219     int                 online;             
00221     xht                 hosts;              
00224     int                 query_rate_total;
00225     int                 query_rate_seconds;
00226     int                 query_rate_wait;
00227     xht                 query_rates;
00228 };
00229 
00231 struct user_st {
00232     pool_t              p;                  
00234     sm_t                sm;                 
00236     jid_t               jid;                
00238     xht                 roster;             
00240     sess_t              sessions;           
00241     sess_t              top;                
00242     int                 available;          
00244     time_t              active;             
00246     void                **module_data;      
00247 };
00248 
00250 struct sess_st {
00251     pool_t              p;                  
00253     user_t              user;               
00255     jid_t               jid;                
00257     char                c2s[1024];          
00259     char                sm_id[41];          
00260     char                c2s_id[44];         
00262     pkt_t               pres;               
00264     int                 available;          
00265     int                 pri;                
00266     int                 fake;               
00268     jid_t               A;                  
00269     jid_t               E;                  
00271     void                **module_data;      
00273     sess_t              next;               
00274 };
00275 
00276 extern sig_atomic_t sm_lost_router;
00277 
00278 /* functions */
00279 SM_API xht             aci_load(sm_t sm);
00280 SM_API int             aci_check(xht acls, char *type, jid_t jid);
00281 SM_API void            aci_unload(xht acls);
00282 
00283 SM_API int             sm_sx_callback(sx_t s, sx_event_t e, void *data, void *arg);
00284 SM_API int             sm_mio_callback(mio_t m, mio_action_t a, mio_fd_t fd, void *data, void *arg);
00285 SM_API void            sm_timestamp(time_t t, char timestamp[18]);
00286 SM_API void            sm_c2s_action(sess_t dest, char *action, char *target);
00287 SM_API void            sm_signature(sm_t sm, char *str);
00288 
00289 SM_API int             sm_register_ns(sm_t sm, char *uri);
00290 SM_API void            sm_unregister_ns(sm_t sm, char *uri);
00291 SM_API int             sm_get_ns(sm_t sm, char *uri);
00292 
00293 SM_API int             sm_storage_rate_limit(sm_t sm, const char *owner);
00294 
00295 SM_API void            dispatch(sm_t sm, pkt_t pkt);
00296 
00297 SM_API pkt_t           pkt_error(pkt_t pkt, int err);
00298 SM_API pkt_t           pkt_tofrom(pkt_t pkt);
00299 SM_API pkt_t           pkt_dup(pkt_t pkt, const char *to, const char *from);
00300 SM_API pkt_t           pkt_new(sm_t sm, nad_t nad);
00301 SM_API void            pkt_free(pkt_t pkt);
00302 SM_API pkt_t           pkt_create(sm_t sm, const char *elem, const char *type, const char *to, const char *from);
00303 SM_API void            pkt_id(pkt_t src, pkt_t dest);
00304 SM_API void            pkt_id_new(pkt_t pkt);
00305 SM_API void            pkt_delay(pkt_t pkt, time_t t, const char *from);
00306 
00307 SM_API void            pkt_router(pkt_t pkt);
00308 SM_API void            pkt_sess(pkt_t pkt, sess_t sess);
00309 
00310 SM_API int             pres_trust(user_t user, jid_t jid);
00311 SM_API void            pres_roster(sess_t sess, item_t item);
00312 SM_API void            pres_update(sess_t sess, pkt_t pres);
00313 SM_API void            pres_error(sess_t sess, jid_t jid);
00314 SM_API void            pres_deliver(sess_t sess, pkt_t pres);
00315 SM_API void            pres_in(user_t user, pkt_t pres);
00316 SM_API void            pres_probe(user_t user);
00317 
00318 SM_API void            sess_route(sess_t sess, pkt_t pkt);
00319 SM_API sess_t          sess_start(sm_t sm, jid_t jid);
00320 SM_API void            sess_end(sess_t sess);
00321 SM_API sess_t          sess_match(user_t user, char *resource);
00322 
00323 SM_API user_t          user_load(sm_t sm, jid_t jid);
00324 SM_API void            user_free(user_t user);
00325 SM_API int             user_create(sm_t sm, jid_t jid);
00326 SM_API void            user_delete(sm_t sm, jid_t jid);
00327 
00328 SM_API void            feature_register(sm_t sm, char *feature);
00329 SM_API void            feature_unregister(sm_t sm, char *feature);
00330 
00331 
00332 /* driver module manager */
00333 
00335 typedef enum {
00336     mod_HANDLED,                
00337     mod_PASS                    
00338 } mod_ret_t;
00339 
00341 typedef enum {
00342     chain_SESS_START,           
00343     chain_SESS_END,             
00344     chain_IN_SESS,              
00345     chain_IN_ROUTER,            
00346     chain_OUT_SESS,             
00347     chain_OUT_ROUTER,           
00348     chain_PKT_SM,               
00349     chain_PKT_USER,             
00350     chain_PKT_ROUTER,           
00351     chain_USER_LOAD,            
00352     chain_USER_CREATE,          
00353     chain_USER_DELETE,          
00354     chain_USER_UNLOAD,          
00355     chain_DISCO_EXTEND          
00356 } mod_chain_t;
00357 
00358 typedef struct module_st *module_t;
00359 typedef struct mod_instance_st *mod_instance_t;
00360 
00362 struct mm_st {
00363     sm_t                sm;         
00365     xht                 modules;    
00367     int                 nindex;     
00370     mod_instance_t      *sess_start;    int nsess_start;
00372     mod_instance_t      *sess_end;      int nsess_end;
00374     mod_instance_t      *in_sess;       int nin_sess;
00376     mod_instance_t      *in_router;     int nin_router;
00378     mod_instance_t      *out_sess;      int nout_sess;
00380     mod_instance_t      *out_router;    int nout_router;
00382     mod_instance_t      *pkt_sm;        int npkt_sm;
00384     mod_instance_t      *pkt_user;      int npkt_user;
00386     mod_instance_t      *pkt_router;    int npkt_router;
00388     mod_instance_t      *user_load;     int nuser_load;
00390     mod_instance_t      *user_create;   int nuser_create;
00392     mod_instance_t      *user_delete;   int nuser_delete;
00394     mod_instance_t      *disco_extend;  int ndisco_extend;
00396     mod_instance_t      *user_unload;     int nuser_unload;
00397 };
00398 
00400 struct module_st {
00401     mm_t                mm;         
00403     char                *name;      
00405     int                 index;      
00409     void                *handle;    
00411     int                 (*module_init_fn)(mod_instance_t);    
00413     int                 init;       
00415     void                *private;   
00417     int                 (*sess_start)(mod_instance_t mi, sess_t sess);              
00418     void                (*sess_end)(mod_instance_t mi, sess_t sess);                
00420     mod_ret_t           (*in_sess)(mod_instance_t mi, sess_t sess, pkt_t pkt);      
00421     mod_ret_t           (*in_router)(mod_instance_t mi, pkt_t pkt);                 
00423     mod_ret_t           (*out_sess)(mod_instance_t mi, sess_t sess, pkt_t pkt);     
00424     mod_ret_t           (*out_router)(mod_instance_t mi, pkt_t pkt);                
00426     mod_ret_t           (*pkt_sm)(mod_instance_t mi, pkt_t pkt);                    
00427     mod_ret_t           (*pkt_user)(mod_instance_t mi, user_t user, pkt_t pkt);     
00429     mod_ret_t           (*pkt_router)(mod_instance_t mi, pkt_t pkt);                
00431     int                 (*user_load)(mod_instance_t mi, user_t user);               
00432     int                 (*user_unload)(mod_instance_t mi, user_t user);               
00434     int                 (*user_create)(mod_instance_t mi, jid_t jid);               
00435     void                (*user_delete)(mod_instance_t mi, jid_t jid);               
00437     void                (*disco_extend)(mod_instance_t mi, pkt_t pkt);              
00439     void                (*free)(module_t mod);                                      
00440 };
00441 
00443 struct mod_instance_st {
00444     sm_t                sm;         
00446     module_t            mod;        
00448     int                 seq;        
00450     mod_chain_t         chain;      
00452     char                *arg;       
00453 };
00454 
00456 SM_API mm_t                    mm_new(sm_t sm);
00458 SM_API void                    mm_free(mm_t mm);
00459 
00461 SM_API int                     mm_sess_start(mm_t mm, sess_t sess);
00463 SM_API void                    mm_sess_end(mm_t mm, sess_t sess);
00464 
00466 SM_API mod_ret_t               mm_in_sess(mm_t mm, sess_t sess, pkt_t pkt);
00468 SM_API mod_ret_t               mm_in_router(mm_t mm, pkt_t pkt);
00469 
00471 SM_API mod_ret_t               mm_out_sess(mm_t mm, sess_t sess, pkt_t pkt);
00473 SM_API mod_ret_t               mm_out_router(mm_t mm, pkt_t pkt);
00474 
00476 SM_API mod_ret_t               mm_pkt_sm(mm_t mm, pkt_t pkt);
00478 SM_API mod_ret_t               mm_pkt_user(mm_t mm, user_t user, pkt_t pkt);
00479 
00481 SM_API mod_ret_t               mm_pkt_router(mm_t mm, pkt_t pkt);
00482 
00484 SM_API int                     mm_user_load(mm_t mm, user_t user);
00485 
00487 SM_API int                     mm_user_unload(mm_t mm, user_t user);
00488 
00490 SM_API int                     mm_user_create(mm_t mm, jid_t jid);
00492 SM_API void                    mm_user_delete(mm_t mm, jid_t jid);
00493 
00495 SM_API void                    mm_disco_extend(mm_t mm, pkt_t pkt);