PageRenderTime 60ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/isdn4k-utils-CVS-2010-05-01-patched/isdnlog/connect/socket.c

#
C | 495 lines | 332 code | 122 blank | 41 comment | 88 complexity | dac6bbf1c6f1dd46e0b660adb44e9cbe MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, CC-BY-SA-3.0
  1. /*
  2. * ISDN accounting for isdn4linux.
  3. *
  4. * Copyright 1996 by Stefan Luethje (luethje@sl-gw.lake.de)
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2, or (at your option)
  9. * any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. */
  21. /****************************************************************************/
  22. #define _SOCKET_C_
  23. /****************************************************************************/
  24. #include "socket.h"
  25. /****************************************************************************/
  26. int get_msg(socket_queue *sock, buffer *buf);
  27. int bufcat(buffer *s1,buffer *s2, int first, int len);
  28. int initbuf(buffer *buf);
  29. /****************************************************************************/
  30. int Write(socket_queue* sock)
  31. {
  32. /* ACHTUNG IN DIESER FKT DARF KEIN print_msg() AUFGERUFEN WERDEN !!!!! */
  33. int RetCode;
  34. static buffer *buf = NULL;
  35. char *Ptr;
  36. if (sock->descriptor < 0)
  37. {
  38. #ifdef DEBUG
  39. syslog(LOG_DEBUG,"Write: Invalid Descriptor %d",sock->descriptor);
  40. #endif
  41. return 1;
  42. }
  43. if (!buf)
  44. {
  45. buf = (buffer*) calloc(1,sizeof(buffer));
  46. if (!buf)
  47. return NO_MEMORY;
  48. if ((RetCode = initbuf(buf)) != 0)
  49. return RetCode;
  50. }
  51. if ((Ptr = itos(sock->msgbuf.used+_MSG_LEN+_MSG_MSG,_MSG_LEN)) == NULL)
  52. return -1;
  53. memcpy(buf->buf,Ptr,_MSG_LEN);
  54. buf->used = _MSG_LEN;
  55. if ((Ptr = itos(sock->msg,_MSG_MSG)) == NULL)
  56. return -1;
  57. #ifdef DEBUG
  58. syslog(LOG_DEBUG,"Write: Message %d:*%s*",sock->msg,sock->msgbuf.buf);
  59. #endif
  60. sock->msg = NO_MSG;
  61. memcpy(buf->buf+_MSG_LEN,Ptr,_MSG_MSG);
  62. buf->used += _MSG_MSG;
  63. if ((RetCode = bufcat(buf,&(sock->msgbuf),0,sock->msgbuf.used)) != 0)
  64. return RetCode;
  65. return write(sock->descriptor,buf->buf,buf->used);
  66. }
  67. /****************************************************************************/
  68. int Read(socket_queue* sock)
  69. {
  70. int RetCode;
  71. int SelRet;
  72. fd_set readmask;
  73. struct timeval timeout = {0,0};
  74. static buffer *buf = NULL;
  75. if (sock->descriptor < 0)
  76. {
  77. #ifdef DEBUG
  78. syslog(LOG_DEBUG,"Write: Invalid Descriptor %d",sock->descriptor);
  79. #endif
  80. return 1;
  81. }
  82. if (sock->restbuf.len > BUF_SIZE && sock->restbuf.used == 0)
  83. {
  84. free(sock->restbuf.buf);
  85. sock->restbuf.buf = NULL;
  86. initbuf(&(sock->restbuf));
  87. }
  88. if (sock->msgbuf.len > BUF_SIZE && sock->msgbuf.used == 0)
  89. {
  90. free(sock->msgbuf.buf);
  91. sock->msgbuf.buf = NULL;
  92. initbuf(&(sock->msgbuf));
  93. }
  94. else
  95. sock->msgbuf.used = 0;
  96. if (!buf)
  97. {
  98. if ((buf = (buffer*) calloc(1,sizeof(buffer))) == NULL)
  99. return NO_MEMORY;
  100. if ((RetCode = initbuf(buf)) != 0)
  101. return RetCode;
  102. }
  103. else
  104. if (buf->len > BUF_SIZE)
  105. {
  106. free(buf->buf);
  107. buf->buf = NULL;
  108. initbuf(buf);
  109. }
  110. FD_ZERO(&readmask);
  111. FD_SET(sock->descriptor,&readmask);
  112. while ((SelRet = select(sock->descriptor+1,&readmask,NULL,NULL,&timeout)) > 0 &&
  113. FD_ISSET(sock->descriptor,&readmask))
  114. if ((RetCode = buf->used = read(sock->descriptor,buf->buf,buf->len)) > 0)
  115. {
  116. if ((RetCode = bufcat(&(sock->restbuf),buf,0,buf->used)) != 0)
  117. return RetCode;
  118. }
  119. else
  120. return -1;
  121. if (SelRet < 0)
  122. return -1;
  123. if (sock->restbuf.used)
  124. if ((RetCode = get_msg(sock,buf)) != 0)
  125. return RetCode;
  126. return -1; /* Sollte nur ein Wert > 0 zurueckliefern */
  127. }
  128. /****************************************************************************/
  129. int init_socket(socket_queue *sock)
  130. {
  131. int RetCode;
  132. if (!sock)
  133. return -1;
  134. if ((RetCode = initbuf(&(sock->msgbuf))) != 0)
  135. return RetCode;
  136. if ((RetCode = initbuf(&(sock->restbuf))) != 0)
  137. return RetCode;
  138. return 0;
  139. }
  140. /****************************************************************************/
  141. int get_msg(socket_queue *sock, buffer *buf)
  142. {
  143. int len;
  144. int RetCode;
  145. buf->used = 0;
  146. sock->msgbuf.used = 0;
  147. if (sock->restbuf.used < _MSG_LEN + _MSG_MSG)
  148. {
  149. sock->status = NO_NEXT_MSG;
  150. sock->msg = NO_MSG;
  151. return 0;
  152. }
  153. len = (int) stoi(sock->restbuf.buf,_MSG_LEN);
  154. /* printf("stoi %d,%d\n",len,sock->restbuf.used); */
  155. if (len > sock->restbuf.used)
  156. {
  157. sock->status = NO_NEXT_MSG;
  158. sock->msg = NO_MSG;
  159. return 0;
  160. }
  161. sock->msg = (int) stoi(sock->restbuf.buf+_MSG_LEN,_MSG_MSG);
  162. if ((RetCode = bufcat(buf,&(sock->restbuf),0,sock->restbuf.used)) != 0)
  163. return RetCode;
  164. if ((RetCode = bufcat(&(sock->msgbuf),buf,_MSG_LEN+_MSG_MSG,len - _MSG_LEN - _MSG_MSG)) != 0)
  165. return RetCode;
  166. sock->restbuf.used = 0;
  167. if ((RetCode = bufcat(&(sock->restbuf),buf,len,buf->used - len)) != 0)
  168. return RetCode;
  169. if (sock->restbuf.used >= _MSG_LEN + _MSG_MSG &&
  170. stoi(sock->restbuf.buf,_MSG_LEN) <= sock->restbuf.used)
  171. sock->status = NEXT_MSG;
  172. else
  173. sock->status = NO_NEXT_MSG;
  174. return buf->used;
  175. }
  176. /****************************************************************************/
  177. int msgcpy(socket_queue *sock, char *String, int len)
  178. {
  179. if (sock->msgbuf.len < len)
  180. {
  181. sock->msgbuf.len = len;
  182. if ((sock->msgbuf.buf = (char*) realloc(sock->msgbuf.buf, len * sizeof(char))) == NULL)
  183. return NO_MEMORY;
  184. }
  185. memcpy(sock->msgbuf.buf,String,len);
  186. sock->msgbuf.used = len;
  187. return 0;
  188. }
  189. /****************************************************************************/
  190. int bufcat(buffer *s1, buffer *s2, int first, int len)
  191. {
  192. if (s1->used + len - first > s1->len)
  193. {
  194. s1->len = s1->used + len - first;
  195. s1->buf = (char*) realloc(s1->buf, (s1->used + len - first) * sizeof(char));
  196. if (s1->buf == NULL)
  197. return NO_MEMORY;
  198. }
  199. memcpy((void*) (s1->buf + s1->used),(void*) (s2->buf + first), len);
  200. s1->used += len;
  201. return 0;
  202. }
  203. /****************************************************************************/
  204. int initbuf(buffer *buf)
  205. {
  206. if (buf && buf->buf == NULL)
  207. {
  208. buf->buf = (char*) calloc(BUF_SIZE,sizeof(char));
  209. buf->len = BUF_SIZE;
  210. buf->used = 0;
  211. }
  212. return buf->buf?0:NO_MEMORY;
  213. }
  214. /****************************************************************************/
  215. unsigned long stoi (unsigned char* s, int len)
  216. {
  217. unsigned long val = 0;
  218. unsigned long Cnt = 0;
  219. if (len > 4)
  220. return 0;
  221. while (Cnt < len)
  222. val = (val << 8) + s[Cnt++];
  223. return val;
  224. }
  225. /****************************************************************************/
  226. char *itos (unsigned long val, int len)
  227. {
  228. static char s[16];
  229. if (len > 4)
  230. return NULL;
  231. while(len-- > 0)
  232. {
  233. s[len] = (char) val % 256;
  234. val = val >> 8;
  235. }
  236. return s;
  237. }
  238. /****************************************************************************/
  239. int add_socket(socket_queue **sock, int new_socket)
  240. {
  241. int Cnt = 0;
  242. if ((*sock) == NULL)
  243. {
  244. if (!((*sock) = (socket_queue*) calloc(2,sizeof(socket_queue))))
  245. return NO_MEMORY;
  246. (*sock)[0].descriptor = new_socket;
  247. (*sock)[1].descriptor = NO_SOCKET;
  248. }
  249. else
  250. {
  251. Cnt = socket_size((*sock));
  252. (*sock) = (socket_queue*) realloc((*sock),sizeof(socket_queue)*(Cnt+2));
  253. memset(&((*sock)[Cnt+1]),0,sizeof(socket_queue));
  254. (*sock)[Cnt+1].descriptor = NO_SOCKET;
  255. (*sock)[Cnt].descriptor = new_socket;
  256. }
  257. if (init_socket(&((*sock)[Cnt])))
  258. return NO_MEMORY;
  259. return 0;
  260. }
  261. /****************************************************************************/
  262. int del_socket(socket_queue **sock, int position)
  263. {
  264. int Cnt;
  265. if (*sock == NULL)
  266. return -1;
  267. Cnt = socket_size((*sock));
  268. if (position < 0 || position >= Cnt)
  269. return -1;
  270. free((*sock)[position].msgbuf.buf);
  271. free((*sock)[position].restbuf.buf);
  272. free((*sock)[position].f_hostname);
  273. free((*sock)[position].f_username);
  274. if (position == 0 && Cnt == 1)
  275. {
  276. free(*sock);
  277. *sock = NULL;
  278. return 0;
  279. }
  280. close((*sock)[position].descriptor);
  281. memcpy(&((*sock)[position]),
  282. &((*sock)[Cnt-1]),
  283. sizeof(socket_queue));
  284. memset(&((*sock)[Cnt-1]),0,sizeof(socket_queue));
  285. (*sock)[Cnt-1].descriptor = NO_SOCKET;
  286. (*sock) = (socket_queue*) realloc((*sock),sizeof(socket_queue)*Cnt);
  287. return (*sock)?0:NO_MEMORY;
  288. }
  289. /****************************************************************************/
  290. int socket_size(socket_queue *sock)
  291. {
  292. int Cnt = 0;
  293. if (sock != NULL)
  294. while(sock[Cnt].descriptor != NO_SOCKET)
  295. Cnt++;
  296. return Cnt;
  297. }
  298. /****************************************************************************/
  299. int Set_Info_Struct(CALL **Info, char *String)
  300. {
  301. int Cnt = 0;
  302. int channel;
  303. char** Array = String_to_Array(String,C_DELIMITER);
  304. while(Array[Cnt++]);
  305. if (Cnt-1 != PROT_ELEMENTS)
  306. {
  307. del_Array(Array);
  308. fprintf(stderr,"Internal error: wrong structure (%d elements), %d expected!\n",Cnt, PROT_ELEMENTS);
  309. return -1;
  310. }
  311. if (*Info == NULL)
  312. if (((*Info) = (CALL*) calloc(1,sizeof(CALL))) == NULL)
  313. return NO_MEMORY;
  314. Cnt = 0;
  315. channel = atoi(Array[Cnt++]);
  316. (*Info)->stat = atoi(Array[Cnt++]);
  317. (*Info)->dialin = atoi(Array[Cnt++]);
  318. strcpy((*Info)->num[_ME((*Info))],Array[Cnt++]); /* Meine MSN */
  319. strcpy((*Info)->alias[_ME((*Info))],Array[Cnt++]);
  320. strcpy((*Info)->num[_OTHER((*Info))],Array[Cnt++]);
  321. strcpy((*Info)->vorwahl[_OTHER((*Info))],Array[Cnt++]);
  322. strcpy((*Info)->rufnummer[_OTHER((*Info))],Array[Cnt++]);
  323. strcpy((*Info)->alias[_OTHER((*Info))],Array[Cnt++]);
  324. strcpy((*Info)->area[_OTHER((*Info))],Array[Cnt++]);
  325. (*Info)->connect = atoi(Array[Cnt++]);
  326. (*Info)->t_duration = atoi(Array[Cnt++]);
  327. (*Info)->aoce = atoi(Array[Cnt++]);
  328. strcpy((*Info)->money,Array[Cnt++]);
  329. strcpy((*Info)->currency,Array[Cnt++]);
  330. (*Info)->ibytes = atoi(Array[Cnt++]);
  331. (*Info)->obytes = atoi(Array[Cnt++]);
  332. (*Info)->ibps = atof(Array[Cnt++]);
  333. (*Info)->obps = atof(Array[Cnt++]);
  334. strcpy((*Info)->msg,Array[Cnt++]);
  335. del_Array(Array);
  336. return channel;
  337. }
  338. /****************************************************************************/
  339. char *GetHostByAddr(struct sockaddr *Addr)
  340. {
  341. char *RetCode = NULL;
  342. char *Ptr;
  343. struct hostent *hp = NULL;
  344. struct in_addr *In = &((struct sockaddr_in*) Addr)->sin_addr;
  345. if ((hp = gethostbyaddr((char*) In,sizeof(long int),AF_INET)) != NULL)
  346. {
  347. if ((RetCode = (char*) calloc(strlen(hp->h_name)+1,sizeof(char))) ==NULL)
  348. return NULL;
  349. strcpy(RetCode,hp->h_name);
  350. }
  351. else
  352. if ((Ptr = inet_ntoa(*In)) != NULL)
  353. {
  354. if ((RetCode = (char*) calloc(strlen(Ptr)+1,sizeof(char))) ==NULL)
  355. return NULL;
  356. strcpy(RetCode,Ptr);
  357. }
  358. return RetCode;
  359. }
  360. /****************************************************************************/
  361. char *GetHostByName(char *Name)
  362. {
  363. char *RetCode = NULL;
  364. struct hostent *hp = NULL;
  365. if ((hp = gethostbyname(Name)) != NULL)
  366. {
  367. if ((RetCode = (char*) calloc(strlen(hp->h_name)+1,sizeof(char))) ==NULL)
  368. return NULL;
  369. strcpy(RetCode,hp->h_name);
  370. }
  371. return RetCode;
  372. }
  373. /****************************************************************************/