]> andersk Git - libfaim.git/blob - faim/aim.h
Locking around tx seqnum.
[libfaim.git] / faim / aim.h
1 /* 
2  * Main libfaim header.  Must be included in client for prototypes/macros.
3  *
4  */
5
6 #ifndef __AIM_H__
7 #define __AIM_H__
8
9 #include <faim/faimconfig.h>
10 #include <faim/aim_cbtypes.h>
11
12 #ifndef FAIM_USEPTHREADS
13 #error pthreads are currently required.
14 #endif
15
16 #include <stdio.h>
17 #include <string.h>
18 #include <fcntl.h>
19 #include <sys/types.h>
20 #include <stdlib.h>
21 #include <stdarg.h>
22 #include <errno.h>
23
24 #ifdef FAIM_USEPTHREADS
25 #include <pthread.h>
26 #define faim_mutex_t pthread_mutex_t 
27 #define faim_mutex_init pthread_mutex_init
28 #define faim_mutex_lock pthread_mutex_lock
29 #define faim_mutex_unlock pthread_mutex_unlock
30 #endif
31
32 #ifdef _WIN32
33 #include <windows.h>
34 #include <time.h>
35 #include <io.h>
36 #else
37 #include <netdb.h>
38 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <sys/time.h>
41 #include <unistd.h>
42 #endif
43
44 /* Portability stuff (DMP) */
45
46 #ifdef _WIN32
47 #define sleep Sleep
48 #define strlen(x) (int)strlen(x)  /* win32 has a unsigned size_t */
49 #endif
50
51 #if defined(_WIN32) || (defined(mach) && defined(__APPLE__)) 
52 #define gethostbyname2(x,y) gethostbyname(x) /* revert to IPv4 */
53 #endif 
54
55 /* 
56  * Current Maximum Length for Screen Names (not including NULL) 
57  */
58 #define MAXSNLEN 16
59
60 /*
61  * Standard size of an AIM authorization cookie
62  */
63 #define AIM_COOKIELEN            0x100
64
65 #if debug > 0
66 #define faimdprintf(l, x...) {if (l >= debug) printf(x); }
67 #else
68 #define faimdprintf(l, x...)
69 #endif
70
71 /*
72  * Login info.  Passes information from the Authorization
73  * stage of login to the service (BOS, etc) connection
74  * phase.
75  *
76  */
77 struct aim_login_struct {
78   char screen_name[MAXSNLEN+1];
79   char *BOSIP;
80   char cookie[AIM_COOKIELEN];
81   char *email;
82   u_short regstatus;
83   char *errorurl;
84   u_short errorcode;
85 };
86
87 /*
88  * Client info.  Filled in by the client and passed
89  * in to aim_login().  The information ends up
90  * getting passed to OSCAR through the initial
91  * login command.
92  *
93  * XXX: Should this be per-session? -mid
94  *
95  */
96 struct client_info_s {
97   char clientstring[100]; /* arbitrary size */
98   int major;
99   int minor;
100   int build;
101   char country[3];
102   char lang[3];
103 };
104
105 #ifndef TRUE
106 #define TRUE 1
107 #define FALSE 0
108 #endif
109
110 /* 
111  * These could be arbitrary, but its easier to use the actual AIM values 
112  */
113 #define AIM_CONN_TYPE_AUTH          0x0007
114 #define AIM_CONN_TYPE_ADS           0x0005
115 #define AIM_CONN_TYPE_BOS           0x0002
116 #define AIM_CONN_TYPE_CHAT          0x000e
117 #define AIM_CONN_TYPE_CHATNAV       0x000d
118
119 /*
120  * Status values returned from aim_conn_new().  ORed together.
121  */
122 #define AIM_CONN_STATUS_READY       0x0001
123 #define AIM_CONN_STATUS_INTERNALERR 0x0002
124 #define AIM_CONN_STATUS_RESOLVERR   0x0080
125 #define AIM_CONN_STATUS_CONNERR     0x0040
126
127 struct aim_conn_t {
128   int fd;
129   int type;
130   int seqnum;
131   int status;
132   void *priv; /* misc data the client may want to store */
133   time_t lastactivity; /* time of last transmit */
134   int forcedlatency; 
135   struct aim_rxcblist_t *handlerlist;
136   faim_mutex_t active; /* lock around read/writes */
137   faim_mutex_t seqnum_lock; /* lock around ->seqnum changes */
138 };
139
140 /* struct for incoming commands */
141 struct command_rx_struct {
142                             /* byte 1 assumed to always be 0x2a */
143   char type;                /* type code (byte 2) */
144   u_int seqnum;             /* sequence number (bytes 3 and 4) */
145   u_int commandlen;         /* total packet len - 6 (bytes 5 and 6) */
146   u_char *data;             /* packet data (from 7 byte on) */
147   u_int lock;               /* 0 = open, !0 = locked */
148   u_int handled;            /* 0 = new, !0 = been handled */
149   u_int nofree;             /* 0 = free data on purge, 1 = only unlink */
150   struct aim_conn_t *conn;  /* the connection it came in on... */
151   struct command_rx_struct *next; /* ptr to next struct in list */
152 };
153
154 /* struct for outgoing commands */
155 struct command_tx_struct {
156                             /* byte 1 assumed to be 0x2a */
157   char type;                /* type/family code */
158   u_int seqnum;             /* seqnum dynamically assigned on tx */
159   u_int commandlen;         /* SNAC length */
160   u_char *data;             /* packet data */
161   u_int lock;               /* 0 = open, !0 = locked */
162   u_int sent;               /* 0 = pending, !0 = has been sent */
163   struct aim_conn_t *conn; 
164   struct command_tx_struct *next; /* ptr to next struct in list */
165 };
166
167
168 /*
169  * AIM Session: The main client-data interface.  
170  *
171  */
172 struct aim_session_t {
173
174   /* ---- Client Accessible ------------------------ */
175   /* 
176    * Login information.  See definition above.
177    *
178    */
179   struct aim_login_struct logininfo;
180   
181   /*
182    * Pointer to anything the client wants to 
183    * explicitly associate with this session.
184    */
185   void *aux_data;
186
187
188   /* ---- Internal Use Only ------------------------ */
189   /* 
190    * Connection information
191    */
192   struct aim_conn_t conns[AIM_CONN_MAX];
193   
194   /* 
195    * TX/RX queues 
196    */
197   struct command_tx_struct *queue_outgoing;   
198   struct command_rx_struct *queue_incoming; 
199   
200   /*
201    * Tx Enqueuing function
202    */
203   int (*tx_enqueue)(struct aim_session_t *, struct command_tx_struct *);
204
205   /*
206    * This is a dreadful solution to the what-room-are-we-joining
207    * problem.  (There's no connection between the service
208    * request and the resulting redirect.)
209    */ 
210   char *pendingjoin;
211
212   /*
213    * Outstanding snac handling 
214    *
215    * XXX: Should these be per-connection? -mid
216    **/
217   struct aim_snac_t *outstanding_snacs;
218   u_long snac_nextid;
219 };
220
221
222 /*
223  * AIM User Info, Standard Form.
224  */
225 struct aim_userinfo_s {
226   char sn[MAXSNLEN+1];
227   u_short warnlevel;
228   u_short idletime;
229   u_short class;
230   u_long membersince;
231   u_long onlinesince;
232   u_long sessionlen;  
233   u_short capabilities;
234 };
235
236 #define AIM_CLASS_TRIAL         0x0001
237 #define AIM_CLASS_UNKNOWN2      0x0002
238 #define AIM_CLASS_AOL           0x0004
239 #define AIM_CLASS_UNKNOWN4      0x0008
240 #define AIM_CLASS_FREE          0x0010
241 #define AIM_CLASS_AWAY          0x0020
242 #define AIM_CLASS_UNKNOWN40     0x0040
243 #define AIM_CLASS_UNKNOWN80     0x0080
244
245 /*
246  * TLV handling
247  */
248
249 /* Generic TLV structure. */
250 struct aim_tlv_t {
251   u_short type;
252   u_short length;
253   u_char *value;
254 };
255
256 /* List of above. */
257 struct aim_tlvlist_t {
258   struct aim_tlv_t *tlv;
259   struct aim_tlvlist_t *next;
260 };
261
262 /* TLV-handling functions */
263 struct aim_tlvlist_t *aim_readtlvchain(u_char *buf, int maxlen);
264 void aim_freetlvchain(struct aim_tlvlist_t **list);
265 struct aim_tlv_t *aim_grabtlv(u_char *src);
266 struct aim_tlv_t *aim_grabtlvstr(u_char *src);
267 struct aim_tlv_t *aim_gettlv(struct aim_tlvlist_t *, u_short, int);
268 char *aim_gettlv_str(struct aim_tlvlist_t *, u_short, int);
269 int aim_puttlv (u_char *dest, struct aim_tlv_t *newtlv);
270 struct aim_tlv_t *aim_createtlv(void);
271 int aim_freetlv(struct aim_tlv_t **oldtlv);
272 int aim_puttlv_16(u_char *, u_short, u_short);
273 int aim_puttlv_32(u_char *, u_short, u_long);
274 int aim_puttlv_str(u_char *buf, u_short t, u_short l, u_char *v);
275 int aim_writetlvchain(u_char *buf, int buflen, struct aim_tlvlist_t **list);
276 int aim_addtlvtochain16(struct aim_tlvlist_t **list, unsigned short type, unsigned short val);
277 int aim_addtlvtochain32(struct aim_tlvlist_t **list, unsigned short type, unsigned long val);
278 int aim_addtlvtochain_str(struct aim_tlvlist_t **list, unsigned short type, char *str, int len);
279 int aim_counttlvchain(struct aim_tlvlist_t **list);
280
281 /*
282  * Get command from connections / Dispatch commands
283  * already in queue.
284  */
285 int aim_get_command(struct aim_session_t *, struct aim_conn_t *);
286 int aim_rxdispatch(struct aim_session_t *);
287
288 int aim_logoff(struct aim_session_t *);
289
290
291 typedef int (*rxcallback_t)(struct aim_session_t *, struct command_rx_struct *, ...);
292 int aim_register_callbacks(rxcallback_t *);
293
294 u_long aim_genericreq_n(struct aim_session_t *, struct aim_conn_t *conn, u_short family, u_short subtype);
295 u_long aim_genericreq_l(struct aim_session_t *, struct aim_conn_t *conn, u_short family, u_short subtype, u_long *);
296 u_long aim_genericreq_s(struct aim_session_t *, struct aim_conn_t *conn, u_short family, u_short subtype, u_short *);
297
298 /* aim_login.c */
299 int aim_sendconnack(struct aim_session_t *sess, struct aim_conn_t *conn);
300 int aim_request_login (struct aim_session_t *sess, struct aim_conn_t *conn, char *sn);
301 int aim_send_login (struct aim_session_t *, struct aim_conn_t *, char *, char *, struct client_info_s *);
302 int aim_encode_password(const char *, u_char *);
303 unsigned long aim_sendauthresp(struct aim_session_t *sess, 
304                                struct aim_conn_t *conn, 
305                                char *sn, char *bosip, 
306                                char *cookie, char *email, 
307                                int regstatus);
308 int aim_gencookie(unsigned char *buf);
309 int aim_sendserverready(struct aim_session_t *sess, struct aim_conn_t *conn);
310 unsigned long aim_sendredirect(struct aim_session_t *sess, 
311                                struct aim_conn_t *conn, 
312                                unsigned short servid, 
313                                char *ip,
314                                char *cookie);
315 void aim_purge_rxqueue(struct aim_session_t *);
316
317
318 int aim_parse_unknown(struct aim_session_t *, struct command_rx_struct *command, ...);
319 int aim_parse_missed_im(struct aim_session_t *, struct command_rx_struct *, ...);
320 int aim_parse_last_bad(struct aim_session_t *, struct command_rx_struct *, ...);
321
322 struct command_tx_struct *aim_tx_new(int, struct aim_conn_t *, int);
323 int aim_tx_enqueue__queuebased(struct aim_session_t *, struct command_tx_struct *);
324 int aim_tx_enqueue__immediate(struct aim_session_t *, struct command_tx_struct *);
325 #define aim_tx_enqueue(x, y) ((*(x->tx_enqueue))(x, y))
326 int aim_tx_sendframe(struct command_tx_struct *cur);
327 u_int aim_get_next_txseqnum(struct aim_conn_t *);
328 int aim_tx_flushqueue(struct aim_session_t *);
329 int aim_tx_printqueue(struct aim_session_t *);
330 void aim_tx_purgequeue(struct aim_session_t *);
331
332 struct aim_rxcblist_t {
333   u_short family;
334   u_short type;
335   rxcallback_t handler;
336   u_short flags;
337   struct aim_rxcblist_t *next;
338 };
339
340 int aim_conn_setlatency(struct aim_conn_t *conn, int newval);
341
342 int aim_conn_addhandler(struct aim_session_t *, struct aim_conn_t *conn, u_short family, u_short type, rxcallback_t newhandler, u_short flags);
343 rxcallback_t aim_callhandler(struct aim_conn_t *conn, u_short family, u_short type);
344 int aim_clearhandlers(struct aim_conn_t *conn);
345
346 /*
347  * Generic SNAC structure.  Rarely if ever used.
348  */
349 struct aim_snac_t {
350   u_long id;
351   u_short family;
352   u_short type;
353   u_short flags;
354   void *data;
355   time_t issuetime;
356   struct aim_snac_t *next;
357 };
358 u_long aim_newsnac(struct aim_session_t *, struct aim_snac_t *newsnac);
359 struct aim_snac_t *aim_remsnac(struct aim_session_t *, u_long id);
360 int aim_cleansnacs(struct aim_session_t *, int maxage);
361 int aim_putsnac(u_char *, int, int, int, u_long);
362
363
364 void aim_connrst(struct aim_session_t *);
365 struct aim_conn_t *aim_conn_getnext(struct aim_session_t *);
366 void aim_conn_close(struct aim_conn_t *deadconn);
367 struct aim_conn_t *aim_getconn_type(struct aim_session_t *, int type);
368 struct aim_conn_t *aim_newconn(struct aim_session_t *, int type, char *dest);
369 int aim_conngetmaxfd(struct aim_session_t *);
370 struct aim_conn_t *aim_select(struct aim_session_t *, struct timeval *, int *);
371 int aim_conn_isready(struct aim_conn_t *);
372 int aim_conn_setstatus(struct aim_conn_t *, int);
373 void aim_session_init(struct aim_session_t *);
374
375 /* aim_misc.c */
376
377 #define AIM_VISIBILITYCHANGE_PERMITADD    0x05
378 #define AIM_VISIBILITYCHANGE_PERMITREMOVE 0x06
379 #define AIM_VISIBILITYCHANGE_DENYADD      0x07
380 #define AIM_VISIBILITYCHANGE_DENYREMOVE   0x08
381
382 u_long aim_bos_setidle(struct aim_session_t *, struct aim_conn_t *, u_long);
383 u_long aim_bos_changevisibility(struct aim_session_t *, struct aim_conn_t *, int, char *);
384 u_long aim_bos_setbuddylist(struct aim_session_t *, struct aim_conn_t *, char *);
385 u_long aim_bos_setprofile(struct aim_session_t *, struct aim_conn_t *, char *, char *, unsigned int);
386 u_long aim_bos_setgroupperm(struct aim_session_t *, struct aim_conn_t *, u_long);
387 u_long aim_bos_clientready(struct aim_session_t *, struct aim_conn_t *);
388 u_long aim_bos_reqrate(struct aim_session_t *, struct aim_conn_t *);
389 u_long aim_bos_ackrateresp(struct aim_session_t *, struct aim_conn_t *);
390 u_long aim_bos_setprivacyflags(struct aim_session_t *, struct aim_conn_t *, u_long);
391 u_long aim_bos_reqpersonalinfo(struct aim_session_t *, struct aim_conn_t *);
392 u_long aim_bos_reqservice(struct aim_session_t *, struct aim_conn_t *, u_short);
393 u_long aim_bos_reqrights(struct aim_session_t *, struct aim_conn_t *);
394 u_long aim_bos_reqbuddyrights(struct aim_session_t *, struct aim_conn_t *);
395 u_long aim_bos_reqlocaterights(struct aim_session_t *, struct aim_conn_t *);
396 u_long aim_bos_reqicbmparaminfo(struct aim_session_t *, struct aim_conn_t *);
397 u_long aim_setversions(struct aim_session_t *sess, struct aim_conn_t *conn);
398
399 /* aim_rxhandlers.c */
400 int aim_rxdispatch(struct aim_session_t *);
401 int aim_authparse(struct aim_session_t *, struct command_rx_struct *);
402 int aim_handleredirect_middle(struct aim_session_t *, struct command_rx_struct *, ...);
403 int aim_parse_unknown(struct aim_session_t *, struct command_rx_struct *, ...);
404 int aim_parse_last_bad(struct aim_session_t *, struct command_rx_struct *, ...);
405 int aim_parse_generalerrs(struct aim_session_t *, struct command_rx_struct *command, ...);
406 int aim_parsemotd_middle(struct aim_session_t *sess, struct command_rx_struct *command, ...);
407
408 /* aim_im.c */
409 #define AIM_IMFLAGS_AWAY 0x01 /* mark as an autoreply */
410 #define AIM_IMFLAGS_ACK  0x02 /* request a receipt notice */
411
412 u_long aim_send_im(struct aim_session_t *, struct aim_conn_t *, char *, u_int, char *);
413 int aim_parse_incoming_im_middle(struct aim_session_t *, struct command_rx_struct *);
414 u_long aim_seticbmparam(struct aim_session_t *, struct aim_conn_t *conn);
415 int aim_parse_msgerror_middle(struct aim_session_t *, struct command_rx_struct *);
416
417 /* aim_info.c */
418 #define AIM_CAPS_BUDDYICON 0x01
419 #define AIM_CAPS_VOICE 0x02
420 #define AIM_CAPS_IMIMAGE 0x04
421 #define AIM_CAPS_CHAT 0x08
422 #define AIM_CAPS_GETFILE 0x10
423 #define AIM_CAPS_SENDFILE 0x20
424 extern u_char aim_caps[6][16];
425
426 #define AIM_GETINFO_GENERALINFO 0x00001
427 #define AIM_GETINFO_AWAYMESSAGE 0x00003
428
429 u_long aim_getinfo(struct aim_session_t *, struct aim_conn_t *, const char *, unsigned short);
430 int aim_extractuserinfo(u_char *, struct aim_userinfo_s *);
431 int aim_parse_userinfo_middle(struct aim_session_t *, struct command_rx_struct *);
432 int aim_parse_oncoming_middle(struct aim_session_t *, struct command_rx_struct *);
433 int aim_parse_offgoing_middle(struct aim_session_t *, struct command_rx_struct *);
434 int aim_putuserinfo(u_char *buf, int buflen, struct aim_userinfo_s *info);
435 int aim_sendbuddyoncoming(struct aim_session_t *sess, struct aim_conn_t *conn, struct aim_userinfo_s *info);
436 int aim_sendbuddyoffgoing(struct aim_session_t *sess, struct aim_conn_t *conn, char *sn);
437
438
439 /* aim_auth.c */
440 int aim_auth_sendcookie(struct aim_session_t *, struct aim_conn_t *, u_char *);
441 u_long aim_auth_clientready(struct aim_session_t *, struct aim_conn_t *);
442 u_long aim_auth_changepasswd(struct aim_session_t *, struct aim_conn_t *, char *, char *);
443
444 /* aim_buddylist.c */
445 u_long aim_add_buddy(struct aim_session_t *, struct aim_conn_t *, char *);
446 u_long aim_remove_buddy(struct aim_session_t *, struct aim_conn_t *, char *);
447
448 /* aim_search.c */
449 u_long aim_usersearch_address(struct aim_session_t *, struct aim_conn_t *, char *);
450 /* u_long aim_usersearch_name(struct aim_session_t *, struct aim_conn_t *, char *); */
451
452
453 struct aim_chat_roominfo {
454   u_short exchange;
455   char *name;
456   u_short instance;
457 };
458 int aim_chat_readroominfo(u_char *buf, struct aim_chat_roominfo *outinfo);
459 int aim_chat_parse_infoupdate(struct aim_session_t *sess, struct command_rx_struct *command);
460 int aim_chat_parse_joined(struct aim_session_t *sess, struct command_rx_struct *command);
461 int aim_chat_parse_leave(struct aim_session_t *sess, struct command_rx_struct *command);
462 int aim_chat_parse_incoming(struct aim_session_t *sess, struct command_rx_struct *command);
463 u_long aim_chat_send_im(struct aim_session_t *sess, struct aim_conn_t *conn, char *msg);
464 u_long aim_chat_join(struct aim_session_t *sess, struct aim_conn_t *conn, u_short exchange, const char *roomname);
465 u_long aim_chat_clientready(struct aim_session_t *sess, struct aim_conn_t *conn);
466 int aim_chat_attachname(struct aim_conn_t *conn, char *roomname);
467 char *aim_chat_getname(struct aim_conn_t *conn);
468 struct aim_conn_t *aim_chat_getconn(struct aim_session_t *, char *name);
469
470 u_long aim_chatnav_reqrights(struct aim_session_t *sess, struct aim_conn_t *conn);
471 u_long aim_chatnav_clientready(struct aim_session_t *sess, struct aim_conn_t *conn);
472
473 u_long aim_chat_invite(struct aim_session_t *sess, struct aim_conn_t *conn, char *sn, char *msg, u_short exchange, char *roomname, u_short instance);
474
475 struct aim_chat_exchangeinfo {
476   u_short number;
477   char *name;
478   char *charset1;
479   char *lang1;
480   char *charset2;
481   char *lang2;
482 };
483 int aim_chatnav_parse_info(struct aim_session_t *sess, struct command_rx_struct *command);
484 u_long aim_chatnav_createroom(struct aim_session_t *sess, struct aim_conn_t *conn, char *name, u_short exchange);
485 int aim_chat_leaveroom(struct aim_session_t *sess, char *name);
486
487 /* aim_util.c */
488 #ifdef AIMUTIL_USEMACROS
489 /*
490  * These are really ugly.  You'd think this was LISP.  I wish it was.
491  */
492 #define aimutil_put8(buf, data) ((*(buf) = (u_char)(data)&0xff),1)
493 #define aimutil_get8(buf) ((*(buf))&0xff)
494 #define aimutil_put16(buf, data) ( \
495                                   (*(buf) = (u_char)((data)>>8)&0xff), \
496                                   (*((buf)+1) = (u_char)(data)&0xff),  \
497                                   2)
498 #define aimutil_get16(buf) ((((*(buf))<<8)&0xff00) + ((*((buf)+1)) & 0xff))
499 #define aimutil_put32(buf, data) ( \
500                                   (*((buf)) = (u_char)((data)>>24)&0xff), \
501                                   (*((buf)+1) = (u_char)((data)>>16)&0xff), \
502                                   (*((buf)+2) = (u_char)((data)>>8)&0xff), \
503                                   (*((buf)+3) = (u_char)(data)&0xff), \
504                                   4)
505 #define aimutil_get32(buf) ((((*(buf))<<24)&0xff000000) + \
506                             (((*((buf)+1))<<16)&0x00ff0000) + \
507                             (((*((buf)+2))<< 8)&0x0000ff00) + \
508                             (((*((buf)+3)    )&0x000000ff)))
509 #else
510 #warning Not using aimutil macros.  May have performance problems.
511 int aimutil_put8(u_char *, u_char);
512 u_char aimutil_get8(u_char *buf);
513 int aimutil_put16(u_char *, u_short);
514 u_short aimutil_get16(u_char *);
515 int aimutil_put32(u_char *, u_long);
516 u_long aimutil_get32(u_char *);
517 #endif
518
519 int aimutil_putstr(u_char *, const u_char *, int);
520 int aimutil_tokslen(char *toSearch, int index, char dl);
521 int aimutil_itemcnt(char *toSearch, char dl);
522 char *aimutil_itemidx(char *toSearch, int index, char dl);
523
524 int aim_snlen(const char *sn);
525 int aim_sncmp(const char *sn1, const char *sn2);
526
527 #endif /* __AIM_H__ */
528
This page took 0.078513 seconds and 5 git commands to generate.