Advertisement
Guest User

Untitled

a guest
Mar 16th, 2011
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 8.33 KB | None | 0 0
  1. Index: sbin/natd/natd.c
  2. ===================================================================
  3. --- sbin/natd/natd.c    (revision 218960)
  4. +++ sbin/natd/natd.c    (working copy)
  5. @@ -45,6 +45,8 @@
  6.  #include <syslog.h>
  7.  #include <unistd.h>
  8.  
  9. +#include <pthread.h>
  10. +
  11.  #include "natd.h"
  12.  
  13.  struct instance {
  14. @@ -132,13 +134,20 @@
  15.  static void    DoGlobal (int fd);
  16.  static int CheckIpfwRulenum(unsigned int rnum);
  17.  
  18. +static void*    InfoThread(void*);
  19. +static void*    InfoThreadStart(void*);
  20. +static int      infoSocket;
  21. +
  22. +
  23.  /*
  24.   * Globals.
  25.   */
  26.  
  27. -static int         verbose;
  28. -static     int         background;
  29. -static int         running;
  30. +static  pthread_t               info_thread;
  31. +
  32. +static  int                     verbose;
  33. +static  int                     background;
  34. +static  int                     running;
  35.  static int         logFacility;
  36.  
  37.  static     int         dynamicMode;
  38. @@ -149,6 +158,7 @@
  39.  static int         globalPort;
  40.  static int         divertGlobal;
  41.  static int         exitDelay;
  42. +static  int                     shouldExit;
  43.  
  44.  
  45.  int main (int argc, char** argv)
  46. @@ -366,6 +376,8 @@
  47.             LibAliasSetAddress (mla, mip->aliasAddr);
  48.     }
  49.  
  50. +   pthread_create(&info_thread, NULL, InfoThreadStart, NULL);
  51. +
  52.     while (running) {
  53.         mip = LIST_FIRST(&root);    /* XXX: simon */
  54.  
  55. @@ -989,8 +1001,12 @@
  56.   * shutdown existing connections when system
  57.   * is shut down.
  58.   */
  59. +   void *ret;
  60.     siginterrupt(SIGALRM, 1);
  61.     signal (SIGALRM, Shutdown);
  62. +   shouldExit=1;
  63. +   close(infoSocket)
  64. +   pthread_join(info_thread, &ret);
  65.     ualarm(exitDelay*1000, 1000);
  66.  }
  67.  
  68. @@ -2015,3 +2031,74 @@
  69.  
  70.     return 0;
  71.  }
  72. +
  73. +static void*    
  74. +InfoThreadStart(void* p)
  75. +{
  76. +   struct sockaddr_in s_addr;
  77. +   int     sz=1;
  78. +
  79. +   if((infoSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  80. +       return NULL;
  81. +
  82. +   memset((char *) &s_addr, 0, sizeof(s_addr));
  83. +
  84. +   s_addr.sin_family = AF_INET;
  85. +   s_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  86. +   s_addr.sin_port = htons(666);
  87. +
  88. +   setsockopt( infoSocket, SOL_SOCKET, SO_REUSEADDR, &sz, sizeof(int) );
  89. +
  90. +   if(bind(infoSocket, (struct sockaddr *) &s_addr, sizeof(s_addr)) < 0)
  91. +       return NULL;
  92. +
  93. +   listen(infoSocket, 1);
  94. +
  95. +   return InfoThread((void*)&infoSocket);
  96. +}
  97. +
  98. +static void*    
  99. +InfoThread(void* p)
  100. +{
  101. +   int     s,s2;
  102. +   struct sockaddr_in s2_addr;
  103. +   int     sz=1;
  104. +   char buf[256];
  105. +
  106. +   s = *(int*)p;
  107. +
  108. +   sz = sizeof( struct sockaddr_in );
  109. +
  110. +   for(;;){
  111. +       s2 = accept(s, (struct sockaddr *) &s2_addr, &sz);
  112. +       struct instance *tip;
  113. +       LIST_FOREACH(tip, &root, list) {
  114. +           sprintf(buf, "if:%s inport:%d outport:%d inoutport:%d\n",
  115. +                   tip->ifName, tip->inPort, tip->outPort, tip->inOutPort);
  116. +
  117. +           write(s2,buf,strlen(buf)+1);
  118. +
  119. +           sprintf(buf,"---\n");
  120. +           write(s2,buf,strlen(buf)+1);
  121. +
  122. +           ShowOutInfo(s2,tip->la);
  123. +
  124. +           sprintf(buf,"---\n");
  125. +           write(s2,buf,strlen(buf)+1);
  126. +
  127. +           ShowInInfo(s2,tip->la);
  128. +
  129. +           sprintf(buf,"---\n");
  130. +           write(s2,buf,strlen(buf)+1);
  131. +
  132. +       }
  133. +
  134. +       sprintf(buf,"This is natd. Fuck you! :-)\n");
  135. +       write(s2, buf, strlen(buf)+1);
  136. +       close(s2);
  137. +       if(shouldExit) return NULL;
  138. +   }
  139. +   close(s);
  140. +   pthread_exit(&sz);
  141. +   return NULL;
  142. +}
  143. Index: sbin/natd/Makefile
  144. ===================================================================
  145. --- sbin/natd/Makefile  (revision 218960)
  146. +++ sbin/natd/Makefile  (working copy)
  147. @@ -3,8 +3,9 @@
  148.  PROG       = natd
  149.  SRCS       = natd.c icmp.c
  150.  WARNS?=    3
  151. -LDADD      = -lalias
  152. +LDADD      = -lalias -lpthread
  153.  DPADD      = ${LIBALIAS}
  154.  MAN        = natd.8
  155. +CFLAGS+=   -I/usr/src/sys/netinet/libalias/
  156.  
  157.  .include <bsd.prog.mk>
  158. Index: sys/netinet/libalias/alias.h
  159. ===================================================================
  160. --- sys/netinet/libalias/alias.h    (revision 218960)
  161. +++ sys/netinet/libalias/alias.h    (working copy)
  162. @@ -42,6 +42,7 @@
  163.  #include <netinet/in_systm.h>
  164.  #include <netinet/in.h>
  165.  #include <netinet/ip.h>
  166. +#include <sys/queue.h>
  167.  
  168.  #define LIBALIAS_BUF_SIZE 128
  169.  #ifdef _KERNEL
  170. @@ -138,6 +139,11 @@
  171.  /* Mbuf helper function. */
  172.  struct mbuf    *m_megapullup(struct mbuf *, int);
  173.  
  174. +/* Alias info function and structures */
  175. +
  176. +void       ShowOutInfo(int, struct libalias *);
  177. +void            ShowInInfo(int, struct libalias *);
  178. +
  179.  /*
  180.   * Mode flags and other constants.
  181.   */
  182. Index: sys/netinet/libalias/alias_db.c
  183. ===================================================================
  184. --- sys/netinet/libalias/alias_db.c (revision 218960)
  185. +++ sys/netinet/libalias/alias_db.c (working copy)
  186. @@ -161,6 +161,7 @@
  187.  
  188.  #include <sys/socket.h>
  189.  #include <netinet/tcp.h>
  190. +#include <netdb.h>
  191.  
  192.  #ifdef _KERNEL  
  193.  #include <netinet/libalias/alias.h>
  194. @@ -1784,7 +1785,7 @@
  195.      SetAckModified(), GetAckModified()
  196.      GetDeltaAckIn(), GetDeltaSeqOut(), AddSeq()
  197.      SetProtocolFlags(), GetProtocolFlags()
  198. -    SetDestCallId()
  199. +    SetDestCallId(), GetProto()
  200.  */
  201.  
  202.  
  203. @@ -2158,7 +2159,14 @@
  204.     la->deleteAllLinks = 0;
  205.  }
  206.  
  207. +int
  208. +GetProto(struct alias_link *lnk)
  209. +{
  210.  
  211. +   return (lnk->link_type);
  212. +}
  213. +
  214. +
  215.  /* Miscellaneous Functions
  216.  
  217.      HouseKeeping()
  218. @@ -2934,3 +2942,85 @@
  219.         return(redir); /* address redirect */
  220.     }
  221.  }
  222. +
  223. +void
  224. +ShowOutInfo(int socket, struct libalias *la) {
  225. +   struct alias_link *lnk;
  226. +   char buf[1024];
  227. +   char _original_addr[20]="", _dest_addr[20]="", _alias_addr[20]="", _proxy_addr[20]="";
  228. +   char original_addr[30]="", dest_addr[30]="", alias_addr[30]="", proxy_addr[30]="";
  229. +   struct protoent *proto;
  230. +   int i;
  231. +   sprintf(buf,"Out table\n");
  232. +   write(socket, buf, strlen(buf));
  233. +   for(i=0; i<LINK_TABLE_OUT_SIZE; i++){
  234. +       LIST_FOREACH(lnk, &la->linkTableOut[i], list_out){
  235. +
  236. +           inet_ntoa_r(GetOriginalAddress(lnk), _original_addr, 20);
  237. +           inet_ntoa_r(GetDestAddress(lnk), _dest_addr, 20);
  238. +           inet_ntoa_r(GetAliasAddress(lnk), _alias_addr, 20);
  239. +           inet_ntoa_r(GetProxyAddress(lnk), _proxy_addr, 20);
  240. +
  241. +           snprintf(original_addr, 30, "%s:%d", _original_addr, ntohs(GetOriginalPort(lnk)));
  242. +           snprintf(dest_addr, 30, "%s:%d", _dest_addr, ntohs(GetDestPort(lnk)));
  243. +           snprintf(alias_addr, 30, "%s:%d", _alias_addr, ntohs(GetAliasPort(lnk)));
  244. +           snprintf(proxy_addr, 30, "%s:%d", _proxy_addr, ntohs(GetProxyPort(lnk)));
  245. +
  246. +           proto = getprotobynumber(GetProto(lnk));
  247. +           snprintf(buf, 1024, "%25s %25s %25s %25s ",
  248. +           original_addr,
  249. +           dest_addr,
  250. +           alias_addr,
  251. +           proxy_addr
  252. +           );
  253. +           write(socket, buf, strlen(buf));
  254. +           if(proto){
  255. +               snprintf(buf, 1024, "%s\n", proto->p_name);
  256. +           }else{
  257. +               snprintf(buf, 1024, "%d\n", GetProto(lnk));
  258. +           }
  259. +           write(socket, buf, strlen(buf));
  260. +       }
  261. +   }
  262. +}
  263. +
  264. +void
  265. +ShowInInfo(int socket, struct libalias *la) {
  266. +   struct alias_link *lnk;
  267. +   char buf[1024];
  268. +   char _original_addr[20]="", _dest_addr[20]="", _alias_addr[20]="", _proxy_addr[20]="";
  269. +   char original_addr[30]="", dest_addr[30]="", alias_addr[30]="", proxy_addr[30]="";
  270. +   struct protoent *proto;
  271. +   int i;
  272. +   sprintf(buf, "In table\n");
  273. +   write(socket, buf, strlen(buf));
  274. +   for(i=0; i<LINK_TABLE_IN_SIZE; i++){
  275. +       LIST_FOREACH(lnk, &la->linkTableIn[i], list_in){
  276. +
  277. +           inet_ntoa_r(GetOriginalAddress(lnk), _original_addr, 20);
  278. +           inet_ntoa_r(GetDestAddress(lnk), _dest_addr, 20);
  279. +           inet_ntoa_r(GetAliasAddress(lnk), _alias_addr, 20);
  280. +           inet_ntoa_r(GetProxyAddress(lnk), _proxy_addr, 20);
  281. +
  282. +           snprintf(original_addr, 30, "%s:%d", _original_addr, ntohs(GetOriginalPort(lnk)));
  283. +           snprintf(dest_addr, 30, "%s:%d", _dest_addr, ntohs(GetDestPort(lnk)));
  284. +           snprintf(alias_addr, 30, "%s:%d", _alias_addr, ntohs(GetAliasPort(lnk)));
  285. +           snprintf(proxy_addr, 30, "%s:%d", _proxy_addr, ntohs(GetProxyPort(lnk)));
  286. +
  287. +           proto = getprotobynumber(GetProto(lnk));
  288. +           snprintf(buf, 1024, "%25s %25s %25s %25s ",
  289. +           original_addr,
  290. +           dest_addr,
  291. +           alias_addr,
  292. +           proxy_addr
  293. +           );
  294. +           write(socket, buf, strlen(buf));
  295. +           if(proto){
  296. +               snprintf(buf, 1024, "%s\n", proto->p_name);
  297. +           }else{
  298. +               snprintf(buf, 1024, "%d\n", GetProto(lnk));
  299. +           }
  300. +           write(socket, buf, strlen(buf));
  301. +       }
  302. +   }
  303. +}
  304. Index: sys/netinet/libalias/alias_local.h
  305. ===================================================================
  306. --- sys/netinet/libalias/alias_local.h  (revision 218960)
  307. +++ sys/netinet/libalias/alias_local.h  (working copy)
  308. @@ -341,6 +341,7 @@
  309.  void       SetProtocolFlags(struct alias_link *_lnk, int _pflags);
  310.  int        GetProtocolFlags(struct alias_link *_lnk);
  311.  void       SetDestCallId(struct alias_link *_lnk, u_int16_t _cid);
  312. +int        GetProto(struct alias_link *_lnk);
  313.  
  314.  #ifndef NO_FW_PUNCH
  315.  void       PunchFWHole(struct alias_link *_lnk);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement