home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume28 / m0 / part03 / c_udp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-06  |  5.4 KB  |  274 lines

  1. /*
  2.     c_udp.c
  3. */
  4. /*  Copyright (c) 1994 Christian F. Tschudin. All rights reserved.
  5.  
  6.     Distributed under the terms of the GNU General Public License
  7.     version 2 of june 1991 as published by the Free Software
  8.     Foundation, Inc.
  9.  
  10.              This file is part of M0.
  11.  
  12. M0 is distributed in the hope that it will be useful, but WITHOUT ANY
  13. WARRANTY.  No author or distributor accepts responsibility to anyone for
  14. the consequences of using it or for whether it serves any particular
  15. purpose or works at all, unless he says so in writing.  Refer to the GNU
  16. General Public License for full details. 
  17.  
  18. Everyone is granted permission to copy, modify and redistribute M0, but
  19. only under the conditions described in the GNU General Public License. 
  20. A copy of this license is supposed to have been given to you along with
  21. M0 so you can know your rights and responsibilities.  It should be in a
  22. file named LICENSE.  Among other things, the copyright notice and this
  23. notice must be preserved on all copies.  */
  24.  
  25. #include "c_proto.h"
  26.  
  27.  
  28. #ifdef CHANNEL_UDP
  29.  
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <malloc.h>
  33.  
  34. #include <sys/types.h>
  35. #include <sys/socket.h>
  36. #include <sys/time.h>
  37. #include <sys/ioctl.h>
  38. #include <signal.h>
  39. #include <netinet/in.h>
  40. #include <arpa/inet.h>
  41. #include <fcntl.h>
  42. #include <errno.h>
  43. #include <netdb.h>
  44.  
  45. #ifndef MAXHOSTNAMELEN
  46. # define MAXHOSTNAMELEN    64
  47. #endif
  48.  
  49. struct udp_data {
  50.     eindex chankey;
  51.     ushort chan;
  52.     uint host;
  53.     ushort port;
  54. };
  55.  
  56. eindex udp_addr;
  57. eindex udp_name;
  58.  
  59. #define MAXUDPPORTS    5
  60. static int send_socket[MAXUDPPORTS];
  61.  
  62.  
  63. int
  64. udp_listen(uint host, int port)
  65. {
  66.     struct sockaddr_in host_addr;
  67.     int s;
  68.  
  69.     if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
  70.         return -1;
  71.  
  72.     bzero( &host_addr, sizeof(host_addr) );
  73.     host_addr.sin_family = AF_INET;
  74.     host_addr.sin_addr.s_addr = htonl(host ? host : INADDR_ANY);
  75.     host_addr.sin_port = htons((ushort) port);
  76.  
  77.     if (bind(s, &host_addr, sizeof(struct sockaddr)) < 0)
  78.         return -1;
  79.  
  80.     return s;
  81. }
  82.  
  83.  
  84. uint*
  85. get_ip_addresses()
  86. {
  87. static uint* my_addresses;
  88.  
  89.     if (!my_addresses) {
  90.             char myname[MAXHOSTNAMELEN];
  91.             struct hostent *hentry;
  92.         int i;
  93.         uint **ipp;
  94.  
  95.             gethostname(myname, sizeof(myname));
  96.         hentry = gethostbyname(myname);
  97.  
  98.         for (i = 0, ipp = (uint**)(hentry->h_addr_list);
  99.                             *ipp ; i++, ipp++);
  100.  
  101.         my_addresses = calloc(i+1, sizeof(uint));
  102.         for (i = 0, ipp = (uint**)(hentry->h_addr_list);
  103.                             *ipp ; i++, ipp++)
  104.             my_addresses[i] = ntohl(**ipp);
  105.         my_addresses[i] = 0;
  106.     }
  107.     return my_addresses;
  108. }
  109.  
  110.  
  111. int
  112. udp_init(uint *ip, int port)
  113. {
  114.     uint *p2;
  115.     int s, i;
  116.     eindex p;
  117.     eptr pp;
  118.  
  119.     for (p2 = ip, i = 0; p2 && *p2; p2++, i++);
  120.     if (!i)
  121.         return -1;
  122.  
  123.     if (i > MAXUDPPORTS)
  124.         i = MAXUDPPORTS;
  125.     udp_addr = new_array(0, i);
  126.     udp_name = name_add((byteptr)"udpip", 5, A_EXECUTABLE);
  127.  
  128.     p = new_element(0, T_INT);
  129.     pp = gaddr(p);
  130.     pp->V.i = port;
  131.  
  132.     for (p2 = ip, i = 0; *p2 && i < MAXUDPPORTS; p2++) {
  133.         eindex a, e;
  134.  
  135.         s = udp_listen(*p2, port);
  136.         if (s > 0) {
  137.             add_incoming(s, udp_recv, udp_name, i);
  138.             send_socket[i] = s;
  139.             a = new_array(0, 2);
  140.             e = new_element(0, T_INT);
  141.             gaddr(e)->V.i = (sint) *p2;
  142.             array_put(0, a, 0, e);
  143.             array_put(0, a, 1, p);
  144.             increfp(pp);
  145.             epattr(gaddr(a)) &= ~A_WRITE;
  146.             array_put(0, udp_addr, i++, a);
  147.         }
  148.     }
  149.  
  150.     decrefp(0, p, pp);
  151.     if (!i) {
  152.         decref(0, udp_addr);
  153.         udp_addr = 0;
  154.         decref(0, udp_name);
  155.         udp_name = 0;
  156.         return -1;
  157.     }
  158.     eplen(gaddr(udp_addr)) = i;
  159.  
  160.     return 0;
  161. }
  162.  
  163.  
  164. void
  165. udp_recv(int s, eindex *msgr, eindex *src)
  166. {
  167. #define UDPFRAMESIZE    8092
  168.     struct sockaddr_in from;
  169.     int len, cnt, fromlen = sizeof(from);
  170.     eindex a, h, p;
  171.     byteptr buf;
  172.  
  173.     *msgr = 0;
  174.     if (ioctl(s, FIONREAD, &len) < 0) {
  175.         perror("udp_recv, FIONREAD");
  176.         return;
  177.     }
  178.     if (len > UDPFRAMESIZE)
  179.         len = UDPFRAMESIZE;
  180.     buf = malloc(len);
  181.  
  182.     cnt = recvfrom(s, buf, len, 0, &from, &fromlen);
  183.      if (cnt < 0) {
  184.         perror("recvfrom");
  185.         free(buf);
  186.         return;
  187.     }
  188.  
  189.     *msgr = str_import(0, buf, cnt, len);
  190.     if (!*msgr) {
  191.         free(buf);
  192.         return;
  193.     }
  194.     h = new_element(0, T_INT);
  195.     gaddr(h)->V.i = ntohl(from.sin_addr.s_addr);
  196.     p = new_element(0, T_INT);
  197.     gaddr(p)->V.i = ntohs(from.sin_port);
  198.     a = new_array(0, 2);
  199.     array_put(0, a, 0, h);
  200.     array_put(0, a, 1, p);
  201.     *src = a;
  202. }
  203.  
  204.  
  205. int udp_send(int s, uint host, int port, byteptr packet, int len)
  206. {
  207. static struct sockaddr_in sockaddr;
  208.  
  209.     TRACE(5, printf("udp send: %u %d (%d bytes)\n", host, port, len))
  210.  
  211.     bzero(&sockaddr, sizeof(struct sockaddr_in));
  212.     sockaddr.sin_family = AF_INET;
  213.     sockaddr.sin_addr.s_addr = htonl(host);
  214.      sockaddr.sin_port = htons(port);
  215.  
  216.      return sendto(s, packet, len, 0,
  217.               (struct sockaddr *)&sockaddr, sizeof (struct sockaddr));
  218. }
  219.  
  220.  
  221. char* 
  222. iptoa(uint i)
  223. {
  224. static char str[30];
  225.     byteptr ip = (byteptr)&i;
  226.  
  227.     i = htonl(i);
  228.     sprintf(str, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
  229.     return str;
  230. }
  231.  
  232.  
  233. static void
  234. udp_submit(mproc p, void *data, eindex m)
  235. {
  236.     struct udp_data *d = (struct udp_data*) data;
  237.     uint len = elen(p,m);
  238.     byteptr s;
  239.  
  240.     len = len > 8000 ? 8000 : len;
  241.     s = malloc(len);
  242.     str_export(p, s, m, 0, len);
  243.  
  244.     if (udp_send(send_socket[d->chan], d->host, d->port, s, len) < 0)
  245.         perror("udp_submit");
  246.  
  247.     dict_undef(0, channeldict, d->chankey);
  248.     free(d);
  249.     free(s);
  250.     return;
  251. }
  252.  
  253.  
  254. eindex
  255. add_udp_channel(sint chan, uint host, ushort port)
  256. {
  257.     eindex key;
  258.     byte bits[8];
  259.     struct udp_data *d;
  260.  
  261.     random64(bits);
  262.     key = key_add(bits);
  263.     d = (struct udp_data*) malloc(sizeof(struct udp_data));
  264.     d->chankey = key;
  265.     d->chan = chan;
  266.     d->host = host;
  267.     d->port = port;
  268.     new_channel(key, d, udp_submit);
  269.  
  270.     return key;
  271. }
  272.  
  273. #endif /* CHANNEL_UDP */
  274.